Skip to content

Commit c88e8bb

Browse files
committed
WIP: let player use magic
1 parent 1fab3f2 commit c88e8bb

File tree

4 files changed

+61
-19
lines changed

4 files changed

+61
-19
lines changed

core/core.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@ def handle_user_input(self):
4848

4949
def check_damages(self):
5050
if self.hero.is_dead:
51-
print 'You lose'
52-
self.stop()
51+
self.stop(message='You lose !')
5352

5453
def draw(self):
5554
self.hero.show_stats()
@@ -85,3 +84,4 @@ def stop(self, message=None):
8584
self.running = False
8685
if message:
8786
print message
87+
_ = raw_input('Press any key...')

core/entities/__init__.py

+37
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ def right(self):
4343
def left(self):
4444
self.update_level(**{'action': ['left', self.movement]})
4545

46+
def fight_with(self, enemy):
47+
raise NotImplementedError
48+
49+
def perform_physical_attack(self, enemy):
50+
raise NotImplementedError
51+
4652

4753
class Enemy(Entity):
4854

@@ -59,6 +65,12 @@ def __init__(self, *args, **kwargs):
5965
def move(self):
6066
raise NotImplementedError
6167

68+
def fight_with(self, enemy):
69+
self.perform_physical_attack(enemy)
70+
71+
def perform_physical_attack(self, enemy):
72+
enemy.hp = enemy.hp - self.attack
73+
6274

6375
class Blob(Enemy):
6476
symbol = 'O'
@@ -105,6 +117,10 @@ class Hero(Entity):
105117
hp = 10
106118
max_hp = 10
107119
attack = 5
120+
battle_operations = {
121+
0: 'attack',
122+
1: 'magic'
123+
}
108124

109125
def __init__(self, *args, **kwargs):
110126
initial_attrs = kwargs.pop('hero_attrs', {})
@@ -136,3 +152,24 @@ def use_item(self):
136152
item.use(self)
137153
except ValueError:
138154
pass
155+
156+
def fight_with(self, enemy):
157+
attacking = True
158+
while attacking:
159+
print '0) attack'
160+
print '1) magic'
161+
choose = raw_input('Choose an action')
162+
try:
163+
ch = int(choose)
164+
choice = self.battle_operations.get(ch)
165+
if choice:
166+
if choice == 'attack':
167+
self.perform_physical_attack(enemy)
168+
elif choice == 'magic':
169+
continue
170+
attacking = False
171+
except ValueError:
172+
pass
173+
174+
def perform_physical_attack(self, enemy):
175+
enemy.hp = enemy.hp - self.attack

core/levels/__init__.py

+11-6
Original file line numberDiff line numberDiff line change
@@ -142,15 +142,13 @@ def update(self, *args, **kwargs):
142142

143143
def fight(self, attacker, defender):
144144
while True:
145-
defender.hp = defender.hp - attacker.attack
145+
attacker.fight_with(defender)
146146
if defender.is_dead:
147147
if isinstance(defender, Enemy):
148-
x = self.coordinates[defender]['x']
149-
y = self.coordinates[defender]['y']
150-
self.level_map[x][y] = None
151-
del self.coordinates[defender]
152-
self.enemies.remove(defender)
148+
self.remove_entity(defender)
153149
self.draw()
150+
print 'You win !'
151+
_ = raw_input('Press any key...')
154152
raise BattleFinishedException
155153
attacker, defender = defender, attacker
156154

@@ -162,6 +160,13 @@ def add_entity_at_coordinates(self, entity, x, y):
162160
}
163161
self.level_map[x][y] = entity
164162

163+
def remove_entity(self, entity):
164+
x = self.coordinates[entity]['x']
165+
y = self.coordinates[entity]['y']
166+
self.level_map[x][y] = None
167+
del self.coordinates[entity]
168+
self.enemies.remove(entity)
169+
165170
def move_enemies(self):
166171
for enemy in self.enemies:
167172
enemy.move()

test_level.json

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
{
22
"map":
33
[
4-
["#", "#", "#", "#", "#", "#", "#", "#"],
5-
["#", ".", "M",".", ".", ".", ".", "#"],
6-
["#", ".", ".",".", ".", ".", "Z", "#"],
7-
["#", ".", ".",".", ".", ".", ".", "#"],
8-
["#", ".", ".",".", ".", "M", ".", "#"],
9-
["#", "O", ".",".", "#", ".", ".", "#"],
10-
["#", ".", ".",".", "#", ".", ".", "#"],
11-
["#", ".", "#", ".", "#", "#", ".", "#"],
12-
["#", ".", ".",".", "#", ".", "O", "#"],
13-
["#", ".", ".",".", "#", ".", ".", "#"],
14-
["#", "#", "#", "#", "#", "#", "#", "#"]
4+
["#", "#", "#", "#", "#", "#", "#", "#", "#"],
5+
["#", ".", "M",".", ".", ".", ".", ".", "#"],
6+
["#", ".", ".",".", ".", ".", "Z", ".", "#"],
7+
["#", ".", ".",".", ".", ".", ".", ".", "#"],
8+
["#", ".", ".",".", ".", "M", ".", ".", "#"],
9+
["#", "O", ".",".", "#", ".", ".", ".", "#"],
10+
["#", ".", ".",".", "#", ".", ".", ".", "#"],
11+
["#", ".", "#", ".", "#", "#", ".", ".", "#"],
12+
["#", ".", ".",".", "#", ".", "O", ".", "#"],
13+
["#", ".", ".",".", "#", ".", ".", ".", "#"],
14+
["#", "#", "#", "#", "#", "#", "#", "#", "#"]
1515
],
1616
"name": "level_1"
1717
}

0 commit comments

Comments
 (0)