all repos — Legends-RPG @ 3303fa3f89ef24c3ff37369bf3a52f0c27363552

A fantasy mini-RPG built with Python and Pygame.

Town sprites can now move around. Collision for these moving sprites work.
Justin Armstrong justinmeister@gmail.com
Fri, 14 Mar 2014 19:00:14 -0700
commit

3303fa3f89ef24c3ff37369bf3a52f0c27363552

parent

90efb3275223987ac539b64b19322402ed7bfac6

4 files changed, 121 insertions(+), 34 deletions(-)

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

@@ -6,26 +6,31 @@ """Handles collisions between the user, blockers and computer

characters""" def __init__(self, player, blockers, sprites): self.player = player + self.static_blockers = blockers self.blockers = self.make_blocker_list(blockers, sprites) self.sprites = sprites - self.collided = False def make_blocker_list(self, blockers, sprites): """Return a combined list of sprite blockers and object blockers""" + blocker_list = [] + + for blocker in blockers: + blocker_list.append(blocker) + for sprite in sprites: - blockers.extend(sprite.blockers) + blocker_list.extend(sprite.blockers) - return blockers + return blocker_list def update(self, keys, current_time): """Checks for collisions between game objects""" - self.update_blockers() + self.blockers = self.make_blocker_list(self.static_blockers, + self.sprites) self.player.rect.move_ip(self.player.x_vel, self.player.y_vel) self.check_for_blockers() - self.update_blockers() for sprite in self.sprites: sprite.rect.move_ip(sprite.x_vel, sprite.y_vel) self.check_for_blockers()

@@ -33,31 +38,49 @@

if self.player.rect.x % 32 == 0 and self.player.rect.y % 32 == 0: self.player.begin_resting() - - def update_blockers(self): - """Update blockers list""" - pass + for sprite in self.sprites: + if sprite.state == 'automoving': + if sprite.rect.x % 32 == 0 and sprite.rect.y % 32 == 0: + sprite.begin_auto_resting() - def check_for_blockers(self): """Checks for collisions with blocker rects""" + player_collided = False + sprite_collided_list = [] + for blocker in self.blockers: if self.player.rect.colliderect(blocker): - self.collided = True + player_collided = True - if self.collided: - self.reset_after_collision() - self.collided = False + if player_collided: + self.reset_after_collision(self.player) self.player.begin_resting() + + for sprite in self.sprites: + for blocker in self.static_blockers: + if sprite.rect.colliderect(blocker): + sprite_collided_list.append(sprite) + if sprite.rect.colliderect(self.player.rect): + sprite_collided_list.append(sprite) + sprite.kill() + if pg.sprite.spritecollideany(sprite, self.sprites): + sprite_collided_list.append(sprite) + self.sprites.add(sprite) - def reset_after_collision(self): + for sprite in sprite_collided_list: + self.reset_after_collision(sprite) + sprite.begin_auto_resting() + + + + def reset_after_collision(self, sprite): """Put player back to original position""" - if self.player.x_vel != 0: - self.player.rect.x -= self.player.x_vel + if sprite.x_vel != 0: + sprite.rect.x -= sprite.x_vel else: - self.player.rect.y -= self.player.y_vel + sprite.rect.y -= sprite.y_vel
M data/components/person.pydata/components/person.py

@@ -1,5 +1,5 @@

__author__ = 'justinarmstrong' -import math +import math, random import pygame as pg from .. import setup

@@ -24,8 +24,9 @@ self.vector_dict = self.create_vector_dict()

self.x_vel = 0 self.y_vel = 0 self.timer = 0.0 + self.move_timer = 0.0 self.current_time = 0.0 - self.state = 'resting' + self.state = 'animated resting' self.blockers = self.set_blockers() self.location = self.get_tile_location()

@@ -73,7 +74,9 @@ def create_state_dict(self):

"""Return a dictionary of all state methods""" state_dict = {'resting': self.resting, 'moving': self.moving, - 'animated resting': self.animated_resting} + 'animated resting': self.animated_resting, + 'autoresting': self.auto_resting, + 'automoving': self.auto_moving} return state_dict

@@ -81,10 +84,10 @@

