all repos — Legends-RPG @ d0d079d24ad07181968d3c82c447f2dbd6a6b922

A fantasy mini-RPG built with Python and Pygame.

data/collision.py (view raw)

 1__author__ = 'justinarmstrong'
 2
 3class CollisionHandler(object):
 4    """Handles collisions between the user, blockers and computer
 5    characters"""
 6    def __init__(self, player, blockers):
 7        self.player = player
 8        self.blockers = blockers
 9        self.collided = False
10
11    def update(self):
12        """Checks for collisions between game objects"""
13        self.player.rect.x += self.player.x_vel
14        self.player.rect.y += self.player.y_vel
15
16        self.check_for_blockers()
17
18        if self.player.rect.x % 32 == 0 and self.player.rect.y % 32 == 0:
19            self.player.begin_resting()
20
21
22    def check_for_blockers(self):
23        """Checks for collisions with blocker rects"""
24        for blocker in self.blockers:
25            if self.player.rect.colliderect(blocker):
26                self.collided = True
27
28        if self.collided:
29            if self.player.x_vel != 0:
30                self.player.rect.x -= self.player.x_vel
31            else:
32                self.player.rect.y -= self.player.y_vel
33
34            self.collided = False
35            self.player.begin_resting()
36