From 190b2bec0199dcb3519b68694483237ba955a61a Mon Sep 17 00:00:00 2001 From: korzepadawid Date: Fri, 11 Mar 2022 19:42:17 +0100 Subject: [PATCH 1/2] refactor: file structure and cleanup --- common/__init__.py | 0 colors.py => common/colors.py | 0 constants.py => common/constants.py | 2 +- helpers.py => common/helpers.py | 0 logic/__init__.py | 0 field.py => logic/field.py | 0 game.py => logic/game.py | 32 ++++++++++++++--------------- grid.py => logic/grid.py | 9 ++++---- spawner.py => logic/spawner.py | 6 +++--- main.py | 2 +- models/__init__.py | 0 knight.py => models/knight.py | 30 ++++++++++++--------------- monster.py => models/monster.py | 3 +-- ui/__init__.py | 0 logs.py => ui/logs.py | 6 +++--- stats.py => ui/stats.py | 12 +++++------ 16 files changed, 49 insertions(+), 53 deletions(-) create mode 100644 common/__init__.py rename colors.py => common/colors.py (100%) rename constants.py => common/constants.py (99%) rename helpers.py => common/helpers.py (100%) create mode 100644 logic/__init__.py rename field.py => logic/field.py (100%) rename game.py => logic/game.py (86%) rename grid.py => logic/grid.py (89%) rename spawner.py => logic/spawner.py (85%) create mode 100644 models/__init__.py rename knight.py => models/knight.py (57%) rename monster.py => models/monster.py (85%) create mode 100644 ui/__init__.py rename logs.py => ui/logs.py (78%) rename stats.py => ui/stats.py (78%) diff --git a/common/__init__.py b/common/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/colors.py b/common/colors.py similarity index 100% rename from colors.py rename to common/colors.py diff --git a/constants.py b/common/constants.py similarity index 99% rename from constants.py rename to common/constants.py index 7c7cb50..fb59fbc 100644 --- a/constants.py +++ b/common/constants.py @@ -19,4 +19,4 @@ TILES = [ 'sand.png', 'water.png', 'grass_with_tree.jpg', -] \ No newline at end of file +] diff --git a/helpers.py b/common/helpers.py similarity index 100% rename from helpers.py rename to common/helpers.py diff --git a/logic/__init__.py b/logic/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/field.py b/logic/field.py similarity index 100% rename from field.py rename to logic/field.py diff --git a/game.py b/logic/game.py similarity index 86% rename from game.py rename to logic/game.py index ea90610..f1cb9a3 100644 --- a/game.py +++ b/logic/game.py @@ -1,22 +1,23 @@ -import pygame import sys -from colors import FONT_DARK, WHITE -from constants import GAME_TITLE, WINDOW_WIDTH, WINDOW_HEIGHT, FPS_COUNT, TILES -from grid import Grid -from helpers import draw_text -from logs import Logs -from stats import Stats -from knight import Knight -from monster import Monster -from spawner import Spawner +import pygame + +from common.colors import FONT_DARK, WHITE +from common.constants import GAME_TITLE, WINDOW_WIDTH, WINDOW_HEIGHT, FPS_COUNT, TILES +from common.helpers import draw_text +from models.knight import Knight +from models.monster import Monster +from ui.logs import Logs +from ui.stats import Stats +from .grid import Grid +from .spawner import Spawner class Game: def __init__(self): pygame.init() pygame.display.set_caption(GAME_TITLE) - pygame.display.set_icon(pygame.image.load('resources/icons/sword.png')) + pygame.display.set_icon(pygame.image.load('./resources/icons/sword.png')) self.screen = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT)) self.clock = pygame.time.Clock() @@ -26,7 +27,7 @@ class Game: converted_tile = pygame.image.load('resources/textures/' + tile_path).convert_alpha() 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") click = False @@ -113,15 +114,14 @@ class Game: grid = Grid(self.tiles) stats = Stats() logs = Logs() - knight1 = Knight("/resources/textures/knight.png") - knight2 = Knight("/resources/textures/knight.png") + knight1 = Knight("./resources/textures/knight.png") + knight2 = Knight("./resources/textures/knight.png") spawn = Spawner(grid, [knight1, knight2], 4, 5, 8, 0) spawn.spawn() knights_list = pygame.sprite.Group() knights_list.add(knight1) knights_list.add(knight2) - while running: self.screen.blit(self.bg, (0, 0)) @@ -138,7 +138,7 @@ class Game: logs.draw(self.screen) knights_list.draw(self.screen) - monster1 = Monster(self.screen, "/resources/textures/dragon.png") + monster1 = Monster(self.screen, "./resources/textures/dragon.png") monster_list = pygame.sprite.Group() monster_list.add(monster1) pygame.display.update() diff --git a/grid.py b/logic/grid.py similarity index 89% rename from grid.py rename to logic/grid.py index 7553f1a..7c5ef4c 100644 --- a/grid.py +++ b/logic/grid.py @@ -1,8 +1,10 @@ -import pygame import random -from field import Field -from constants import ROWS, COLUMNS, GRID_CELL_PADDING, GRID_CELL_WIDTH, GRID_CELL_HEIGHT, BORDER_WIDTH, BORDER_RADIUS +import pygame + +from common.constants import ROWS, COLUMNS, GRID_CELL_PADDING, GRID_CELL_WIDTH, GRID_CELL_HEIGHT, BORDER_WIDTH, \ + BORDER_RADIUS +from .field import Field class Grid: @@ -33,4 +35,3 @@ class Grid: GRID_CELL_HEIGHT] image = self.grid[row][column].converted_texture screen.blit(pygame.transform.scale(image, (GRID_CELL_WIDTH, GRID_CELL_HEIGHT)), box_rect) - diff --git a/spawner.py b/logic/spawner.py similarity index 85% rename from spawner.py rename to logic/spawner.py index 4117262..fe8322e 100644 --- a/spawner.py +++ b/logic/spawner.py @@ -1,6 +1,7 @@ -from constants import ROWS, COLUMNS, GRID_CELL_PADDING, GRID_CELL_WIDTH, GRID_CELL_HEIGHT, BORDER_WIDTH, BORDER_RADIUS import random +from common.constants import GRID_CELL_PADDING, GRID_CELL_WIDTH, GRID_CELL_HEIGHT + class Spawner: def __init__(self, grid, objs_to_spawn_list: list, width, height, pos_row, pos_column): @@ -20,9 +21,8 @@ class Spawner: (GRID_CELL_PADDING + GRID_CELL_HEIGHT) * row + GRID_CELL_PADDING + 7]) for obj in self.objs_to_spawn_list: - random_tile = random.randint(0, len(coords)-1) + random_tile = random.randint(0, len(coords) - 1) obj.rect.x = coords[random_tile][0] obj.rect.y = coords[random_tile][1] coords.pop(random_tile) obj.update() - diff --git a/main.py b/main.py index 3cf2c8a..83ef2f6 100644 --- a/main.py +++ b/main.py @@ -1,4 +1,4 @@ -from game import Game +from logic.game import Game if __name__ == '__main__': game = Game() diff --git a/models/__init__.py b/models/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/knight.py b/models/knight.py similarity index 57% rename from knight.py rename to models/knight.py index b11128c..c2b282b 100644 --- a/knight.py +++ b/models/knight.py @@ -1,17 +1,13 @@ -import pygame.image - -from constants import ROWS, COLUMNS, GRID_CELL_PADDING, GRID_CELL_WIDTH, GRID_CELL_HEIGHT, BORDER_WIDTH, BORDER_RADIUS, \ - WINDOW_WIDTH, WINDOW_HEIGHT - -class Knight(pygame.sprite.Sprite): - def __init__(self, img): - super().__init__() - self.images = [] - self.image = pygame.image.load("resources/textures/knight.png") - self.image = pygame.transform.scale(self.image, (40, 40)) - self.images.append(self.image) - self.rect = self.image.get_rect() - - knights_list = pygame.sprite.Group() - - +import pygame.image + + +class Knight(pygame.sprite.Sprite): + def __init__(self, img): + super().__init__() + self.images = [] + self.image = pygame.image.load("./resources/textures/knight.png") + self.image = pygame.transform.scale(self.image, (40, 40)) + self.images.append(self.image) + self.rect = self.image.get_rect() + + knights_list = pygame.sprite.Group() diff --git a/monster.py b/models/monster.py similarity index 85% rename from monster.py rename to models/monster.py index 9b7b5e5..7e46a7a 100644 --- a/monster.py +++ b/models/monster.py @@ -1,5 +1,4 @@ import pygame.image -import random class Monster(pygame.sprite.Sprite): @@ -7,7 +6,7 @@ class Monster(pygame.sprite.Sprite): def __init__(self, screen, img): super().__init__() self.images = [] - self.image = pygame.image.load("resources/textures/dragon.png") + self.image = pygame.image.load("./resources/textures/dragon.png") self.image = pygame.transform.scale(self.image, (40, 40)) self.images.append(self.image) self.rect = self.image.get_rect() diff --git a/ui/__init__.py b/ui/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/logs.py b/ui/logs.py similarity index 78% rename from logs.py rename to ui/logs.py index beaff27..a3455e9 100644 --- a/logs.py +++ b/ui/logs.py @@ -1,8 +1,8 @@ import pygame -from colors import FONT_DARK, ORANGE, WHITE, RED -from constants import COLUMNS, GRID_CELL_PADDING, GRID_CELL_WIDTH, BORDER_WIDTH, BORDER_RADIUS -from helpers import draw_text +from common.colors import FONT_DARK, ORANGE, WHITE +from common.constants import COLUMNS, GRID_CELL_PADDING, GRID_CELL_WIDTH, BORDER_WIDTH, BORDER_RADIUS +from common.helpers import draw_text class Logs: diff --git a/stats.py b/ui/stats.py similarity index 78% rename from stats.py rename to ui/stats.py index f1e9994..30be6cb 100644 --- a/stats.py +++ b/ui/stats.py @@ -1,8 +1,8 @@ import pygame -from colors import FONT_DARK, ORANGE, WHITE, RED -from constants import COLUMNS, GRID_CELL_PADDING, GRID_CELL_WIDTH, BORDER_WIDTH, BORDER_RADIUS -from helpers import draw_text +from common.colors import FONT_DARK, ORANGE, WHITE, RED +from common.constants import COLUMNS, GRID_CELL_PADDING, GRID_CELL_WIDTH, BORDER_WIDTH, BORDER_RADIUS +from common.helpers import draw_text class Stats: @@ -21,8 +21,8 @@ class Stats: pygame.draw.rect(screen, ORANGE, pygame.Rect(x, y + 65, 340, 3)) # shields - shield_blue = pygame.image.load('resources/textures/shield_blue.png') - shield_red = pygame.image.load('resources/textures/shield_red.png') + shield_blue = pygame.image.load('./resources/textures/shield_blue.png') + shield_red = pygame.image.load('./resources/textures/shield_red.png') screen.blit(shield_blue, (x + 20, y + 80)) screen.blit(shield_red, (x + 200, y + 80)) draw_text('VS', FONT_DARK, screen, x + 150, y + 120, 36) @@ -41,4 +41,4 @@ class Stats: # points pygame.draw.rect(screen, ORANGE, pygame.Rect(x, y + 390, 340, 3)) draw_text('PUNKTY: 10', FONT_DARK, screen, x + 35, y + 408, 18, True) - draw_text('PUNKTY: 10', FONT_DARK, screen, x + 215, y + 408, 18, True) \ No newline at end of file + draw_text('PUNKTY: 10', FONT_DARK, screen, x + 215, y + 408, 18, True) -- 2.20.1 From b647ba342364b6fa94691f85987f3099862e0858 Mon Sep 17 00:00:00 2001 From: korzepadawid Date: Fri, 11 Mar 2022 20:00:42 +0100 Subject: [PATCH 2/2] 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) -- 2.20.1