Skip to content

Commit 838d16e

Browse files
committed
Added flight restriction boundary
The planes are now bound to a certain flying region and turn to center when trying to escape said region. Additional refactors
1 parent 0869258 commit 838d16e

File tree

1 file changed

+103
-96
lines changed

1 file changed

+103
-96
lines changed

main.py

+103-96
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,31 @@
44
from PIL import Image
55
from pynput.mouse import Button, Controller
66

7-
## Updates
7+
# TODO:
88
# Spawn Optimization
99
# Landing Bug
1010

11-
# Initalize Pygame
11+
# Initialize Pygame
1212
pygame.init()
1313

14-
# Setup the clock for a decent framerate
14+
# Setup the clock for a decent frame rate
1515
clock = pygame.time.Clock()
1616

1717
# Background Image
1818
background = pygame.image.load('Flight-Control-Bot/Map.png')
1919

2020
# 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))
2424

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
2732
return distance
2833

2934

@@ -40,57 +45,50 @@ def orientation(p1, p2, p3):
4045
(float(p2[0] - p1[0]) * (p3[1] - p2[1]))
4146
# print(val)
4247

43-
if (val == 0):
48+
if val == 0:
4449
# Clockwise orientation
45-
if (dist(p1, p3) < dist(p2, p3)):
50+
if dist(p1, p3) < dist(p2, p3):
4651
result = 0
4752
else:
4853
result = 100
4954
else:
5055
# Counterclockwise orientation
51-
result = -1 * val
56+
result = -1 * val
5257

5358
# print(result)
54-
return (result - 0.5)
55-
59+
return result - 0.5
5660

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')
6061

6162
class Plane:
6263
global screen
6364

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):
6566

6667
# Images
6768
self.Image_Path = "Flight-Control-Bot/Plane1.png"
6869
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)
7172

7273
# Speed
7374
self.Speed = 0.4
7475
self.flying = True
7576

7677
# 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)]
7879

7980
# 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)
8182
print(f"{self.Angle}")
8283

83-
self.OffsetAngle = offset_Angle
84-
84+
self.OffsetAngle = offset_angle
8585

8686
self.landing_strip = np.array([(372, 192), (565, 248)])
8787

8888
# 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)]
9090

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 = []
9492

9593
self.Tow = False
9694

@@ -104,10 +102,17 @@ def __init__(self, spawn_at = [750,100], offset_Angle = -2, LandAngle = 0, Path
104102
self.red_dots.append(i)
105103
self.turn(0)
106104

105+
# Turns the plane by a certain bank angle
107106
def turn(self, bank):
108107
self.Angle += bank
109108
self.img_gpy = pygame.transform.rotate(self.img_og, self.OffsetAngle + self.Angle)
110109

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
111116
def fade(self):
112117
self.img_og.fill((255, 255, 255, int(self.alpha)), special_flags=pygame.BLEND_RGBA_MULT)
113118
self.alpha /= 1.2
@@ -122,40 +127,52 @@ def fade(self):
122127
def fly(self):
123128
self.tick += 1
124129

125-
bank = 5
130+
# Tuning Turing Intensity
126131
fine_bank = 0.01
127132

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:
134138
self.fade()
135139

136-
137-
138-
if (len(self.Path) != 0):
140+
if len(self.Path) != 0:
139141
# if (self.Path[0] not in red_dots):
140142
# red_dots.append(self.Path[0])
141143
dest = self.Path[0]
142144
head = self.head_pose()
143145
tail = self.tail_pose()
144146
mid = (head + tail) / 2
145147

146-
turn_direction = orientation(head, tail ,dest) * fine_bank
148+
turn_direction = orientation(head, tail, dest) * fine_bank
147149
# print("Ore", orientation(head, tail, dest))
148150
# print("turnDir", turnDir)
149151
self.turn(turn_direction)
150152

151-
if (dist(mid, self.Path[0]) < 15):
153+
if dist(mid, self.Path[0]) < 15:
152154
reached = self.Path.pop(0)
153155
self.red_dots.pop(0)
154156
# green_dots.append(reached)
155157
# print("Reached", reached)
156158

159+
self.restrict_fly_zone()
160+
157161
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")
159176

160177
def land(self):
161178
if (len(self.LandLoc) == 1):
@@ -164,73 +181,69 @@ def land(self):
164181
def draw(self):
165182
screen.blit(self.img_gpy, self.position)
166183
# Path Visualization
184+
# Comment to disable Path Visualization
167185
self.path_vis()
168186

169187
def Towing(self, mouse_position, click_status):
170188
# 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:
175193
self.Path.clear()
176194
self.red_dots.clear()
177195
self.Tow = True
178196
# print("Towing")
179197

180-
if (self.Tow == True):
181-
if (click_status[0] == False):
198+
if self.Tow:
199+
if not click_status[0]:
182200
self.Tow = False
183201
# print("UnTOWED")
184-
elif (dist(mouse_position, self.landing_strip[0]) < 15):
202+
elif dist(mouse_position, self.landing_strip[0]) < 15:
185203
self.flying = False
186204
for i in self.landing_strip:
187205
self.Path.append(i)
188206
self.red_dots.append(i)
189207
# print("Locked")
190-
self.Tow = False;
208+
self.Tow = False
191209

192210
else:
193-
print(mouse_position)
211+
# print(mouse_position)
194212
self.red_dots.append(mouse_position)
195213
self.Path.append(mouse_position)
196214
# print(self.Path)
197215

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+
198230
def head_pose(self):
199231
# green_dots.append(self.Pos)
200232
theta = 90
201233
angle = self.Angle + theta
202234
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))))
206238

