2022-07-21 16:07:13 -05:00
|
|
|
from turtle import Turtle
|
2022-07-26 14:12:41 -05:00
|
|
|
import json
|
2022-07-28 14:55:15 -05:00
|
|
|
from audio import Audio
|
2022-07-26 14:12:41 -05:00
|
|
|
import time
|
|
|
|
|
2022-07-21 16:07:13 -05:00
|
|
|
|
|
|
|
class Scoreboard(Turtle):
|
|
|
|
|
|
|
|
def __init__(self, x, y):
|
2022-07-28 14:55:15 -05:00
|
|
|
"""
|
|
|
|
Initialize the scoreboard.
|
|
|
|
:param x: X coordinate of the scoreboard.
|
|
|
|
:param y: Y coordinate of the scoreboard.
|
|
|
|
"""
|
2022-07-26 14:12:41 -05:00
|
|
|
super().__init__()
|
|
|
|
self.score = 0
|
|
|
|
|
|
|
|
# Level Data
|
|
|
|
with open('level_data.json', 'r') as file:
|
|
|
|
self.level_data = json.load(file)
|
|
|
|
|
|
|
|
self.levels = list(self.level_data.keys())
|
|
|
|
self.__current_level = 0 # must use dunderscore here because of @property, do not want a setter
|
|
|
|
self.__drones_remaining = self.level_data[self.levels[self.current_level]]['NumDrones']
|
|
|
|
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.__spawn_time = self.level_data[self.levels[self.current_level]]['SpawnTime']
|
|
|
|
|
|
|
|
try:
|
|
|
|
with open('data.txt') as file:
|
|
|
|
self.high_score = int(file.read())
|
|
|
|
except FileNotFoundError:
|
|
|
|
self.high_score = 0
|
|
|
|
self.color('white')
|
|
|
|
self.penup()
|
|
|
|
|
|
|
|
self.hideturtle()
|
|
|
|
self.setposition(x, y)
|
|
|
|
self.SCORE_FONT = ('Courier', 18, 'bold')
|
|
|
|
self.GAME_OVER_FONT = ('Courier', 36, 'bold')
|
|
|
|
self.LEFT_ALIGN = 'left'
|
|
|
|
self.CENTER_ALIGN = 'center'
|
|
|
|
self.draw_scoreboard()
|
|
|
|
|
|
|
|
self.gameover_sound_played = False
|
|
|
|
|
|
|
|
@property
|
|
|
|
def current_level(self):
|
|
|
|
return self.__current_level
|
|
|
|
|
|
|
|
@property
|
|
|
|
def drones_remaining(self):
|
|
|
|
return self.__drones_remaining
|
|
|
|
|
|
|
|
@property
|
|
|
|
def max_drones(self):
|
|
|
|
return self.__max_drones
|
|
|
|
|
|
|
|
@property
|
|
|
|
def drone_speed(self):
|
|
|
|
return self.__drone_speed
|
|
|
|
|
|
|
|
@property
|
|
|
|
def spawn_time(self):
|
|
|
|
return self.__spawn_time
|
2022-07-21 16:07:13 -05:00
|
|
|
|
|
|
|
def draw_scoreboard(self):
|
2022-07-26 14:12:41 -05:00
|
|
|
self.clear()
|
|
|
|
display = (
|
|
|
|
f'Score: {self.score} :: Level: {self.current_level + 1} :: Drones Remaining: {self.drones_remaining}'
|
|
|
|
f' :: High Score: {self.high_score}')
|
|
|
|
self.write(display, align=self.LEFT_ALIGN, font=self.SCORE_FONT)
|
2022-07-21 16:07:13 -05:00
|
|
|
|
|
|
|
def reset(self):
|
2022-07-26 14:12:41 -05:00
|
|
|
if self.score > self.high_score:
|
|
|
|
self.high_score = self.score
|
|
|
|
with open('data.txt', 'w') as file:
|
|
|
|
file.write(str(self.high_score))
|
|
|
|
self.score = 0
|
|
|
|
self.draw_scoreboard()
|
2022-07-21 16:07:13 -05:00
|
|
|
|
|
|
|
def game_over(self):
|
2022-07-28 14:55:15 -05:00
|
|
|
Audio.stop_music()
|
2022-07-26 14:12:41 -05:00
|
|
|
if self.score > self.high_score:
|
|
|
|
with open('data.txt', 'w') as file:
|
|
|
|
file.write(str(self.score))
|
|
|
|
self.score = -1
|
|
|
|
if self.current_level == len(self.levels) and self.drones_remaining == 0:
|
|
|
|
self.game_over_won()
|
|
|
|
else:
|
|
|
|
self.game_over_lost()
|
|
|
|
|
|
|
|
def game_over_lost(self):
|
|
|
|
self.goto(0, 0)
|
|
|
|
self.write('GAME OVER', align=self.CENTER_ALIGN, font=self.GAME_OVER_FONT)
|
|
|
|
self.goto(0, -0.15)
|
|
|
|
self.write('The world has been destroyed', align=self.CENTER_ALIGN, font=self.GAME_OVER_FONT)
|
|
|
|
if not self.gameover_sound_played:
|
|
|
|
self.gameover_sound_played = True
|
2022-07-28 14:55:15 -05:00
|
|
|
Audio.play_game_lost_sound()
|
2022-07-26 14:12:41 -05:00
|
|
|
|
|
|
|
def game_over_won(self):
|
|
|
|
self.goto(0, 0.15)
|
|
|
|
self.write('Victory', align=self.CENTER_ALIGN, font=self.GAME_OVER_FONT)
|
|
|
|
self.goto(0, 0)
|
|
|
|
self.write('You saved the world!', align=self.CENTER_ALIGN, font=self.GAME_OVER_FONT)
|
|
|
|
self.goto(0, -0.15)
|
|
|
|
self.write('You have been elected', align=self.CENTER_ALIGN, font=self.GAME_OVER_FONT)
|
|
|
|
self.goto(0, -0.30)
|
|
|
|
self.write('Grand Poobah!', align=self.CENTER_ALIGN, font=self.GAME_OVER_FONT)
|
|
|
|
if not self.gameover_sound_played:
|
|
|
|
self.gameover_sound_played = True
|
2022-07-28 14:55:15 -05:00
|
|
|
Audio.play_game_won_sound()
|
2022-07-26 14:12:41 -05:00
|
|
|
|
|
|
|
def increment(self, amount, droneLost=True):
|
|
|
|
self.score += amount
|
|
|
|
if droneLost:
|
|
|
|
self.__drones_remaining -= 1
|
|
|
|
self.draw_scoreboard()
|
2022-07-21 16:07:13 -05:00
|
|
|
|
2022-07-26 14:12:41 -05:00
|
|
|
def new_level(self):
|
|
|
|
self.__current_level += 1
|
|
|
|
if self.__current_level < len(self.levels):
|
|
|
|
self.__drones_remaining = self.level_data[self.levels[self.current_level]]['NumDrones']
|
|
|
|
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.__spawn_time = self.level_data[self.levels[self.current_level]]['SpawnTime']
|
2022-07-28 14:55:15 -05:00
|
|
|
Audio.play_change_level_sound()
|
2022-07-26 14:12:41 -05:00
|
|
|
count = 5
|
2022-07-26 14:26:42 -05:00
|
|
|
count_down = Turtle()
|
|
|
|
count_down.hideturtle()
|
|
|
|
count_down.penup()
|
|
|
|
count_down.goto(0, 0)
|
2022-07-28 14:55:15 -05:00
|
|
|
Audio.pause_music()
|
2022-07-26 14:12:41 -05:00
|
|
|
while count >= 0:
|
2022-07-26 14:26:42 -05:00
|
|
|
count_down.write(f'{count}', align=self.CENTER_ALIGN, font=self.GAME_OVER_FONT)
|
2022-07-26 14:12:41 -05:00
|
|
|
time.sleep(1.0)
|
2022-07-26 14:26:42 -05:00
|
|
|
count_down.clear()
|
2022-07-26 14:12:41 -05:00
|
|
|
count -= 1
|
2022-07-28 14:55:15 -05:00
|
|
|
Audio.unpause_music()
|
2022-07-26 14:12:41 -05:00
|
|
|
self.draw_scoreboard()
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|