all repos — Legends-RPG @ b221c8b07a9492dde7bdc22d5c245c0aa2bf5b06

A fantasy mini-RPG built with Python and Pygame.

Enemies attack you before you run, a knock back effect when damage is taken.
Justin Armstrong justinmeister@gmail.com
Sun, 04 May 2014 11:01:33 -0700
commit

b221c8b07a9492dde7bdc22d5c245c0aa2bf5b06

parent

7ec97552dc243a24c58dcd48fc20549cc8788e01

5 files changed, 71 insertions(+), 8 deletions(-)

jump to
M data/collision.pydata/collision.py

@@ -95,7 +95,7 @@ def check_for_battle(self):

""" Switch scene to battle 1/5 times if battles are allowed. """ - random_number = random.randint(0, 2) + random_number = random.randint(0, 10) if random_number == 0: self.level.switch_to_battle = True
M data/components/person.pydata/components/person.py

@@ -87,7 +87,8 @@ 'battle resting': self.battle_resting,

'attack': self.attack, 'enemy attack': self.enemy_attack, c.RUN_AWAY: self.run_away, - c.VICTORY_DANCE: self.victory_dance} + c.VICTORY_DANCE: self.victory_dance, + c.KNOCK_BACK: self.knock_back} return state_dict

@@ -406,6 +407,41 @@ for image in self.small_image_list:

self.image_list.append(pg.transform.scale2x(image)) self.animation(500) + def knock_back(self): + """ + Knock back when hit. + """ + FORWARD_VEL = -2 + + self.rect.x += self.x_vel + + if self.name == 'player': + if self.rect.x >= (self.origin_pos[0] + 10): + self.x_vel = FORWARD_VEL + elif self.rect.x <= self.origin_pos[0]: + self.rect.x = self.origin_pos[0] + self.state = 'battle resting' + self.x_vel = 0 + else: + if self.rect.x <= (self.origin_pos[0] - 10): + self.x_vel = 2 + elif self.rect.x >= self.origin_pos[0]: + self.rect.x = self.origin_pos[0] + self.state = 'battle resting' + self.x_vel = 0 + + + def enter_knock_back_state(self): + """ + Set values for entry to knock back state. + """ + if self.name == 'player': + self.x_vel = 4 + else: + self.x_vel = -4 + + self.state = c.KNOCK_BACK + self.origin_pos = self.rect.topleft class Player(Person):
M data/constants.pydata/constants.py

@@ -54,6 +54,8 @@ DRINK_HEALING_POTION = 'drink healing potion'

CURE_SPELL = 'cure spell' FIRE_SPELL = 'fire spell' VICTORY_DANCE = 'victory dance' +KNOCK_BACK = 'knock back' +FLEE = 'flee' #EVENTS
M data/observer.pydata/observer.py

@@ -39,9 +39,10 @@ c.ENEMY_ATTACK: self.enemy_attack,

c.SWITCH_ENEMY: self.switch_enemy, c.PLAYER_ATTACK: self.player_attack, c.ATTACK_ANIMATION: self.enemy_damaged, - c.RUN_AWAY: self.run_away, + c.RUN_AWAY: self.try_to_run_away, + c.FLEE: self.flee, c.BATTLE_WON: self.battle_won, - c.ENEMY_ATTACK_DAMAGE: self.display_enemy_attack_damage, + c.ENEMY_ATTACK_DAMAGE: self.player_damaged, c.DRINK_HEALING_POTION: self.drink_healing_potion, c.CURE_SPELL: self.cure_spell, c.FIRE_SPELL: self.fire_spell}

@@ -97,7 +98,7 @@ if self.level.enemy_index < len(self.level.enemy_list) - 1:

self.level.enemy_index += 1 self.on_notify(c.ENEMY_ATTACK) - def display_enemy_attack_damage(self): + def player_damaged(self): if self.enemy_index > len(self.enemy_list) - 1: self.enemy_index = 0 enemy = self.enemy_list[self.enemy_index]

@@ -112,6 +113,7 @@ self.level.set_timer_to_current_time()

self.level.player_damaged(player_damage) if player_damage: self.player.damaged = True + self.player.enter_knock_back_state() def player_attack(self): enemy_posx = self.arrow.rect.x + 60

@@ -144,11 +146,27 @@ self.level.attack_enemy(enemy_damage)

self.level.set_timer_to_current_time() self.level.state = c.ENEMY_HIT - def run_away(self): + + def try_to_run_away(self): + """ + Sets a flag in the battle state indicating a desire to run. + Allows enemies to get one last hit in. + """ + self.level.run_away = True + self.level.state = c.ENEMY_ATTACK + self.arrow.become_invisible_surface() + self.level.enemy_index = 0 + self.on_notify(c.ENEMY_ATTACK) + + def flee(self): + """ + Actually run away after enemies get there chance to attack. + """ self.info_box.state = c.RUN_AWAY self.level.set_timer_to_current_time() self.arrow.become_invisible_surface() self.player.state = c.RUN_AWAY + def battle_won(self): self.info_box.state = c.BATTLE_WON
M data/states/battle.pydata/states/battle.py

@@ -46,6 +46,7 @@ self.game_data = game_data

self.inventory = game_data['player inventory'] self.state = c.SELECT_ACTION self.next = game_data['last state'] + self.run_away = False self.player = self.make_player() self.attack_animations = pg.sprite.Group()

@@ -214,14 +215,17 @@ self.state = c.BATTLE_WON

self.timer = self.current_time self.notify(self.state) - elif self.state == c.RUN_AWAY or self.state == c.BATTLE_WON: + elif self.state == c.FLEE or self.state == c.BATTLE_WON: if (self.current_time - self.timer) > 1500: self.end_battle() elif self.state == c.DISPLAY_ENEMY_ATTACK_DAMAGE: if (self.current_time - self.timer) > 600: if self.enemy_index == (len(self.enemy_list) - 1): - self.state = c.SELECT_ACTION + if self.run_away: + self.state = c.FLEE + else: + self.state = c.SELECT_ACTION else: self.state = c.SWITCH_ENEMY self.timer = self.current_time

@@ -255,6 +259,7 @@ enemy.health -= enemy_damage

self.set_enemy_indices() if enemy: + enemy.enter_knock_back_state() if enemy.health <= 0: enemy.kill() self.enemy_list.pop(enemy.index)

@@ -324,6 +329,8 @@ self.attack_animations.add(fire_sprite)

if enemy.health <= 0: enemy.kill() self.arrow.remove_pos(enemy) + else: + enemy.enter_knock_back_state() self.enemy_list = [enemy for enemy in self.enemy_list if enemy.health > 0] self.enemy_index = 0 self.arrow.index = 0