-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasteroid.py
208 lines (184 loc) · 9.56 KB
/
asteroid.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
import pygame
import parameters
import random
DEBUG = False
class AsteroidGoldLoader:
def __init__(self):
# gold asteroid
self.mass = 1
self.asteroid_animation_frame_timer_limit = 3
self.asteroid_image = []
self.asteroid_mask = []
self.asteroid_frames = 30
for frame in range(0, self.asteroid_frames):
load_filename = 'images/asteroid_animated_908/blender_output' \
+ str('{:0>4}'.format(frame)) \
+ '.png'
print(load_filename)
asteroid_image_tmp = pygame.image.load(load_filename)
self.asteroid_image.append(asteroid_image_tmp)
self.asteroid_mask.append(pygame.mask.from_surface(asteroid_image_tmp))
self.asteroid_treasure = True
class Asteroid800Loader:
def __init__(self):
# 800 asteroid
self.mass = 5
self.asteroid_frames = 30
self.asteroid_animation_frame_timer_limit = 3
self.asteroid_image = []
self.asteroid_mask = []
self.asteroid_frames = 29
for frame in range(0, self.asteroid_frames + 1):
load_filename = 'images/asteroid_animated_800/' \
+ str('{:0>4}'.format(frame)) \
+ '.png'
asteroid_image_tmp = pygame.image.load(load_filename)
self.asteroid_image.append(asteroid_image_tmp)
self.asteroid_mask.append(pygame.mask.from_surface(asteroid_image_tmp))
self.asteroid_treasure = False
class Asteroid802Loader:
def __init__(self):
# 802 asteroid
self.mass = 2
self.asteroid_animation_frame_timer_limit = 3
self.asteroid_image = []
self.asteroid_mask = []
self.asteroid_frames = 29
for frame in range(0, self.asteroid_frames + 1):
load_filename = 'images/asteroid_animated_802/' \
+ str('{:0>4}'.format(frame)) \
+ '.png'
asteroid_image_tmp = pygame.image.load(load_filename)
self.asteroid_image.append(asteroid_image_tmp)
self.asteroid_mask.append(pygame.mask.from_surface(asteroid_image_tmp))
self.asteroid_treasure = False
class AsteroidLavaBig:
def __init__(self):
# 802 asteroid
self.mass = 2
self.asteroid_animation_frame_timer_limit = 1
self.asteroid_image = []
self.asteroid_mask = []
self.asteroid_frames = 36
for frame in range(1, self.asteroid_frames + 1):
load_filename = 'images/asteroid_lava_big/' \
+ str('{:0>4}'.format(frame)) \
+ '.png'
asteroid_image_tmp = pygame.image.load(load_filename)
self.asteroid_image.append(asteroid_image_tmp)
self.asteroid_mask.append(pygame.mask.from_surface(asteroid_image_tmp))
self.asteroid_treasure = False
class AsteroidLoader:
def __init__(self, screen):
self.screen = screen
self.asteroid_gold_data = AsteroidGoldLoader()
self.asteroid_800_data = Asteroid800Loader()
self.asteroid_802_data = Asteroid802Loader()
self.asteroid_lava_big = AsteroidLavaBig()
def reset(self, count_of_asteroids):
asteroids = []
for asteroid_id in range(count_of_asteroids):
asteroid_type = random.randint(0, 3)
if asteroid_type == 0:
asteroids.append(Asteroid(self.asteroid_gold_data, self.screen))
if asteroid_type == 1:
asteroids.append(Asteroid(self.asteroid_800_data, self.screen))
if asteroid_type == 2:
asteroids.append(Asteroid(self.asteroid_802_data, self.screen))
if asteroid_type == 3:
asteroids.append(Asteroid(self.asteroid_lava_big, self.screen))
return asteroids
class Asteroid:
def __init__(self, asteroid_data, screen):
self.screen = screen
self.asteroid_image = asteroid_data.asteroid_image
self.asteroid_mask = asteroid_data.asteroid_mask
self.collision_buffer = []
self.width = asteroid_data.asteroid_image[0].get_width()
self.height = asteroid_data.asteroid_image[0].get_height()
self.asteroid_animation_frame = random.randint(0, 19)
self.asteroid_animation_frame_timer = random.randint(0, 8)
self.asteroid_frames = asteroid_data.asteroid_frames
self.asteroid_animation_frame_timer_limit = 3
self.asteroid_treasure = asteroid_data.asteroid_treasure
self.asteroid_position_x, self.asteroid_position_y, \
self.asteroid_acceleration_x, self.asteroid_acceleration_y = self.initial_inertia()
self.mass = asteroid_data.mass
self.asteroid_hit = False
self.asteroid_destroyed = False
def in_collision(self, other_asteroid):
offset_x_a = other_asteroid.asteroid_position_x - self.asteroid_position_x
offset_x_b = other_asteroid.asteroid_position_x + other_asteroid.width - self.asteroid_position_x + self.width
offset_y_a = other_asteroid.asteroid_position_y - self.asteroid_position_y
offset_y_b = other_asteroid.asteroid_position_y + other_asteroid.width - self.asteroid_position_y + self.width
if offset_x_a < self.width and offset_x_b > 0 and offset_y_a < self.height and offset_y_b > 0:
return True
f = 3 # extend to future frames
for f in range(1, f + 1):
offset_x_a = other_asteroid.asteroid_position_x + (f * other_asteroid.asteroid_acceleration_x) \
- self.asteroid_position_x + (f * self.asteroid_acceleration_x)
offset_x_b = other_asteroid.asteroid_position_x + (f * self.asteroid_acceleration_x) \
+ other_asteroid.width - self.asteroid_position_x + self.width + (f * self.asteroid_acceleration_x)
offset_y_a = other_asteroid.asteroid_position_y + (f * self.asteroid_acceleration_y) \
- self.asteroid_position_y + (f * self.asteroid_acceleration_y)
offset_y_b = other_asteroid.asteroid_position_y + (f * self.asteroid_acceleration_y) \
+ other_asteroid.width - self.asteroid_position_y + self.width + (f * self.asteroid_acceleration_y)
if offset_x_a < self.width and offset_x_b > 0 and offset_y_a < self.height and offset_y_b > 0:
return True
return False
def in_viewport(self):
if self.asteroid_position_x < parameters.VIRTUAL_SCREEN_RESPAWN_STRIP_LEFT_TO:
return False
elif self.asteroid_position_x > parameters.VIRTUAL_SCREEN_RESPAWN_STRIP_RIGHT_FROM:
return False
elif self.asteroid_position_y < parameters.VIRTUAL_SCREEN_RESPAWN_STRIP_TOP_TO:
return False
elif self.asteroid_position_y > parameters.VIRTUAL_SCREEN_RESPAWN_STRIP_BOTTOM_FROM:
return False
else:
return True
@staticmethod
def initial_inertia():
if random.randint(0, 1):
initial_x = random.randint(parameters.VIRTUAL_SCREEN_RESPAWN_STRIP_LEFT_FROM,
parameters.VIRTUAL_SCREEN_RESPAWN_STRIP_LEFT_TO)
else:
initial_x = random.randint(parameters.VIRTUAL_SCREEN_RESPAWN_STRIP_RIGHT_FROM,
parameters.VIRTUAL_SCREEN_RESPAWN_STRIP_RIGHT_TO)
if random.randint(0, 1):
initial_y = random.randint(parameters.VIRTUAL_SCREEN_RESPAWN_STRIP_TOP_FROM,
parameters.VIRTUAL_SCREEN_RESPAWN_STRIP_TOP_TO)
else:
initial_y = random.randint(parameters.VIRTUAL_SCREEN_RESPAWN_STRIP_BOTTOM_FROM,
parameters.VIRTUAL_SCREEN_RESPAWN_STRIP_BOTTOM_TO)
inertia_x = random.uniform(-2.5, +2.5)
while inertia_x == 0.0:
inertia_x = random.uniform(-2.5, +2.5)
inertia_y = random.uniform(-2.5, +2.5)
while inertia_y == 0.0:
inertia_y = random.uniform(-2.5, +2.5)
return (float(initial_x), float(initial_y),
float(inertia_x), float(inertia_y))
def recalculate_position(self):
self.asteroid_position_x += self.asteroid_acceleration_x
self.asteroid_position_y += self.asteroid_acceleration_y
if self.asteroid_position_x < parameters.VIRTUAL_SCREEN_RESPAWN_STRIP_LEFT_FROM or \
self.asteroid_position_x > parameters.VIRTUAL_SCREEN_RESPAWN_STRIP_RIGHT_TO or \
self.asteroid_position_y < parameters.VIRTUAL_SCREEN_RESPAWN_STRIP_TOP_FROM or \
self.asteroid_position_y > parameters.VIRTUAL_SCREEN_RESPAWN_STRIP_BOTTOM_TO:
# inertia reset
self.asteroid_position_x, self.asteroid_position_y, \
self.asteroid_acceleration_x, self.asteroid_acceleration_y = self.initial_inertia()
def get_collision_data(self):
return (self.asteroid_mask[self.asteroid_animation_frame],
int(self.asteroid_position_x), int(self.asteroid_position_y))
def redraw(self):
self.recalculate_position()
self.asteroid_animation_frame_timer += 1
if self.asteroid_animation_frame_timer > self.asteroid_animation_frame_timer_limit:
self.asteroid_animation_frame_timer = 0
self.asteroid_animation_frame += 1
if self.asteroid_animation_frame > self.asteroid_frames - 1:
self.asteroid_animation_frame = 0
self.screen.blit(self.asteroid_image[self.asteroid_animation_frame],
(int(self.asteroid_position_x), int(self.asteroid_position_y)))