-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathworld.py
executable file
·256 lines (208 loc) · 9.03 KB
/
world.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
#!/usr/bin/env python
#-*- coding: utf8 -*-
from animations import BulletExplosion, FullSizeExplosion
from stuff_on_map import *
import ai
import math
import random
class World (object):
def __init__(self, game_map, players, texture_loader):
self.players = players
self.map = game_map
self.texture_loader = texture_loader
self._drawables = []
self.enemies = []
self.ai = ai.ZombieDriver(self)
self.enemies_killed = 0
self.enemeis_to_kill = 20
self.un_flags = []
self._bullets = pygame.sprite.RenderUpdates()
self._visible_terrain = pygame.sprite.RenderUpdates()
self._all_unpassable = pygame.sprite.RenderUpdates()
self._all_passable = pygame.sprite.RenderUpdates()
self._movable = pygame.sprite.RenderUpdates()
self._animations = pygame.sprite.RenderUpdates()
def init(self):
for player in self.players:
self._movable.add(player.tank)
self._visible_terrain.add(*[self.map.objects + self.map.unpassable +
self.map.un_flags + self.map.passable])
self._all_unpassable.add(*[self.map.objects + self.map.unpassable +
self.map.limits_guard + self.map.un_flags])
self._all_passable.add(*self.map.passable)
self.map.render.set_background(self._visible_terrain)
for flag in self.map.un_flags:
self.un_flags.append(flag)
def get_end_game_stats(self):
return _("Enemies killed: %d / %d") % (self.enemies_killed, self.enemeis_to_kill)
def tick_only_animations(self, deltat, events):
'''
Progress the currently active animations and nothing else.
'''
self.map.render.clear([self._movable, self._bullets, self._animations])
for anim in self._animations:
if anim.finished:
self._animations.remove(anim)
self._animations.update(deltat)
self._drawables = [self._movable, self._bullets, self._animations]
def tick(self, deltat, events):
'''
Progresses the game world forward. This includes moving the game objects, processing
events from players, trying for collisions and checking the game objectives.
'''
if self.enemies_killed >= self.enemeis_to_kill:
return GAME_WON
bullets = self._bullets
unpassable = self._all_unpassable
self.map.render.clear([bullets, self._movable, self._animations])
for anim in self._animations:
if anim.finished:
self._animations.remove(anim)
self._animations.update(deltat)
players_tanks = []
alive_enemies = len(self.enemies)
if alive_enemies < 6 and random.randint(0, 100) < 0.05 and \
(self.enemies_killed + alive_enemies) < self.enemeis_to_kill:
self.spawn_enemy()
for player in self.players:
player.process_events(events)
if player.tank is None:
continue
players_tanks.append(player.tank)
bullets.add(*player.tank.bullets)
if len(players_tanks) < 1:
return GAME_OVER
self.ai.tick(deltat, self.enemies)
for enemy in self.enemies:
bullets.add(*enemy.bullets)
tanks = pygame.sprite.RenderUpdates(*(players_tanks + self.enemies))
bullet_stoppers = players_tanks + self.map.objects + self.enemies + \
bullets.sprites() + self.map.limits_guard + self.map.un_flags
bullet_stoppers = pygame.sprite.Group(bullet_stoppers)
collisions = pygame.sprite.groupcollide(bullets, bullet_stoppers, False, False)
for bullet in collisions:
collided_with = collisions[bullet]
if len(collided_with) == 1 and bullet in collided_with:
continue
if bullet.owner is not None:
bullet.owner.bullets.remove(bullet)
bullet.explode_sound()
bullets.remove(bullet)
non_self = None
for obj in collided_with:
if obj is bullet:
continue
non_self = obj
ex, ey = bullet.rect.center
if bullet.direction == DIRECTION_LEFT:
ex = non_self.rect.centerx + non_self.rect.width * 0.5
if bullet.direction == DIRECTION_RIGHT:
ex = non_self.rect.centerx - non_self.rect.width * 0.5
if bullet.direction == DIRECTION_UP:
ey = non_self.rect.centery + non_self.rect.height * 0.5
if bullet.direction == DIRECTION_DOWN:
ey = non_self.rect.centery - non_self.rect.height * 0.5
explosion_animation = BulletExplosion((ex, ey))
self._animations.add(explosion_animation)
for collided in collided_with:
if collided == bullet:
continue
if isinstance(collided, UnFlag):
self.map.un_flags.remove(collided)
self._visible_terrain.remove(collided)
self.map.render.set_background(self._visible_terrain)
explosion_animation = FullSizeExplosion(collided.rect.center)
self._animations.add(explosion_animation)
return GAME_OVER
if not isinstance(collided, BasicTank):
continue
if collided is bullet.owner:
continue
if not collided.is_player and not bullet.is_player_bullet:
continue
for orphan in collided.bullets:
orphan.owner = None
self._movable.remove(collided)
explosion_animation = FullSizeExplosion(collided.rect.center)
self._animations.add(explosion_animation)
if isinstance(collided, EnemyTank):
self.enemies.remove(collided)
collided.stop()
collided.explode_sound()
self.enemies_killed += 1
if isinstance(collided, Tank):
tanks.remove(collided)
collided.stop()
for player in self.players:
if player.tank is collided:
player.tank.explode_sound()
player.tank = None
bullets.update(deltat)
for tank in tanks:
tank.reset_speed_modifier()
collisions = pygame.sprite.spritecollide(tank, self._all_passable, False, False)
has_sand = False
has_ice = False
for collision in collisions:
if isinstance(collision, Sand):
has_sand = True
break
if isinstance(collision, Ice):
has_ice = True
if has_sand:
tank.set_speed_modifier(0.7)
if has_ice:
tank.set_speed_modifier(1.2)
for tank in tanks:
other_tanks = [t for t in tanks if t != tank]
previously_collided = pygame.sprite.spritecollide(tank, other_tanks, False, False)
tank.update(deltat)
collision = pygame.sprite.spritecollideany(tank, unpassable)
if collision is not None:
tank.undo()
continue
others = pygame.sprite.spritecollide(tank, other_tanks, False, False)
if len(others) < 1:
continue
for other in others:
if other not in previously_collided:
tank.undo()
break
dist = math.sqrt(
abs(tank.rect.centerx - other.rect.centerx) ** 2 +
abs(tank.rect.centery - other.rect.centery) ** 2
)
if dist < self.map.scaled_box_size * 0.75:
tank.undo()
break
self._drawables = [self._movable, bullets, self._animations]
return GAME_CONTINUE
def active_animations_count(self):
return len(self._animations)
def spawn_enemy(self):
player_objects = []
for player in self.players:
if player.tank is None:
continue
player_objects.append(player.tank)
player_objects += player.tank.bullets
for i in range(10):
index = random.randint(0, len(self.map.enemy_starts) - 1)
position = self.map.enemy_starts[index]
new_enemy = EnemyTank(position, self.texture_loader)
collisions = pygame.sprite.groupcollide(
[new_enemy],
self._movable,
False,
False
)
if len(collisions):
# we should not spawn an enemy on top of an other enemy
continue
self._movable.add(new_enemy)
self.enemies.append(new_enemy)
break
def get_drawables(self):
return self._drawables
def objects_at(self, coords):
return []