WMICraft/logic/game.py

165 lines
6.0 KiB
Python
Raw Normal View History

2022-03-09 16:59:58 +01:00
import sys
2022-03-11 19:42:17 +01:00
import pygame
from common.colors import FONT_DARK, WHITE
2022-03-24 16:32:14 +01:00
from common.constants import *
2022-03-11 19:42:17 +01:00
from common.helpers import draw_text
2022-04-04 21:23:32 +02:00
from logic.knights_queue import KnightsQueue
2022-03-24 16:32:14 +01:00
from models.castle import Castle
2022-03-11 19:42:17 +01:00
from models.knight import Knight
from models.monster import Monster
from ui.logs import Logs
2022-03-21 20:30:02 +01:00
from ui.screens.credits import Credits
2022-03-11 20:00:42 +01:00
from ui.screens.options import Options
2022-03-11 19:42:17 +01:00
from ui.stats import Stats
from .grid import Grid
from .spawner import Spawner
2022-03-09 16:59:58 +01:00
class Game:
def __init__(self):
pygame.init()
pygame.display.set_caption(GAME_TITLE)
2022-03-11 19:42:17 +01:00
pygame.display.set_icon(pygame.image.load('./resources/icons/sword.png'))
2022-03-09 16:59:58 +01:00
self.screen = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
self.clock = pygame.time.Clock()
self.tiles = []
for tile_path in TILES:
converted_tile = pygame.image.load('resources/textures/' + tile_path).convert_alpha()
self.tiles.append((tile_path, converted_tile))
2022-03-11 19:42:17 +01:00
self.bg = pygame.image.load("./resources/textures/bg.jpg")
2022-03-11 20:00:42 +01:00
self.screens = {'credits': Credits(self.screen, self.clock), 'options': Options(self.screen, self.clock)}
2022-03-09 16:59:58 +01:00
click = False
def main_menu(self):
while True:
self.screen.blit(self.bg, (0, 0))
pygame.draw.rect(self.screen, (255, 255, 255), pygame.Rect(800, 100, 400, 500), 0, 5)
draw_text('MAIN MENU', FONT_DARK, self.screen, 850, 150, 30, True)
mx, my = pygame.mouse.get_pos()
button_1 = pygame.Rect(850, 250, 300, 50)
button_2 = pygame.Rect(850, 350, 300, 50)
button_3 = pygame.Rect(850, 450, 300, 50)
if button_1.collidepoint((mx, my)):
if click:
self.game()
if button_2.collidepoint((mx, my)):
if click:
2022-03-11 20:00:42 +01:00
self.screens['options'].display_screen()
2022-03-09 16:59:58 +01:00
if button_3.collidepoint((mx, my)):
if click:
2022-03-11 20:00:42 +01:00
self.screens['credits'].display_screen()
2022-03-09 16:59:58 +01:00
pygame.draw.rect(self.screen, (0, 191, 255), button_1, 0, 4)
draw_text('PLAY', WHITE, self.screen, 870, 255)
pygame.draw.rect(self.screen, (0, 191, 255), button_2, 0, 4)
draw_text('OPTIONS', WHITE, self.screen, 870, 355)
pygame.draw.rect(self.screen, (0, 191, 255), button_3, 0, 4)
draw_text('CREDITS', WHITE, self.screen, 870, 455)
click = False
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
click = True
pygame.display.update()
self.clock.tick(60)
def game(self):
running = True
grid = Grid(self.tiles)
stats = Stats()
logs = Logs()
2022-03-21 20:30:02 +01:00
knights_sprite_group = pygame.sprite.Group()
2022-03-24 12:54:22 +01:00
monsters_sprite_group = pygame.sprite.Group()
2022-03-24 16:32:14 +01:00
castle_sprite_group = pygame.sprite.Group()
2022-03-21 20:30:02 +01:00
knights_left = self.generate_knights_team(knights_sprite_group)
knights_right = self.generate_knights_team(knights_sprite_group)
2022-04-04 21:23:32 +02:00
knights_queue = KnightsQueue(knights_left, knights_right)
2022-03-21 20:30:02 +01:00
spawn_left_team = Spawner(grid, knights_left, width=KNIGHTS_SPAWN_WIDTH, height=KNIGHTS_SPAWN_HEIGHT,
pos_row=KNIGHTS_SPAWN_FIRST_ROW, pos_column=KNIGHTS_SPAWN_FIRST_COL)
spawn_right_team = Spawner(grid, knights_right, width=KNIGHTS_SPAWN_WIDTH, height=KNIGHTS_SPAWN_HEIGHT,
pos_row=KNIGHTS_SPAWN_FIRST_ROW,
pos_column=COLUMNS - KNIGHTS_SPAWN_WIDTH - 1) # - 1 to avoid list index out of range
spawn_left_team.spawn()
spawn_right_team.spawn()
2022-03-24 13:09:43 +01:00
spawned_monsters = self.generate_monster(monsters_sprite_group)
2022-03-24 12:54:22 +01:00
monster_spawn = Spawner(grid, spawned_monsters, width=MONSTER_SPAWN_WIDTH, height=MONSTER_SPAWN_HEIGHT,
2022-03-24 16:32:14 +01:00
pos_row=MONSTER_SPAWN_FIRST_ROW, pos_column=MONSTER_SPAWN_FIRST_COL)
2022-03-24 12:54:22 +01:00
2022-03-24 13:09:43 +01:00
monster_spawn.spawn_monsters()
2022-03-24 12:54:22 +01:00
2022-03-24 16:32:14 +01:00
spawned_castle = self.generate_castle(castle_sprite_group)
castle_spawn = Spawner(grid, [spawned_castle], width=CASTLE_SPAWN_WIDTH, height=CASTLE_SPAWN_HEIGHT,
pos_row=CASTLE_SPAWN_FIRST_ROW, pos_column=CASTLE_SPAWN_FIRST_COL)
castle_spawn.spawn()
2022-03-09 16:59:58 +01:00
while running:
self.screen.blit(self.bg, (0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
running = False
grid.draw(self.screen)
stats.draw(self.screen)
logs.draw(self.screen)
2022-03-21 20:30:02 +01:00
knights_sprite_group.draw(self.screen)
2022-03-24 12:54:22 +01:00
monsters_sprite_group.draw(self.screen)
2022-03-24 16:32:14 +01:00
castle_sprite_group.draw(self.screen)
2022-03-09 16:59:58 +01:00
pygame.display.update()
self.clock.tick(FPS_COUNT)
2022-03-21 20:30:02 +01:00
@staticmethod
def generate_knights_team(knights_sprite_group):
knights = []
for i in range(4):
knight = Knight("./resources/textures/knight.png")
knights.append(knight)
knights_sprite_group.add(knight)
return knights
2022-03-24 12:54:22 +01:00
@staticmethod
def generate_monster(monsters_sprite_group):
monsters = []
2022-03-24 13:09:43 +01:00
for i in range(2):
monster = Monster()
monsters.append(monster)
monsters_sprite_group.add(monster)
2022-03-24 12:54:22 +01:00
return monsters
2022-03-24 16:32:14 +01:00
@staticmethod
def generate_castle(castle_sprite_group):
castle = Castle()
castle_sprite_group.add(castle)
return castle