added menu support

This commit is contained in:
Alessio Rondelli 2024-07-13 20:10:33 +02:00
parent 2fc9381d93
commit 0e106be4d7
2 changed files with 78 additions and 15 deletions

52
game.py
View File

@ -2,7 +2,7 @@ import pygame
import numpy as np import numpy as np
from scripts.tiles import Tilemap from scripts.tiles import Tilemap
from scripts.entities import Entity, Partner, GroupEntities from scripts.entities import Entity, Partner, GroupEntities
from scripts.utils import load_image, load_images, to_isometric_pixel from scripts.utils import load_image, load_images, to_isometric_pixel, Button, change_status
from scripts.camera import Camera from scripts.camera import Camera
# SETTINGS # SETTINGS
@ -18,7 +18,15 @@ class Game:
pygame.display.set_caption(TITLE) pygame.display.set_caption(TITLE)
self.clock = pygame.time.Clock() self.clock = pygame.time.Clock()
def iso_render(self):
self.display.fill('white')
self.tilemap.render(self.display, offset = self.camera.scroll)
self.entities.render(self.display, offset = self.camera.scroll)
#self.player.render(self.display, offset = self.camera.scroll)
#self.partner.render(self.display, offset = self.camera.scroll)
self.screen.blit(pygame.transform.scale(self.display,self.screen.get_size()), (0,0))
pygame.display.update()
def iso_game(self):
self.player = Entity(self, 'player', (64., 64.), (28, 42)) self.player = Entity(self, 'player', (64., 64.), (28, 42))
self.partner = Partner(self, (80.,80.), (28,42)) self.partner = Partner(self, (80.,80.), (28,42))
self.entities = GroupEntities(self, [self.player, self.partner]) self.entities = GroupEntities(self, [self.player, self.partner])
@ -31,21 +39,11 @@ class Game:
} }
self.tilemap = Tilemap(self, (64,32), 'map_1.csv') self.tilemap = Tilemap(self, (64,32), 'map_1.csv')
self.camera = Camera(self) self.camera = Camera(self)
def render(self): while self.status == 'iso_game':
self.display.fill('white')
self.tilemap.render(self.display, offset = self.camera.scroll)
self.entities.render(self.display, offset = self.camera.scroll)
#self.player.render(self.display, offset = self.camera.scroll)
#self.partner.render(self.display, offset = self.camera.scroll)
self.screen.blit(pygame.transform.scale(self.display,self.screen.get_size()), (0,0))
pygame.display.update()
def run(self):
self.status = True
while self.status:
delta = np.array([0.0,0.0]) delta = np.array([0.0,0.0])
for event in pygame.event.get(): for event in pygame.event.get():
if event.type == pygame.QUIT: if event.type == pygame.QUIT:
self.status = False self.status = 'quit'
keys = pygame.key.get_pressed() keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]: if keys[pygame.K_LEFT]:
delta += np.array([-5,5]) delta += np.array([-5,5])
@ -59,8 +57,32 @@ class Game:
self.camera.update() self.camera.update()
self.player.update(self.tilemap, delta) self.player.update(self.tilemap, delta)
self.partner.update() self.partner.update()
self.render() self.iso_render()
self.clock.tick(60) self.clock.tick(60)
def menu(self):
buttons = [
Button(self,[100,100],[300,90],'Start Isometric',(lambda: change_status(self,'iso_game'))),
Button(self,[100,200],[300,90],'Level Editor',(lambda: change_status(self,'level_editor'))),
Button(self,[100,300],[300,90],'Quit',(lambda: change_status(self,'quit')))
]
while self.status == 'menu':
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.status = 'quit'
self.screen.fill('gray')
for button in buttons:
button.process()
pygame.display.update()
self.clock.tick(60)
def run(self):
self.status = 'menu'
while self.status != 'quit':
if self.status == 'menu':
self.menu()
if self.status == 'iso_game':
self.iso_game()
if self.status == 'level_editor':
self.level_editor()
pygame.quit() pygame.quit()
Game().run() Game().run()

View File

@ -59,3 +59,44 @@ class Animation:
def img(self): def img(self):
return self.images[int(self.frame / self.img_duration)] return self.images[int(self.frame / self.img_duration)]
def change_status(game,new_status):
game.status = new_status
class Button:
def __init__(self, game, pos, size, buttonText='Button', onclickFunction=None, onePress=False):
self.game = game
self.pos = np.array(pos)
self.size = np.array(size)
self.onclickFunction = onclickFunction
self.onePress = onePress
self.alreadyPressed = False
self.fillColors = {
'normal': '#ffffff',
'hover': '#666666',
'pressed': '#333333',
}
#font = pygame.font.SysFont('Arial', 40)
self.buttonSurface = pygame.Surface(self.size)
self.buttonRect = pygame.Rect(self.pos, self.size)
self.buttonSurf = pygame.font.SysFont('Arial', 40).render(buttonText, True, (20, 20, 20))
def process(self):
mousePos = pygame.mouse.get_pos()
self.buttonSurface.fill(self.fillColors['normal'])
if self.buttonRect.collidepoint(mousePos):
self.buttonSurface.fill(self.fillColors['hover'])
if pygame.mouse.get_pressed(num_buttons=3)[0]:
self.buttonSurface.fill(self.fillColors['pressed'])
if self.onePress:
self.onclickFunction()
elif not self.alreadyPressed:
self.onclickFunction()
self.alreadyPressed = True
else:
self.alreadyPressed = False
self.buttonSurface.blit(self.buttonSurf, [
self.buttonRect.width/2 - self.buttonSurf.get_rect().width/2,
self.buttonRect.height/2 - self.buttonSurf.get_rect().height/2
])
self.game.screen.blit(self.buttonSurface, self.buttonRect)