4
4
from PIL import Image
5
5
from pynput .mouse import Button , Controller
6
6
7
- ## Updates
7
+ # TODO:
8
8
# Spawn Optimization
9
9
# Landing Bug
10
10
11
- # Initalize Pygame
11
+ # Initialize Pygame
12
12
pygame .init ()
13
13
14
- # Setup the clock for a decent framerate
14
+ # Setup the clock for a decent frame rate
15
15
clock = pygame .time .Clock ()
16
16
17
17
# Background Image
18
18
background = pygame .image .load ('Flight-Control-Bot/Map.png' )
19
19
20
20
# Creating a Window
21
- screen_width = 1920 / 2
22
- screen_height = 1080 / 2
23
- screen = pygame .display .set_mode ((screen_width , screen_height ))
21
+ screen_width = 1920 / 2
22
+ screen_height = 1080 / 2
23
+ screen = pygame .display .set_mode ((1920 / 2 , 1080 / 2 ))
24
24
25
- def dist (x ,y ):
26
- distance = (((x [0 ] - y [0 ]) ** 2 ) + ((x [1 ] - y [1 ] ) ** 2 )) ** (0.5 )
25
+ green_dots = []
26
+ red_dot = pygame .image .load ('Flight-Control-Bot/red_dot.png' )
27
+ green_dot = pygame .image .load ('Flight-Control-Bot/green_dot.png' )
28
+
29
+
30
+ def dist (x , y ):
31
+ distance = (((x [0 ] - y [0 ]) ** 2 ) + ((x [1 ] - y [1 ]) ** 2 )) ** 0.5
27
32
return distance
28
33
29
34
@@ -40,57 +45,50 @@ def orientation(p1, p2, p3):
40
45
(float (p2 [0 ] - p1 [0 ]) * (p3 [1 ] - p2 [1 ]))
41
46
# print(val)
42
47
43
- if ( val == 0 ) :
48
+ if val == 0 :
44
49
# Clockwise orientation
45
- if ( dist (p1 , p3 ) < dist (p2 , p3 ) ):
50
+ if dist (p1 , p3 ) < dist (p2 , p3 ):
46
51
result = 0
47
52
else :
48
53
result = 100
49
54
else :
50
55
# Counterclockwise orientation
51
- result = - 1 * val
56
+ result = - 1 * val
52
57
53
58
# print(result)
54
- return (result - 0.5 )
55
-
59
+ return result - 0.5
56
60
57
- green_dots = [[0 , screen_height - 60 ], [screen_width / 2 , screen_height / 2 ]]
58
- red_dot = pygame .image .load ('Flight-Control-Bot/red_dot.png' )
59
- green_dot = pygame .image .load ('Flight-Control-Bot/green_dot.png' )
60
61
61
62
class Plane :
62
63
global screen
63
64
64
- def __init__ (self , spawn_at = [750 ,100 ], offset_Angle = - 2 , LandAngle = 0 , Path = None ):
65
+ def __init__ (self , spawn_at = [750 , 100 ], offset_angle = - 2 ):
65
66
66
67
# Images
67
68
self .Image_Path = "Flight-Control-Bot/Plane1.png"
68
69
self .img_pure = Image .open (self .Image_Path )
69
- self .img_og = pygame .image .load (self .Image_Path )
70
- self .img_gpy = pygame .transform .rotate (self .img_og , offset_Angle )
70
+ self .img_og = pygame .image .load (self .Image_Path )
71
+ self .img_gpy = pygame .transform .rotate (self .img_og , offset_angle )
71
72
72
73
# Speed
73
74
self .Speed = 0.4
74
75
self .flying = True
75
76
76
77
# Position
77
- self .position = spawn_at # [random.randint(0, 1920/2), random.randint(0, 540)]
78
+ self .position = spawn_at # [random.randint(0, 1920/2), random.randint(0, 540)]
78
79
79
80
# Angle In Degrees
80
- self .Angle = self .angle_to_center () # random.randint(0, 180)
81
+ self .Angle = self .angle_to_center () # random.randint(0, 180)
81
82
print (f"{ self .Angle } " )
82
83
83
- self .OffsetAngle = offset_Angle
84
-
84
+ self .OffsetAngle = offset_angle
85
85
86
86
self .landing_strip = np .array ([(372 , 192 ), (565 , 248 )])
87
87
88
88
# self.Tow = False
89
- self .shrinkage = [i for i in range (80 , 10 , - 3 )]
89
+ self .shrinkage = [j for j in range (80 , 10 , - 3 )]
90
90
91
- self .Path = Path #[[200, 200], [300, 300], [400, 200], [500, 300], [600, 200]]
92
- if self .Path is None :
93
- self .Path = []
91
+ self .Path = []
94
92
95
93
self .Tow = False
96
94
@@ -104,10 +102,17 @@ def __init__(self, spawn_at = [750,100], offset_Angle = -2, LandAngle = 0, Path
104
102
self .red_dots .append (i )
105
103
self .turn (0 )
106
104
105
+ # Turns the plane by a certain bank angle
107
106
def turn (self , bank ):
108
107
self .Angle += bank
109
108
self .img_gpy = pygame .transform .rotate (self .img_og , self .OffsetAngle + self .Angle )
110
109
110
+ # Turns the plane to a Certain Angle
111
+ def turn_to (self , angle ):
112
+ self .Angle = angle
113
+ self .img_gpy = pygame .transform .rotate (self .img_og , self .OffsetAngle + self .Angle )
114
+
115
+ # Starts to fade the sprite on approaching landing
111
116
def fade (self ):
112
117
self .img_og .fill ((255 , 255 , 255 , int (self .alpha )), special_flags = pygame .BLEND_RGBA_MULT )
113
118
self .alpha /= 1.2
@@ -122,40 +127,52 @@ def fade(self):
122
127
def fly (self ):
123
128
self .tick += 1
124
129
125
- bank = 5
130
+ # Tuning Turing Intensity
126
131
fine_bank = 0.01
127
132
128
-
129
- if (self .flying == False ):
130
- if (self .tick % 40 == 0 ):
131
- print ("Tick" )
132
- print ("landing: " ,self .landing_strip )
133
- if (self .Path [0 ] in self .landing_strip ):
133
+ if not self .flying :
134
+ if self .tick % 40 == 0 :
135
+ # print("Tick")
136
+ # print("landing: ",self.landing_strip)
137
+ if self .Path [0 ] in self .landing_strip :
134
138
self .fade ()
135
139
136
-
137
-
138
- if (len (self .Path ) != 0 ):
140
+ if len (self .Path ) != 0 :
139
141
# if (self.Path[0] not in red_dots):
140
142
# red_dots.append(self.Path[0])
141
143
dest = self .Path [0 ]
142
144
head = self .head_pose ()
143
145
tail = self .tail_pose ()
144
146
mid = (head + tail ) / 2
145
147
146
- turn_direction = orientation (head , tail , dest ) * fine_bank
148
+ turn_direction = orientation (head , tail , dest ) * fine_bank
147
149
# print("Ore", orientation(head, tail, dest))
148
150
# print("turnDir", turnDir)
149
151
self .turn (turn_direction )
150
152
151
- if ( dist (mid , self .Path [0 ]) < 15 ) :
153
+ if dist (mid , self .Path [0 ]) < 15 :
152
154
reached = self .Path .pop (0 )
153
155
self .red_dots .pop (0 )
154
156
# green_dots.append(reached)
155
157
# print("Reached", reached)
156
158
159
+ self .restrict_fly_zone ()
160
+
157
161
self .position = [self .position [0 ] + self .Speed * math .cos (math .radians (self .Angle )),
158
- self .position [1 ] - self .Speed * math .sin (math .radians (self .Angle ))]
162
+ self .position [1 ] - self .Speed * math .sin (math .radians (self .Angle ))]
163
+
164
+ # Keeps the plane bound to the screen
165
+ def restrict_fly_zone (self ):
166
+ # No restriction during early flight
167
+
168
+ if self .tick < 500 :
169
+ return
170
+
171
+ if - 25 < self .position [0 ] < screen_width - 10 and - 25 < self .position [1 ] < screen_height - 10 :
172
+ return
173
+
174
+ self .turn_to (self .angle_to_center ())
175
+ print ("Out of Bounds" )
159
176
160
177
def land (self ):
161
178
if (len (self .LandLoc ) == 1 ):
@@ -164,73 +181,69 @@ def land(self):
164
181
def draw (self ):
165
182
screen .blit (self .img_gpy , self .position )
166
183
# Path Visualization
184
+ # Comment to disable Path Visualization
167
185
self .path_vis ()
168
186
169
187
def Towing (self , mouse_position , click_status ):
170
188
# When Clicked Start towing plane with mouse
171
- if ( click_status [0 ] == True ) :
172
- if (dist (mouse_position , (self .position [0 ] + (self .img_pure .size [0 ]/ 2 ),
173
- self .position [1 ] + (self .img_pure .size [1 ]/ 2 ))) < 35 ):
174
- if ( len (self .Path ) > 0 ) :
189
+ if click_status [0 ]:
190
+ if (dist (mouse_position , (self .position [0 ] + (self .img_pure .size [0 ] / 2 ),
191
+ self .position [1 ] + (self .img_pure .size [1 ] / 2 ))) < 35 ):
192
+ if len (self .Path ) > 0 :
175
193
self .Path .clear ()
176
194
self .red_dots .clear ()
177
195
self .Tow = True
178
196
# print("Towing")
179
197
180
- if ( self .Tow == True ) :
181
- if ( click_status [0 ] == False ) :
198
+ if self .Tow :
199
+ if not click_status [0 ]:
182
200
self .Tow = False
183
201
# print("UnTOWED")
184
- elif ( dist (mouse_position , self .landing_strip [0 ]) < 15 ) :
202
+ elif dist (mouse_position , self .landing_strip [0 ]) < 15 :
185
203
self .flying = False
186
204
for i in self .landing_strip :
187
205
self .Path .append (i )
188
206
self .red_dots .append (i )
189
207
# print("Locked")
190
- self .Tow = False ;
208
+ self .Tow = False
191
209
192
210
else :
193
- print (mouse_position )
211
+ # print(mouse_position)
194
212
self .red_dots .append (mouse_position )
195
213
self .Path .append (mouse_position )
196
214
# print(self.Path)
197
215
216
+ # Give angle to center from current position
217
+ def angle_to_center (self ):
218
+ screen_center = [screen_width / 2 , screen_height / 2 ]
219
+ return self .angle (self .position , screen_center )
220
+
221
+ @staticmethod
222
+ def angle (coord1 , coord2 ):
223
+
224
+ numerator = coord1 [1 ] - coord2 [1 ]
225
+ denominator = coord2 [0 ] - coord1 [0 ]
226
+ degrees = math .degrees (math .atan2 (numerator , denominator ))
227
+
228
+ return degrees
229
+
198
230
def head_pose (self ):
199
231
# green_dots.append(self.Pos)
200
232
theta = 90
201
233
angle = self .Angle + theta
202
234
head_loc = (self .position [0 ] + 30 + (27 * math .cos (math .radians (self .Angle ))
203
- - 2 * math .sin (math .radians (self .Angle ))),
204
- self .position [1 ] + 30 + (- 27 * math .sin (math .radians (self .Angle ))
205
- - 2 * math .cos (math .radians (self .Angle ))))
235
+ - 2 * math .sin (math .radians (self .Angle ))),
236
+ self .position [1 ] + 30 + (- 27 * math .sin (math .radians (self .Angle ))
237
+ - 2 * math .cos (math .radians (self .Angle ))))
206
238
207
239
# red_dots.append(head_loc)
208
240
return np .array (head_loc )
209
241
210
- def angle_to_center (self ):
211
- screen_center = [screen_width / 2 , screen_height / 2 ]
212
- return self .angle (self .position , screen_center )
213
-
214
- @staticmethod
215
- def angle (coord1 , coord2 ):
216
- print (coord1 ,coord2 )
217
- try :
218
- slope = - 1 * (coord2 [1 ] - coord1 [1 ]) / (coord2 [0 ] - coord1 [0 ])
219
- except ZeroDivisionError :
220
- if (coord2 [1 ] - coord1 [1 ] > 0 ):
221
- return - 90
222
- else :
223
- return 90
224
-
225
- degs = math .degrees (math .atan2 (- 1 * (coord2 [1 ] - coord1 [1 ]) , (coord2 [0 ] - coord1 [0 ])))
226
- print (degs )
227
- return degs
228
-
229
242
def tail_pose (self ):
230
243
theta = 90
231
244
angle = self .Angle + theta
232
245
tail_loc = (self .position [0 ] + 30 + (- 29 * math .cos (math .radians (self .Angle ))),
233
- self .position [1 ] + 30 + ( 29 * math .sin (math .radians (self .Angle ))))
246
+ self .position [1 ] + 30 + (29 * math .sin (math .radians (self .Angle ))))
234
247
235
248
# red_dots.append(tail_loc)
236
249
return np .array (tail_loc )
@@ -246,43 +259,46 @@ def path_vis(self):
246
259
247
260
class Fleet :
248
261
249
- planes = []
262
+ def __init__ (self ):
263
+
264
+ self .planes = []
250
265
251
266
def Manage (self , mouse_position , mouse_press_status ):
252
267
# if (time.time() == 1000)
253
268
for i in self .planes :
254
269
i .draw ()
255
270
i .fly ()
256
271
i .Towing (mouse_position , mouse_press_status )
257
- if ( i .alpha < 100 ) :
272
+ if i .alpha < 100 :
258
273
self .planes .pop (self .planes .index (i ))
259
274
260
- def speacialPlane (self , plane ):
275
+ def specialPlane (self , plane ):
261
276
self .planes .append (plane )
262
277
263
278
def Spawner (self ):
264
- should_spawn = random .choices ([1 ,0 ], weights = (1 , 1000 ), k = 1 )[0 ]
265
- if ( should_spawn ) :
279
+ should_spawn = random .choices ([1 , 0 ], weights = (1 , 10000 ), k = 1 )[0 ]
280
+ if should_spawn :
266
281
self .spawn ()
267
282
268
-
269
283
def spawn (self ):
270
- spawn_spots = {'up' : [[0 , - 60 ], [screen_width , - 10 ]], 'down' : [[0 , screen_height - 60 ], [screen_width , screen_height ]],
271
- 'left' : [[- 60 , 0 ], [0 , screen_height ]], 'right' : [[screen_width , 0 ], [screen_width + 60 , screen_height ]]}
284
+ spawn_spots = {'up' : [[0 , - 60 ], [screen_width , - 10 ]],
285
+ 'down' : [[0 , screen_height - 60 ], [screen_width , screen_height ]],
286
+ 'left' : [[- 60 , 0 ], [0 , screen_height ]],
287
+ 'right' : [[screen_width , 0 ], [screen_width + 60 , screen_height ]]}
272
288
273
289
_ , spawn_rect = random .choice (list (spawn_spots .items ()))
274
290
275
291
spawn_location = [random .randint (spawn_rect [0 ][0 ], spawn_rect [1 ][0 ]),
276
292
random .randint (spawn_rect [0 ][1 ], spawn_rect [1 ][1 ])]
277
- self .planes .append (Plane (spawn_at = spawn_location ))
293
+ self .planes .append (Plane (spawn_at = spawn_location ))
278
294
279
295
280
296
test_fleet = Fleet ()
281
297
282
298
# Special Plane Instance
283
299
# Plane1 h = 37.5685 w = 37.56
284
300
# plane1 = Plane( Angle = 90, Pos = (20, 170))
285
- # test_fleet.speacialPlane (plane1)
301
+ # test_fleet.specialPlane (plane1)
286
302
287
303
# Running Indicator
288
304
running = True
@@ -294,35 +310,26 @@ def spawn(self):
294
310
while running :
295
311
# Screen Color
296
312
screen .fill ((255 , 255 , 255 ))
297
- screen .blit (background , (0 ,0 ))
298
-
313
+ screen .blit (background , (0 , 0 ))
299
314
300
315
# Loop through pygame functions
301
316
for event in pygame .event .get ():
302
- ## Here we are looking for a very specific event
303
- ## Which is quit. and when it has occured we change a variable which
304
- ## causes our program to end
317
+ # Here we are looking for a very specific event
318
+ # Which is quit. and when it has occurred we change a variable which
319
+ # causes our program to end
305
320
if event .type == pygame .QUIT :
306
321
running = False
307
322
308
323
test_fleet .Spawner ()
309
324
test_fleet .Manage (pygame .mouse .get_pos (), pygame .mouse .get_pressed ())
310
325
311
-
312
- ## DEBUGGING TOOLS
326
+ # DEBUGGING TOOLS
313
327
for i in green_dots :
314
328
screen .blit (green_dot , (i [0 ] - 4 , i [1 ] - 4 ))
315
329
316
-
317
-
318
-
319
330
# if(int(time.time())%10 == 0):
320
331
# print(pygame.mouse.get_pressed())
321
332
# for i in planes:
322
333
# i.Head()
323
334
324
-
325
335
pygame .display .update ()
326
-
327
-
328
-
0 commit comments