refactor: separated dir for screens

This commit is contained in:
korzepadawid 2022-03-11 20:00:42 +01:00
parent 190b2bec01
commit b647ba3423
5 changed files with 45 additions and 34 deletions

View File

@ -7,7 +7,9 @@ from common.constants import GAME_TITLE, WINDOW_WIDTH, WINDOW_HEIGHT, FPS_COUNT,
from common.helpers import draw_text from common.helpers import draw_text
from models.knight import Knight from models.knight import Knight
from models.monster import Monster from models.monster import Monster
from ui.screens.credits import Credits
from ui.logs import Logs from ui.logs import Logs
from ui.screens.options import Options
from ui.stats import Stats from ui.stats import Stats
from .grid import Grid from .grid import Grid
from .spawner import Spawner from .spawner import Spawner
@ -28,6 +30,7 @@ class Game:
self.tiles.append((tile_path, converted_tile)) self.tiles.append((tile_path, converted_tile))
self.bg = pygame.image.load("./resources/textures/bg.jpg") 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 click = False
@ -48,10 +51,10 @@ class Game:
self.game() self.game()
if button_2.collidepoint((mx, my)): if button_2.collidepoint((mx, my)):
if click: if click:
self.options() self.screens['options'].display_screen()
if button_3.collidepoint((mx, my)): if button_3.collidepoint((mx, my)):
if click: if click:
self.credits() self.screens['credits'].display_screen()
pygame.draw.rect(self.screen, (0, 191, 255), button_1, 0, 4) pygame.draw.rect(self.screen, (0, 191, 255), button_1, 0, 4)
draw_text('PLAY', WHITE, self.screen, 870, 255) draw_text('PLAY', WHITE, self.screen, 870, 255)
@ -77,38 +80,6 @@ class Game:
pygame.display.update() pygame.display.update()
self.clock.tick(60) 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): def game(self):
running = True running = True
grid = Grid(self.tiles) grid = Grid(self.tiles)

0
ui/screens/__init__.py Normal file
View File

6
ui/screens/credits.py Normal file
View File

@ -0,0 +1,6 @@
from ui.screens.screen import Screen
class Credits(Screen):
def __init__(self, screen, clock):
super().__init__('credits', screen, clock)

6
ui/screens/options.py Normal file
View File

@ -0,0 +1,6 @@
from ui.screens.screen import Screen
class Options(Screen):
def __init__(self, screen, clock):
super().__init__('options', screen, clock)

28
ui/screens/screen.py Normal file
View File

@ -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)