-
Notifications
You must be signed in to change notification settings - Fork 0
/
swarm.py
206 lines (193 loc) · 8.96 KB
/
swarm.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
from gameobject import GameObject
from enemy import Enemy
from config import *
from euclid3 import Vector2
import pyglet
from projectile import Projectile
import time
from random import randrange
class Swarm(GameObject):
def __init__(self):
self.buffer = SWARM_SPACE
self.enemy_width = (BLOCK_SIZE * 10) + self.buffer
self.enemy_height = (BLOCK_SIZE * 10) + self.buffer
GameObject.__init__(self, (self.enemy_width * 11) - self.buffer, (self.enemy_height * 5) - self.buffer)
self.enemies = [
[
Enemy(parent=self, shape=enemy1, color=0xF404D8),
Enemy(parent=self, shape=enemy1, color=0xF404D8),
Enemy(parent=self, shape=enemy1, color=0xF404D8),
Enemy(parent=self, shape=enemy1, color=0xF404D8),
Enemy(parent=self, shape=enemy1, color=0xF404D8),
Enemy(parent=self, shape=enemy1, color=0xF404D8),
Enemy(parent=self, shape=enemy1, color=0xF404D8),
Enemy(parent=self, shape=enemy1, color=0xF404D8),
Enemy(parent=self, shape=enemy1, color=0xF404D8),
Enemy(parent=self, shape=enemy1, color=0xF404D8),
Enemy(parent=self, shape=enemy1, color=0xF404D8),
],
[
Enemy(parent=self, shape=enemy1, color=0x8601FE),
Enemy(parent=self, shape=enemy1, color=0x8601FE),
Enemy(parent=self, shape=enemy1, color=0x8601FE),
Enemy(parent=self, shape=enemy1, color=0x8601FE),
Enemy(parent=self, shape=enemy1, color=0x8601FE),
Enemy(parent=self, shape=enemy1, color=0x8601FE),
Enemy(parent=self, shape=enemy1, color=0x8601FE),
Enemy(parent=self, shape=enemy1, color=0x8601FE),
Enemy(parent=self, shape=enemy1, color=0x8601FE),
Enemy(parent=self, shape=enemy1, color=0x8601FE),
Enemy(parent=self, shape=enemy1, color=0x8601FE),
],
[
Enemy(parent=self, shape=enemy2, color=0x12EBF7),
Enemy(parent=self, shape=enemy2, color=0x12EBF7),
Enemy(parent=self, shape=enemy2, color=0x12EBF7),
Enemy(parent=self, shape=enemy2, color=0x12EBF7),
Enemy(parent=self, shape=enemy2, color=0x12EBF7),
Enemy(parent=self, shape=enemy2, color=0x12EBF7),
Enemy(parent=self, shape=enemy2, color=0x12EBF7),
Enemy(parent=self, shape=enemy2, color=0x12EBF7),
Enemy(parent=self, shape=enemy2, color=0x12EBF7),
Enemy(parent=self, shape=enemy2, color=0x12EBF7),
Enemy(parent=self, shape=enemy2, color=0x12EBF7),
],
[
Enemy(parent=self, shape=enemy2, color=0x00FF00),
Enemy(parent=self, shape=enemy2, color=0x00FF00),
Enemy(parent=self, shape=enemy2, color=0x00FF00),
Enemy(parent=self, shape=enemy2, color=0x00FF00),
Enemy(parent=self, shape=enemy2, color=0x00FF00),
Enemy(parent=self, shape=enemy2, color=0x00FF00),
Enemy(parent=self, shape=enemy2, color=0x00FF00),
Enemy(parent=self, shape=enemy2, color=0x00FF00),
Enemy(parent=self, shape=enemy2, color=0x00FF00),
Enemy(parent=self, shape=enemy2, color=0x00FF00),
Enemy(parent=self, shape=enemy2, color=0x00FF00),
],
[
Enemy(parent=self, shape=enemy3, color=0xEEF328),
Enemy(parent=self, shape=enemy3, color=0xEEF328),
Enemy(parent=self, shape=enemy3, color=0xEEF328),
Enemy(parent=self, shape=enemy3, color=0xEEF328),
Enemy(parent=self, shape=enemy3, color=0xEEF328),
Enemy(parent=self, shape=enemy3, color=0xEEF328),
Enemy(parent=self, shape=enemy3, color=0xEEF328),
Enemy(parent=self, shape=enemy3, color=0xEEF328),
Enemy(parent=self, shape=enemy3, color=0xEEF328),
Enemy(parent=self, shape=enemy3, color=0xEEF328),
Enemy(parent=self, shape=enemy3, color=0xEEF328),
],
[
Enemy(parent=self, shape=enemy3, color=0xFA1904),
Enemy(parent=self, shape=enemy3, color=0xFA1904),
Enemy(parent=self, shape=enemy3, color=0xFA1904),
Enemy(parent=self, shape=enemy3, color=0xFA1904),
Enemy(parent=self, shape=enemy3, color=0xFA1904),
Enemy(parent=self, shape=enemy3, color=0xFA1904),
Enemy(parent=self, shape=enemy3, color=0xFA1904),
Enemy(parent=self, shape=enemy3, color=0xFA1904),
Enemy(parent=self, shape=enemy3, color=0xFA1904),
Enemy(parent=self, shape=enemy3, color=0xFA1904),
Enemy(parent=self, shape=enemy3, color=0xFA1904),
],
]
for row in self.enemies:
for enemy in row:
self.children.append(enemy)
self.direction = Vector2(1, 0)
self.interval = 4.5
for row_index, row in enumerate(self.enemies):
for col_index, enemy in enumerate(row):
enemy.Translate(col_index * self.enemy_width, row_index * self.enemy_height, 0.0)
pyglet.clock.schedule_interval(self.fire, 0.5)
self.time_elapsed_since_last_action = 0
self.swarm_projectile_pool = [
Projectile(Vector2(0, 1)),
Projectile(Vector2(0, 1)),
Projectile(Vector2(0, 1)),
Projectile(Vector2(0, 1)),
Projectile(Vector2(0, 1)),
Projectile(Vector2(0, 1)),
]
def faster(self):
if self.interval > 0.1:
self.interval -= 0.2
def render(self):
super().render()
for row in self.enemies:
for enemy in row:
if enemy:
enemy.render()
def update(self, dt):
super().update(dt)
for row in self.enemies:
for enemy in row:
if enemy:
enemy.update(dt)
self.time_elapsed_since_last_action += pyglet.clock.tick()
if self.time_elapsed_since_last_action > self.interval / 1000:
self.move()
self.time_elapsed_since_last_action = 0
def get_firing_enemy(self):
transpose = list(map(list, zip(*self.enemies)))
last_of_each_col = []
for row in transpose:
for enemy in reversed(row):
if enemy:
last_of_each_col.append(enemy)
break
col = randrange(len(last_of_each_col))
if last_of_each_col[col]:
return last_of_each_col[col]
def fire(self, dt):
# pick a random space invader to fire
enemy = self.get_firing_enemy()
if enemy:
usable = list(filter(lambda x: not x.in_use, self.swarm_projectile_pool))
if len(usable):
#sound = pyglet.resource.media('assets/shoot.wav', streaming=False)
#sound.play()
p = usable[0]
#p.Translate((self.position.x + self.width * 0.5) - (p.width * 0.5), self.position.y, 0)
p.Translate((self.position.x + enemy.position.x + enemy.width * 0.5) - (p.width * 0.5), self.position.y + enemy.position.y + enemy.height + self.buffer, 0)
p.in_use = True
def move(self, dt=0):
move_amount = (10 * self.direction.x)
if self.direction.y == 1:
self.translate(0.0, 10, 0.0)
self.direction.y = 0
if self.position.x <= 0:
self.direction.x = 1
else:
self.direction.x = -1
if self.direction.x == 1:
if self.position.x + self.width + move_amount >= WIDTH:
self.Translate(WIDTH - self.width, self.position.y, self.position.z)
self.direction.y = 1
self.direction.x = 0
self.faster()
else:
self.translate(move_amount, 0.0, 0.0)
elif self.direction.x == -1:
if self.position.x + move_amount <= 0:
self.Translate(0, self.position.y, self.position.z)
self.direction.y = 1
self.direction.x = 0
self.faster()
else:
self.translate(move_amount, 0.0, 0.0)
#pyglet.clock.schedule_once(self.move, self.interval)
def check_collide(self, bounds):
item = []
collided = False
for row_index, row in enumerate(self.enemies):
for col_index, enemy in enumerate(row):
if enemy:
if enemy.bounds.check_intersect(bounds):
item = [row_index, col_index]
if item:
#del self.enemies[item[0]][item[1]]
self.enemies[item[0]][item[1]] = None
collided = True
return collided