-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsprites.py
412 lines (368 loc) · 14.9 KB
/
sprites.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
import pygame as pg
from random import uniform, choice, randint, random
import settings as st
from tilemap import collide_hit_rect
import pytweening as tween
from itertools import chain
from pygame.math import Vector2 as vec
from itertools import cycle
def collide_with_walls(sprite, group, dir):
if dir == 'x':
hits = pg.sprite.spritecollide(sprite, group, False, collide_hit_rect)
if hits:
if hits[0].rect.centerx > sprite.hit_rect.centerx:
sprite.pos.x = hits[0].rect.left - sprite.hit_rect.width / 2
if hits[0].rect.centerx < sprite.hit_rect.centerx:
sprite.pos.x = hits[0].rect.right + sprite.hit_rect.width / 2
sprite.vel.x = 0
sprite.hit_rect.centerx = sprite.pos.x
if dir == 'y':
hits = pg.sprite.spritecollide(sprite, group, False, collide_hit_rect)
if hits:
if hits[0].rect.centery > sprite.hit_rect.centery:
sprite.pos.y = hits[0].rect.top - sprite.hit_rect.height / 2
if hits[0].rect.centery < sprite.hit_rect.centery:
sprite.pos.y = hits[0].rect.bottom + sprite.hit_rect.height / 2
sprite.vel.y = 0
sprite.hit_rect.centery = sprite.pos.y
class Player(pg.sprite.Sprite):
def __init__(self, game, x, y):
self._layer = st.PLAYER_LAYER
self.groups = game.all_sprites
pg.sprite.Sprite.__init__(self, self.groups)
self.game = game
self.image = game.player_img[0]
self.rect = self.image.get_rect()
self.rect.center = (x, y)
self.hit_rect = st.PLAYER_HIT_RECT
self.hit_rect.center = self.rect.center
self.vel = vec(0, 0)
self.pos = vec(x, y)
self.rot = 0
self.last_shot = 0
self.last_anim = 0
self.current_image = self.game.player_img[0]
self.pool = cycle(self.game.player_img)
self.health = st.PLAYER_HEALTH
self.weapon = 'pistol'
self.damaged = False
self.action = False
self.ammo = 12
def animate(self):
now = pg.time.get_ticks()
if now - self.last_anim > 20:
self.current_image = next(self.pool)
self.last_anim = now
def get_keys(self):
self.rot_speed = 0
self.vel = vec(0, 0)
if self.game.left:
self.rot_speed = st.PLAYER_ROT_SPEED
self.game.set_mask(self.rot)
elif self.game.right:
self.rot_speed = -st.PLAYER_ROT_SPEED
self.game.set_mask(self.rot)
elif self.game.up:
self.vel = vec(st.PLAYER_SPEED, 0).rotate(-self.rot)
elif self.game.down:
self.vel = vec(-st.PLAYER_SPEED / 2, 0).rotate(-self.rot)
elif self.game.shoot:
self.shoot()
elif self.game.action:
self.action=True
else: self.action = False
if any([self.game.left, self.game.right, self.game.up, self.game.down]):
self.animate()
def opening(self):
self.action = not self.action
print("action = ",self.action)
def shoot(self):
now = pg.time.get_ticks()
if now - self.last_shot > st.WEAPONS[self.weapon]['rate']:
self.last_shot = now
if st.AMMO > 0:
dir = vec(1, 0).rotate(-self.rot)
pos = self.pos + st.BARREL_OFFSET.rotate(-self.rot)
self.vel = vec(-st.WEAPONS[self.weapon]
['kickback'], 0).rotate(-self.rot)
for i in range(st.WEAPONS[self.weapon]['bullet_count']):
spread = uniform(-st.WEAPONS[self.weapon]
['spread'], st.WEAPONS[self.weapon]['spread'])
Bullet(self.game, pos, dir.rotate(spread),
st.WEAPONS[self.weapon]['damage'])
snd = self.game.weapon_sounds[self.weapon]
if snd.get_num_channels() > 2:
snd.stop()
snd.play()
MuzzleFlash(self.game, pos)
st.AMMO -= 1
def hit(self):
self.damaged = True
self.damage_alpha = chain(st.DAMAGE_ALPHA * 4)
def update(self):
self.get_keys()
self.rot = (self.rot + self.rot_speed * self.game.dt) % 360
self.image = pg.transform.rotate(self.current_image, self.rot)
if self.damaged:
try:
self.image.fill((255, 255, 255, next(
self.damage_alpha)), special_flags=pg.BLEND_RGBA_MULT)
except:
self.damaged = False
self.rect = self.image.get_rect()
self.rect.center = self.pos
self.pos += self.vel * self.game.dt
self.hit_rect.centerx = self.pos.x
collide_with_walls(self, self.game.walls, 'x')
self.hit_rect.centery = self.pos.y
collide_with_walls(self, self.game.walls, 'y')
self.rect.center = self.hit_rect.center
def add_health(self, amount):
st.PLAYER_HEALTH += amount
if st.PLAYER_HEALTH > 100:
st.PLAYER_HEALTH = 100
class Mob(pg.sprite.Sprite):
def __init__(self, game, x, y):
self._layer = st.MOB_LAYER
self.groups = game.all_sprites, game.mobs
pg.sprite.Sprite.__init__(self, self.groups)
self.game = game
self.image = game.mob_img[0].copy()
self.rect = self.image.get_rect()
self.rect.center = (x, y)
self.hit_rect = st.MOB_HIT_RECT.copy()
self.hit_rect.center = self.rect.center
self.pos = vec(x, y)
self.vel = vec(0, 0)
self.acc = vec(0, 0)
self.rect.center = self.pos
self.rot = 0
self.health = st.MOB_HEALTH
self.speed = choice(st.MOB_SPEEDS)
self.target = game.player
self.last_anim = 0
self.current_image = game.mob_img[0]
self.pool = cycle(game.mob_img)
def avoid_mobs(self):
for mob in self.game.mobs:
if mob != self:
dist = self.pos - mob.pos
if 0 < dist.length() < st.AVOID_RADIUS:
self.acc += dist.normalize()
def animate(self):
now = pg.time.get_ticks()
if now - self.last_anim > 20:
self.current_image = next(self.pool)
self.last_anim = now
def update(self):
target_dist = self.target.pos - self.pos
if target_dist.length_squared() < st.DETECT_RADIUS**2:
if random() < 0.002:
choice(self.game.zombie_moan_sounds).play()
self.rot = target_dist.angle_to(vec(1, 0))
# self.image = pg.transform.rotate(self.game.mob_img, self.rot)
self.image = pg.transform.rotate(self.current_image, self.rot)
self.rect.center = self.pos
self.acc = vec(1, 0).rotate(-self.rot)
self.avoid_mobs()
self.acc.scale_to_length(self.speed)
self.acc += self.vel * -1
self.vel += self.acc * self.game.dt
self.pos += self.vel * self.game.dt + 0.5 * self.acc * self.game.dt ** 2
self.hit_rect.centerx = self.pos.x
collide_with_walls(self, self.game.walls, 'x')
self.hit_rect.centery = self.pos.y
collide_with_walls(self, self.game.walls, 'y')
self.rect.center = self.hit_rect.center
if self.health <= 0:
choice(self.game.zombie_hit_sounds).play()
self.kill()
self.game.map_img.blit(self.game.splat_img, self.pos - vec(32, 32))
self.animate()
self.draw_health()
def draw_health(self):
if self.health > 60:
col = st.GREEN
elif self.health > 30:
col = st.YELLOW
else:
col = st.RED
width = int(self.rect.width * self.health / st.MOB_HEALTH)
self.health_bar = pg.Rect(0, 0, width, 7)
if self.health < st.MOB_HEALTH:
pg.draw.rect(self.image, col, self.health_bar)
class Bullet(pg.sprite.Sprite):
def __init__(self, game, pos, dir, damage):
self._layer = st.BULLET_LAYER
self.groups = game.all_sprites, game.bullets
pg.sprite.Sprite.__init__(self, self.groups)
self.game = game
self.image = game.bullet_img[st.WEAPONS[game.player.weapon]['bullet_size']]
self.rect = self.image.get_rect()
self.hit_rect = self.rect
self.pos = vec(pos)
self.dir = dir
self.rect.center = pos
#spread = uniform(-GUN_SPREAD, GUN_SPREAD)
self.vel = dir * \
st.WEAPONS[game.player.weapon]['bullet_speed'] * uniform(0.9, 1.1)
self.spawn_time = pg.time.get_ticks()
self.damage = damage
def update(self):
self.pos += self.vel * self.game.dt
self.rect.center = self.pos
if pg.sprite.spritecollideany(self, self.game.walls):
self.kill()
if pg.time.get_ticks() - self.spawn_time > st.WEAPONS[self.game.player.weapon]['bullet_lifetime']:
self.kill()
class Obstacle(pg.sprite.Sprite):
def __init__(self, game, x, y, w, h):
self.groups = game.walls
pg.sprite.Sprite.__init__(self, self.groups)
self.game = game
self.rect = pg.Rect(x, y, w, h)
self.hit_rect = self.rect
self.x = x
self.y = y
self.rect.x = x
self.rect.y = y
class MuzzleFlash(pg.sprite.Sprite):
def __init__(self, game, pos):
self._layer = st.EFFECTS_LAYER
self.groups = game.all_sprites
pg.sprite.Sprite.__init__(self, self.groups)
self.game = game
size = randint(20, 50)
self.image = pg.transform.scale(choice(game.gun_flashes), (size, size))
self.rect = self.image.get_rect()
self.hit_rect = self.rect
self.pos = pos
self.rect.center = pos
self.spawn_time = pg.time.get_ticks()
def update(self):
if pg.time.get_ticks() - self.spawn_time > st.FLASH_DURATION:
self.kill()
class Item(pg.sprite.Sprite):
def __init__(self, game, pos, type):
self._layer = st.ITEMS_LAYER
self.groups = game.all_sprites, game.items
pg.sprite.Sprite.__init__(self, self.groups)
self.game = game
self.image = game.item_img[type]
self.rect = self.image.get_rect()
self.hit_rect = self.rect
self.type = type
self.pos = pos
self.rect.center = pos
self.tween = tween.easeInOutSine
self.step = 0
self.dir = 1
def update(self):
# bobbing motion
offset = st.BOB_RANGE * (self.tween(self.step / st.BOB_RANGE) - 0.5)
self.rect.centery = self.pos.y + offset * self.dir
self.step += st.BOB_SPEED
if self.step > st.BOB_RANGE:
self.step = 0
self.dir *= -1
class Door(pg.sprite.Sprite):
def __init__(self, game, pos, dir, link):
self._layer = st.ITEMS_LAYER
self.groups = game.all_sprites, game.items, game.walls
pg.sprite.Sprite.__init__(self, self.groups)
self.game = game
self.type = 'door'
self.dir = dir
self.link = link
self.pos = pos
self.crossed = False
self.states = ['opened','closed']
self.pool = cycle(self.states)
self.status = 'closed'
self.door_states = self.create_img()
self.image = self.door_states[0]
self.rect = self.image.get_rect()
self.image.set_colorkey((0, 0, 0))
self.hit_rect = self.rect
self.rect.center = pos
def create_img(self):
if self.dir == 'left':
image1 = self.game.get_imgel(self.game.doors_img, 0, 128, 64, 128)
image2 = self.game.get_imgel(self.game.doors_img, 0, 64, 128, 64)
if self.dir == 'right':
image1 = self.game.get_imgel(self.game.doors_img, 64, 128, 64, 128)
image2 = self.game.get_imgel(self.game.doors_img, 0, 0, 128, 64)
if self.dir == 'bottom':
image1 = self.game.get_imgel(self.game.doors_img, 0, 64, 128, 64)
image2 = self.game.get_imgel(self.game.doors_img, 64, 128, 64, 128)
if self.dir == 'top':
image1 = self.game.get_imgel(self.game.doors_img, 0, 0, 128, 64)
image2 = self.game.get_imgel(self.game.doors_img, 0, 128, 64, 128)
door_states=[]
door_states.append(image1)
door_states.append(image2)
# print("door_states=",type(door_states),"of length=",len(door_states))
return door_states
def change_state(self):
self.game.player.vel = vec(0, 0)
self.game.player.acc = vec(0, 0)
# self.game.player.pos =
if self.dir == 'left':
close_offset_x = self.pos.x - 32
close_offset_y = self.pos.y - 64
open_offset_x = self.pos.x + 32
open_offset_y = self.pos.y + 32
if self.dir == 'right':
close_offset_x = self.pos.x - 32
close_offset_y = self.pos.y - 64
open_offset_x = self.pos.x - 155
open_offset_y = self.pos.y + 35
if self.dir == 'top':
close_offset_x = self.pos.x - 64
close_offset_y = self.pos.y - 32
open_offset_x = self.pos.x + 32
open_offset_y = self.pos.y + 32
if self.dir == 'bottom':
close_offset_x = self.pos.x - 64
close_offset_y = self.pos.y - 32
open_offset_x = self.pos.x - 96
open_offset_y = self.pos.y - 155
newstatus = next(self.pool)
if newstatus== 'closed':
self.image = self.door_states[0]
self.rect = self.image.get_rect()
self.rect.x = close_offset_x
self.rect.y = close_offset_y
self.status = 'closed'
elif newstatus == 'opened':
self.image = self.door_states[1]
self.image.set_colorkey((0, 0, 0))
self.rect = self.image.get_rect()
self.rect.x = open_offset_x
self.rect.y = open_offset_y
self.status = 'opened'
def update(self):
if self.status == 'opened':
if self.dir == 'left':
if self.game.player.hit_rect.right < self.hit_rect.right:
self.game.go_next(self.link)
if self.dir == 'right':
if self.game.player.hit_rect.left > self.hit_rect.left:
self.game.go_next(self.link)
if self.dir == 'top':
if self.game.player.hit_rect.bottom < self.hit_rect.top:
self.game.go_next(self.link)
if self.dir == 'bottom':
if self.game.player.hit_rect.top > self.hit_rect.bottom:
self.game.go_next(self.link)
class Light(pg.sprite.Sprite):
def __init__(self, game, x, y, w, h):
self.groups = game.walls
pg.sprite.Sprite.__init__(self, self.groups)
self.game = game
self.rect = pg.Rect(x, y, w, h)
self.hit_rect = self.rect
self.x = x
self.y = y
self.rect.x = x
self.rect.y = y