2022-07-21 16:07:13 -05:00
|
|
|
from boundedturtle import BoundedTurtle
|
|
|
|
import random
|
|
|
|
|
2022-07-21 16:36:45 -05:00
|
|
|
|
2022-07-21 16:07:13 -05:00
|
|
|
class Drone(BoundedTurtle):
|
2022-07-21 16:36:45 -05:00
|
|
|
droneList = [] # static variable
|
2022-07-21 16:07:13 -05:00
|
|
|
|
2022-07-21 16:36:45 -05:00
|
|
|
def __init__(self, speed, x_min, x_max, y_min, y_max):
|
2022-07-28 16:00:29 -05:00
|
|
|
"""
|
|
|
|
Initialize the drone.
|
|
|
|
:param speed: Speed of drone.
|
|
|
|
:param x_min: Minimum x coordinate of the screen.
|
|
|
|
:param x_max: Maximum x coordinate of the screen.
|
|
|
|
:param y_min: Minimum y coordinate of the screen.
|
|
|
|
:param y_max: Maximum y coordinate of the screen.
|
|
|
|
"""
|
|
|
|
super().__init__(speed, x_min, x_max, y_min, y_max)
|
2022-07-21 16:07:13 -05:00
|
|
|
|
2022-07-28 16:00:29 -05:00
|
|
|
self.penup()
|
|
|
|
self.getscreen().tracer(False)
|
|
|
|
if 'images/Drone64.gif' not in self.getscreen().getshapes():
|
|
|
|
self.getscreen().addshape('images/Drone64.gif')
|
|
|
|
|
|
|
|
self.shape('images/Drone64.gif')
|
|
|
|
self.resizemode('user')
|
|
|
|
self.turtlesize(10, 10, 1)
|
2022-07-28 16:18:07 -05:00
|
|
|
x = random.uniform(x_min * 0.9, x_max * 0.9)
|
2022-07-28 16:00:29 -05:00
|
|
|
self.goto(x, y_max * 1.1)
|
|
|
|
self.setheading(270)
|
|
|
|
self.getscreen().tracer(True)
|
|
|
|
|
|
|
|
Drone.droneList = Drone.get_drones()
|
|
|
|
Drone.droneList.append(self)
|
|
|
|
self.__alive = True
|
|
|
|
|
|
|
|
def move(self):
|
|
|
|
"""
|
|
|
|
Moves the drone.
|
|
|
|
"""
|
|
|
|
self.forward(self.get_speed())
|
|
|
|
if self.below_bottom_bound():
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def remove(self):
|
|
|
|
self.__alive = False
|
|
|
|
self.hideturtle()
|
|
|
|
|
|
|
|
@property
|
|
|
|
def alive(self):
|
|
|
|
return self.__alive
|
2022-07-21 16:07:13 -05:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def destory_all():
|
2022-07-28 16:00:29 -05:00
|
|
|
"""
|
|
|
|
Destroy all drones.
|
|
|
|
:return: None
|
|
|
|
"""
|
2022-07-21 16:07:13 -05:00
|
|
|
for drone in Drone.droneList:
|
|
|
|
drone.remove()
|
|
|
|
Drone.droneList = []
|
|
|
|
|
2022-07-28 16:00:29 -05:00
|
|
|
@staticmethod
|
|
|
|
def get_drones():
|
|
|
|
"""
|
|
|
|
:return: List of alive drones.
|
|
|
|
"""
|
|
|
|
return [x for x in Drone.droneList if x.__alive]
|