-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplane.py
281 lines (228 loc) · 9.04 KB
/
plane.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import pygame
import os
class Plane(pygame.sprite.Sprite):
def __init__(self, screen, scenery):
# static
self.TAKEOFFSPEED = 100
pygame.sprite.Sprite.__init__(self)
self.plane_img = pygame.image.load(os.path.join('graphics', 'plane_small_gear.png'))
self.plane_rect = self.plane_img.get_rect()
self.plane_rect.centerx = 400
self.plane_rect.centery = 660
# Distance travelled in x-direction
self.travelled_x_distance = 0
self.screen = screen
self.scenery = scenery
self.gear_down = True
self.air_brake_out = False
self.engine_running = True
self.angle = 0
self.angle_old = 0
self.set_angle = 0
self.thrust = 1
self.thrust_old = 0
self.set_thrust = 0
# 485 Knots = 900 km/h
self.knots = 0
self.feet = 0
self.images_computes = self.__precompute_rotated_plane(self.plane_img, self.plane_rect)
self.explosion_images = self.__load_explosion_images()
self.explosion_counter = 0
def __precompute_rotated_plane(self, image, rect):
images_computes = {}
for degree in xrange(-180, 180):
images_computes[degree] = self.__rot_center(image, rect, degree)
return images_computes
def __rot_center(self, image, rect, angle):
rot_image = pygame.transform.rotate(image, angle)
rot_rect = rot_image.get_rect(center=rect.center)
return (rot_image, rot_rect)
def __load_explosion_images(self):
retval = []
for i in range(1, 25):
image = pygame.image.load(
os.path.join(
'graphics',
"explosion_{}.png".format(i)
)
)
rect = image.get_rect()
retval.append((image, rect))
return retval
def increase_speed(self):
if self.set_thrust < 100 and self.engine_running:
self.set_thrust = self.set_thrust + 10
def decrease_speed(self):
if self.set_thrust > 0:
self.set_thrust = self.set_thrust - 10
def turn_engines_off(self):
self.engine_running = False
def get_angle(self):
return self.angle
def get_speed(self):
return self.knots
def set_speed(self, knots):
self.knots = knots
def get_y_coords(self):
return self.plane_rect.centery
def push_down(self):
if self.set_angle > -20 and self.feet > 0:
self.set_angle = self.set_angle - 5
def pull_up(self):
if self.set_angle < 20 and self.knots > self.TAKEOFFSPEED:
self.set_angle = self.set_angle + 5
def get_feet(self):
return self.feet
def get_thrust(self):
return self.thrust
def get_travelled_x_distance(self):
return self.travelled_x_distance
def get_rect(self):
return self.plane_rect
def update(self):
# Bring real angle to set_angle
self.__update_angles()
# Bring real thrust to set thrust
self.__update_thrust()
# Compute the speed
self.__update_knots()
# Update travelled x direction
self.__update_x_travelled_instance()
# Compute the height
if self.feet >= 0:
self.feet = self.feet + self.__calculate_climbrate(self.angle, self.knots)
if self.feet < 0:
self.feet = 0
# Incrementally rotate the plane sprite
if int(self.angle) is not int(self.angle_old):
result = self.__compute_single_plane(self.feet, self.angle)
self.plane_img = result[0]
self.plane_rect = result[1]
# Render the plane
self.screen.blit(self.plane_img, self.plane_rect)
# Shutdown thrust if engines are shutdown
if not self.engine_running and self.thrust > 0:
self.decrease_speed()
# Render the explosion frame by frame if the engines are out
if not self.engine_running and self.explosion_counter < 24:
explosion_rect = self.explosion_images[self.explosion_counter][1]
# center the explosion rect on the plane engines position
explosion_rect.centerx = self.plane_rect.centerx + 20
explosion_rect.centery = self.plane_rect.centery + 20
self.screen.blit(
self.explosion_images[self.explosion_counter][0],
explosion_rect
)
# Slow down the explosion effect
if self.travelled_x_distance % 4 == 0:
self.explosion_counter += 1
def __update_thrust(self):
# Incrementally change the thrust
if self.thrust is self.set_thrust:
return
self.thrust_old = self.thrust
if self.thrust < self.set_thrust:
self.thrust = self.thrust + 1
if self.thrust > self.set_thrust:
self.thrust = self.thrust - 1
def __update_angles(self):
if int(self.angle) is not int(self.set_angle):
self.angle_old = self.angle
# Pull nose up
if self.angle < self.set_angle:
self.angle = self.angle + 0.5
# Push nose down
if self.angle > self.set_angle:
self.angle = self.angle - 0.5
def __calculate_climbrate(self, angle, speed):
if int(self.angle) is 0 and int(self.set_angle) is 0:
return 0
value = 3 # default
if speed < 100:
return 0
if speed < 200:
value = 1
if speed < 400:
value = 2
if angle < 0:
return value * -1
return value
def toggle_landing_gear(self):
# Below 50 feet the landing gear cannot be retracted
if self.feet < 50:
return
# Toggle the gear state
plane_temp_x = self.plane_rect.centerx
plane_temp_y = self.plane_rect.centery
if self.gear_down:
self.plane_img = pygame.image.load(os.path.join('graphics', 'plane_small.png'))
self.gear_down = False
else:
self.plane_img = pygame.image.load(os.path.join('graphics', 'plane_small_gear.png'))
self.gear_down = True
# Calculate the new images
self.plane_rect = self.plane_img.get_rect()
self.plane_rect.centerx = plane_temp_x
self.plane_rect.centery = plane_temp_y
self.images_computes = self.__precompute_rotated_plane(self.plane_img, self.plane_rect)
def toggle_airbrake(self):
self.air_brake_out = not self.air_brake_out
def __compute_single_plane(self, feet, angle):
smoothed_angle = int(angle + 0.5)
plane_img = self.images_computes[smoothed_angle][0]
plane_rect = self.images_computes[smoothed_angle][1]
x_delta = int((feet * 1.6) + 0.5)
plane_rect.centery = 745 - x_delta
# Limit sliding space of the plane to the upper and lower barrier
if plane_rect.centery > 660:
plane_rect.centery = 660
if plane_rect.centery < 280:
plane_rect.centery = 280
return (plane_img, plane_rect)
def __update_knots(self):
# Bring real knots to set_knots
self.knots = self.knots + self.__calculate_acceleration(self.angle, self.thrust)
def __update_x_travelled_instance(self):
if self.knots <= 0:
return
if self.knots > 40:
self.travelled_x_distance += int(round(self.knots // 40))
return
if self.knots > 30:
self.travelled_x_distance += int(round(self.knots // 30))
return
if self.knots > 20:
self.travelled_x_distance += int(round(self.knots // 20))
return
if self.knots > 10:
self.travelled_x_distance += int(round(self.knots // 10))
return
if self.knots > 5:
self.travelled_x_distance += int(round(self.knots // 5))
return
if self.knots > 2:
self.travelled_x_distance += int(round(self.knots // 2))
return
self.travelled_x_distance += int(round(self.knots))
def __calculate_acceleration(self, angle, thrust):
thrust_coeff = 0.002
air_drag = 0.08
temp_val = 0
# If the gear is down the air drag increases
if self.gear_down and self.feet > 50:
air_drag = air_drag * 2
# If the airbreak is out increase the airdrag
if self.air_brake_out and self.knots > 0:
air_drag = air_drag * 2
# in case of climb decrease acceleration based on angle
if int(angle + 0.5) > 0:
return (thrust * thrust_coeff) - (angle * (thrust_coeff * 6))
# in case of decent increas acceleration based on angle
if int(angle + 0.5) < 0:
return (thrust * thrust_coeff) - (angle * (thrust_coeff * 6)) - air_drag
# in horizontal and moving
if int(angle + 0.5) == 0:
temp_val = (thrust * thrust_coeff)
if self.knots > 0:
temp_val = temp_val - air_drag
return temp_val