all repos — Legends-RPG @ 68e9a52fe83686831824476d53ff7135dbcec2cf

A fantasy mini-RPG built with Python and Pygame.

Added transitions between scenes.
Justin Armstrong justinmeister@gmail.com
Tue, 27 May 2014 13:18:03 -0700
commit

68e9a52fe83686831824476d53ff7135dbcec2cf

parent

7ffb96f9e29029896fd0d90de035beb42211b4ce

M data/components/person.pydata/components/person.py

@@ -277,10 +277,10 @@ #self.image = self.image_list[self.index]

self.image_list = self.animation_dict[self.direction] self.image = self.image_list[self.index] - assert(self.rect.y % 32 == 0), ('Player not centered on tile: ' - + str(self.rect.y)) - assert(self.rect.x % 32 == 0), ('Player not centered on tile' - + str(self.rect.x)) + if self.rect.y % 32 != 0: + self.correct_position(self.rect.y) + if self.rect.x % 32 != 0: + self.correct_position(self.rect.x) if (self.current_time - self.move_timer) > 2000: direction_list = ['up', 'down', 'left', 'right']

@@ -288,6 +288,17 @@ random.shuffle(direction_list)

direction = direction_list[0] self.begin_auto_moving(direction) self.move_timer = self.current_time + + def correct_position(self, rect_pos): + """ + Adjust sprite position to be centered on tile. + """ + diff = rect_pos % 32 + if diff <= 16: + rect_pos - diff + else: + rect_pos + diff + def battle_resting(self): """

@@ -475,6 +486,8 @@ self.damage_alpha = 0

self.healing_alpha = 0 self.fade_in = True self.game_data = game_data + self.index = 1 + self.image = self.image_list[self.index] @property def level(self):
M data/components/textbox.pydata/components/textbox.py

@@ -145,29 +145,38 @@ self.check_for_item()

elif self.talking_sprite.battle: self.game_data['battle type'] = self.talking_sprite.battle self.dialogue_reset() - self.talking_sprite = None - self.level.state = 'normal' - self.level.switch_to_battle = True - self.textbox = None - self.last_textbox_timer = current_time - self.reset_sprite_direction() + self.end_dialogue(current_time) elif self.talking_sprite.name == 'oldmanbrother' and \ self.game_data['talked to sick brother'] and \ not self.game_data['has brother elixir']: self.talking_sprite.item = 'ELIXIR' self.game_data['has brother elixir'] = True self.check_for_item() + elif self.talking_sprite.name == 'oldman': + if self.game_data['has brother elixir'] and \ + not self.game_data['elixir received']: + del self.game_data['player inventory']['ELIXIR'] + self.game_data['elixir received'] = True + self.dialogue_reset() + else: + self.end_dialogue(current_time) else: self.dialogue_reset() - self.talking_sprite = None - self.level.state = 'normal' - self.textbox = None - self.last_textbox_timer = current_time - self.reset_sprite_direction() + self.end_dialogue(current_time) + if not keys[pg.K_SPACE]: self.allow_input = True + def end_dialogue(self, current_time): + """ + End dialogue state for level. + """ + self.talking_sprite = None + self.level.state = 'normal' + self.textbox = None + self.last_textbox_timer = current_time + self.reset_sprite_direction() def check_for_dialogue(self, sprite): """Checks if a sprite is in the correct location to give dialogue"""

@@ -194,6 +203,8 @@ if sprite.location == [tile_x + 1, tile_y]:

self.textbox = DialogueBox(sprite.dialogue) sprite.direction = 'left' self.talking_sprite = sprite + + def check_for_item(self):
M data/constants.pydata/constants.py

@@ -31,9 +31,11 @@ BLACK_BLUE = 19, 15, 48

NEAR_BLACK_BLUE = 20, 15, 48 LIGHT_BLUE = 0, 153, 204 DARK_RED = 118, 27, 12 +REALLY_DARK_RED = 15, 0, 0 RED = 255, 0, 0 GREEN = 0, 255, 0 PINK = 208, 32, 144 +TRANSITION_COLOR = BLACK_BLUE MAIN_FONT = 'DroidSans'

@@ -78,3 +80,4 @@ CLOTH_BELT = 'cloth_belt'

SWORD = 'sword' FIRE = 'fire' +TRANSITION_SPEED = 25
M data/shopgui.pydata/shopgui.py

@@ -236,7 +236,7 @@ self.item_to_be_purchased = self.items[1]

else: if self.level.name in self.no_selling: - self.level.done = True + self.level.state = 'transition out' self.game_data['last state'] = self.level.name else: self.state = 'buysell'

@@ -492,7 +492,7 @@ self.arrow_index = 0

else: - self.level.done = True + self.level.state = 'transition out' self.game_data['last state'] = self.level.name self.arrow_index = 0
M data/states/battle.pydata/states/battle.py

@@ -20,7 +20,7 @@ self.timer = current_time

self.allow_input = False self.game_data = game_data self.inventory = game_data['player inventory'] - self.state = c.SELECT_ACTION + self.state = 'transition in' self.next = game_data['last state'] self.run_away = False

@@ -51,6 +51,8 @@ self.player_level = self.game_data['player stats']['Level']

self.enemies_to_attack = [] self.action_selected = False self.just_leveled_up = False + self.transition_rect = setup.SCREEN.get_rect() + self.transition_alpha = 255 def make_player_action_dict(self): """

