-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenemymanager.py
More file actions
136 lines (112 loc) · 4.16 KB
/
enemymanager.py
File metadata and controls
136 lines (112 loc) · 4.16 KB
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
"""
This module handles the spawning of enemies.
"""
import pygame
import enemy
import Queue
class EnemyManager:
"""
The event ID for a event to spawn a basic enemy. When a
basic enemy is scheduled to spawn, an event with this ID
is posted to the event queue.
"""
SPAWN_EVENT_BASIC = pygame.USEREVENT
"""
The time (in milliseconds) at which the last spawning of
a wave of enemies occurred.
"""
last_wave_time = 0
"""
The amount of time between waves.
"""
wave_interval = 2000
"""
The number of basic enemies to spawn during a given wave.
"""
basic_enemies = 1
"""
The time between spawning two enemies during a wave. This
is so that enemies don't overlap on the screen.
"""
spawn_interval = 1500
"""
The list of enemies in the game.
"""
enemies = []
"""
The time at which the last update occurred.
"""
last_update_time = pygame.time.get_ticks()
"""
The sprites of all enemies in the game.
"""
spritegroup = pygame.sprite.Group()
"""
The priority queue of enemies to be spawned. This queue is arranged
on the scheduled time for an enemy to spawn.
"""
enemy_queue = Queue.PriorityQueue()
"""
The last time something was spawned.
"""
last_spawn_time = pygame.time.get_ticks()
def __init__(self, size):
self.last_wave_time = pygame.time.get_ticks()
self.last_update_time = pygame.time.get_ticks()
self.size = size
"""
Updates the enemies and schedules waves, if necessary. This returns the number
of enemies that have hit the destination.
"""
def update(self, mapdata):
retval = 0
# Update the enemies
for curr in EnemyManager.enemies:
curr.update(pygame.time.get_ticks()-self.last_update_time, mapdata)
if(curr.dead() or curr.offscreen(mapdata)):
EnemyManager.enemies.remove(enemy)
curr.sprite.kill() # Remove the sprite from the sprite group
if(curr.atDestination(mapdata)):
EnemyManager.enemies.remove(curr)
curr.sprite.kill()
retval += 1
self.last_update_time = pygame.time.get_ticks()
current_time = pygame.time.get_ticks()
# Spawn enemies that need to be spawned
spawning = True
while(spawning and not EnemyManager.enemy_queue.empty()):
current_enemy = EnemyManager.enemy_queue.get()
if(current_enemy[0] > current_time): # Then we don't need to spawn yet
EnemyManager.enemy_queue.put(current_enemy)
spawning = False
else:
EnemyManager.enemies.append(current_enemy[1])
# If enough time has passed, and we're not spawning a wave, spawn a wave
if(current_time - EnemyManager.last_wave_time >= EnemyManager.wave_interval):
# Spawn a wave!
start = mapdata.getStartingTile()
size = mapdata.getTileSize()
EnemyManager.last_wave_time = current_time
for index in range(0, EnemyManager.basic_enemies):
new_enemy = enemy.Enemy(start[0], start[1], EnemyManager.spritegroup, size)
scheduled_time = index*EnemyManager.spawn_interval+current_time
EnemyManager.enemy_queue.put((scheduled_time, new_enemy))
# Increase the difficulty!
#EnemyManager.basic_enemies = 0
return retval
"""
Draw all enemies in the game to the screen.
"""
def draw(self, surface):
EnemyManager.spritegroup.draw(surface)
"""
Given a pygame event from the event queue, spawn an enemy (if necessary).
The coordinates are the x and y coordinates of the starting tile (a tuple).
"""
def spawnEnemy(self, event, coordinates):
if(event.type == EnemyManager.SPAWN_EVENT_BASIC):
EnemyManager.enemies.append(enemy.Enemy(coordinates[0], coordinates[1],
EnemyManager.spritegroup, self.size))
# A little trick so we can run the game from here in IDLE
if __name__ == '__main__':
execfile("main.py")