droneinvaders.py finished

This commit is contained in:
ben 2022-07-26 16:27:55 -05:00
parent fbc6e4cccb
commit e18a419f62

View file

@ -9,6 +9,7 @@ from sound_and_music import SoundAndMusic
class DroneInvaders: class DroneInvaders:
def __init__(self, x_min, x_max, y_min, y_max): def __init__(self, x_min, x_max, y_min, y_max):
self.done = None
self.x_min = x_min self.x_min = x_min
self.x_max = x_max self.x_max = x_max
self.y_min = y_min self.y_min = y_min
@ -19,20 +20,81 @@ class DroneInvaders:
self.screen.bgcolor('light green') self.screen.bgcolor('light green')
self.screen.setworldcoordinates(self.x_min, self.y_min, self.x_max, self.y_max) self.screen.setworldcoordinates(self.x_min, self.y_min, self.x_max, self.y_max)
self.scoreboard = Scoreboard(x_min + 0.02, y_max - 0.2) self.scoreboard = Scoreboard(x_min + 0.02, y_max - 0.2)
self.lasercannon = Rocket(self.x_min, self.x_max, self.y_min, self.y_max, self.screen, self.scoreboard) self.rocket = Rocket(self.x_min, self.x_max, self.y_min, self.y_max, self.screen, self.scoreboard)
self.delay = 0.1 self.delay = 0.1
self.game_states = {'Intro': 1, 'Playing': 2, 'InterLevel': 3, 'GameOver': 4, 'Pause': 5} self.game_states = {'Intro': 1, 'Playing': 2, 'InterLevel': 3, 'GameOver': 4, 'Pause': 5}
self.game_state = self.game_states['Intro'] self.game_state = self.game_states['Intro']
self.pause_turtle = turtle.Turtle()
self.pause_turtle.hideturtle()
self.pause_turtle.penup()
self.PAUSE_FONT = ('Arial', 24, 'bold')
def play(self): def play(self):
self.scoreboard.draw_scoreboard()
self.screen.ontimer(self.add_drone, self.scoreboard.spawn_time)
# Register listeners
self.screen.onclick(self.rocket.aim)
self.screen.onkey(self.rocket.shoot, 'space')
self.screen.onkey(self.rocket.shoot, 's')
self.screen.onkey(self.pause, 'p')
self.screen.onkey(self.quit, 'Escape')
self.screen.onkey(self.quit, 'q')
self.screen.onkey(self.music_up, 'Up')
self.screen.onkey(self.music_down, 'Down')
self.screen.onkey(self.sfx_up, '+') # Only works with plus on num pad
self.screen.onkey(self.sfx_down, '=') # For plus beside backspace
self.screen.onkey(self.sfx_down, '-') # Works with either minus
self.screen.listen()
self.done = False
while not self.done:
self.screen.update()
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():
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']
Drone.destory_all()
self.rocket.hideturtle()
if self.scoreboard.new_level():
self.rocket.showturtle()
self.game_state = self.game_states['Playing']
self.screen.ontimer(self.add_drone, self.scoreboard.spawn_time)
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
time.sleep(self.delay)
self.screen.exitonclick() self.screen.exitonclick()
def add_drone(self): def add_drone(self):
if self.game_state != self.game_states['Playing']: if self.game_state != self.game_states['Playing']:
return return
if self.scoreboard.drones_remaining > len(Drone.getDrones()) and len( if self.scoreboard.drones_remaining > len(Drone.get_drones()) and len(
Drone.getDrones()) < self.scoreboard.max_drones: Drone.get_drones()) < self.scoreboard.max_drones:
Drone(self.scoreboard.drone_speed, self.x_min, self.x_max, self.y_min, self.y_max) Drone(self.scoreboard.drone_speed, self.x_min, self.x_max, self.y_min, self.y_max)
if not self.done: if not self.done:
self.screen.ontimer(self.add_drone, self.scoreboard.spawn_time) self.screen.ontimer(self.add_drone, self.scoreboard.spawn_time)
@ -44,18 +106,17 @@ class DroneInvaders:
SoundAndMusic.pause_music() SoundAndMusic.pause_music()
# Write to screen # Write to screen
# self.pause_screen.bgcolor('light blue') # self.pause_screen.bgcolor('light blue')
self.lasercannon.hideturtle() self.rocket.hideturtle()
for drone in Drone.getDrones(): for drone in Drone.get_drones():
drone.hideturtle() drone.hideturtle()
self.draw_pause_message() self.draw_pause_message()
else: else:
self.screen.ontimer(self.add_drone, self.scoreboard.spawn_time) self.screen.ontimer(self.add_drone, self.scoreboard.spawn_time)
self.game_state = self.game_states['Playing'] self.game_state = self.game_states['Playing']
SoundAndMusic.unpause_music() SoundAndMusic.unpause_music()
self.lasercannon.showturtle() self.rocket.showturtle()
for drone in Drone.getDrones(): for drone in Drone.get_drones():
drone.showturtle() drone.showturtle()
self.pause_turtle.clear() self.pause_turtle.clear()
@ -96,4 +157,4 @@ class DroneInvaders:
def quit(self): def quit(self):
self.screen.bye() self.screen.bye()
sys.exit() exit(0)