From 9f1ca750f136b713d5378184930dda65ad5a875e Mon Sep 17 00:00:00 2001 From: korzepadawid Date: Fri, 4 Mar 2022 20:10:55 +0100 Subject: [PATCH] refactor: Game class --- game.py | 41 +++++++++++++++++++++++++++++++++++++++++ main.py | 44 +++----------------------------------------- 2 files changed, 44 insertions(+), 41 deletions(-) create mode 100644 game.py diff --git a/game.py b/game.py new file mode 100644 index 0000000..b375423 --- /dev/null +++ b/game.py @@ -0,0 +1,41 @@ +import pygame + +GAME_TITLE = 'WMICraft' +WINDOW_HEIGHT = 900 +WINDOW_WIDTH = 900 +GRID_CELL_PADDING = 3 +GRID_CELL_WIDTH = 42 +GRID_CELL_HEIGHT = 42 +ROWS = 20 +COLUMNS = 20 +FPS_COUNT = 60 +GREEN = (0, 255, 0) + + +class Game: + def __init__(self): + pygame.init() + pygame.display.set_caption(GAME_TITLE) + pygame.display.set_icon(pygame.image.load('resources/icons/sword.png')) + self.screen = pygame.display.set_mode((WINDOW_HEIGHT, WINDOW_WIDTH)) + self.clock = pygame.time.Clock() + + def start(self): + running = True + while running: + for event in pygame.event.get(): + if event.type == pygame.QUIT: + running = False + self.draw_grid() + pygame.display.update() + self.clock.tick(FPS_COUNT) + pygame.quit() + + def draw_grid(self): + for row in range(ROWS): + for column in range(COLUMNS): + box_rect = [(GRID_CELL_PADDING + GRID_CELL_WIDTH) * column + GRID_CELL_PADDING, + (GRID_CELL_PADDING + GRID_CELL_HEIGHT) * row + GRID_CELL_PADDING, + GRID_CELL_WIDTH, + GRID_CELL_HEIGHT] + pygame.draw.rect(self.screen, GREEN, box_rect) diff --git a/main.py b/main.py index 6538ccf..e31b37c 100644 --- a/main.py +++ b/main.py @@ -1,43 +1,5 @@ -import pygame - -pygame.init() - -GAME_TITLE = 'WMICraft' -WINDOW_HEIGHT = 900 -WINDOW_WIDTH = 900 -GRID_CELL_PADDING = 3 -GRID_CELL_WIDTH = 42 -GRID_CELL_HEIGHT = 42 -GREEN = (0, 255, 0) - -screen = pygame.display.set_mode((WINDOW_HEIGHT, WINDOW_WIDTH)) -pygame.display.set_caption(GAME_TITLE) -icon = pygame.image.load('resources/icons/sword.png') -pygame.display.set_icon(icon) - - -def draw_grid(): - for row in range(20): - for column in range(20): - box_rect = [(GRID_CELL_PADDING + GRID_CELL_WIDTH) * column + GRID_CELL_PADDING, - (GRID_CELL_PADDING + GRID_CELL_HEIGHT) * row + GRID_CELL_PADDING, - GRID_CELL_WIDTH, - GRID_CELL_HEIGHT] - pygame.draw.rect(screen, GREEN, box_rect) - - -def main(): - running = True - - while running: - for event in pygame.event.get(): - if event.type == pygame.QUIT: - running = False - draw_grid() - pygame.display.update() - - pygame.quit() - +from game import Game if __name__ == '__main__': - main() + game = Game() + game.start()