From b647ba342364b6fa94691f85987f3099862e0858 Mon Sep 17 00:00:00 2001 From: korzepadawid Date: Fri, 11 Mar 2022 20:00:42 +0100 Subject: [PATCH] refactor: separated dir for screens --- logic/game.py | 39 +++++---------------------------------- ui/screens/__init__.py | 0 ui/screens/credits.py | 6 ++++++ ui/screens/options.py | 6 ++++++ ui/screens/screen.py | 28 ++++++++++++++++++++++++++++ 5 files changed, 45 insertions(+), 34 deletions(-) create mode 100644 ui/screens/__init__.py create mode 100644 ui/screens/credits.py create mode 100644 ui/screens/options.py create mode 100644 ui/screens/screen.py diff --git a/logic/game.py b/logic/game.py index f1cb9a3..7f7b129 100644 --- a/logic/game.py +++ b/logic/game.py @@ -7,7 +7,9 @@ from common.constants import GAME_TITLE, WINDOW_WIDTH, WINDOW_HEIGHT, FPS_COUNT, from common.helpers import draw_text from models.knight import Knight from models.monster import Monster +from ui.screens.credits import Credits from ui.logs import Logs +from ui.screens.options import Options from ui.stats import Stats from .grid import Grid from .spawner import Spawner @@ -28,6 +30,7 @@ class Game: self.tiles.append((tile_path, converted_tile)) self.bg = pygame.image.load("./resources/textures/bg.jpg") + self.screens = {'credits': Credits(self.screen, self.clock), 'options': Options(self.screen, self.clock)} click = False @@ -48,10 +51,10 @@ class Game: self.game() if button_2.collidepoint((mx, my)): if click: - self.options() + self.screens['options'].display_screen() if button_3.collidepoint((mx, my)): if click: - self.credits() + self.screens['credits'].display_screen() pygame.draw.rect(self.screen, (0, 191, 255), button_1, 0, 4) draw_text('PLAY', WHITE, self.screen, 870, 255) @@ -77,38 +80,6 @@ class Game: pygame.display.update() self.clock.tick(60) - def options(self): - running = True - while running: - self.screen.fill((0, 0, 0)) - - draw_text('options', WHITE, self.screen, 20, 20, 30) - for event in pygame.event.get(): - if event.type == pygame.QUIT: - running = False - if event.type == pygame.KEYDOWN: - if event.key == pygame.K_ESCAPE: - running = False - - pygame.display.update() - self.clock.tick(60) - - def credits(self): - running = True - while running: - self.screen.fill((0, 0, 0)) - - draw_text('credits', WHITE, self.screen, 20, 20, 30) - for event in pygame.event.get(): - if event.type == pygame.QUIT: - running = False - if event.type == pygame.KEYDOWN: - if event.key == pygame.K_ESCAPE: - running = False - - pygame.display.update() - self.clock.tick(60) - def game(self): running = True grid = Grid(self.tiles) diff --git a/ui/screens/__init__.py b/ui/screens/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/ui/screens/credits.py b/ui/screens/credits.py new file mode 100644 index 0000000..158cafd --- /dev/null +++ b/ui/screens/credits.py @@ -0,0 +1,6 @@ +from ui.screens.screen import Screen + + +class Credits(Screen): + def __init__(self, screen, clock): + super().__init__('credits', screen, clock) diff --git a/ui/screens/options.py b/ui/screens/options.py new file mode 100644 index 0000000..ebfa9a9 --- /dev/null +++ b/ui/screens/options.py @@ -0,0 +1,6 @@ +from ui.screens.screen import Screen + + +class Options(Screen): + def __init__(self, screen, clock): + super().__init__('options', screen, clock) diff --git a/ui/screens/screen.py b/ui/screens/screen.py new file mode 100644 index 0000000..d6637cc --- /dev/null +++ b/ui/screens/screen.py @@ -0,0 +1,28 @@ +import pygame + +from common.colors import WHITE +from common.helpers import draw_text + + +class Screen: + def __init__(self, screen_name, screen, clock): + self.screen_name = screen_name + self.screen = screen + self.clock = clock + + def display_screen(self): + """override this method in order to get specific layout""" + running = True + while running: + self.screen.fill((0, 0, 0)) + + draw_text(self.screen_name, WHITE, self.screen, 20, 20, 30) + for event in pygame.event.get(): + if event.type == pygame.QUIT: + running = False + if event.type == pygame.KEYDOWN: + if event.key == pygame.K_ESCAPE: + running = False + + pygame.display.update() + self.clock.tick(60)