@@ -346,7 +348,7 @@ self.game_data['crown quest'] = True

self.game_data['last state'] = self.name self.game_data['battle counter'] = random.randint(50, 255) self.game_data['battle type'] = None - self.done = True + self.state = 'transition out' def attack_enemy(self, enemy_damage): enemy = self.player.attacked_enemy

@@ -377,6 +379,31 @@ surface.blit(self.select_box.image, self.select_box.rect)

surface.blit(self.arrow.image, self.arrow.rect) self.player_health_box.draw(surface) self.damage_points.draw(surface) + self.draw_transition(surface) + + def draw_transition(self, surface): + """ + Fade in and out of state. + """ + if self.state == 'transition in': + + transition_image = pg.Surface(self.transition_rect.size) + transition_image.fill(c.TRANSITION_COLOR) + transition_image.set_alpha(self.transition_alpha) + surface.blit(transition_image, self.transition_rect) + self.transition_alpha -= c.TRANSITION_SPEED + if self.transition_alpha <= 0: + self.state = c.SELECT_ACTION + self.transition_alpha = 0 + + elif self.state == 'transition out': + transition_image = pg.Surface(self.transition_rect.size) + transition_image.fill(c.TRANSITION_COLOR) + transition_image.set_alpha(self.transition_alpha) + surface.blit(transition_image, self.transition_rect) + self.transition_alpha += c.TRANSITION_SPEED + if self.transition_alpha >= 255: + self.done = True def player_damaged(self, damage): self.game_data['player stats']['health']['current'] -= damage

@@ -422,6 +449,7 @@ """

self.notify(c.FIRE) self.state = self.info_box.state = c.FIRE_SPELL POWER = self.inventory['Fire Blast']['power'] + POWER += self.game_data['player stats']['Level'] * 5 MAGIC_POINTS = self.inventory['Fire Blast']['magic points'] self.game_data['player stats']['magic']['current'] -= MAGIC_POINTS for enemy in self.enemy_list:
M data/states/levels.pydata/states/levels.py

@@ -50,7 +50,7 @@ Call when the State object is flipped to.

""" self.game_data = game_data self.current_time = current_time - self.state = 'normal' + self.state = 'transition_in' self.reset_dialogue = () self.switch_to_battle = False self.allow_input = False

@@ -74,6 +74,8 @@ self.dialogue_handler = textbox.TextHandler(self)

self.state_dict = self.make_state_dict() self.portals = self.make_level_portals() self.menu_screen = player_menu.Player_Menu(game_data, self) + self.transition_rect = setup.SCREEN.get_rect() + self.transition_alpha = 255 def make_viewport(self, map_image): """

@@ -197,9 +199,8 @@ 'treasurechest': person.Chest(x, y, id)}

sprite = sprite_dict[properties['type']] if sprite.name == 'oldman': - if self.game_data['old man gift']: + if self.game_data['old man gift'] and not self.game_data['elixir received']: sprite.item = self.game_data['old man gift'] - self.game_data['old man gift'] = {} else: sprite.item = item else:

@@ -237,8 +238,6 @@ 'This ELIXIR will cure my ailment.',

'As a reward, I will teach you a magic spell.', 'Use it wisely.', 'You learned FIRE BLAST.'] - del self.game_data['player inventory']['ELIXIR'] - self.game_data['elixir received'] = True dialogue = ['My good health is thanks to you.', 'I will be forever in your debt.'] self.reset_dialogue = sprite, dialogue

@@ -291,7 +290,9 @@ Make a dictionary of states the level can be in.

""" state_dict = {'normal': self.running_normally, 'dialogue': self.handling_dialogue, - 'menu': self.goto_menu} + 'menu': self.goto_menu, + 'transition_in': self.transition_in, + 'transition_out': self.transition_out} return state_dict

@@ -337,7 +338,7 @@ if portal and self.player.state == 'resting':

self.player.location = self.player.get_tile_location() self.next = portal.name self.update_game_data() - self.done = True + self.state = 'transition_out' def check_for_battle(self): """

@@ -348,7 +349,7 @@ if self.switch_to_battle and self.allow_battles and not self.done:

self.player.location = self.player.get_tile_location() self.update_game_data() self.next = 'battle' - self.done = True + self.state = 'transition_out' def check_for_menu(self, keys): """

@@ -391,8 +392,6 @@ location[0] += 1

elif direction == 'right': location[0] -= 1 - - def handling_dialogue(self, surface, keys, current_time): """ Update only dialogue boxes.

@@ -400,14 +399,12 @@ """

