@@ -59,16 +59,18 @@ def orientation(p1, p2, p3):
59
59
return result - 0.5
60
60
61
61
62
- class Plane :
63
- global screen
62
+ class Plane (pygame .sprite .Sprite ):
64
63
65
64
def __init__ (self , spawn_at = [750 , 100 ], offset_angle = - 2 ):
66
-
65
+ print ("Plane" )
66
+ super ().__init__ ()
67
67
# Images
68
- self .Image_Path = "Flight-Control-Bot/Plane1.png"
69
- self .img_pure = Image .open (self .Image_Path )
70
- self .img_og = pygame .image .load (self .Image_Path )
71
- self .img_gpy = pygame .transform .rotate (self .img_og , offset_angle )
68
+ self .image_path = "Flight-Control-Bot/Plane1.png"
69
+ self .image_aligned = pygame .image .load (self .image_path )
70
+ self .image = pygame .image .load (self .image_path )
71
+ self .rect = self .image .get_rect ()
72
+ # self.img_pure = Image.open(self.Image_Path)
73
+ # self.img_gpy = pygame.transform.rotate(self.img_og, offset_angle)
72
74
73
75
# Speed
74
76
self .Speed = 0.4
@@ -102,17 +104,31 @@ def __init__(self, spawn_at=[750, 100], offset_angle=-2):
102
104
self .red_dots .append (i )
103
105
self .turn (0 )
104
106
105
- # Turns the plane by a certain bank angle
107
+ # # Turns the plane by a certain bank angle
106
108
def turn (self , bank ):
107
109
self .Angle += bank
108
- self .img_gpy = pygame .transform .rotate (self .img_og , self .OffsetAngle + self .Angle )
110
+ self .image = pygame .transform .rotate (self .image_aligned , self .OffsetAngle + self .Angle )
111
+ self .rect = self .image .get_rect ()
109
112
110
113
# Turns the plane to a Certain Angle
111
114
def turn_to (self , angle ):
112
115
self .Angle = angle
113
- self .img_gpy = pygame .transform .rotate (self .img_og , self .OffsetAngle + self .Angle )
116
+ self .image = pygame .transform .rotate (self .image_aligned , self .OffsetAngle + self .Angle )
117
+ self .rect = self .image .get_rect ()
114
118
115
119
# Starts to fade the sprite on approaching landing
120
+
121
+ def update (self ):
122
+ # for i in self.planes:
123
+ # i.draw()
124
+ # i.fly()
125
+ # i.Towing(mouse_position, mouse_press_status)
126
+ # if i.alpha < 100:
127
+ # self.planes.pop(self.planes.index(i))
128
+ self .draw ()
129
+ self .fly ()
130
+ self .Towing (pygame .mouse .get_pos (), pygame .mouse .get_pressed ())
131
+
116
132
def fade (self ):
117
133
self .img_og .fill ((255 , 255 , 255 , int (self .alpha )), special_flags = pygame .BLEND_RGBA_MULT )
118
134
self .alpha /= 1.2
@@ -175,20 +191,21 @@ def restrict_fly_zone(self):
175
191
print ("Out of Bounds" )
176
192
177
193
def land (self ):
178
- if ( len (self .LandLoc ) == 1 ) :
194
+ if len (self .LandLoc ) == 1 :
179
195
self .flying = False
180
196
181
197
def draw (self ):
182
- screen .blit (self .img_gpy , self .position )
198
+ # print( self.position)
199
+ self .rect .center = self .position
200
+ # screen.blit(self.img_gpy, self.position)
183
201
# Path Visualization
184
202
# Comment to disable Path Visualization
185
203
self .path_vis ()
186
204
187
205
def Towing (self , mouse_position , click_status ):
188
206
# When Clicked Start towing plane with mouse
189
207
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 ):
208
+ if self .rect .collidepoint (mouse_position ):
192
209
if len (self .Path ) > 0 :
193
210
self .Path .clear ()
194
211
self .red_dots .clear ()
@@ -261,16 +278,13 @@ class Fleet:
261
278
262
279
def __init__ (self ):
263
280
264
- self .planes = []
265
-
266
- def Manage (self , mouse_position , mouse_press_status ):
281
+ self .planes_group = pygame . sprite . Group ()
282
+ print ( dir ( self . planes_group ))
283
+ def Manage (self ):
267
284
# if (time.time() == 1000)
268
- for i in self .planes :
269
- i .draw ()
270
- i .fly ()
271
- i .Towing (mouse_position , mouse_press_status )
272
- if i .alpha < 100 :
273
- self .planes .pop (self .planes .index (i ))
285
+ self .planes_group .draw (screen )
286
+ self .planes_group .update ()
287
+
274
288
275
289
def specialPlane (self , plane ):
276
290
self .planes .append (plane )
@@ -290,11 +304,16 @@ def spawn(self):
290
304
291
305
spawn_location = [random .randint (spawn_rect [0 ][0 ], spawn_rect [1 ][0 ]),
292
306
random .randint (spawn_rect [0 ][1 ], spawn_rect [1 ][1 ])]
293
- self .planes .append (Plane (spawn_at = spawn_location ))
294
307
308
+ plane_sprite = Plane (spawn_at = spawn_location )
309
+
310
+ self .planes_group .add (plane_sprite )
295
311
312
+
313
+ # test_fleet = Fleet()
296
314
test_fleet = Fleet ()
297
315
316
+
298
317
# Special Plane Instance
299
318
# Plane1 h = 37.5685 w = 37.56
300
319
# plane1 = Plane( Angle = 90, Pos = (20, 170))
@@ -321,7 +340,7 @@ def spawn(self):
321
340
running = False
322
341
323
342
test_fleet .Spawner ()
324
- test_fleet .Manage (pygame . mouse . get_pos (), pygame . mouse . get_pressed () )
343
+ test_fleet .Manage ()
325
344
326
345
# DEBUGGING TOOLS
327
346
for i in green_dots :
0 commit comments