def create_vector_dict(self): """Return a dictionary of x and y velocities set to direction keys.""" - vector_dict = {'up': (0, -2), - 'down': (0, 2), - 'left': (-2, 0), - 'right': (2, 0)} + vector_dict = {'up': (0, -1), + 'down': (0, 1), + 'left': (-1, 0), + 'right': (1, 0)} return vector_dict

@@ -102,10 +105,10 @@ def set_blockers(self):

"""Sets blockers to prevent collision with other sprites""" blockers = [] - if self.state == 'resting': + if self.state == 'animated resting' or self.state == 'autoresting': blockers.append(pg.Rect(self.rect.x, self.rect.y, 32, 32)) - elif self.state == 'moving': + elif self.state == 'moving' or self.state == 'automoving': if self.rect.x % 32 == 0: tile_float = self.rect.y / float(32) tile1 = (self.rect.x, math.ceil(tile_float)*32)

@@ -187,8 +190,8 @@ """Transition the player into the 'moving' state."""

self.direction = direction self.image_list = self.animation_dict[direction] self.timer = self.current_time + self.move_timer = self.current_time self.state = 'moving' - self.old_rect = self.rect if self.rect.x % 32 == 0: self.y_vel = self.vector_dict[self.direction][1]

@@ -203,11 +206,71 @@ self.index = 1

self.x_vel = self.y_vel = 0 + def begin_auto_moving(self, direction): + """Transition sprite to a automatic moving state""" + self.direction = direction + self.image_list = self.animation_dict[direction] + self.state = 'automoving' + self.x_vel = self.vector_dict[direction][0] + self.y_vel = self.vector_dict[direction][1] + self.move_timer = self.current_time + + + def begin_auto_resting(self): + """Transition sprite to an automatic resting state""" + self.state = 'autoresting' + self.index = 0 + self.x_vel = self.y_vel = 0 + self.move_timer = self.current_time + + + def auto_resting(self): + """ + Determine when to move a sprite from resting to moving in a random + direction. + """ + #self.image = self.image_list[self.index] + self.animation(700) + + 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.current_time - self.move_timer) > 2000: + direction_list = ['up', 'down', 'left', 'right'] + random.shuffle(direction_list) + direction = direction_list[0] + self.begin_auto_moving(direction) + self.move_timer = self.current_time + + + + def auto_moving(self): + """Animate sprite and check to stop""" + self.animation() + + assert(self.rect.x % 32 == 0 or self.rect.y % 32 == 0), \ + 'Not centered on tile' + + + class Player(Person): """User controlled character""" def __init__(self, direction): super(Player, self).__init__('player', 0, 0, direction) + + + def create_vector_dict(self): + """Return a dictionary of x and y velocities set to + direction keys.""" + vector_dict = {'up': (0, -2), + 'down': (0, 2), + 'left': (-2, 0), + 'right': (2, 0)} + + return vector_dict def update(self, keys, current_time):
M data/states/town.pydata/states/town.py

@@ -40,6 +40,7 @@ if sprite.location == (9, 49):

sprite.dialogue = 'Welcome to our town, Mr. Traveller!' elif sprite.location == (16, 42): sprite.dialogue = 'You seem tired, why not rest at our Inn?' + sprite.begin_auto_resting() elif sprite.location == (14, 14): sprite.dialogue = 'Welcome to the castle, citizen.' elif sprite.location == (11, 14):
M data/states/town_blocker_layer.txtdata/states/town_blocker_layer.txt

@@ -12,11 +12,11 @@ 00BBBBBBBBB00BBBBBBBBB000

00B00000000000000000B0000 BBBBBBBBBBB00BBBBBBBBBBBB B00000000000000000000000B -000000000001100000000000B -T00000000001100000000000B -000000000001100000000000B -T00000000001100000000000B -000000000001100000000000B +B00000000001100000000000B +B00000000001100000000000B +B00000000001100000000000B +B00000000001100000000000B +B00000000001100000000000B BBBB0BBB000110000BBB0BBBB 033B0B3B000110000B3B0B330 T22B0B2B000110000B2B0B22T

@@ -34,7 +34,7 @@ BBBBBBBBBBB11BBBBBBBBBBBB

0B000000000110000000000B0 TB000000000110000000000BT 0B000000000110000000000B0 -T0T0B000000110000000000BT +T0B0B000000110000000000BT 0T0BB000000110000000000B0 T0TB000000011000000BBBB0T 0T0BBB0000011000000B330T0