generated from StRobertCHSCS/python-base
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollision.py
297 lines (280 loc) · 13 KB
/
collision.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
"""
-------------------------------------------------------------------------------
Name: collisino.py
Purpose: Collision engine for all sprites.
Author: Wang.D
Created: 23/01/2020
-------------------------------------------------------------------------------
"""
import arcade
import time
from boss import Boss
from boss_bullet import BossBullet
from executioner import Executioner
from minion import Minion
from player import Player
from tiledmap import TiledMap
from arcade.geometry import check_for_collision_with_list
from slime import Slime
from mage import Fireball, Mage
class CollisionDetection(arcade.PhysicsEngineSimple):
def __init__(self, player, walls = None, traps=None) -> None:
"""
Class in charge of monitoring collisions between player, walls, enemies, and other sprites
:param player: user
:param walls: list of walls
"""
super().__init__(player, walls)
# initiate class variables
self.player = player
self.walls = walls
self.traps = traps
def update(self, direction=None, player_to_follow=None) -> None:
"""
Move everything and resolve collisions
:return: none
"""
# player and other enemy detection
if isinstance(player_to_follow, arcade.SpriteList):
# checks for player and other entity collisions
hit_list = \
check_for_collision_with_list(self.player,
player_to_follow)
if len(hit_list) > 0:
for item in hit_list:
if self.player.is_attack_state:
if not isinstance(item, Fireball):
item.health -= 1.5
if isinstance(item, Executioner):
item.is_player_hit = True
if isinstance(item, Slime):
item.is_player_hit = True
if isinstance(item, Fireball):
item.is_wall_hit = True
item.is_player_hit = True
if isinstance(item, Boss):
if self.player.is_attack_state:
item.health -= 0.5
# if the player is the user
elif isinstance(self.player, Player):
if self.player.health < 1:
self.player.game_over()
# --- Move sprite
self.player.move_player(direction)
# update x position
self.player.center_x += self.player.change_x
# Check for wall hit
hit_list = \
check_for_collision_with_list(self.player,
self.walls)
# If we hit a wall, move so the edges are at the same point
if len(hit_list) > 0:
if self.player.change_x > 0:
for item in hit_list:
self.player.right = min(item.left,
self.player.right) - 5
self.player.direction = None
return arcade.key.RIGHT
elif self.player.change_x < 0:
for item in hit_list:
self.player.left = max(item.right,
self.player.left) + 5
self.player.direction = None
return arcade.key.LEFT
# update y position
self.player.center_y += self.player.change_y
# Check for wall hit
hit_list = \
check_for_collision_with_list(self.player,
self.walls)
# If we hit a wall, move so the edges are at the same point
if len(hit_list) > 0:
if self.player.change_y > 0:
for item in hit_list:
self.player.top = min(item.bottom,
self.player.top) - 5
self.player.direction = None
return arcade.key.UP
elif self.player.change_y < 0:
for item in hit_list:
self.player.bottom = max(item.top,
self.player.bottom) + 5
self.player.direction = None
return arcade.key.DOWN
# check for trap hit
hit_list = \
check_for_collision_with_list(self.player, self.traps)
if len(hit_list) > 0:
self.player.health -= 0.1
self.player.change_x, self.player.change_y = 0, 0
# if it isn't the player
elif isinstance(self.player, Slime):
# --- Move sprite
self.player.follow(player_to_follow)
# update x position
self.player.center_x += self.player.change_x
# Check for wall hit
hit_list = \
check_for_collision_with_list(self.player,
self.walls)
# If we hit a wall, move so the edges are at the same point
if len(hit_list) > 0:
if self.player.change_x > 0:
for item in hit_list:
self.player.right = min(item.left,
self.player.right) - 5
self.player.direction = None
self.player.movement = not self.player.movement
self.player.hit = True
elif self.player.change_x < 0:
for item in hit_list:
self.player.left = max(item.right,
self.player.left) + 5
self.player.direction = None
self.player.movement = not self.player.movement
self.player.hit = True
# update y position
self.player.center_y += self.player.change_y
# Check for wall hit
hit_list = \
check_for_collision_with_list(self.player,
self.walls)
# If we hit a wall, move so the edges are at the same point
if len(hit_list) > 0:
if self.player.change_y > 0:
for item in hit_list:
self.player.top = min(item.bottom,
self.player.top) - 5
self.player.direction = None
self.player.movement = not self.player.movement
self.player.hit = True
elif self.player.change_y < 0:
for item in hit_list:
self.player.bottom = max(item.top,
self.player.bottom) + 5
self.player.direction = None
self.player.movement = not self.player.movement
self.player.hit = True
else:
print("Error, collision while enemy wasn't moving.")
self.player.change_x, self.player.change_y = 0, 0
elif isinstance(self.player, Minion):
# --- Move sprite
self.player.follow(player_to_follow)
if arcade.check_for_collision(self.player, player_to_follow):
self.player.is_player_hit = True
# update x position
self.player.center_x += self.player.change_x
self.player.center_y += self.player.change_y
# Check for wall hit
if self.player.center_x > 890:
self.player.center_x = 890
if self.player.center_x < 20:
self.player.center_x = 20
if self.player.center_y > 890:
self.player.center_y = 890
if self.player.center_y < 20:
self.player.center_y = 20
self.player.change_x, self.player.change_y = 0, 0
elif isinstance(self.player, Executioner):
# --- Move sprite
self.player.follow(player_to_follow)
# update x position
self.player.center_x += self.player.change_x
# Check for wall hit
hit_list = \
check_for_collision_with_list(self.player,
self.walls)
# If we hit a wall, move so the edges are at the same point
if len(hit_list) > 0:
if self.player.change_x > 0:
for item in hit_list:
self.player.right = min(item.left,
self.player.right) - 5
self.player.direction = None
self.player.movement = not self.player.movement
self.player.hit = True
elif self.player.change_x < 0:
for item in hit_list:
self.player.left = max(item.right,
self.player.left) + 5
self.player.direction = None
self.player.movement = not self.player.movement
self.player.hit = True
else:
print("Error, collision while enemy wasn't moving.")
# update y position
self.player.center_y += self.player.change_y
# Check for wall hit
hit_list = \
check_for_collision_with_list(self.player,
self.walls)
# If we hit a wall, move so the edges are at the same point
if len(hit_list) > 0:
if self.player.change_y > 0:
for item in hit_list:
self.player.top = min(item.bottom,
self.player.top) - 5
self.player.direction = None
self.player.movement = not self.player.movement
self.player.hit = True
elif self.player.change_y < 0:
for item in hit_list:
self.player.bottom = max(item.top,
self.player.bottom) + 5
self.player.direction = None
self.player.movement = not self.player.movement
self.player.hit = True
else:
print("Error, collision while enemy wasn't moving.")
self.player.change_x, self.player.change_y = 0, 0
elif isinstance(self.player, Fireball):
# --- Move sprite
# update x position
self.player.center_x += self.player.change_x
# Check for wall hit
hit_list = \
check_for_collision_with_list(self.player,
self.walls)
# If we hit a wall, move so the edges are at the same point
if len(hit_list) > 0:
if self.player.change_x > 0:
for item in hit_list:
self.player.right = min(item.left,
self.player.right) - 5
elif self.player.change_x < 0:
for item in hit_list:
self.player.left = max(item.right,
self.player.left) + 5
else:
print("Error, collision while enemy wasn't moving.")
self.player.is_wall_hit = True
self.player.reset = True
# update y position
self.player.center_y += self.player.change_y
# Check for wall hit
hit_list = \
check_for_collision_with_list(self.player,
self.walls)
# If we hit a wall, move so the edges are at the same point
if len(hit_list) > 0:
if self.player.change_y > 0:
for item in hit_list:
self.player.top = min(item.bottom,
self.player.top) - 5
elif self.player.change_y < 0:
for item in hit_list:
self.player.bottom = max(item.top,
self.player.bottom) + 5
else:
print("Error, collision while enemy wasn't moving.")
self.player.is_wall_hit = True
self.player.reset = True
# elif isinstance(self.player, BossBullet):
# hit_list = \
# check_for_collision_with_list(self.player, self.walls)
# if len(hit_list) > 0:
# self.player.stop = True
# if arcade.geometry.check_for_collision(self.player, direction):
# self.player.stop = True
# direction.health -= 1