-
Notifications
You must be signed in to change notification settings - Fork 0
/
entities.py
196 lines (164 loc) · 6.22 KB
/
entities.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
import pygame
from pygame.locals import *
from settings import *
from enums import *
import random
class Invader:
INVADER1_IMG = pygame.image.load('assets/invader1.png')
INVADER2_IMG = pygame.image.load('assets/invader2.png')
INVADER3_IMG = pygame.image.load('assets/invader3.png')
IMAGES = [INVADER1_IMG, INVADER2_IMG, INVADER3_IMG]
def __init__(self, x, y, t):
self.t = t # invader type
self.img_x = 0 # animation state
# position
self.x = x
self.y = y
def rect(self):
if self.t == 2:
return pygame.Rect(self.x, self.y, 12, 8)
if self.t == 1:
return pygame.Rect(self.x + 1, self.y, 11, 8)
return pygame.Rect(self.x + 2, self.y, 8, 8)
def render(self, canvas):
tex = Invader.IMAGES[self.t]
canvas.blit(tex, (self.x, self.y), (self.img_x, 0, 12, 8))
class Horde:
def __init__(self):
self.state = HordeStates.SPAWNING
self.invaders = []
self.invaders_updated = 0
# appending invaders in correct order and position
for y in range(128, 64 - 1, -16):
for x in range(26, 186 + 1, 16):
self.invaders.append(Invader(x, y, 0))
# fixing wrong invader types
for i in range(22):
self.invaders[i + 22].t = 1 # 2nd and 3rd rows
self.invaders[i ].t = 2 # 4th and 5th rows
# velocity
self.dx = 2
self.dy = 0
def has_reached_border(self):
bounds = pygame.Rect(12, 0, WIDTH - 24, HEIGHT)
for invader in self.invaders:
if not bounds.contains((invader.x, invader.y, 12, 8)):
return True
return False
def update(self):
if self.state == HordeStates.SPAWNING:
self.invaders_updated += 1
if self.invaders_updated == len(self.invaders):
self.state = HordeStates.MOVING
self.invaders_updated = 0
elif self.state == HordeStates.MOVING:
# don't try to update an empty horde
if len(self.invaders) == 0:
return
# update invader
invader = self.invaders[self.invaders_updated]
invader.x += self.dx
invader.y += self.dy
invader.img_x = (invader.img_x + 12) % 24
self.invaders_updated += 1
if self.invaders_updated == len(self.invaders):
if self.has_reached_border():
self.dx = -self.dx
self.dy = 8
else:
self.dy = 0
self.invaders_updated = 0
def render(self, canvas):
if self.state == HordeStates.SPAWNING:
for i in range(self.invaders_updated):
self.invaders[i].render(canvas)
elif self.state == HordeStates.MOVING:
for invader in self.invaders:
invader.render(canvas)
class Cannon:
IMAGE = pygame.image.load('assets/cannon.png')
def __init__(self):
self.state = CannonStates.DEAD
self.x = WIDTH
self.lives = 3
self.timer = 1000
# dead state variables
self.death_anim_timer = 75
self.death_img_x = 16
def update(self, timelapse):
if self.state == CannonStates.ALIVE:
keyboard = pygame.key.get_pressed()
# movement
if keyboard[pygame.K_LEFT]:
self.x -= 1
if keyboard[pygame.K_RIGHT]:
self.x += 1
# shooting
if self.timer <= 0 and keyboard[pygame.K_SPACE]:
print('shooting')
self.timer = 1000
elif self.state == CannonStates.DYING:
# update death animation state
if self.death_anim_timer <= 0:
self.death_anim_timer = 75
self.death_img_x = (self.death_img_x + 16) % 32
self.death_anim_timer -= timelapse
# state change to DEAD
if self.timer <= 0:
self.state = CannonStates.DEAD
self.x = WIDTH # prevent unexpected collisions
self.lives -= 1
self.timer = 2000
self.death_anim_timer = 75
self.death_img_x = 0
elif self.state == CannonStates.DEAD:
# state change to ALIVE
if self.timer <= 0:
self.state = CannonStates.ALIVE
self.x = 12
self.timer = 0
self.timer -= timelapse # always update timer
def render(self, canvas):
pos = self.x, HEIGHT - 32
if self.state == CannonStates.ALIVE:
canvas.blit(Cannon.IMAGE, pos, (0, 0, 16, 8))
elif self.state == CannonStates.DYING:
canvas.blit(Cannon.IMAGE, pos, (16 + self.death_img_x, 0, 16, 8))
elif self.state == CannonStates.DEAD:
pass
class Tourist:
IMAGE = pygame.image.load('assets/tourist.png')
def __init__(self):
self.state = TouristStates.SPAWNING
self.x = 16
self.dx = 2
self.score = 0
self.timer = 0
def update(self, timelapse):
if self.state == TouristStates.ALIVE:
self.x += self.dx
# bound checking
bounds = pygame.Rect(16, 0, WIDTH - 32, HEIGHT)
if not bounds.contains((self.x, 40, 16, 8)):
self.state = TouristStates.SPAWNING
self.x = WIDTH
self.dx = 0
self.score = random.choice((100, 150, 200, 250, 300))
self.timer = 20000 # 20s
elif self.state == TouristStates.SPAWNING:
if self.timer <= 0:
r = random.randint(0, 1)
self.state = TouristStates.ALIVE
self.x = (16, WIDTH - 32)[r]
self.dx = (2, -2)[r]
self.timer = 0
self.timer -= timelapse
def render(self, canvas):
if self.state == TouristStates.ALIVE:
canvas.blit(Tourist.IMAGE, (self.x, 40), (4, 0, 16, 8))
elif self.state == TouristStates.DYING:
canvas.blit(Tourist.IMAGE, (self.x - 4, 40), (24, 0, 16, 8))
elif self.state == TouristStates.DEAD:
pass
elif self.state == TouristStates.SPAWNING:
pass