self.dialogue_handler.update(keys, current_time) self.draw_level(surface) - def goto_menu(self, surface, keys, *args): """ Go to menu screen. """ self.menu_screen.update(surface, keys) self.menu_screen.draw(surface) - def check_for_dialogue(self): """

@@ -415,6 +412,35 @@ Check if the level needs to freeze.

""" if self.dialogue_handler.textbox: self.state = 'dialogue' + + def transition_out(self, surface, *args): + """ + Transition level to new scene. + """ + transition_image = pg.Surface(self.transition_rect.size) + transition_image.fill(c.TRANSITION_COLOR) + transition_image.set_alpha(self.transition_alpha) + self.draw_level(surface) + surface.blit(transition_image, self.transition_rect) + self.transition_alpha += c.TRANSITION_SPEED + if self.transition_alpha >= 255: + self.transition_alpha = 255 + self.done = True + + def transition_in(self, surface, *args): + """ + Transition into level. + """ + self.viewport_update() + transition_image = pg.Surface(self.transition_rect.size) + transition_image.fill(c.TRANSITION_COLOR) + transition_image.set_alpha(self.transition_alpha) + self.draw_level(surface) + surface.blit(transition_image, self.transition_rect) + self.transition_alpha -= c.TRANSITION_SPEED + if self.transition_alpha <= 0: + self.state = 'normal' + self.transition_alpha = 0 def update(self, surface, keys, current_time): """
M data/states/shop.pydata/states/shop.py

@@ -24,7 +24,8 @@ def startup(self, current_time, game_data):

"""Startup state""" self.game_data = game_data self.current_time = current_time - self.state = 'normal' + self.state_dict = self.make_state_dict() + self.state = 'transition in' self.next = c.TOWN self.get_image = tools.get_image self.dialogue = self.make_dialogue()

@@ -33,7 +34,18 @@ self.accept_sale_dialogue = self.make_accept_sale_dialogue()

self.items = self.make_purchasable_items() self.background = self.make_background() self.gui = shopgui.Gui(self) + self.transition_rect = setup.SCREEN.get_rect() + self.transition_alpha = 255 + def make_state_dict(self): + """ + Make a dictionary for all state methods. + """ + state_dict = {'normal': self.normal_update, + 'transition in': self.transition_in, + 'transition out': self.transition_out} + + return state_dict def make_dialogue(self): """Make the list of dialogue phrases"""

@@ -102,12 +114,44 @@ sprite.rect = sprite.image.get_rect(left=550, top=225)

return sprite - def update(self, surface, keys, current_time): - """Update level state""" + """ + Update scene. + """ + state_function = self.state_dict[self.state] + state_function(surface, keys, current_time) + + def normal_update(self, surface, keys, current_time): + """Update level normally""" self.gui.update(keys, current_time) self.draw_level(surface) + def transition_in(self, surface, *args): + """ + Transition into level. + """ + transition_image = pg.Surface(self.transition_rect.size) + transition_image.fill(c.TRANSITION_COLOR) + transition_image.set_alpha(self.transition_alpha) + self.draw_level(surface) + surface.blit(transition_image, self.transition_rect) + self.transition_alpha -= c.TRANSITION_SPEED + if self.transition_alpha <= 0: + self.state = 'normal' + self.transition_alpha = 0 + + def transition_out(self, surface, *args): + """ + Transition level to new scene. + """ + transition_image = pg.Surface(self.transition_rect.size) + transition_image.fill(c.TRANSITION_COLOR) + transition_image.set_alpha(self.transition_alpha) + self.draw_level(surface) + surface.blit(transition_image, self.transition_rect) + self.transition_alpha += c.TRANSITION_SPEED + if self.transition_alpha >= 255: + self.done = True def draw_level(self, surface): """Blit graphics to game surface"""

@@ -169,7 +213,7 @@ longsword_dialogue = 'Long Sword (100 gold)'

rapier_dialogue = 'Rapier (50 gold)' item2 = {'type': 'Long Sword', - 'price': 100, + 'price': 150, 'quantity': 1, 'power': 10, 'dialogue': longsword_dialogue}
M data/tools.pydata/tools.py

@@ -190,8 +190,6 @@ 'Healing Potion': dict([('quantity',2),

('value',15)]), 'Ether Potion': dict([('quantity',1), ('value', 15)]), - 'Cure': dict([('magic points', 25), - ('power', 50)]), 'Rapier': dict([('quantity', 1), ('value', 50), ('power', 5)]),
M resources/tmx/overworld.tmxresources/tmx/overworld.tmx

@@ -158,7 +158,7 @@ <property name="direction" value="down"/>

<property name="state" value="town"/> </properties> </object> - <object name="start point" gid="123" x="256" y="496"> + <object name="start point" gid="123" x="256" y="368"> <properties> <property name="direction" value="down"/> <property name="state" value="main menu"/>