-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFightGame.py
118 lines (93 loc) · 3.14 KB
/
FightGame.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import random
class RangeError(Exception):
def __init__(self, value):
self.value = value
def __str__(self):
return repr(self.value)
class Character:
actions = ['upper', 'middle', 'lower']
def __init__(self):
self.name = 'Character'
self.hp = 100
self.defense = self.actions[random.randint(0,2)]
self.attack = self.actions[random.randint(0,2)]
def action(self, other):
if self.attack != other.defense:
attack_power = random.randint(5,10)
other.hp -= attack_power
print self.name,'hit',other.name,'for',attack_power,'hp!'
print self.name,'attacked with',self.attack,'!'
print other.name,'tried to block with',self.defense,'.'
else:
print other.name,'successfully blocked',self.name,'\'s', self.attack,'attack!'
def askForAction(self, other):
print '1 -> upper'
print '2 -> middle'
print '3 -> lower'
while True:
try:
attack = int(raw_input('How do you want to attack? '))
if not (attack >= 1 and attack <= 3):
raise RangeError('You can only enter the numbers 1 to 3.')
break
except ValueError:
print 'You can only enter numbers!'
except RangeError as e:
print e.value
self.attack = self.actions[attack-1];
while True:
try:
defend = int(raw_input('How do you want to defend? '))
if not (defend >= 1 and defend <= 3):
raise RangeError('You can only enter the numbers 1 to 3.')
break
except ValueError:
print 'You can only enter numbers!'
except RangeError as e:
print e.value
self.defense = self.actions[defend-1];
self.action(other)
class Player(Character):
def __init__(self):
Character.__init__(self)
self.name = 'Player'
class Enemy(Character):
def __init__(self):
Character.__init__(self)
self.name = 'Powerful Enemy'
self.hp = 200
def decideAction(self, other):
self.defense = self.actions[random.randint(0,2)]
self.attack = self.actions[random.randint(0,2)]
self.action(other)
player = Player()
enemy = Enemy()
player.name = str(raw_input('What is your name? '))
print 'Player:'
print player.name
print player.hp
print 'versus'
print 'Enemy:'
print enemy.name
print enemy.hp
def we_do_not_have_a_winner(player, enemy):
if player.hp <= 0:
return False
if enemy.hp <= 0:
return False
return True
def who_is_the_winner(player, enemy):
if player.hp <= 0:
congratulations_winner(enemy)
if enemy.hp <= 0:
congratulations_winner(player)
def congratulations_winner(character):
print "Congratulations",character.name,", you are the winner!"
while we_do_not_have_a_winner(player, enemy):
player.askForAction(enemy)
print
enemy.decideAction(player)
print
print 'Player hp:',player.hp,'Enemy hp:',enemy.hp
print
who_is_the_winner(player, enemy)