Skip to content

Commit 7617174

Browse files
committed
Sprites and Groups
Default pygame classes were implemented it requires changes to be made to alot of the rest of the code. Turning is fixed. Towing is yet to be fixed and still glitches :(
1 parent 838d16e commit 7617174

File tree

1 file changed

+44
-25
lines changed

1 file changed

+44
-25
lines changed

main.py

+44-25
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,18 @@ def orientation(p1, p2, p3):
5959
return result - 0.5
6060

6161

62-
class Plane:
63-
global screen
62+
class Plane(pygame.sprite.Sprite):
6463

6564
def __init__(self, spawn_at=[750, 100], offset_angle=-2):
66-
65+
print("Plane")
66+
super().__init__()
6767
# 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)
7274

7375
# Speed
7476
self.Speed = 0.4
@@ -102,17 +104,31 @@ def __init__(self, spawn_at=[750, 100], offset_angle=-2):
102104
self.red_dots.append(i)
103105
self.turn(0)
104106

105-
# Turns the plane by a certain bank angle
107+
# # Turns the plane by a certain bank angle
106108
def turn(self, bank):
107109
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()
109112

110113
# Turns the plane to a Certain Angle
111114
def turn_to(self, angle):
112115
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()
114118

115119
# 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+
116132
def fade(self):
117133
self.img_og.fill((255, 255, 255, int(self.alpha)), special_flags=pygame.BLEND_RGBA_MULT)
118134
self.alpha /= 1.2
@@ -175,20 +191,21 @@ def restrict_fly_zone(self):
175191
print("Out of Bounds")
176192

177193
def land(self):
178-
if (len(self.LandLoc) == 1):
194+
if len(self.LandLoc) == 1:
179195
self.flying = False
180196

181197
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)
183201
# Path Visualization
184202
# Comment to disable Path Visualization
185203
self.path_vis()
186204

187205
def Towing(self, mouse_position, click_status):
188206
# When Clicked Start towing plane with mouse
189207
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):
192209
if len(self.Path) > 0:
193210
self.Path.clear()
194211
self.red_dots.clear()
@@ -261,16 +278,13 @@ class Fleet:
261278

262279
def __init__(self):
263280

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):
267284
# 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+
274288

275289
def specialPlane(self, plane):
276290
self.planes.append(plane)
@@ -290,11 +304,16 @@ def spawn(self):
290304

291305
spawn_location = [random.randint(spawn_rect[0][0], spawn_rect[1][0]),
292306
random.randint(spawn_rect[0][1], spawn_rect[1][1])]
293-
self.planes.append(Plane(spawn_at=spawn_location))
294307

308+
plane_sprite = Plane(spawn_at=spawn_location)
309+
310+
self.planes_group.add(plane_sprite)
295311

312+
313+
# test_fleet = Fleet()
296314
test_fleet = Fleet()
297315

316+
298317
# Special Plane Instance
299318
# Plane1 h = 37.5685 w = 37.56
300319
# plane1 = Plane( Angle = 90, Pos = (20, 170))
@@ -321,7 +340,7 @@ def spawn(self):
321340
running = False
322341

323342
test_fleet.Spawner()
324-
test_fleet.Manage(pygame.mouse.get_pos(), pygame.mouse.get_pressed())
343+
test_fleet.Manage()
325344

326345
# DEBUGGING TOOLS
327346
for i in green_dots:

0 commit comments

Comments
 (0)