If the player returns to the town after going to the castle, he will be placed at the place he entered the castle, rather than just resetting to the bottom of the map.
jump to
@@ -8,8 +8,9 @@ class Person(pg.sprite.Sprite):
"""Base class for all world characters controlled by the computer""" - def __init__(self, sheet_key, x, y, direction='down', state='animated resting'): + def __init__(self, sheet_key, x, y, direction='right', state='resting'): super(Person, self).__init__() + self.name = sheet_key self.get_image = setup.tools.get_image self.spritesheet_dict = self.create_spritesheet_dict(sheet_key) self.animation_dict = self.create_animation_dict()@@ -33,7 +34,6 @@ self.state = state
self.blockers = self.set_blockers() self.location = self.get_tile_location() self.dialogue = ['Placeholder Dialogue'] - self.name = sheet_key def create_spritesheet_dict(self, sheet_key):@@ -112,7 +112,7 @@ def set_blockers(self):
"""Sets blockers to prevent collision with other sprites""" blockers = [] - if self.state == 'animated resting' or self.state == 'autoresting': + if self.state == 'resting' or self.state == 'autoresting': blockers.append(pg.Rect(self.rect.x, self.rect.y, 32, 32)) elif self.state == 'moving' or self.state == 'automoving':@@ -139,20 +139,22 @@
def get_tile_location(self): """Converts pygame coordinates into tile coordinates""" if self.rect.x == 0: - tile_x = 1 + tile_x = 0 elif self.rect.x % 32 == 0: - tile_x = (self.rect.x / 32) + 1 + tile_x = (self.rect.x / 32) else: tile_x = 0 + if self.rect.y == 0: - tile_y = 1 + tile_y = 0 elif self.rect.y % 32 == 0: - tile_y = (self.rect.y / 32) + 1 + tile_y = (self.rect.y / 32) + else: tile_y = 0 - return (tile_x, tile_y) + return [tile_x, tile_y] def resting(self):@@ -238,7 +240,8 @@ Determine when to move a sprite from resting to moving in a random
direction. """ #self.image = self.image_list[self.index] - self.animation(700) + 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))@@ -309,7 +312,7 @@
class Soldier(Person): """Soldier for the castle""" - def __init__(self, x, y, direction='down', state='animated resting'): + def __init__(self, x, y, direction='down', state='resting'): super(Soldier, self).__init__('soldier', x, y, direction, state)@@ -319,6 +322,7 @@ """Female Person for town"""
def __init__(self, x, y): super(FemaleVillager, self).__init__('femalevillager', x, y) + self.index = 1 class MaleVillager(Person):
@@ -115,19 +115,19 @@ player = self.player
tile_x, tile_y = player.location if player.direction == 'up': - if sprite.location == (tile_x, tile_y - 1): + if sprite.location == [tile_x, tile_y - 1]: self.textbox = DialogueBox(400, sprite.dialogue) sprite.direction = 'down' elif player.direction == 'down': - if sprite.location == (tile_x, tile_y + 1): + if sprite.location == [tile_x, tile_y + 1]: self.textbox = DialogueBox(400, sprite.dialogue) sprite.direction = 'up' elif player.direction == 'left': - if sprite.location == (tile_x - 1, tile_y): + if sprite.location == [tile_x - 1, tile_y]: self.textbox = DialogueBox(400, sprite.dialogue) sprite.direction = 'right' elif player.direction == 'right': - if sprite.location == (tile_x + 1, tile_y): + if sprite.location == [tile_x + 1, tile_y]: self.textbox = DialogueBox(400, sprite.dialogue) sprite.direction = 'left'
@@ -28,3 +28,5 @@ SFX = tools.load_all_sfx(os.path.join('resources', 'sound'))
FONT = pg.font.Font(FONTS['Fixedsys500c'], 20) + +
@@ -5,10 +5,12 @@ only purpose of castle.py is to assign dialogue to each sprite.
""" from .. import level_state +from ... import constants as c class Castle(level_state.LevelState): def __init__(self, name): super(Castle, self).__init__(name) + self.parent_level = c.TOWN def set_sprite_dialogue(self): """Sets unique dialogue for each sprite"""
@@ -0,0 +1,60 @@
+0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000
@@ -0,0 +1,51 @@
+0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +0000000000000000000000000 +BBBBBBBBBBBBBBBBBBBBBBBBB
@@ -6,6 +6,7 @@ from. This class inherits from the generic state class
found in the tools.py module. """ +import copy import pygame as pg from .. import tools, collision from .. import tilemap as tm@@ -27,20 +28,22 @@ self.viewport = tm.create_viewport(self.town_map)
self.blockers = tm.create_blockers(self.name) self.level_surface = tm.create_level_surface(self.town_map) self.level_rect = self.level_surface.get_rect() - self.player = person.Player('up') - self.town_sprites = pg.sprite.Group() + self.player = person.Player(persist['last direction']) + self.level_sprites = pg.sprite.Group() self.start_positions = tm.set_sprite_positions(self.player, - self.town_sprites, - self.name) + self.level_sprites, + self.name, + self.persist) self.set_sprite_dialogue() self.collision_handler = collision.CollisionHandler(self.player, self.blockers, - self.town_sprites) + self.level_sprites) self.dialogue_handler = textbox.DialogueHandler(self.player, - self.town_sprites, + self.level_sprites, self) self.state_dict = self.make_state_dict() self.portals = tm.make_level_portals(self.name) + self.parent_level = None def set_sprite_dialogue(self):@@ -61,7 +64,7 @@ """Update level normally"""
self.check_for_dialogue() self.check_for_portals() self.player.update(keys, current_time) - self.town_sprites.update(current_time) + self.level_sprites.update(current_time) self.collision_handler.update(keys, current_time) self.dialogue_handler.update(keys, current_time) self.viewport_update()@@ -74,10 +77,43 @@ """Check if the player walks into a door, requiring a level change"""
portal = pg.sprite.spritecollideany(self.player, self.portals) 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 + def update_game_data(self): + """Update the persistant game data dictionary""" + self.persist['last location'] = self.player.location + self.persist['last direction'] = self.player.direction + self.persist['last state'] = self.name + + self.set_new_start_pos() + + + def set_new_start_pos(self): + """Set new start position based on previous state""" + location = copy.deepcopy(self.persist['last location']) + direction = self.persist['last direction'] + state = self.persist['last state'] + + if direction == 'up': + location[1] += 1 + elif direction == 'down': + location[1] -= 1 + elif direction == 'left': + location[0] += 1 + elif direction == 'right': + location[0] -= 1 + + self.persist[state + ' start pos'] = location + + + + + + def handling_dialogue(self, surface, keys, current_time): """Update only dialogue boxes""" self.dialogue_handler.update(keys, current_time)@@ -106,7 +142,7 @@ def draw_level(self, surface):
"""Blits all images to screen""" self.level_surface.blit(self.town_map['surface'], self.viewport, self.viewport) self.level_surface.blit(self.player.image, self.player.rect) - self.town_sprites.draw(self.level_surface) + self.level_sprites.draw(self.level_surface) surface.blit(self.level_surface, (0, 0), self.viewport) self.dialogue_handler.draw(surface)
@@ -9,8 +9,8 @@ 00V000000000000000000V000
00V000000000000000000V000 0000000000000000000000000 0000000000000000000000000 -00000000000BB000000000000 -MMMMMMMMMMMBBMMMMMMMMMMMM +0000000000000000000000000 +MMMMMMMMMMM00MMMMMMMMMMMM 0000000000000000000000000 0000000000011000000000000 0000000000011000000000000
@@ -12,23 +12,23 @@ super(Town, self).__init__(name)
def set_sprite_dialogue(self): """Sets unique dialogue for each sprite""" - for sprite in self.town_sprites: - if sprite.location == (10, 47): + for sprite in self.level_sprites: + if sprite.location == [9, 46]: sprite.dialogue = ['Welcome to our town, Mr. Traveller!', 'The King is loved by all!', 'You should go visit him in his castle.'] - elif sprite.location == (16, 42): + elif sprite.location == [15, 41]: sprite.dialogue = ['You seem tired, why not rest at our Inn?'] sprite.begin_auto_resting() - elif sprite.location == (14, 14): + elif sprite.location == [13, 13]: sprite.dialogue = ['Be careful. There are monsters surrounding our town.', 'Make sure to equip sufficient armour and weapons.', 'Spells and potions are useful too.'] - elif sprite.location == (11, 14): + elif sprite.location == [10, 13]: sprite.dialogue = ['I have heard rumours that the King has lost something...', 'Perhaps you should pay him a visit.'] - elif sprite.location == (11, 8): + elif sprite.location == [10, 7]: sprite.dialogue = ['Welcome to the castle, citizen.'] - elif sprite.location == (14, 8): + elif sprite.location == [13, 7]: sprite.dialogue = ['Move along.']
@@ -328,15 +328,18 @@ """Create the viewport to view the level through"""
return setup.SCREEN.get_rect(bottom=map['rect'].bottom) -def set_sprite_positions(player, level_sprites, state): +def set_sprite_positions(player, level_sprites, state, game_data): """Set the start positions for all the sprites in the level""" + start_pos_key = state + ' start pos' + x = game_data[start_pos_key][0]*32 + y = game_data[start_pos_key][1]*32 + player.rect = pg.Rect(x, y, 32, 32) + tile_map = open(os.path.join('data', 'states', state, 'sprite_start_pos.txt'), 'r') for row, line in enumerate(tile_map): for column, letter in enumerate(line): - if letter == 'P': - player.rect = pg.Rect(column*32, row*32, 32, 32) - elif letter == 'F': + if letter == 'F': fem_villager = person.FemaleVillager(column*32, row*32) level_sprites.add(fem_villager) elif letter == 'S':@@ -361,6 +364,8 @@ for row, line in enumerate(tile_map):
for column, letter in enumerate(line): if letter == 'A': portal_group.add(portal.Portal(column, row, c.CASTLE)) + elif letter == 'B': + portal_group.add(portal.Portal(column, row, c.TOWN)) return portal_group
@@ -150,6 +150,18 @@
return image +def create_game_data_dict(): + """Create a dictionary of persistant values the player + carries between states""" + data_dict = {'last location': None, + 'last state': None, + 'last direction': 'up', + 'town start pos': [12, 49], + 'castle start pos': [12, 49]} + + return data_dict + +