WMICraft/logic/game.py

146 lines
4.9 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
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
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-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:
self.options()
if button_3.collidepoint((mx, my)):
if click:
self.credits()
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 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)
stats = Stats()
logs = Logs()
2022-03-11 19:42:17 +01:00
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)
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)
knights_list.draw(self.screen)
2022-03-11 19:42:17 +01:00
monster1 = Monster(self.screen, "./resources/textures/dragon.png")
2022-03-10 18:34:58 +01:00
monster_list = pygame.sprite.Group()
monster_list.add(monster1)
2022-03-09 16:59:58 +01:00
pygame.display.update()
self.clock.tick(FPS_COUNT)