all repos — Legends-RPG @ 78f7dd5b0cc124cef7ca6aecee53f7cd7caf2486

A fantasy mini-RPG built with Python and Pygame.

When level 3 or higher, player can select two actions.
Justin Armstrong justinmeister@gmail.com
Fri, 23 May 2014 18:55:53 -0700
commit

78f7dd5b0cc124cef7ca6aecee53f7cd7caf2486

parent

8c36aad2af8146cf99a5d9f8a4b1c3ffce431210

3 files changed, 103 insertions(+), 30 deletions(-)

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

@@ -242,7 +242,8 @@ """Make state dictionary"""

state_dict = {'select action': self.select_action, 'select enemy': self.select_enemy, 'select item': self.select_item, - 'select magic': self.select_magic} + 'select magic': self.select_magic, + 'invisible': self.become_invisible_surface} return state_dict

@@ -352,11 +353,11 @@

return pos_list - def become_invisible_surface(self): + def become_invisible_surface(self, *args): """ Make image attribute an invisible surface. """ - self.image = pg.Surface((32, 32)) + self.image = pg.Surface(self.rect.size) self.image.set_colorkey(c.BLACK) def become_select_item_state(self):

@@ -383,6 +384,7 @@ def update(self, keys):

""" Update arrow position. """ + self.image = setup.GFX['smallarrow'] state_function = self.state_dict[self.state] state_function(keys)
M data/components/person.pydata/components/person.py

@@ -588,6 +588,7 @@ """

def __init__(self, sheet_key, x, y, direction='down', state='resting', index=0): super(Enemy, self).__init__(sheet_key, x, y, direction, state, index) self.level = 1 + self.type = 'enemy' class Chest(Person):
M data/states/battle.pydata/states/battle.py

@@ -40,6 +40,23 @@ self.select_action_state_dict = self.make_selection_state_dict()

self.observers = [observer.Battle(self)] self.player.observers.extend(self.observers) self.damage_points = pg.sprite.Group() + self.player_actions = [] + self.player_action_dict = self.make_player_action_dict() + self.player_level = self.game_data['player stats']['Level'] + self.enemies_to_attack = [] + self.action_selected = False + + def make_player_action_dict(self): + """ + Make the dict to execute player actions. + """ + action_dict = {c.PLAYER_ATTACK: self.enter_player_attack_state, + c.CURE_SPELL: self.cast_cure, + c.FIRE_SPELL: self.cast_fire_blast, + c.DRINK_HEALING_POTION: self.enter_drink_healing_potion_state, + c.DRINK_ETHER_POTION: self.enter_drink_ether_potion_state} + + return action_dict def make_enemy_level_dict(self): new_dict = {c.OVERWORLD: 1,

@@ -140,6 +157,7 @@ self.info_box.update()

self.arrow.update(keys) self.sword.update(current_time) self.damage_points.update() + self.execute_player_actions() self.draw_battle(surface)

@@ -158,24 +176,30 @@ self.arrow.rect.topleft]

enter_state_function() elif self.state == c.SELECT_ENEMY: - self.enter_player_attack_state() + self.player_actions.append(c.PLAYER_ATTACK) + self.enemies_to_attack.append(self.get_enemy_to_attack()) + self.action_selected = True elif self.state == c.SELECT_ITEM: if self.arrow.index == (len(self.arrow.pos_list) - 1): self.enter_select_action_state() elif self.info_box.item_text_list[self.arrow.index][:14] == 'Healing Potion': - self.enter_drink_healing_potion_state() + self.player_actions.append(c.DRINK_HEALING_POTION) + self.action_selected = True elif self.info_box.item_text_list[self.arrow.index][:5] == 'Ether': - self.enter_drink_ether_potion_state() + self.player_actions.append(c.DRINK_ETHER_POTION) + self.action_selected = True elif self.state == c.SELECT_MAGIC: if self.arrow.index == (len(self.arrow.pos_list) - 1): self.enter_select_action_state() elif self.info_box.magic_text_list[self.arrow.index] == 'Cure': if self.game_data['player stats']['magic points']['current'] >= 25: - self.cast_cure() + self.player_actions.append(c.CURE_SPELL) + self.action_selected = True elif self.info_box.magic_text_list[self.arrow.index] == 'Fire Blast': if self.game_data['player stats']['magic points']['current'] >= 25: - self.cast_fire_blast() + self.player_actions.append(c.FIRE_SPELL) + self.action_selected = True self.allow_input = False

@@ -196,25 +220,37 @@

if self.state in long_delay: if (self.current_time - self.timer) > 1000: if self.state == c.ENEMY_DAMAGED: - if len(self.enemy_list): - self.enter_enemy_attack_state() + if self.player_actions: + self.player_action_dict[self.player_actions[0]]() + self.player_actions.pop(0) else: - self.enter_battle_won_state() + if len(self.enemy_list): + self.enter_enemy_attack_state() + else: + self.enter_battle_won_state() elif (self.state == c.DRINK_HEALING_POTION or self.state == c.CURE_SPELL or self.state == c.DRINK_ETHER_POTION): - if len(self.enemy_list): - self.enter_enemy_attack_state() + if self.player_actions: + self.player_action_dict[self.player_actions[0]]() + self.player_actions.pop(0) else: - self.enter_battle_won_state() + if len(self.enemy_list): + self.enter_enemy_attack_state() + else: + self.enter_battle_won_state() self.timer = self.current_time elif self.state == c.FIRE_SPELL or self.state == c.CURE_SPELL: if (self.current_time - self.timer) > 1500: - if len(self.enemy_list): - self.enter_enemy_attack_state() + if self.player_actions: + self.player_action_dict[self.player_actions[0]]() + self.player_actions.pop(0) else: - self.enter_battle_won_state() + if len(self.enemy_list): + self.enter_enemy_attack_state() + else: + self.enter_battle_won_state() self.timer = self.current_time elif self.state == c.RUN_AWAY:

@@ -372,8 +408,7 @@ 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 - self.arrow.become_invisible_surface() - self.arrow.state = c.SELECT_ACTION + self.arrow.state = 'invisible' self.set_timer_to_current_time() def cast_cure(self):

@@ -385,7 +420,7 @@ HEAL_AMOUNT = self.inventory['Cure']['power']

MAGIC_POINTS = self.inventory['Cure']['magic points'] self.player.healing = True self.set_timer_to_current_time() - self.arrow.become_invisible_surface() + self.arrow.state = 'invisible' self.enemy_index = 0 self.damage_points.add( attackitems.HealthPoints(HEAL_AMOUNT, self.player.rect.topright, False))

@@ -399,7 +434,7 @@ """

