all repos — Legends-RPG @ 2d133e01ba83b3ec61fe15d83267616c4375ac79

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.blockers = blockers
10        self.sprites = sprites
11        self.collided = False
12
13    def update(self):
14        """Checks for collisions between game objects"""
15        self.player.rect.x += self.player.x_vel
16        self.player.rect.y += self.player.y_vel
17
18        self.check_for_blockers()
19
20        if self.player.rect.x % 32 == 0 and self.player.rect.y % 32 == 0:
21            self.player.begin_resting()
22
23
24    def check_for_blockers(self):
25        """Checks for collisions with blocker rects"""
26        for blocker in self.blockers:
27            if self.player.rect.colliderect(blocker):
28                self.collided = True
29
30        if self.collided:
31            self.reset_after_collision()
32            self.collided = False
33            self.player.begin_resting()
34
35        elif pg.sprite.spritecollide(self.player, self.sprites, False):
36            self.reset_after_collision()
37            self.player.begin_resting()
38
39
40    def reset_after_collision(self):
41        """Put player back to original position"""
42        if self.player.x_vel != 0:
43                self.player.rect.x -= self.player.x_vel
44        else:
45            self.player.rect.y -= self.player.y_vel
46
47
48