Drone Invaders now crashes when shooting a bomb
This commit is contained in:
parent
0280400537
commit
d5f2439dfe
5 changed files with 81 additions and 20 deletions
|
@ -1,7 +1,7 @@
|
|||
from boundedturtle import BoundedTurtle
|
||||
import math
|
||||
from drone import Drone
|
||||
from pygame import mixer
|
||||
from audio import Audio
|
||||
|
||||
|
||||
class Bomb(BoundedTurtle):
|
||||
|
@ -16,10 +16,27 @@ class Bomb(BoundedTurtle):
|
|||
:param y_max: Maximum y coordinate of the screen.
|
||||
:param scoreboard: Scoreboard object.
|
||||
"""
|
||||
pass
|
||||
super().__init__(speed, x_min, x_max, y_min, y_max)
|
||||
|
||||
self.resizemode('user')
|
||||
self.color('red', 'red')
|
||||
self.shape('circle')
|
||||
self.turtlesize(0.25)
|
||||
self.setheading(init_heading)
|
||||
self.getscreen().tracer(False)
|
||||
self.getscreen().ontimer(self.move, 100)
|
||||
self.scoreboard = scoreboard
|
||||
|
||||
def move(self):
|
||||
pass
|
||||
self.forward(self.get_speed())
|
||||
for drone in Drone.get_drones():
|
||||
if self.distance(drone) < self.get_speed() and self.isvisible():
|
||||
drone.remove()
|
||||
self.remove()
|
||||
Audio.play_explosion_sound()
|
||||
self.scoreboard.increment(4)
|
||||
else:
|
||||
self.getscreen().ontimer(self.move, 100)
|
||||
|
||||
def distance(self, other):
|
||||
p1 = self.position()
|
||||
|
@ -27,4 +44,5 @@ class Bomb(BoundedTurtle):
|
|||
return math.dist(p1, p2)
|
||||
|
||||
def remove(self):
|
||||
pass
|
||||
self.clear()
|
||||
self.hideturtle()
|
||||
|
|
|
@ -6,24 +6,63 @@ class Drone(BoundedTurtle):
|
|||
droneList = [] # static variable
|
||||
|
||||
def __init__(self, speed, x_min, x_max, y_min, y_max):
|
||||
pass
|
||||
"""
|
||||
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)
|
||||
|
||||
@staticmethod
|
||||
def get_drones():
|
||||
return [x for x in Drone.droneList if x.__alive]
|
||||
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)
|
||||
x = random.uniform(x_min * 0.1, x_max * 0.1)
|
||||
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
|
||||
|
||||
@staticmethod
|
||||
def destory_all():
|
||||
"""
|
||||
Destroy all drones.
|
||||
:return: None
|
||||
"""
|
||||
for drone in Drone.droneList:
|
||||
drone.remove()
|
||||
Drone.droneList = []
|
||||
|
||||
def move(self):
|
||||
pass
|
||||
|
||||
@property
|
||||
def is_alive(self):
|
||||
pass
|
||||
|
||||
def remove(self):
|
||||
pass
|
||||
@staticmethod
|
||||
def get_drones():
|
||||
"""
|
||||
:return: List of alive drones.
|
||||
"""
|
||||
return [x for x in Drone.droneList if x.__alive]
|
||||
|
|
|
@ -66,17 +66,19 @@ class DroneInvaders:
|
|||
if self.game_state == self.game_states['Intro']:
|
||||
self.game_state = self.game_states['Playing']
|
||||
self.screen.ontimer(self.add_drone, self.scoreboard.spawn_time)
|
||||
|
||||
elif self.game_state == self.game_states['Playing']:
|
||||
# Update drones
|
||||
if self.scoreboard.drones_remaining <= 0:
|
||||
self.game_state = self.game_states['InterLevel']
|
||||
else:
|
||||
for drone in Drone.get_drones():
|
||||
drone.update()
|
||||
if drone.out_of_bounds():
|
||||
out_of_bounds = drone.move()
|
||||
if out_of_bounds:
|
||||
Drone.destory_all()
|
||||
self.game_state = self.game_states['GameOver']
|
||||
break
|
||||
|
||||
elif self.game_state == self.game_states['InterLevel']:
|
||||
self.screen.ontimer(self.add_drone, 0)
|
||||
self.game_state = self.game_states['Playing']
|
||||
|
@ -89,11 +91,13 @@ class DroneInvaders:
|
|||
self.scoreboard.draw_scoreboard()
|
||||
else:
|
||||
self.game_state = self.game_states['GameOver']
|
||||
|
||||
elif self.game_state == self.game_states['GameOver']:
|
||||
self.screen.ontimer(self.add_drone, 0)
|
||||
Drone.destory_all()
|
||||
self.rocket.hideturtle()
|
||||
self.scoreboard.game_over()
|
||||
|
||||
elif self.game_state == self.game_states['Pause']:
|
||||
pass
|
||||
|
||||
|
|
|
@ -48,4 +48,5 @@ class Rocket(Turtle):
|
|||
:return: None
|
||||
"""
|
||||
Bomb(self.heading(), 0.1, self.x_min, self.x_max, self.y_min, self.y_max, self.scoreboard)
|
||||
self.scoreboard.increment(-1, droneLost=False)
|
||||
Audio.play_laser_sound()
|
||||
|
|
|
@ -5,7 +5,6 @@ import time
|
|||
|
||||
|
||||
class Scoreboard(Turtle):
|
||||
|
||||
def __init__(self, x, y):
|
||||
"""
|
||||
Initialize the scoreboard.
|
||||
|
|
Loading…
Reference in a new issue