self.state = self.info_box.state = c.DRINK_ETHER_POTION self.player.healing = True self.set_timer_to_current_time() - self.arrow.become_invisible_surface() + self.arrow.state = 'invisible' self.enemy_index = 0 self.damage_points.add( attackitems.HealthPoints(30,

@@ -414,7 +449,7 @@ Drink Healing Potion.

""" self.state = self.info_box.state = c.DRINK_HEALING_POTION self.player.healing = True - self.arrow.become_invisible_surface() + self.arrow.state = 'invisible' self.enemy_index = 0 self.damage_points.add( attackitems.HealthPoints(30,

@@ -449,7 +484,7 @@ """

Transition battle into the run away state. """ self.run_away = True - self.arrow.become_invisible_surface() + self.arrow.state = 'invisible' self.enemy_index = 0 self.enter_enemy_attack_state()

@@ -466,6 +501,20 @@ """

Transition battle into the Player attack state. """ self.state = c.PLAYER_ATTACK + enemy_to_attack = self.enemies_to_attack.pop(0) + if enemy_to_attack in self.enemy_list: + self.player.enter_attack_state(enemy_to_attack) + else: + if self.enemy_list: + self.player.enter_attack_state(self.enemy_list[0]) + else: + self.enter_battle_won_state() + self.arrow.state = 'invisible' + + def get_enemy_to_attack(self): + """ + Get enemy for player to attack by arrow position. + """ enemy_posx = self.arrow.rect.x + 60 enemy_posy = self.arrow.rect.y - 20 enemy_pos = (enemy_posx, enemy_posy)

@@ -475,8 +524,8 @@ for enemy in self.enemy_list:

if enemy.rect.topleft == enemy_pos: enemy_to_attack = enemy - self.player.enter_attack_state(enemy_to_attack) - self.arrow.become_invisible_surface() + return enemy_to_attack + def enter_drink_healing_potion_state(self): """

@@ -485,7 +534,7 @@ """

self.state = self.info_box.state = c.DRINK_HEALING_POTION self.player.healing = True self.set_timer_to_current_time() - self.arrow.become_invisible_surface() + self.arrow.state = 'invisible' self.enemy_index = 0 self.damage_points.add( attackitems.HealthPoints(30,

@@ -499,7 +548,7 @@ Transition battle into the Drink Ether Potion state.

""" self.state = self.info_box.state = c.DRINK_ETHER_POTION self.player.healing = True - self.arrow.become_invisible_surface() + self.arrow.state = 'invisible' self.enemy_index = 0 self.damage_points.add( attackitems.HealthPoints(30,

@@ -516,7 +565,6 @@ """

self.state = self.info_box.state = c.SELECT_ACTION self.arrow.index = 0 self.arrow.state = self.state - self.arrow.image = setup.GFX['smallarrow'] def enter_player_damaged_state(self): """

@@ -550,7 +598,6 @@ self.player.attacked_enemy.rect.topright))

self.info_box.set_enemy_damage(enemy_damage) - self.arrow.state = c.SELECT_ACTION self.arrow.index = 0 self.attack_enemy(enemy_damage) self.set_timer_to_current_time()

@@ -568,7 +615,7 @@ """

Transition battle into the run away state. """ self.state = self.info_box.state = c.RUN_AWAY - self.arrow.become_invisible_surface() + self.arrow.state = 'invisible' self.player.state = c.RUN_AWAY self.set_timer_to_current_time()

@@ -594,6 +641,29 @@ """

self.state = self.info_box.state = c.LEVEL_UP self.info_box.reset_level_up_message() self.set_timer_to_current_time() + + def execute_player_actions(self): + """ + Execute the player actions. + """ + if self.player_level < 3: + if self.player_actions: + enter_state = self.player_action_dict[self.player_actions[0]] + enter_state() + self.player_actions.pop(0) + else: + if len(self.player_actions) == 2: + enter_state = self.player_action_dict[self.player_actions[0]] + enter_state() + self.player_actions.pop(0) + self.action_selected = False + else: + if self.action_selected: + self.enter_select_action_state() + self.action_selected = False + + +