Character added, can sort of move around
Justin Armstrong justinmeister@gmail.com
Thu, 06 Mar 2014 10:52:16 -0800
7 files changed,
205 insertions(+),
24 deletions(-)
A
data/components/player.py
@@ -0,0 +1,97 @@
+__author__ = 'justinarmstrong' + +import pygame as pg +from .. import setup + +class Player(pg.sprite.Sprite): + """User controllable character""" + def __init__(self): + super(Player, self).__init__() + self.get_image = setup.tools.get_image + self.spritesheet_dict = self.create_spritesheet() + self.image = self.spritesheet_dict['facing down 1'] + self.rect = self.image.get_rect() + self.state_dict = self.create_state_dict() + self.direction_dict = self.create_direction_dict() + self.state = 'resting' + self.x_vel = 0 + self.y_vel = 0 + self.direction = 'up' + + + def create_spritesheet(self): + """Creates the sprite sheet dictionary for player""" + sheet = setup.GFX['player'] + image_list = [] + for row in range(2): + for column in range(4): + image_list.append(self.get_image( + self, column*32, row*32, 32, 32, sheet)) + + dict = {'facing up 1': image_list[0], + 'facing up 2': image_list[1], + 'facing down 1': image_list[2], + 'facing down 2': image_list[3], + 'facing left 1': image_list[4], + 'facing left 2': image_list[5], + 'facing right 1': image_list[6], + 'facing right 2': image_list[7]} + + return dict + + + def create_state_dict(self): + """Creates a dictionary of all the states the player + can be in""" + dict = {'resting': self.resting, + 'moving': self.moving} + + return dict + + + def create_direction_dict(self): + """Creates a dictionary of directions with truth values + corresponding to the player's direction""" + dict = {'up': (0, -2), + 'down': (0, 2), + 'left': (-2, 0), + 'right': (2, 0)} + + return dict + + + def update(self): + """Updates player behavior""" + state = self.state_dict[self.state] + state() + + + def resting(self): + """When the player is not moving between tiles. + Checks if the player is centered on a tile""" + 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)) + + + def moving(self): + """When the player is moving between tiles""" + self.rect.x += self.x_vel + self.rect.y += self.y_vel + + + def begin_moving(self, direction): + """Transitions the player into the moving state""" + self.state = 'moving' + self.direction = direction + self.x_vel, self.y_vel = self.direction_dict[self.direction] + + + def begin_resting(self): + """Transitions the player into the resting state""" + self.state = 'resting' + self.x_vel, self.y_vel = 0 + + +
A
data/states/sprite_start_pos.txt
@@ -0,0 +1,50 @@
+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 +0000000000011000000000000 +0000000000011000000000000 +0000000000011000000000000 +0000000000011000000000000 +0000000000011000000000000 +0000000000011000000000000 +0000000000011000000000000 +0000000000011000000333000 +0003330000011000000333000 +0003330000011000000222000 +0002220000011000000222000 +0002220000011000000010000 +0000100000011000000010000 +0000111111111111111110000 +0000000000011000000000000 +0000000000011000000000000 +0000000000011000000000000 +0000000000011000000000000 +00000000000P1000000000000
M
data/states/town.py
→
data/states/town.py
@@ -4,6 +4,7 @@ import os
import pygame as pg from .. import setup, tools from .. import constants as c +from ..components.player import Player class Town(tools._State): def __init__(self):@@ -15,12 +16,15 @@ """Called when the State object is created"""
self.persist = persist self.current_time = current_time self.get_image = setup.tools.get_image - self.sprite_sheet_dict = self.create_sprite_sheet_dict() + self.town_map_dict = self.create_town_sprite_sheet_dict() self.town_map = self.create_town_map() self.viewport = self.create_viewport() + self.level_surface = self.create_level_surface() + self.player = Player() + self.start_positions = self.set_sprite_positions() - def create_sprite_sheet_dict(self): + def create_town_sprite_sheet_dict(self): """Create a dictionary of sprite sheet tiles""" dict = {} tileset2 = setup.GFX['tileset2']@@ -84,15 +88,15 @@
for row, line in enumerate(tile_map): for column, letter in enumerate(line): if letter == '1': - tile = self.sprite_sheet_dict['pavement'] + tile = self.town_map_dict['pavement'] self.blit_tile_to_map(tile, row, column, map) elif letter == '2': - tile = self.sprite_sheet_dict['house wall'] + tile = self.town_map_dict['house wall'] self.blit_tile_to_map(tile, row, column, map) elif letter == '3': - tile = self.sprite_sheet_dict['house roof'] + tile = self.town_map_dict['house roof'] self.blit_tile_to_map(tile, row, column, map) tile_map.close()@@ -107,8 +111,7 @@
for row, line in enumerate(tile_map): for column, letter in enumerate(line): if letter == 'D': - print 'hi' - tile = self.sprite_sheet_dict['house door'] + tile = self.town_map_dict['house door'] self.blit_tile_to_map(tile, row, column, map) tile_map.close()@@ -117,7 +120,7 @@ return map
def scale_map(self, map): - """Doubles the size of map to fit a (800x600) aspect ratio""" + """Double resolution of map to 32x32""" map['surface'] = pg.transform.scale2x(map['surface']) map['rect'] = map['surface'].get_rect()@@ -137,11 +140,57 @@ """Create the viewport to view the level through"""
return setup.SCREEN.get_rect(bottom=self.town_map['rect'].bottom) + def create_level_surface(self): + """Creates the surface all images are blitted to""" + width = self.town_map['rect'].width + height = self.town_map['rect'].height + + return pg.Surface((width, height)).convert() + + + def set_sprite_positions(self): + """Set the start positions for all the sprites in the level""" + tile_map = open(os.path.join('data', 'states', 'sprite_start_pos.txt'), 'r') + dict = {} + + for row, line in enumerate(tile_map): + for column, letter in enumerate(line): + if letter == 'P': + dict['player'] = pg.Rect(column*32, row*32, 32, 32) + + self.player.rect = dict['player'] + + return dict + + def update(self, surface, keys, current_time): """Updates state""" self.keys = keys self.current_time = current_time - surface.blit(self.town_map['surface'], (0,0), self.viewport) + self.check_for_player_input() + self.player.update() + + self.draw_level(surface) + + + 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) + + surface.blit(self.level_surface, (0,0), self.viewport) + + + def check_for_player_input(self): + """Checks for player input""" + if self.keys[pg.K_UP]: + self.player.begin_moving('up') + elif self.keys[pg.K_DOWN]: + self.player.begin_moving('down') + elif self.keys[pg.K_LEFT]: + self.player.begin_moving('left') + elif self.keys[pg.K_RIGHT]: + self.player.begin_moving('right')
M
data/states/town_layer2.txt
→
data/states/town_layer2.txt
@@ -48,5 +48,3 @@ 0000000000011000000000000
0000000000011000000000000 0000000000011000000000000 0000000000011000000000000 -0000000000011000000000000 -0000000000011000000000000
M
data/states/town_map.txt
→
data/states/town_map.txt
@@ -48,5 +48,3 @@ 0000000000011000000000000
0000000000011000000000000 0000000000011000000000000 0000000000011000000000000 -0000000000011000000000000 -0000000000011000000000000
D
town.tmx
@@ -1,11 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?> -<map version="1.0" orientation="orthogonal" width="25" height="50" tilewidth="16" tileheight="16"> - <tileset firstgid="1" name="tileset1" tilewidth="16" tileheight="16"> - <image source="resources/graphics/tileset1.png" width="160" height="80"/> - </tileset> - <layer name="Tile Layer 1" width="25" height="50"> - <data encoding="base64" compression="zlib"> - eJzt1bENwCAQQ1FmCCkDJaRM2H+4eAWfhATKf5LL07lzSgAAAJjlULJyTvxxKUWp5p3TrSlduc0fTrdHeZVh/oh2c0S7AcDfrbqDbq/I1ri9IlvDBu7nA3MVBmE= - </data> - </layer> -</map>