-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex_13_3.py
97 lines (83 loc) · 2.91 KB
/
ex_13_3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# Requirements
from time import sleep
from random import randint
from os import path
import json
# Player class
class Player:
def __init__(self, options):
if options["nom"] == None:
print("Quel est votre nom ?")
options["nom"] = input()
self.nom = options["nom"]
if options["niveau"]: options["niveau"] = 1337
self.niveau = options["niveau"]
if options["attaque"] == None: options["attaque"] = 10
self.attaque = options["attaque"]
if options["resistance"] == None: options["resistance"]= 1
self.resistance = options["resistance"]
if options["pv"] == None: options["pv"] = 10
self.pv = options["pv"]
# Check progress
if path.exists("./ex_13_3_progress.json"):
# Load existing progress
progress_file = open("./ex_13_3_progress.json", "r").read()
player = Player(json.loads(progress_file))
print(f"Heureux de vous revoir, {player.nom}")
else:
# No existing progress, create one
print("Quel est votre nom ?")
name = input()
player = Player({"nom": name, "niveau": 1337, "attaque": 10, "resistance": 1, "pv": 10})
def do_fight():
robot = Player({"nom": "Bip", "niveau": 420, "attaque": randint(0, 5), "resistance": randint(0, 5), "pv": randint(5, 20)})
print(f"{player.nom} VS {robot.nom}")
sleep(1)
turns = 0
player_turn = True
match_nul = False
while player.pv > 0 and robot.pv > 0:
if turns == 10:
match_nul = True
break
if player_turn:
attacking = player
defending = robot
else:
attacking = robot
defending = player
diff = attacking.attaque - defending.resistance
if diff > 0:
defending.pv -= diff
print(f"{defending.nom} a perdu {diff} points de vie.")
else:
print(f"{defending.nom} n'a pas perdu de points de vie.")
sleep(1)
turns+=0.5
player_turn = not player_turn
if match_nul:
print(f"Après un combat acharné, persone n'a réussi à battre son adversaire.")
else:
if player.pv <= 0: winner = robot
if robot.pv <= 0: winner = player
print(f"Bravo {winner.nom}, vous avez vaincu votre adversaire.")
if winner == robot:
print("Vous avez perdu :(")
if path.exists("./ex_13_2_progress.json"):
remove("./ex_13_2_progress.json")
exit()
else:
player.pv = 10
player.resistance += randint(1, 3)
ask_next_step()
def ask_next_step():
print(f"Voulez vous combattre un autre adversaire (C) ou quitter le jeu et sauvegarder votre profil ? (Q)")
choice = input()
if choice == "C":
do_fight()
if choice == "Q":
content_to_write = json.dumps(player.__dict__)
progress_file = open("./ex_13_3_progress.json", "w")
progress_file.write(content_to_write)
exit()
do_fight()