Merge branch 'main' into menu

This commit is contained in:
akp 2025-03-22 18:03:21 +00:00
commit aafc3e02f6
No known key found for this signature in database
GPG key ID: CF8D58F3DEB20755
6 changed files with 106 additions and 54 deletions

View file

@ -1,42 +1,37 @@
from sched import Event
import pygame
import resources
import menu
import util
import gameplay
def main():
# pygame setup
pygame.init()
screen = pygame.display.set_mode((1280, 720))
clock = pygame.time.Clock()
running = True
pygame.mouse.set_cursor(*pygame.cursors.arrow)
m = menu.Menu(screen)
screen.fill("lightblue")
active_scene = menu.Menu(screen)
while running:
# poll for events
# pygame.QUIT event means the user clicked X to close your window
for _ in pygame.event.get(eventtype=pygame.QUIT):
running = False
# fill the screen with a color to wipe away anything from last frame
for event in pygame.event.get(eventtype=util.TRANSITION_EVENT_TYPE):
raise NotImplementedError("switch to scene " + event.dict["to"])
next_scene = event.dict["to"]
match next_scene:
case "menu":
active_scene = menu.Menu(screen)
case "gameplay":
active_scene = gameplay.GamePlay(screen)
case _:
raise NotImplementedError("switch to scene " + event.dict["to"])
screen.fill(0x000000)
m.do()
# flip() the display to put your work on screen
active_scene.do()
pygame.display.flip()
# limits FPS to 60
# dt is delta time in seconds since last frame, used for framerate-
# independent physics.
dt = clock.tick(60) / 1000
pygame.quit()

View file

@ -4,37 +4,15 @@ import pygame
import resources
class Button:
class SurfaceButton:
surface: pygame.SurfaceType
text: str
size: tuple[int, int]
font: pygame.font.Font
already_pressed: bool
already_collided: bool
def __init__(self, text, size, font=resources.FONT_SM):
self.surface = pygame.Surface(size)
self.surface.fill((255, 0, 255))
self.surface.set_colorkey((255, 0, 255))
def __init__(self, surface):
self.surface = surface
self.already_pressed = False
self.already_collided = False
self.font = font
self.text = text
self.size = size
(text_width, text_height) = self.font.size(text)
self.text_pos = (
(size[0] - text_width) / 2,
(size[1] - text_height) / 2,
)
def _draw(self):
pygame.draw.rect(self.surface, 0xdf3062, pygame.Rect((0, 0), self.size), border_radius=5)
self.surface.blit(
self.font.render(self.text, True, "black"),
self.text_pos,
)
def blit_onto(self, output_surface: pygame.SurfaceType, pos: tuple[int, int]) -> bool:
"""
@ -44,14 +22,12 @@ class Button:
:param pos: position to place the button
:return: if the button is clicked or not - is debounced
"""
self._draw()
output_surface.blit(
self.surface,
pos
)
does_mouse_collide = pygame.Rect(*pos, *self.size).collidepoint(*pygame.mouse.get_pos())
does_mouse_collide = pygame.Rect(*pos, *self.surface.get_size()).collidepoint(*pygame.mouse.get_pos())
if does_mouse_collide != self.already_collided:
self.already_collided = does_mouse_collide
@ -71,3 +47,23 @@ class Button:
return False
return False
def text_button(text, size, font=resources.FONT_SM) -> SurfaceButton:
surface = pygame.Surface(size)
surface.fill((255, 0, 255))
surface.set_colorkey((255, 0, 255))
(text_width, text_height) = font.size(text)
text_pos = (
(size[0] - text_width) / 2,
(size[1] - text_height) / 2,
)
pygame.draw.rect(surface, 0xdf3062, pygame.Rect((0, 0), size), border_radius=5)
surface.blit(
font.render(text, True, "black"),
text_pos,
)
return SurfaceButton(surface)

61
game/gameplay.py Normal file
View file

@ -0,0 +1,61 @@
import pygame
import components
import util
import resources
import random
class GamePlay:
surface: pygame.SurfaceType
play_button: components.Button
def __init__(self, surface):
self.surface = surface
self.play_button = components.Button("Switch", (250, 50))
self.status = "Counter"
self.orderUp = False
def sandwich(screen):
fillings = ["lettuce", "ham", "tomatoes"]
amountOfFilling = random.randrange(1,4)
sandwich = ["bread"]
for _ in range(amountOfFilling):
filling = random.randrange(0,3)
sandwich.append(fillings[filling])
sandwich.append("bread")
position = 200
for i in sandwich:
screen.blit(
resources.FONT.render(i, True, (0, 0, 0)),
(1000, position),
)
position += 50
def do(self):
#width = self.get_width()
#height = self.get_height()
#gap = 15
#central_button_block = util.center_within(self.surface.get_size(), (250, (self.play_button.size[1] * 2) + gap))
if self.play_button.blit_onto(self.surface, (1000, 10)):
#status = "Food"
if self.status == "Counter":
self.surface.fill("blue")
self.surface.blit(
resources.FONT.render("Food", True, (0, 0, 0)),
(100, 100),
)
self.status = "Food"
else:
self.surface.fill("lightblue")
self.surface.blit(
resources.FONT.render("Counter", True, (0, 0, 0)),
(100, 100),
)
self.status = "Counter"
self.orderUp = False
if self.status == "Counter" and self.orderUp == False:
GamePlay.sandwich(self.surface)
self.orderUp = True

View file

@ -5,13 +5,13 @@ import util
class Menu:
surface: pygame.SurfaceType
play_button: components.Button
quit_button: components.Button
play_button: components.SurfaceButton
quit_button: components.SurfaceButton
def __init__(self, surface):
self.surface = surface
self.play_button = components.Button("Play", (250, 50), font=resources.FONT)
self.quit_button = components.Button("Quit", (250, 50), font=resources.FONT)
self.play_button = components.text_button("Play", (250, 50), font=resources.FONT)
self.quit_button = components.text_button("Quit", (250, 50), font=resources.FONT)
def do(self):
self.surface.blit(resources.SPLASH_SCREEN_IMAGE, (0, 0))
@ -24,10 +24,10 @@ class Menu:
)
gap = 15
central_button_block = util.center_within(self.surface.get_size(), (250, (self.play_button.size[1] * 2) + gap))
central_button_block = util.center_within(self.surface.get_size(), (250, (50 * 2) + gap))
if self.play_button.blit_onto(self.surface, central_button_block):
pygame.event.post(util.make_transition_event("gameplay"))
if self.quit_button.blit_onto(self.surface, util.add_coord(central_button_block, (0, self.play_button.size[1] + gap))):
if self.quit_button.blit_onto(self.surface, util.add_coord(central_button_block, (0, 50 + gap))):
pygame.event.post(pygame.event.Event(pygame.QUIT))

BIN
game/resources/counter.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 227 KiB

Binary file not shown.