207239
# red_dots.append(head_loc)
208240
return np.array(head_loc)
209241

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-
229242
def tail_pose(self):
230243
theta = 90
231244
angle = self.Angle + theta
232245
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))))
234247

235248
# red_dots.append(tail_loc)
236249
return np.array(tail_loc)
@@ -246,43 +259,46 @@ def path_vis(self):
246259

247260
class Fleet:
248261

249-
planes = []
262+
def __init__(self):
263+
264+
self.planes = []
250265

251266
def Manage(self, mouse_position, mouse_press_status):
252267
# if (time.time() == 1000)
253268
for i in self.planes:
254269
i.draw()
255270
i.fly()
256271
i.Towing(mouse_position, mouse_press_status)
257-
if(i.alpha < 100):
272+
if i.alpha < 100:
258273
self.planes.pop(self.planes.index(i))
259274

260-
def speacialPlane(self, plane):
275+
def specialPlane(self, plane):
261276
self.planes.append(plane)
262277

263278
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:
266281
self.spawn()
267282

268-
269283
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]]}
272288

273289
_, spawn_rect = random.choice(list(spawn_spots.items()))
274290

275291
spawn_location = [random.randint(spawn_rect[0][0], spawn_rect[1][0]),
276292
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))
278294

279295

280296
test_fleet = Fleet()
281297

282298
# Special Plane Instance
283299
# Plane1 h = 37.5685 w = 37.56
284300
# plane1 = Plane( Angle = 90, Pos = (20, 170))
285-
# test_fleet.speacialPlane(plane1)
301+
# test_fleet.specialPlane(plane1)
286302

287303
# Running Indicator
288304
running = True
@@ -294,35 +310,26 @@ def spawn(self):
294310
while running:
295311
# Screen Color
296312
screen.fill((255, 255, 255))
297-
screen.blit(background, (0,0))
298-
313+
screen.blit(background, (0, 0))
299314

300315
# Loop through pygame functions
301316
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
305320
if event.type == pygame.QUIT:
306321
running = False
307322

308323
test_fleet.Spawner()
309324
test_fleet.Manage(pygame.mouse.get_pos(), pygame.mouse.get_pressed())
310325

311-
312-
## DEBUGGING TOOLS
326+
# DEBUGGING TOOLS
313327
for i in green_dots:
314328
screen.blit(green_dot, (i[0] - 4, i[1] - 4))
315329

316-
317-
318-
319330
# if(int(time.time())%10 == 0):
320331
# print(pygame.mouse.get_pressed())
321332
# for i in planes:
322333
# i.Head()
323334

324-
325335
pygame.display.update()
326-
327-
328-

0 commit comments

Comments
 (0)