WMICraft/game.py

127 lines
4.3 KiB
Python
Raw Permalink Normal View History

2022-03-08 12:11:22 +01:00
import pygame, sys
2022-03-04 20:57:34 +01:00
from glob import glob
from grid import Grid
2022-03-08 12:11:22 +01:00
from constants import GAME_TITLE, WINDOW_WIDTH, WINDOW_HEIGHT, FPS_COUNT, TILES
from stats import Stats
from helpers import draw_text
2022-03-04 20:10:55 +01:00
class Game:
def __init__(self):
pygame.init()
pygame.display.set_caption(GAME_TITLE)
pygame.display.set_icon(pygame.image.load('resources/icons/sword.png'))
2022-03-04 20:57:34 +01:00
2022-03-08 12:11:22 +01:00
self.screen = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
self.font = pygame.font.SysFont(None, 30)
2022-03-04 20:10:55 +01:00
self.clock = pygame.time.Clock()
2022-03-08 12:11:22 +01:00
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-04 20:57:34 +01:00
2022-03-08 12:11:22 +01:00
self.bg = pygame.image.load("resources/textures/menu_bg2.jpg")
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', self.font, (0, 0, 0), self.screen, 850, 150)
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', self.font, (255, 255, 255), self.screen, 870, 265)
pygame.draw.rect(self.screen, (0, 191, 255), button_2, 0, 4)
draw_text('OPTIONS', self.font, (255, 255, 255), self.screen, 870, 365)
pygame.draw.rect(self.screen, (0, 191, 255), button_3, 0, 4)
draw_text('CREDITS', self.font, (255, 255, 255), self.screen, 870, 465)
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):
2022-03-04 20:10:55 +01:00
running = True
while running:
2022-03-08 12:11:22 +01:00
self.screen.fill((0, 0, 0))
draw_text('options', self.font, (255, 255, 255), self.screen, 20, 20)
2022-03-04 20:10:55 +01:00
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
2022-03-08 12:11:22 +01:00
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', self.font, (255, 255, 255), self.screen, 20, 20)
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()
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
2022-03-04 20:57:34 +01:00
grid.draw(self.screen)
2022-03-08 12:11:22 +01:00
stats.draw(self.screen, self.font)
2022-03-04 20:10:55 +01:00
pygame.display.update()
self.clock.tick(FPS_COUNT)