forked from williamfiset/Survival
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Zombie.py
107 lines (66 loc) · 2.85 KB
/
Zombie.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
import pygame
from tile import Tile
from random import randint
from character import Character
from character import Direction
__author__ = 'William Fiset'
class Zombie(Character):
ORIGINAL_ZOMBIE_IMAGE = pygame.image.load('images/zombie.png')
START_HEALTH = 100
list_ = []
spawn_tiles = (9,42,91,134,193,219,274)
_velocity = 4
def __init__(self, x, y):
self.direction = Direction.WEST
self.health = Zombie.START_HEALTH
self.img = Zombie.ORIGINAL_ZOMBIE_IMAGE
Character.__init__(self, x, y)
Zombie.list_.append(self)
@staticmethod
def update(screen, survivor):
for zombie in Zombie.list_:
screen.blit(zombie.img, (zombie.x, zombie.y))
if survivor.x % Tile.width == 0 and survivor.y % Tile.height == 0:
if zombie.x % Tile.width == 0 and zombie.y % Tile.height == 0:
tn = survivor.get_number()
N = tn + -(Tile.V)
S = tn + (Tile.V)
E = tn + (Tile.H)
W = tn + -(Tile.H)
NSEW = [N, S, E, W, tn]
if zombie.get_number() in NSEW:
survivor.health -= 5
if zombie.health <= 0:
Zombie.list_.remove(zombie)
if zombie.dx != 0 and zombie.dy != 0: # Target is set
X = zombie.x - zombie.dx
Y = zombie.y - zombie.dy
if X < 0: # --->
zombie.x += Zombie._velocity
zombie.rotate(Direction.EAST, Zombie.ORIGINAL_ZOMBIE_IMAGE)
elif X > 0: # <----
zombie.x -= Zombie._velocity
zombie.rotate(Direction.WEST, Zombie.ORIGINAL_ZOMBIE_IMAGE)
if Y > 0: # up
zombie.y -= Zombie._velocity
zombie.rotate(Direction.NORTH, Zombie.ORIGINAL_ZOMBIE_IMAGE)
elif Y < 0: # dopwn
zombie.y += Zombie._velocity
zombie.rotate(Direction.SOUTH, Zombie.ORIGINAL_ZOMBIE_IMAGE)
if X == 0 and Y == 0:
zombie.dx, zombie.dy = 0, 0
@staticmethod
def spawn(total_frames, FPS):
if total_frames % (FPS) == 0:
if total_frames % (FPS * 6) == 0:
r = randint(0, 2)
sounds = [pygame.mixer.Sound('audio/zs1.ogg'),
pygame.mixer.Sound('audio/zs2.ogg'),
pygame.mixer.Sound('audio/zs3.ogg')]
sound = sounds[ r ]
# TURNED OFF SOUND FOR DEBUGGING
# sound.play()
r = randint(0, len(Zombie.spawn_tiles) - 1)
tile_num = Zombie.spawn_tiles[r]
spawn_node = Tile.get_tile(tile_num)
Zombie(spawn_node.x, spawn_node.y)