structure_cleanup #4

Merged
s464961 merged 2 commits from structure_cleanup into master 2022-03-13 15:06:54 +01:00
20 changed files with 94 additions and 87 deletions

0
common/__init__.py Normal file
View File

View File

@ -19,4 +19,4 @@ TILES = [
'sand.png', 'sand.png',
'water.png', 'water.png',
'grass_with_tree.jpg', 'grass_with_tree.jpg',
] ]

0
logic/__init__.py Normal file
View File

View File

@ -1,22 +1,25 @@
import pygame
import sys import sys
from colors import FONT_DARK, WHITE import pygame
from constants import GAME_TITLE, WINDOW_WIDTH, WINDOW_HEIGHT, FPS_COUNT, TILES
from grid import Grid from common.colors import FONT_DARK, WHITE
from helpers import draw_text from common.constants import GAME_TITLE, WINDOW_WIDTH, WINDOW_HEIGHT, FPS_COUNT, TILES
from logs import Logs from common.helpers import draw_text
from stats import Stats from models.knight import Knight
from knight import Knight from models.monster import Monster
from monster import Monster from ui.screens.credits import Credits
from spawner import Spawner from ui.logs import Logs
from ui.screens.options import Options
from ui.stats import Stats
from .grid import Grid
from .spawner import Spawner
class Game: class Game:
def __init__(self): def __init__(self):
pygame.init() pygame.init()
pygame.display.set_caption(GAME_TITLE) 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.screen = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
self.clock = pygame.time.Clock() self.clock = pygame.time.Clock()
@ -26,7 +29,8 @@ class Game:
converted_tile = pygame.image.load('resources/textures/' + tile_path).convert_alpha() converted_tile = pygame.image.load('resources/textures/' + tile_path).convert_alpha()
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
@ -47,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)
@ -76,52 +80,19 @@ 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)
stats = Stats() stats = Stats()
logs = Logs() logs = Logs()
knight1 = Knight("/resources/textures/knight.png") knight1 = Knight("./resources/textures/knight.png")
knight2 = Knight("/resources/textures/knight.png") knight2 = Knight("./resources/textures/knight.png")
spawn = Spawner(grid, [knight1, knight2], 4, 5, 8, 0) spawn = Spawner(grid, [knight1, knight2], 4, 5, 8, 0)
spawn.spawn() spawn.spawn()
knights_list = pygame.sprite.Group() knights_list = pygame.sprite.Group()
knights_list.add(knight1) knights_list.add(knight1)
knights_list.add(knight2) knights_list.add(knight2)
while running: while running:
self.screen.blit(self.bg, (0, 0)) self.screen.blit(self.bg, (0, 0))
@ -138,7 +109,7 @@ class Game:
logs.draw(self.screen) logs.draw(self.screen)
knights_list.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 = pygame.sprite.Group()
monster_list.add(monster1) monster_list.add(monster1)
pygame.display.update() pygame.display.update()

View File

@ -1,8 +1,10 @@
import pygame
import random 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: class Grid:
@ -33,4 +35,3 @@ class Grid:
GRID_CELL_HEIGHT] GRID_CELL_HEIGHT]
image = self.grid[row][column].converted_texture image = self.grid[row][column].converted_texture
screen.blit(pygame.transform.scale(image, (GRID_CELL_WIDTH, GRID_CELL_HEIGHT)), box_rect) screen.blit(pygame.transform.scale(image, (GRID_CELL_WIDTH, GRID_CELL_HEIGHT)), box_rect)

View File

@ -1,6 +1,7 @@
from constants import ROWS, COLUMNS, GRID_CELL_PADDING, GRID_CELL_WIDTH, GRID_CELL_HEIGHT, BORDER_WIDTH, BORDER_RADIUS
import random import random
from common.constants import GRID_CELL_PADDING, GRID_CELL_WIDTH, GRID_CELL_HEIGHT
class Spawner: class Spawner:
def __init__(self, grid, objs_to_spawn_list: list, width, height, pos_row, pos_column): 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]) (GRID_CELL_PADDING + GRID_CELL_HEIGHT) * row + GRID_CELL_PADDING + 7])
for obj in self.objs_to_spawn_list: 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.x = coords[random_tile][0]
obj.rect.y = coords[random_tile][1] obj.rect.y = coords[random_tile][1]
coords.pop(random_tile) coords.pop(random_tile)
obj.update() obj.update()

