-
Notifications
You must be signed in to change notification settings - Fork 0
/
sprites.py
338 lines (316 loc) · 9.86 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
from random import uniform, choice, random
import pygame as pg
from settings import *
from tilemap import *
import pytweening as tween
from itertools import chain
vec = pg.math.Vector2
def image(self, rot, fire):
image = 0
if rot == 0:
image = 0
if rot == 45:
image = 2
if rot == 90:
image = 4
if rot == 135:
image = 6
if rot == 180:
image = 8
if rot == 225:
image = 10
if rot == 270:
image = 12
if rot == 315:
image = 14
if fire:
image += 1
self.image = self.game.player_img[image].copy()
return False
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
if hits[0].rect.centerx < sprite.hit_rect.centerx:
sprite.pos.x = hits[0].rect.right
sprite.vel.x = 0
sprite.hit_rect.x = 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
if hits[0].rect.centery < sprite.hit_rect.centery:
sprite.pos.y = hits[0].rect.bottom
sprite.vel.y = 0
sprite.hit_rect.y = sprite.pos.y
class Player(pg.sprite.Sprite):
def __init__(self, game, x, y):
self._layer = 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 = PLAYER_HIT_RECT
self.hit_rect.center = self.rect.center
self.vel = vec(0, 0)
self.pos = vec(x, y)
self.rot = 0
self.fire = False
self.last_shot = 0
self.last_time = 0
self.boost_time = 0
self.health = PLAYER_HEALTH
self.speed = PLAYER_SPEED
self.weapon = 'pistol'
self.damage = False
self.ammo = 0
def get_keys(self):
self.vel = vec(0, 0)
keys = pg.key.get_pressed()
time = pg.time.get_ticks()
if keys[pg.K_LEFT] or keys[pg.K_a]:
self.vel.x = -self.speed
if time - self.last_time > FPS:
self.rot = 180
self.last_time = time
if keys[pg.K_RIGHT] or keys[pg.K_d]:
self.vel.x = self.speed
if time - self.last_time > FPS:
self.rot = 0
self.last_time = time
if keys[pg.K_UP] or keys[pg.K_w]:
self.vel.y = -self.speed
if time - self.last_time > FPS:
self.rot = 270
self.last_time = time
if keys[pg.K_DOWN] or keys[pg.K_s]:
self.vel.y = self.speed
if time - self.last_time > FPS:
self.rot = 90
self.last_time = time
if keys[pg.K_DOWN] and keys[pg.K_LEFT] or keys[pg.K_a] and keys[pg.K_s]:
self.rot = 135
if keys[pg.K_DOWN] and keys[pg.K_RIGHT] or keys[pg.K_d] and keys[pg.K_s]:
self.rot = 45
if keys[pg.K_UP] and keys[pg.K_RIGHT] or keys[pg.K_d] and keys[pg.K_w]:
self.rot = 315
if keys[pg.K_UP] and keys[pg.K_LEFT] or keys[pg.K_a] and keys[pg.K_w]:
self.rot = 225
if self.vel.x != 0 and self.vel.y != 0:
self.vel *= 0.7071
#part for shooting
if keys[pg.K_SPACE] or keys[pg.K_z]:
self.shoot()
def shoot(self):
now = pg.time.get_ticks()
if now - self.last_shot > WEAPONS[self.weapon]['rate']:
self.fire = True
self.last_shot = now
dir = vec(1, 0).rotate(self.rot)
pos = self.rect.center + BARREL_OFFSET.rotate(self.rot)
if self.rot == 180:
pos = pos + vec(0, 12)
if self.rot == 0:
pos = pos + vec(0, -2)
if self.rot == 135:
pos = pos + vec(12, 6)
if self.rot == 225:
pos = pos + vec(-9, 14)
self.vel = vec(-WEAPONS[self.weapon]['kickback'], 0).rotate(self.rot)
for i in range(WEAPONS[self.weapon]['bullet_count']):
spread = uniform(-WEAPONS[self.weapon]['spread'], WEAPONS[self.weapon]['spread'])
Bullet(self.game, pos, dir.rotate(spread), WEAPONS[self.weapon]['damage'], WEAPONS[self.weapon]['bullet_lifetime'])
snd = choice(self.game.weapon_sounds[self.weapon])
if snd.get_num_channels() > 2:
snd.stop()
snd.play()
if self.weapon == 'shotgun':
self.ammo -= 1
def hit(self):
self.damage = True
self.damage_alpha = chain(DAMAGE_ALPHA * 4)
def update(self):
self.get_keys()
self.pos += self.vel * self.game.dt
self.hit_rect.x = self.pos.x
collide_with_walls(self, self.game.walls, 'x')
self.hit_rect.y = self.pos.y
collide_with_walls(self, self.game.walls, 'y')
self.rect.center = self.hit_rect.center
self.rect.move_ip(0, -20)
self.fire = image(self, self.rot, self.fire)
if self.damage :
try:
self.image.fill((255, 255, 255, next(self.damage_alpha)), special_flags=pg.BLEND_RGBA_MULT)
except:
self.damage = False
time = pg.time.get_ticks()
if time - self.boost_time > BOOST_TIME * 1000:
self.speed = PLAYER_SPEED
if self.ammo == 0:
self.weapon = 'pistol'
def add_health(self):
self.health += HEALTH_PACK_AMOUNT
if self.health > PLAYER_HEALTH:
self.health = PLAYER_HEALTH
def speed_boost(self):
self.speed = PLAYER_SPEED * 2
self.boost_time = pg.time.get_ticks()
def shotgun(self):
self.weapon = 'shotgun'
self.ammo = 10
class Mob(pg.sprite.Sprite):
def __init__(self, game, x, y):
self._layer = 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.rect.move_ip(10, -14)
self.hit_rect = MOB_HIT_RECT.copy()
self.hit_rect.center = self.rect.center
self.hit_rect.move_ip(0, 24)
self.pos = vec(x, y)
self.vel = vec(0, 0)
self.acc = vec(0, 0)
self.rot = 0
self.health = MOB_HEALTH
self.target = game.player
self.saved_image = self.image
def avoid_mobs(self):
for mob in self.game.mobs:
if mob != self:
dist = self.pos - mob.pos
if 0 < dist.length() < AVOID_RADIUS:
self.acc += dist.normalize()
def update(self):
target_dist = self.target.pos - self.pos
self.image = self.saved_image.copy()
if target_dist.length_squared() < DETECT_RADIUS**2 or self.health < MOB_HEALTH:
if random() < 0.005:
choice(self.game.zombie_moan_sounds).play()
self.rot = target_dist.angle_to(vec(1, 0))
if self.rot > -135 and self.rot < -45:
self.image = self.game.mob_img[0].copy()
if self.rot < 135 and self.rot > 45:
self.image = self.game.mob_img[1].copy()
if self.rot > 135 or self.rot < -135:
self.image = self.game.mob_img[3].copy()
if self.rot < 45 and self.rot > -45:
self.image = self.game.mob_img[2].copy()
self.rect = self.image.get_rect()
self.rect.center = self.pos
self.acc = vec(1, 0).rotate(-self.rot)
self.avoid_mobs()
if self.acc != 0:
self.acc.scale_to_length(MOB_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.x = self.pos.x
collide_with_walls(self, self.game.walls, 'x')
self.hit_rect.y = self.pos.y
collide_with_walls(self, self.game.walls, 'y')
self.rect.move_ip(10, -14)
self.saved_image = self.image.copy()
if self.health <= 0:
choice(self.game.zombie_hit_sounds).play()
self.kill()
self.game.map_img.blit(self.game.splat, self.pos - vec(32, 32))
def draw_health(self):
for mob in self.game.mobs:
if mob.health > 60:
col = GREEN
elif mob.health > 30:
col = YELLOW
else:
col = RED
width = int(mob.rect.width * mob.health / MOB_HEALTH)
mob.health_bar = pg.Rect(0, 0, width, 7)
if mob.health < MOB_HEALTH:
pg.draw.rect(mob.image, col, mob.health_bar)
class Bullet(pg.sprite.Sprite):
def __init__(self, game, pos, dir, damage, lifetime):
self._layer = BULLET_LAYER
self.groups = game.all_sprites, game.bullets
pg.sprite.Sprite.__init__(self, self.groups)
self.game = game
self.image = game.bullet_images[WEAPONS[game.player.weapon]['bullet_size']]
self.rect = self.image.get_rect()
self.hit_rect = self.rect.copy()
self.pos = vec(pos)
self.rect.center = pos
self.vel = dir * WEAPONS[game.player.weapon]['bullet_speed'] * uniform(0.9, 1.1)
self.spawn_time = pg.time.get_ticks()
self.damage = damage
self.bullet_lifetime = lifetime
def update(self):
self.pos += self.vel * self.game.dt
self.hit_rect.x = self.pos.x
self.hit_rect.y = self.pos.y
self.rect.center = self.hit_rect.center
self.hit_rect.move_ip(0, 15)
if pg.sprite.spritecollideany(self, self.game.walls):
self.kill()
if pg.time.get_ticks() - self.spawn_time > self.bullet_lifetime :
self.kill()
class Obstacle(pg.sprite.Sprite):
def __init__(self, game, x, y, w, h):
self._layer = WALL_LAYER
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 Tree(pg.sprite.Sprite):
def __init__(self, game, x, y, w, h, type):
self._layer = TREE_LAYER
self.groups = game.all_sprites
pg.sprite.Sprite.__init__(self, self.groups)
self.game = game
self.rect = pg.Rect(x, y, w, h)
self.x = x
self.y = y
self.rect.x = x - 15
self.rect.y = y - 15
if type == "green":
self.image = game.tree_img[0]
elif type == "red":
self.rect.y = y - 18
self.image = game.tree_img[1]
self.hit_rect = self.rect.copy()
self.hit_rect.move_ip(15, 20)
class Item(pg.sprite.Sprite):
def __init__(self, game, pos, type):
self._layer = ITEM_LAYER
self.groups = game.all_sprites, game.items
pg.sprite.Sprite.__init__(self, self.groups)
self.game = game
self.image = game.item_images[type]
self.rect = self.image.get_rect()
self.type = type
self.hit_rect = self.rect
self.pos = pos
self.rect.center = pos
self.tween = tween.easeInOutSine
self.step = 0
self.dir = 1
def update(self):
#bobbing motion
offset = BOB_RANGE * (self.tween(self.step / BOB_RANGE) - 0.5)
self.rect.centery = self.pos.y + offset * self.dir
self.step += BOB_SPEED
if self.step > BOB_RANGE:
self.step = 0
self.dir *= -1