Cleaned up code, added doc files

This commit is contained in:
ben 2022-07-28 14:55:15 -05:00
parent 81eb059d5d
commit 936edddd2e
5 changed files with 85 additions and 22 deletions

View file

@ -6,6 +6,16 @@ from pygame import mixer
class Bomb(BoundedTurtle): class Bomb(BoundedTurtle):
def __init__(self, init_heading, speed, x_min, x_max, y_min, y_max, scoreboard): def __init__(self, init_heading, speed, x_min, x_max, y_min, y_max, scoreboard):
"""
Initialize the bomb.
:param init_heading: Heading bomb should travel towards.
:param speed: Speed of bomb.
: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.
:param scoreboard: Scoreboard object.
"""
pass pass
def move(self): def move(self):

View file

@ -1,8 +1,17 @@
from abc import abstractmethod from abc import abstractmethod
from turtle import Turtle from turtle import Turtle
class BoundedTurtle(Turtle): class BoundedTurtle(Turtle):
def __init__(self, speed, x_min, x_max, y_min, y_max): def __init__(self, speed, x_min, x_max, y_min, y_max):
"""
Initialize the bounded turtle.
:param speed: Speed of the turtle.
: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__() super().__init__()
self.__x_min = x_min self.__x_min = x_min
self.__x_max = x_max self.__x_max = x_max

View file

@ -4,11 +4,18 @@ from drone import Drone
from rocket import Rocket from rocket import Rocket
from scoreboard import Scoreboard from scoreboard import Scoreboard
import time import time
from sound_and_music import SoundAndMusic from audio import Audio
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):
"""
Initialize the game.
: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.
"""
self.done = None self.done = None
self.x_min = x_min self.x_min = x_min
self.x_max = x_max self.x_max = x_max
@ -31,6 +38,10 @@ class DroneInvaders:
self.PAUSE_FONT = ('Arial', 24, 'bold') self.PAUSE_FONT = ('Arial', 24, 'bold')
def play(self): def play(self):
"""
Play the game.
:return: None
"""
self.scoreboard.draw_scoreboard() self.scoreboard.draw_scoreboard()
self.screen.ontimer(self.add_drone, self.scoreboard.spawn_time) self.screen.ontimer(self.add_drone, self.scoreboard.spawn_time)
@ -100,10 +111,14 @@ class DroneInvaders:
self.screen.ontimer(self.add_drone, self.scoreboard.spawn_time) self.screen.ontimer(self.add_drone, self.scoreboard.spawn_time)
def pause(self): def pause(self):
"""
Does things when the game is paused.
:return: None
"""
if self.game_state == self.game_states['Playing']: if self.game_state == self.game_states['Playing']:
self.screen.ontimer(self.add_drone, 0) self.screen.ontimer(self.add_drone, 0)
self.game_state = self.game_states['Pause'] self.game_state = self.game_states['Pause']
SoundAndMusic.pause_music() Audio.pause_music()
# Write to screen # Write to screen
# self.pause_screen.bgcolor('light blue') # self.pause_screen.bgcolor('light blue')
self.rocket.hideturtle() self.rocket.hideturtle()
@ -114,22 +129,26 @@ class DroneInvaders:
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() Audio.unpause_music()
self.rocket.showturtle() self.rocket.showturtle()
for drone in Drone.get_drones(): for drone in Drone.get_drones():
drone.showturtle() drone.showturtle()
self.pause_turtle.clear() self.pause_turtle.clear()
def draw_pause_message(self): def draw_pause_message(self):
"""
Draws the pause message to the screen.
:return: None
"""
self.pause_turtle.clear() self.pause_turtle.clear()
up = '' up = ''
down = '' down = ''
self.pause_turtle.color('black') self.pause_turtle.color('black')
self.pause_turtle.goto(-0.5, 0.3) self.pause_turtle.goto(-0.5, 0.3)
self.pause_turtle.write(f'Music volume: {SoundAndMusic.music_volume} of 100', align='left', self.pause_turtle.write(f'Music volume: {Audio.music_volume} of 100', align='left',
font=self.PAUSE_FONT) font=self.PAUSE_FONT)
self.pause_turtle.goto(-0.5, 0.2) self.pause_turtle.goto(-0.5, 0.2)
self.pause_turtle.write(f'Sfx volume: {SoundAndMusic.sfx_volume} of 100', align='left', font=self.PAUSE_FONT) self.pause_turtle.write(f'Sfx volume: {Audio.sfx_volume} of 100', align='left', font=self.PAUSE_FONT)
self.pause_turtle.goto(-0.5, 0) self.pause_turtle.goto(-0.5, 0)
self.pause_turtle.write(f'Music up {up}', align='left', font=self.PAUSE_FONT) self.pause_turtle.write(f'Music up {up}', align='left', font=self.PAUSE_FONT)
self.pause_turtle.goto(-0.5, -0.1) self.pause_turtle.goto(-0.5, -0.1)
@ -140,21 +159,41 @@ class DroneInvaders:
self.pause_turtle.write(f'Sfx down -', align='left', font=self.PAUSE_FONT) self.pause_turtle.write(f'Sfx down -', align='left', font=self.PAUSE_FONT)
def music_up(self): def music_up(self):
SoundAndMusic.change_music_volume(1) """
Increases the music volume.
:return: None
"""
Audio.change_music_volume(1)
self.draw_pause_message() self.draw_pause_message()
def music_down(self): def music_down(self):
SoundAndMusic.change_music_volume(-1) """
Decreases the music volume.
:return: None
"""
Audio.change_music_volume(-1)
self.draw_pause_message() self.draw_pause_message()
def sfx_up(self): def sfx_up(self):
SoundAndMusic.change_sfx_volume(1) """
Increases the sfx volume.
:return: None
"""
Audio.change_sfx_volume(1)
self.draw_pause_message() self.draw_pause_message()
def sfx_down(self): def sfx_down(self):
SoundAndMusic.change_music_volume(-1) """
Decreases the sfx volume.
:return: None
"""
Audio.change_music_volume(-1)
self.draw_pause_message() self.draw_pause_message()
def quit(self): def quit(self):
"""
Quits the game.
:return: None
"""
self.screen.bye() self.screen.bye()
exit(0) exit(0)

View file

@ -1,10 +1,10 @@
from droneinvaders import DroneInvaders from droneinvaders import DroneInvaders
from sound_and_music import SoundAndMusic from audio import Audio
SoundAndMusic.start_pygame() Audio.start_pygame()
SoundAndMusic.load_music() Audio.load_music()
SoundAndMusic.play_music() Audio.play_music()
SoundAndMusic.load_sounds() Audio.load_sounds()
if __name__ == '__main__': if __name__ == '__main__':
di = DroneInvaders(-1, 1, -1, 1) di = DroneInvaders(-1, 1, -1, 1)

View file

@ -1,12 +1,17 @@
from turtle import Turtle from turtle import Turtle
import json import json
from sound_and_music import SoundAndMusic from audio import Audio
import time import time
class Scoreboard(Turtle): class Scoreboard(Turtle):
def __init__(self, x, y): def __init__(self, x, y):
"""
Initialize the scoreboard.
:param x: X coordinate of the scoreboard.
:param y: Y coordinate of the scoreboard.
"""
super().__init__() super().__init__()
self.score = 0 self.score = 0
@ -75,7 +80,7 @@ class Scoreboard(Turtle):
self.draw_scoreboard() self.draw_scoreboard()
def game_over(self): def game_over(self):
SoundAndMusic.stop_music() Audio.stop_music()
if self.score > self.high_score: if self.score > self.high_score:
with open('data.txt', 'w') as file: with open('data.txt', 'w') as file:
file.write(str(self.score)) file.write(str(self.score))
@ -92,7 +97,7 @@ class Scoreboard(Turtle):
self.write('The world has been destroyed', align=self.CENTER_ALIGN, font=self.GAME_OVER_FONT) self.write('The world has been destroyed', align=self.CENTER_ALIGN, font=self.GAME_OVER_FONT)
if not self.gameover_sound_played: if not self.gameover_sound_played:
self.gameover_sound_played = True self.gameover_sound_played = True
SoundAndMusic.play_game_lost_sound() Audio.play_game_lost_sound()
def game_over_won(self): def game_over_won(self):
self.goto(0, 0.15) self.goto(0, 0.15)
@ -105,7 +110,7 @@ class Scoreboard(Turtle):
self.write('Grand Poobah!', align=self.CENTER_ALIGN, font=self.GAME_OVER_FONT) self.write('Grand Poobah!', align=self.CENTER_ALIGN, font=self.GAME_OVER_FONT)
if not self.gameover_sound_played: if not self.gameover_sound_played:
self.gameover_sound_played = True self.gameover_sound_played = True
SoundAndMusic.play_game_won_sound() Audio.play_game_won_sound()
def increment(self, amount, droneLost=True): def increment(self, amount, droneLost=True):
self.score += amount self.score += amount
@ -120,19 +125,19 @@ class Scoreboard(Turtle):
self.__max_drones = self.level_data[self.levels[self.current_level]]['MaxDrones'] self.__max_drones = self.level_data[self.levels[self.current_level]]['MaxDrones']
self.__drone_speed = self.level_data[self.levels[self.current_level]]['DroneSpeed'] self.__drone_speed = self.level_data[self.levels[self.current_level]]['DroneSpeed']
self.__spawn_time = self.level_data[self.levels[self.current_level]]['SpawnTime'] self.__spawn_time = self.level_data[self.levels[self.current_level]]['SpawnTime']
SoundAndMusic.play_change_level_sound() Audio.play_change_level_sound()
count = 5 count = 5
count_down = Turtle() count_down = Turtle()
count_down.hideturtle() count_down.hideturtle()
count_down.penup() count_down.penup()
count_down.goto(0, 0) count_down.goto(0, 0)
SoundAndMusic.pause_music() Audio.pause_music()
while count >= 0: while count >= 0:
count_down.write(f'{count}', align=self.CENTER_ALIGN, font=self.GAME_OVER_FONT) count_down.write(f'{count}', align=self.CENTER_ALIGN, font=self.GAME_OVER_FONT)
time.sleep(1.0) time.sleep(1.0)
count_down.clear() count_down.clear()
count -= 1 count -= 1
SoundAndMusic.unpause_music() Audio.unpause_music()
self.draw_scoreboard() self.draw_scoreboard()
return True return True
else: else: