all repos — Legends-RPG @ f2dca0d393a15a440a0a97993509345ca515d575

A fantasy mini-RPG built with Python and Pygame.

data/collision.py (view raw)

 1__author__ = 'justinarmstrong'
 2import pygame as pg
 3
 4class CollisionHandler(object):
 5    """Handles collisions between the user, blockers and computer
 6    characters"""
 7    def __init__(self, player, blockers, sprites):
 8        self.player = player
 9        self.static_blockers = blockers
10        self.blockers = self.make_blocker_list(blockers, sprites)
11        self.sprites = sprites
12
13
14    def make_blocker_list(self, blockers, sprites):
15        """Return a combined list of sprite blockers and object blockers"""
16        blocker_list = []
17
18        for blocker in blockers:
19            blocker_list.append(blocker)
20
21        for sprite in sprites:
22            blocker_list.extend(sprite.blockers)
23
24        return blocker_list
25
26
27    def update(self, keys, current_time):
28        """Checks for collisions between game objects"""
29        self.blockers = self.make_blocker_list(self.static_blockers,
30                                               self.sprites)
31        self.player.rect.move_ip(self.player.x_vel, self.player.y_vel)
32        self.check_for_blockers()
33
34        for sprite in self.sprites:
35            sprite.rect.move_ip(sprite.x_vel, sprite.y_vel)
36        self.check_for_blockers()
37
38        if self.player.rect.x % 32 == 0 and self.player.rect.y % 32 == 0:
39            self.player.begin_resting()
40
41        for sprite in self.sprites:
42            if sprite.state == 'automoving':
43                if sprite.rect.x % 32 == 0 and sprite.rect.y % 32 == 0:
44                    sprite.begin_auto_resting()
45
46
47    def check_for_blockers(self):
48        """Checks for collisions with blocker rects"""
49        player_collided = False
50        sprite_collided_list = []
51
52        for blocker in self.blockers:
53            if self.player.rect.colliderect(blocker):
54                player_collided = True
55
56        if player_collided:
57            self.reset_after_collision(self.player)
58            self.player.begin_resting()
59
60        for sprite in self.sprites:
61            for blocker in self.static_blockers:
62                if sprite.rect.colliderect(blocker):
63                    sprite_collided_list.append(sprite)
64            if sprite.rect.colliderect(self.player.rect):
65                sprite_collided_list.append(sprite)
66            sprite.kill()
67            if pg.sprite.spritecollideany(sprite, self.sprites):
68                sprite_collided_list.append(sprite)
69            self.sprites.add(sprite)
70            for blocker in sprite.wander_box:
71                if sprite.rect.colliderect(blocker):
72                    sprite_collided_list.append(sprite)
73
74
75        for sprite in sprite_collided_list:
76            self.reset_after_collision(sprite)
77            sprite.begin_auto_resting()
78
79
80
81    def reset_after_collision(self, sprite):
82        """Put player back to original position"""
83        if sprite.x_vel != 0:
84                sprite.rect.x -= sprite.x_vel
85        else:
86            sprite.rect.y -= sprite.y_vel
87
88
89