Add game over screen.
This commit is contained in:
parent
d5637d989e
commit
c4bbe09048
22
snake/game_over_screen.py
Normal file
22
snake/game_over_screen.py
Normal file
@ -0,0 +1,22 @@
|
||||
import pygame
|
||||
from game_object import GameObject
|
||||
from snake import Snake
|
||||
|
||||
|
||||
class GameOverScreen(GameObject):
|
||||
def __init__(self, window_size, color):
|
||||
font = pygame.font.SysFont('Arial', 140)
|
||||
self.__text_surface = font.render('GAME OVER', True, color)
|
||||
self.__pos = [0.5*(win_size - font_size)
|
||||
for win_size, font_size in zip(window_size,
|
||||
self.__text_surface.get_size())]
|
||||
self.__is_visible = False
|
||||
|
||||
def draw(self, surface):
|
||||
if self.__is_visible:
|
||||
surface.blit(self.__text_surface, self.__pos)
|
||||
|
||||
def update(self, user_input, game_objs):
|
||||
for obj in game_objs:
|
||||
if isinstance(obj, Snake) and obj.is_dead():
|
||||
self.__is_visible = True
|
||||
@ -5,6 +5,7 @@ from snake import Snake
|
||||
from game_object import GameObject
|
||||
from wall import Wall
|
||||
from prey import Prey
|
||||
from game_over_screen import GameOverScreen
|
||||
|
||||
|
||||
def draw_all(window: Window, game_objs: list[GameObject]) -> None:
|
||||
@ -29,7 +30,9 @@ def create_game_objects(window: Window):
|
||||
color=(255, 0, 0)),
|
||||
Prey(size=10,
|
||||
color=(255, 255, 0),
|
||||
window_size=window.get_size())]
|
||||
window_size=window.get_size()),
|
||||
GameOverScreen(window_size=window.get_size(),
|
||||
color=(255, 0, 0))]
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
@ -59,4 +59,8 @@ class Snake(GameObject):
|
||||
|
||||
|
||||
def get_bounding_boxes(self) -> list[pygame.rect.Rect]:
|
||||
return [seg.get_bounding_boxes()[0] for seg in self.__segments]
|
||||
return [seg.get_bounding_boxes()[0] for seg in self.__segments]
|
||||
|
||||
|
||||
def is_dead(self):
|
||||
return self.__is_dead
|
||||
Loading…
x
Reference in New Issue
Block a user