data/collision.py (view raw)
1import random
2import pygame as pg
3from . import constants as c
4
5class CollisionHandler(object):
6 """Handles collisions between the user, blockers and computer
7 characters"""
8 def __init__(self, player, blockers, sprites, portals, level):
9 self.player = player
10 self.static_blockers = blockers
11 self.blockers = self.make_blocker_list(blockers, sprites)
12 self.sprites = sprites
13 self.portals = portals
14 self.level = level
15
16 def make_blocker_list(self, blockers, sprites):
17 """
18 Return a combined list of sprite blockers and object blockers.
19 """
20 blocker_list = []
21
22 for blocker in blockers:
23 blocker_list.append(blocker)
24
25 for sprite in sprites:
26 blocker_list.extend(sprite.blockers)
27
28 return blocker_list
29
30 def update(self, keys, current_time):
31 """
32 Check for collisions between game objects.
33 """
34 self.blockers = self.make_blocker_list(self.static_blockers,
35 self.sprites)
36 self.player.rect.move_ip(self.player.x_vel, self.player.y_vel)
37 self.check_for_blockers()
38
39 for sprite in self.sprites:
40 sprite.rect.move_ip(sprite.x_vel, sprite.y_vel)
41 self.check_for_blockers()
42
43 if self.player.rect.x % 32 == 0 and self.player.rect.y % 32 == 0:
44 if not self.player.state == 'resting':
45 self.check_for_portal()
46 self.check_for_battle()
47 self.player.begin_resting()
48
49 for sprite in self.sprites:
50 if sprite.state == 'automoving':
51 if sprite.rect.x % 32 == 0 and sprite.rect.y % 32 == 0:
52 sprite.begin_auto_resting()
53
54 def check_for_portal(self):
55 """
56 Check for a portal to change level scene.
57 """
58 portal = pg.sprite.spritecollideany(self.player, self.portals)
59
60 if portal:
61 self.level.use_portal = True
62 self.level.portal = portal.name
63
64 def check_for_blockers(self):
65 """
66 Checks for collisions with blocker rects.
67 """
68 player_collided = False
69 sprite_collided_list = []
70
71 for blocker in self.blockers:
72 if self.player.rect.colliderect(blocker):
73 player_collided = True
74
75 if player_collided:
76 self.reset_after_collision(self.player)
77 self.player.begin_resting()
78
79 for sprite in self.sprites:
80 for blocker in self.static_blockers:
81 if sprite.rect.colliderect(blocker):
82 sprite_collided_list.append(sprite)
83 if sprite.rect.colliderect(self.player.rect):
84 sprite_collided_list.append(sprite)
85 sprite.kill()
86 if pg.sprite.spritecollideany(sprite, self.sprites):
87 sprite_collided_list.append(sprite)
88 self.sprites.add(sprite)
89 for blocker in sprite.wander_box:
90 if sprite.rect.colliderect(blocker):
91 sprite_collided_list.append(sprite)
92
93
94 for sprite in sprite_collided_list:
95 self.reset_after_collision(sprite)
96 sprite.begin_auto_resting()
97
98 def reset_after_collision(self, sprite):
99 """Put player back to original position"""
100 if sprite.x_vel != 0:
101 sprite.rect.x -= sprite.x_vel
102 else:
103 sprite.rect.y -= sprite.y_vel
104
105 def check_for_battle(self):
106 """
107 Switch scene to battle 1/5 times if battles are allowed.
108 """
109 if self.level.allow_battles:
110 self.level.game_data['battle counter'] -= 5
111 if self.level.game_data['battle counter'] <= 0:
112 self.level.switch_to_battle = True
113
114
115