View File

@ -1,4 +1,4 @@
from game import Game from logic.game import Game
if __name__ == '__main__': if __name__ == '__main__':
game = Game() game = Game()

0
models/__init__.py Normal file
View File

View File

@ -1,17 +1,13 @@
import pygame.image 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):
class Knight(pygame.sprite.Sprite): super().__init__()
def __init__(self, img): self.images = []
super().__init__() self.image = pygame.image.load("./resources/textures/knight.png")
self.images = [] self.image = pygame.transform.scale(self.image, (40, 40))
self.image = pygame.image.load("resources/textures/knight.png") self.images.append(self.image)
self.image = pygame.transform.scale(self.image, (40, 40)) self.rect = self.image.get_rect()
self.images.append(self.image)
self.rect = self.image.get_rect() knights_list = pygame.sprite.Group()
knights_list = pygame.sprite.Group()

View File

@ -1,5 +1,4 @@
import pygame.image import pygame.image
import random
class Monster(pygame.sprite.Sprite): class Monster(pygame.sprite.Sprite):
@ -7,7 +6,7 @@ class Monster(pygame.sprite.Sprite):
def __init__(self, screen, img): def __init__(self, screen, img):
super().__init__() super().__init__()
self.images = [] 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.image = pygame.transform.scale(self.image, (40, 40))
self.images.append(self.image) self.images.append(self.image)
self.rect = self.image.get_rect() self.rect = self.image.get_rect()

0
ui/__init__.py Normal file
View File

View File

@ -1,8 +1,8 @@
import pygame import pygame
from colors import FONT_DARK, ORANGE, WHITE, RED from common.colors import FONT_DARK, ORANGE, WHITE
from constants import COLUMNS, GRID_CELL_PADDING, GRID_CELL_WIDTH, BORDER_WIDTH, BORDER_RADIUS from common.constants import COLUMNS, GRID_CELL_PADDING, GRID_CELL_WIDTH, BORDER_WIDTH, BORDER_RADIUS
from helpers import draw_text from common.helpers import draw_text
class Logs: class Logs:

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)

View File

@ -1,8 +1,8 @@
import pygame import pygame
from colors import FONT_DARK, ORANGE, WHITE, RED from common.colors import FONT_DARK, ORANGE, WHITE, RED
from constants import COLUMNS, GRID_CELL_PADDING, GRID_CELL_WIDTH, BORDER_WIDTH, BORDER_RADIUS from common.constants import COLUMNS, GRID_CELL_PADDING, GRID_CELL_WIDTH, BORDER_WIDTH, BORDER_RADIUS
from helpers import draw_text from common.helpers import draw_text
class Stats: class Stats:
@ -21,8 +21,8 @@ class Stats:
pygame.draw.rect(screen, ORANGE, pygame.Rect(x, y + 65, 340, 3)) pygame.draw.rect(screen, ORANGE, pygame.Rect(x, y + 65, 340, 3))
# shields # shields
shield_blue = pygame.image.load('resources/textures/shield_blue.png') shield_blue = pygame.image.load('./resources/textures/shield_blue.png')
shield_red = pygame.image.load('resources/textures/shield_red.png') shield_red = pygame.image.load('./resources/textures/shield_red.png')
screen.blit(shield_blue, (x + 20, y + 80)) screen.blit(shield_blue, (x + 20, y + 80))
screen.blit(shield_red, (x + 200, y + 80)) screen.blit(shield_red, (x + 200, y + 80))
draw_text('VS', FONT_DARK, screen, x + 150, y + 120, 36) draw_text('VS', FONT_DARK, screen, x + 150, y + 120, 36)
@ -41,4 +41,4 @@ class Stats:
# points # points
pygame.draw.rect(screen, ORANGE, pygame.Rect(x, y + 390, 340, 3)) 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 + 35, y + 408, 18, True)
draw_text('PUNKTY: 10', FONT_DARK, screen, x + 215, y + 408, 18, True) draw_text('PUNKTY: 10', FONT_DARK, screen, x + 215, y + 408, 18, True)