Drone Invaders now crashes when shooting a bomb

This commit is contained in:
ben 2022-07-28 16:00:29 -05:00
parent 0280400537
commit d5f2439dfe
5 changed files with 81 additions and 20 deletions

View file

@ -1,7 +1,7 @@
from boundedturtle import BoundedTurtle from boundedturtle import BoundedTurtle
import math import math
from drone import Drone from drone import Drone
from pygame import mixer from audio import Audio
class Bomb(BoundedTurtle): class Bomb(BoundedTurtle):
@ -16,10 +16,27 @@ class Bomb(BoundedTurtle):
:param y_max: Maximum y coordinate of the screen. :param y_max: Maximum y coordinate of the screen.
:param scoreboard: Scoreboard object. :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): 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): def distance(self, other):
p1 = self.position() p1 = self.position()
@ -27,4 +44,5 @@ class Bomb(BoundedTurtle):
return math.dist(p1, p2) return math.dist(p1, p2)
def remove(self): def remove(self):
pass self.clear()
self.hideturtle()

View file

@ -6,24 +6,63 @@ class Drone(BoundedTurtle):
droneList = [] # static variable droneList = [] # static variable
def __init__(self, speed, x_min, x_max, y_min, y_max): 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 self.penup()
def get_drones(): self.getscreen().tracer(False)
return [x for x in Drone.droneList if x.__alive] 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 @staticmethod
def destory_all(): def destory_all():
"""
Destroy all drones.
:return: None
"""
for drone in Drone.droneList: for drone in Drone.droneList:
drone.remove() drone.remove()
Drone.droneList = [] Drone.droneList = []
def move(self): @staticmethod
pass def get_drones():
"""
@property :return: List of alive drones.
def is_alive(self): """
pass return [x for x in Drone.droneList if x.__alive]
def remove(self):
pass

View file

@ -66,17 +66,19 @@ class DroneInvaders:
if self.game_state == self.game_states['Intro']: if self.game_state == self.game_states['Intro']:
self.game_state = self.game_states['Playing'] self.game_state = self.game_states['Playing']
self.screen.ontimer(self.add_drone, self.scoreboard.spawn_time) self.screen.ontimer(self.add_drone, self.scoreboard.spawn_time)
elif self.game_state == self.game_states['Playing']: elif self.game_state == self.game_states['Playing']:
# Update drones # Update drones
if self.scoreboard.drones_remaining <= 0: if self.scoreboard.drones_remaining <= 0:
self.game_state = self.game_states['InterLevel'] self.game_state = self.game_states['InterLevel']
else: else:
for drone in Drone.get_drones(): for drone in Drone.get_drones():
drone.update() out_of_bounds = drone.move()
if drone.out_of_bounds(): if out_of_bounds:
Drone.destory_all() Drone.destory_all()
self.game_state = self.game_states['GameOver'] self.game_state = self.game_states['GameOver']
break break
elif self.game_state == self.game_states['InterLevel']: elif self.game_state == self.game_states['InterLevel']:
self.screen.ontimer(self.add_drone, 0) self.screen.ontimer(self.add_drone, 0)
self.game_state = self.game_states['Playing'] self.game_state = self.game_states['Playing']
@ -89,11 +91,13 @@ class DroneInvaders:
self.scoreboard.draw_scoreboard() self.scoreboard.draw_scoreboard()
else: else:
self.game_state = self.game_states['GameOver'] self.game_state = self.game_states['GameOver']
elif self.game_state == self.game_states['GameOver']: elif self.game_state == self.game_states['GameOver']:
self.screen.ontimer(self.add_drone, 0) self.screen.ontimer(self.add_drone, 0)
Drone.destory_all() Drone.destory_all()
self.rocket.hideturtle() self.rocket.hideturtle()
self.scoreboard.game_over() self.scoreboard.game_over()
elif self.game_state == self.game_states['Pause']: elif self.game_state == self.game_states['Pause']:
pass pass

View file

@ -48,4 +48,5 @@ class Rocket(Turtle):
:return: None :return: None
""" """
Bomb(self.heading(), 0.1, self.x_min, self.x_max, self.y_min, self.y_max, self.scoreboard) 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() Audio.play_laser_sound()

View file

@ -5,7 +5,6 @@ import time
class Scoreboard(Turtle): class Scoreboard(Turtle):
def __init__(self, x, y): def __init__(self, x, y):
""" """
Initialize the scoreboard. Initialize the scoreboard.