Added blockers so player can't walk into obstacles
Justin Armstrong justinmeister@gmail.com
Sun, 09 Mar 2014 21:40:43 -0700
4 files changed,
116 insertions(+),
15 deletions(-)
A
data/collision.py
@@ -0,0 +1,36 @@
+__author__ = 'justinarmstrong' + +class CollisionHandler(object): + """Handles collisions between the user, blockers and computer + characters""" + def __init__(self, player, blockers): + self.player = player + self.blockers = blockers + self.collided = False + + def update(self): + """Checks for collisions between game objects""" + self.player.rect.x += self.player.x_vel + self.player.rect.y += self.player.y_vel + + self.check_for_blockers() + + if self.player.rect.x % 32 == 0 and self.player.rect.y % 32 == 0: + self.player.begin_resting() + + + def check_for_blockers(self): + """Checks for collisions with blocker rects""" + for blocker in self.blockers: + if self.player.rect.colliderect(blocker): + self.collided = True + + if self.collided: + if self.player.x_vel != 0: + self.player.rect.x -= self.player.x_vel + else: + self.player.rect.y -= self.player.y_vel + + self.collided = False + self.player.begin_resting() +
M
data/components/player.py
→
data/components/player.py
@@ -62,8 +62,6 @@
return dict - - def create_state_dict(self): """Creates a dictionary of all the states the player can be in"""@@ -74,8 +72,8 @@ return dict
def create_direction_dict(self): - """Creates a dictionary of directions with truth values - corresponding to the player's direction""" + """Creates a dictionary of x and y velocities set to + direction keys""" dict = {'up': (0, -2), 'down': (0, 2), 'left': (-2, 0),@@ -89,8 +87,8 @@ """Updates player behavior"""
self.keys = keys self.current_time = current_time self.check_for_input() - state = self.state_dict[self.state] - state() + state_function = self.state_dict[self.state] + state_function() def resting(self):@@ -106,9 +104,6 @@
def moving(self): """When the player is moving between tiles""" - self.rect.x += self.x_vel - self.rect.y += self.y_vel - if (self.current_time - self.timer) > 100: if self.index < (len(self.image_list) - 1): self.index += 1@@ -117,9 +112,6 @@ self.index = 0
self.timer = self.current_time self.image = self.image_list[self.index] - - if self.rect.x % 32 == 0 and self.rect.y % 32 == 0: - self.begin_resting() assert(self.rect.x % 32 == 0 or self.rect.y % 32 == 0), \ 'Not centered on tile'@@ -127,10 +119,11 @@
def begin_moving(self, direction): """Transitions the player into the moving state""" - self.state = 'moving' self.direction = direction self.image_list = self.animation_lists[direction] self.timer = self.current_time + self.state = 'moving' + if self.rect.x % 32 == 0: self.y_vel = self.direction_dict[self.direction][1]@@ -143,6 +136,8 @@ """Transitions the player into the resting state"""
self.state = 'resting' self.index = 1 self.x_vel = self.y_vel = 0 + + def check_for_input(self):
M
data/states/town.py
→
data/states/town.py
@@ -2,9 +2,9 @@ __author__ = 'justinarmstrong'
import os import pygame as pg -from .. import setup, tools +from .. import setup, tools, collision from .. import constants as c -from ..components.player import Player +from .. components.player import Player class Town(tools._State): def __init__(self):@@ -18,11 +18,14 @@ self.current_time = current_time
self.get_image = setup.tools.get_image self.town_map_dict = self.create_town_sprite_sheet_dict() self.town_map = self.create_town_map() + self.blockers = self.create_blockers() self.viewport = self.create_viewport() self.level_surface = self.create_level_surface() self.level_rect = self.level_surface.get_rect() self.player = Player() self.start_positions = self.set_sprite_positions() + self.collision_handler = collision.CollisionHandler(self.player, + self.blockers) def create_town_sprite_sheet_dict(self):@@ -252,6 +255,22 @@
map['surface'].blit(tile['surface'], tile['rect']) + def create_blockers(self): + """Creates invisible rect objects that will prevent the player from + walking into trees, buildings and other solid objects""" + tile_map = open(os.path.join('data', 'states', 'town_blocker_layer.txt'), 'r') + blocker_list = [] + + for row, line in enumerate(tile_map): + for column, letter in enumerate(line): + if letter == 'B': + blocker_list.append(pg.Rect(column*32, row*32, 32, 32)) + + tile_map.close() + + return blocker_list + + def create_viewport(self): """Create the viewport to view the level through""" return setup.SCREEN.get_rect(bottom=self.town_map['rect'].bottom)@@ -285,6 +304,7 @@ """Updates state"""
self.keys = keys self.current_time = current_time self.player.update(keys, current_time) + self.collision_handler.update() self.update_viewport() self.draw_level(surface)
A
data/states/town_blocker_layer.txt
@@ -0,0 +1,50 @@
+0000000000000000000000000 +00BBBBBBBBBBBBBBBBBBBB000 +00BBBBBBBBBBBBBBBBBBBB000 +00B0000BBBBBBBBBB0000B000 +00B0000BBBBBBBBBB0000B000 +00B0000BBBBBBBBBB0000B000 +00B0000BBBB00BBBB0000B000 +0BB000000000000000000B000 +0BB00000000000000000BB000 +00BBBBBBBBB00BBBBBBBBB000 +00BBBBBBBBB00BBBBBBBBB000 +000B0000000000000000B0000 +BBBBBBBBBBB00BBBBBBBBBBBB +B00000000000000000000000B +000000000001100000000000B +T00000000001100000000000B +000000000001100000000000B +T00000000001100000000000B +000000000001100000000000B +BBBB0BBB000110000BBB0BBBB +033B0B3B000110000B3B0B330 +T22B0B2B000110000B2B0B22T +0B2B0B2B000110000B2B0B2B0 +B01000100001100000100010B +0B111111111111111111111B0 +BB000000000110000000000BT +0B000000000110000000000B0 +T0B00000001GG100000000B0T +0T0B000001GGGG1000000B0T0 +T0BB0B00001GG1000000BBB0T +0BB00B00000110000000B0BT0 +0B000000000110000000000B0 +BBBBBBBBBBB11BBBBBBBBBBBB +0B000000000110000000000B0 +TB000000000110000000000BT +0B000000000110000000000B0 +T0T0B000000110000000000BT +0T0BB000000110000000000B0 +T0TB000000011000000BBBB0T +0T0BBB0000011000000B330T0 +T0T33B0000011000000B22T0T +0T02BB0000011000000B2B0T0 +T00B2B0000011000000010B0T +0TB010000001100000001B0T0 +T0B011111111111111111BT0T +0TB000000001100000000B0T0 +B0B0B00B000110000000B0T0T +B0BBB00B000110000000BT0T0 +0T0B0B0000011000000B00T00 +00000B0000011000000B00000