Code formatting

This commit is contained in:
akp 2025-03-23 18:49:40 +00:00
parent c57a4fb30b
commit dae609288a
No known key found for this signature in database
GPG key ID: CF8D58F3DEB20755
6 changed files with 193 additions and 101 deletions

View file

@ -3,7 +3,8 @@ import menu
import util
import gameplay
import resources
def main():
pygame.init()
screen = pygame.display.set_mode((1280, 720))

View file

@ -14,7 +14,9 @@ class SurfaceButton:
self.already_pressed = False
self.already_collided = False
def blit_onto(self, output_surface: pygame.SurfaceType, pos: tuple[int, int]) -> bool:
def blit_onto(
self, output_surface: pygame.SurfaceType, pos: tuple[int, int]
) -> bool:
"""
blits the button onto a surface
@ -22,16 +24,21 @@ class SurfaceButton:
:param pos: position to place the button
:return: if the button is clicked or not - is debounced
"""
output_surface.blit(
self.surface,
pos
)
output_surface.blit(self.surface, pos)
does_mouse_collide = pygame.Rect(*pos, *self.surface.get_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
pygame.mouse.set_cursor(*(pygame.cursors.broken_x if self.already_collided else pygame.cursors.arrow))
pygame.mouse.set_cursor(
*(
pygame.cursors.broken_x
if self.already_collided
else pygame.cursors.arrow
)
)
if does_mouse_collide:
(lmb_pressed, _, _) = pygame.mouse.get_pressed(3)
@ -60,7 +67,7 @@ def text_button(text, size, font=resources.FONT_SM) -> SurfaceButton:
(size[1] - text_height) / 2,
)
pygame.draw.rect(surface, 0xdf3062, pygame.Rect((0, 0), size), border_radius=5)
pygame.draw.rect(surface, 0xDF3062, pygame.Rect((0, 0), size), border_radius=5)
surface.blit(
font.render(text, True, "black"),
text_pos,
@ -77,13 +84,18 @@ class Note:
self.surface = pygame.Surface(base.get_size())
self.text = text
self.surface.blit(base, (0, 0))
util.render_text_centred_at(text, resources.FONT_SM, 0x000000, base.get_size()[0] / 2, 5, self.surface, base.get_size()[0] - 20)
util.render_text_centred_at(
text,
resources.FONT_SM,
0x000000,
base.get_size()[0] / 2,
5,
self.surface,
base.get_size()[0] - 20,
)
def blit_onto(self, output_surface: pygame.Surface, pos: tuple[int, int]):
output_surface.blit(
self.surface,
pos
)
output_surface.blit(self.surface, pos)
class Character:
@ -109,11 +121,17 @@ class Character:
def blit_onto(self, output_surface: pygame.SurfaceType, pos: tuple[int, int]):
output_surface.blit(self.torso, util.add_coord(pos, (0, self.headpos)))
torso_centerpoint = util.center_within(self.torso.get_size(), self.head.get_size())
torso_centerpoint = util.center_within(
self.torso.get_size(), self.head.get_size()
)
output_surface.blit(self.head, util.add_coord(pos, (torso_centerpoint[0], 0)))
if self.head not in resources.CHARACTER_HEADS[-2:]:
output_surface.blit(self.glasses, util.add_coord(pos, (torso_centerpoint[0], 50)))
output_surface.blit(self.hair, util.add_coord(pos, (torso_centerpoint[0], 0)))
output_surface.blit(
self.glasses, util.add_coord(pos, (torso_centerpoint[0], 50))
)
output_surface.blit(
self.hair, util.add_coord(pos, (torso_centerpoint[0], 0))
)
if self.text is not None:
if self.note is None or hash(self.text) != self.text_hash:

View file

@ -18,11 +18,21 @@ class GamePlay:
def __init__(self, surface):
self.surface = surface
self.menu_button = components.text_button("Menu", (250, 50), font=resources.FONT)
self.counter_button = components.text_button("Counter", (250, 50), font=resources.FONT)
self.food_button = components.text_button("Prepare/Next", (250, 50), font=resources.FONT)
self.getorder_button = components.text_button("Get Order", (250, 50), font=resources.FONT)
self.serve_button = components.text_button("Serve", (250, 50), font=resources.FONT)
self.menu_button = components.text_button(
"Menu", (250, 50), font=resources.FONT
)
self.counter_button = components.text_button(
"Counter", (250, 50), font=resources.FONT
)
self.food_button = components.text_button(
"Prepare/Next", (250, 50), font=resources.FONT
)
self.getorder_button = components.text_button(
"Get Order", (250, 50), font=resources.FONT
)
self.serve_button = components.text_button(
"Serve", (250, 50), font=resources.FONT
)
self.drawer_button = components.SurfaceButton(resources.DRAWER_TRANS)
self.draweropen_button = components.SurfaceButton(resources.DRAWER_OPEN)
self.ham_button = components.SurfaceButton(resources.TUB_HAM_SPRITE)
@ -41,15 +51,20 @@ class GamePlay:
self.generate_character()
def generate_character(self):
self.character = components.Character(random.choice(resources.SCRIPT_ASK_SANDWICH))
self.character_pos = (400 + random.randint(0, 100), 100 + random.randint(25, 100))
self.character = components.Character(
random.choice(resources.SCRIPT_ASK_SANDWICH)
)
self.character_pos = (
400 + random.randint(0, 100),
100 + random.randint(25, 100),
)
def displaysandwich(screen, sandwich):
position = 125
picposition = 100
start = 0
for i in sandwich:
screen.blit(
screen.blit(
resources.FONT.render(i, True, (0, 0, 0)),
(1110, position),
)
@ -76,40 +91,38 @@ class GamePlay:
)
elif i == "Bread":
screen.blit(
resources.TICKET_BOTTOM_SPRITE_3X ,
resources.TICKET_BOTTOM_SPRITE_3X,
(975, picposition),
)
elif i == "Cheese":
screen.blit(
resources.TICKET_CHEESE_SPRITE_3X ,
resources.TICKET_CHEESE_SPRITE_3X,
(985, picposition + 15),
)
picposition +=75
picposition += 75
position += 75
def sandwich(self, screen):
#fillings = ["Lettuce", "Ham", "Tomatoes"]
# fillings = ["Lettuce", "Ham", "Tomatoes"]
fillings = ["Lettuce", "Ham", "Tomatoes", "Cheese"]
amountOfFilling = random.randrange(1,6)
amountOfFilling = random.randrange(1, 6)
sandwich = ["Bread"]
for _ in range(amountOfFilling):
filling = random.randrange(0,4)
#filling = random.randrange(0,3)
filling = random.randrange(0, 4)
# filling = random.randrange(0,3)
sandwich.append(fillings[filling])
sandwich.append("Bread")
self.correctsandwich = sandwich
GamePlay.displaysandwich(screen, sandwich)
def do(self):
#print(self.status)
# print(self.status)
if self.start == True:
self.surface.blit(resources.BACKGROUND_SCREEN_IMAGE, (0, 0))
self.character.blit_onto(self.surface, self.character_pos)
self.surface.blit(resources.COUNTER_SCREEN_IMAGE, (0, 0))
self.start = False
self.newOrder = False
if self.menu_button.blit_onto(self.surface, (15, 5)):
pygame.event.post(util.make_transition_event("menu"))
@ -123,7 +136,7 @@ class GamePlay:
self.surface.blit(resources.SUB_BOTTOM_SPRITE_10X, (300, 300))
print("click get food serve")
elif self.status == "Counter" and self.sandwichmade == False:
if self.drawer_button.blit_onto(self.surface, (755, 465)):
print("Drawer")
self.surface.blit(resources.DRAWER_OPEN, (755, 465))
@ -145,15 +158,14 @@ class GamePlay:
self.surface.blit(resources.COUNTER_SCREEN_IMAGE, (0, 0))
self.status = "Counter"
self.newOrder = True
print ("click counter from food")
print("click counter from food")
elif self.status == "Get Order":
if self.getorder_button.blit_onto(self.surface, (1000, 5)):
self.surface.blit(resources.BACKGROUND_SCREEN_IMAGE, (0, 0))
self.surface.blit(resources.COUNTER_SCREEN_IMAGE, (0, 0))
GamePlay.sandwich(self, self.surface)
self.status = "Counter"
print ("click get order ")
print("click get order ")
if self.status == "Food":
if self.serve_button.blit_onto(self.surface, (10, 660)):
@ -162,70 +174,80 @@ class GamePlay:
self.surface.blit(resources.COUNTER_SCREEN_IMAGE, (0, 0))
self.surface.blit(resources.SUB_WRAPPED_SPRITE_3X, (350, 300))
GamePlay.displaysandwich(self.surface, self.correctsandwich)
#serve
# serve
if self.correctsandwich == list(reversed(self.madesandwich)):
if self.suspicion > 0:
self.suspicion -= 1
self.surface.blit(
resources.FONT.render("Correct!!", True, (0, 0, 0)),
(10, 60),
self.surface.blit(
resources.FONT.render("Correct!!", True, (0, 0, 0)),
(10, 60),
)
self.character.text = random.choice(resources.SCRIPT_TAKE_SANDWICH)
else:
self.suspicion += 1
self.surface.blit(
resources.FONT.render("Incorrect!!", True, (0, 0, 0)),
(10, 60),
)
self.character.text = random.choice(resources.SCRIPT_TAKE_SANDWICH_RUDE)
self.character.blit_onto(
self.surface,
self.character_pos
)
self.surface.blit(resources.COUNTER_SCREEN_IMAGE, (0, 0)) # so the customer is under the counter
self.suspicion += 1
self.surface.blit(
resources.FONT.render("Incorrect!!", True, (0, 0, 0)),
(10, 60),
)
self.character.text = random.choice(
resources.SCRIPT_TAKE_SANDWICH_RUDE
)
self.character.blit_onto(self.surface, self.character_pos)
self.surface.blit(
resources.COUNTER_SCREEN_IMAGE, (0, 0)
) # so the customer is under the counter
message = "Suspicion Levels: " + str(self.suspicion)
self.surface.blit(
self.surface.blit(
resources.FONT.render(message, True, (0, 0, 0)),
(10, 85),
)
)
if self.suspicion == 3:
pygame.event.post(util.make_transition_event("gameover"))
self.madesandwich = ["Bread"]
self.correctsandwich = []
self.sandwichmade = False
#self.status = "Get Order"
#change to counter button
#if suspician is over 5 then ooooooo game over?
# self.status = "Get Order"
# change to counter button
# if suspician is over 5 then ooooooo game over?
if self.status == "Counter" and self.newOrder == True:
GamePlay.sandwich(self, self.surface)
self.newOrder = False
elif self.status == "Food" :
elif self.status == "Food":
GamePlay.displaysandwich(self.surface, self.correctsandwich)
variationlr = random.randrange(0,15)
variationud = random.randrange(0,15)
variationlr = random.randrange(0, 15)
variationud = random.randrange(0, 15)
if self.ham_button.blit_onto(self.surface, (100, 200)):
self.surface.blit(resources.SUB_HAM_SPRITE_10X, (300 + variationlr, 300 + variationud))
self.surface.blit(
resources.SUB_HAM_SPRITE_10X, (300 + variationlr, 300 + variationud)
)
self.madesandwich.append("Ham")
elif self.tomato_button.blit_onto(self.surface, (300, 200)):
self.surface.blit(resources.SUB_TOMATO_SPRITE_10X, (300 + variationlr, 300 + variationud))
self.surface.blit(
resources.SUB_TOMATO_SPRITE_10X,
(300 + variationlr, 300 + variationud),
)
self.madesandwich.append("Tomatoes")
elif self.lettuce_button.blit_onto(self.surface, (500, 200)):
self.surface.blit(resources.SUB_LETTUCE_SPRITE_10X, (300 + variationlr, 300 + variationud))
self.surface.blit(
resources.SUB_LETTUCE_SPRITE_10X,
(300 + variationlr, 300 + variationud),
)
self.madesandwich.append("Lettuce")
elif self.cheese_button.blit_onto(self.surface, (700, 200)):
self.surface.blit(resources.SUB_CHEESE_SPRITE_10X, (330 + variationlr, 335 + variationud))
self.surface.blit(
resources.SUB_CHEESE_SPRITE_10X,
(330 + variationlr, 335 + variationud),
)
self.madesandwich.append("Cheese")
elif self.top_button.blit_onto(self.surface, (800, 200)):
self.surface.blit(resources.SUB_TOP_SPRITE_10X, (300 + variationlr, 300 + variationud))
self.surface.blit(
resources.SUB_TOP_SPRITE_10X, (300 + variationlr, 300 + variationud)
)
self.madesandwich.append("Bread")
self.sandwichmade == True

View file

@ -12,8 +12,12 @@ class Menu:
def __init__(self, surface):
self.surface = surface
self.play_button = components.text_button("Play", (250, 50), font=resources.FONT)
self.quit_button = components.text_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))
@ -22,16 +26,25 @@ class Menu:
self.surface.blit(
resources.FONT_XL.render(title_text, True, "black"),
util.add_coord(util.center_within(self.surface.get_size(), resources.FONT_XL.size(title_text)), (0, -200)),
util.add_coord(
util.center_within(
self.surface.get_size(), resources.FONT_XL.size(title_text)
),
(0, -200),
),
)
gap = 15
central_button_block = util.center_within(self.surface.get_size(), (250, (50 * 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, 50 + 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))
@ -47,18 +60,34 @@ class GameOver:
def do(self):
# self.surface.blit(resources.SPLASH_SCREEN_IMAGE, (0, 0))
self.surface.fill(0xff0000)
self.surface.fill(0xFF0000)
title_text = "YOU LOSE"
self.surface.blit(
resources.FONT_XL.render(title_text, True, "black"),
util.add_coord(util.center_within(self.surface.get_size(), resources.FONT_XL.size(title_text)), (0, -200)),
util.add_coord(
util.center_within(
self.surface.get_size(), resources.FONT_XL.size(title_text)
),
(0, -200),
),
)
util.render_text_centred_at(self.desc, resources.FONT, 0x000000, *util.add_coord(util.center_within(self.surface.get_size(), (1, 1)), (0, -100)), self.surface, self.surface.get_size()[0]*0.8)
util.render_text_centred_at(
self.desc,
resources.FONT,
0x000000,
*util.add_coord(
util.center_within(self.surface.get_size(), (1, 1)), (0, -100)
),
self.surface,
self.surface.get_size()[0] * 0.8,
)
gap = 15
central_button_block = util.center_within(self.surface.get_size(), (250, 50))
if self.quit_button.blit_onto(self.surface, util.add_coord(central_button_block, (0, 50 + 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))

View file

@ -20,7 +20,7 @@ PREPARE_SCREEN_IMAGE = pygame.image.load(RESOURCES_DIR / "prepare.png")
BACKGROUND_SCREEN_IMAGE = pygame.image.load(RESOURCES_DIR / "background.png")
SUB_WRAPPED_SPRITE = pygame.image.load(RESOURCES_DIR / "sprites" / "sub_wrapped.png")
SUB_WRAPPED_SPRITE_3X = pygame.transform.scale_by(SUB_WRAPPED_SPRITE, 5)
SUB_PAPER_SPRITE = pygame.image.load(RESOURCES_DIR / "sprites" / "sub_wrap.png")
SUB_PAPER_SPRITE = pygame.image.load(RESOURCES_DIR / "sprites" / "sub_wrap.png")
SUB_PAPER_SPRITE_3X = pygame.transform.scale_by(SUB_PAPER_SPRITE, 6)
SUB_TOMATO_SPRITE = pygame.image.load(RESOURCES_DIR / "sprites" / "sub_tomato.png")
@ -28,7 +28,9 @@ SUB_TOMATO_SPRITE_3X = pygame.transform.scale_by(SUB_TOMATO_SPRITE, 3)
SUB_TOMATO_SPRITE_10X = pygame.transform.scale_by(SUB_TOMATO_SPRITE, 10)
TUB_TOMATO_SPRITE = pygame.image.load(RESOURCES_DIR / "sprites" / "tub_tomato.png")
TUB_TOMATO_SPRITE_3X = pygame.transform.scale_by(TUB_TOMATO_SPRITE, 3)
TICKET_TOMATO_SPRITE = pygame.image.load(RESOURCES_DIR / "sprites" / "ticket_tomato.png")
TICKET_TOMATO_SPRITE = pygame.image.load(
RESOURCES_DIR / "sprites" / "ticket_tomato.png"
)
TICKET_TOMATO_SPRITE_3X = pygame.transform.scale_by(TICKET_TOMATO_SPRITE, 2)
SUB_HAM_SPRITE = pygame.image.load(RESOURCES_DIR / "sprites" / "sub_ham.png")
@ -45,7 +47,9 @@ SUB_LETTUCE_SPRITE_3X = pygame.transform.scale_by(SUB_LETTUCE_SPRITE, 3)
SUB_LETTUCE_SPRITE_10X = pygame.transform.scale_by(SUB_LETTUCE_SPRITE, 10)
TUB_LETTUCE_SPRITE = pygame.image.load(RESOURCES_DIR / "sprites" / "tub_lettuce.png")
TUB_LETTUCE_SPRITE_3X = pygame.transform.scale_by(TUB_LETTUCE_SPRITE, 3)
TICKET_LETTUCE_SPRITE = pygame.image.load(RESOURCES_DIR / "sprites" / "ticket_lettuce.png")
TICKET_LETTUCE_SPRITE = pygame.image.load(
RESOURCES_DIR / "sprites" / "ticket_lettuce.png"
)
TICKET_LETTUCE_SPRITE_3X = pygame.transform.scale_by(TICKET_LETTUCE_SPRITE, 2)
SUB_CHEESE_SPRITE = pygame.image.load(RESOURCES_DIR / "sprites" / "sub_cheese.png")
@ -53,7 +57,9 @@ SUB_CHEESE_SPRITE_3X = pygame.transform.scale_by(SUB_CHEESE_SPRITE, 3)
SUB_CHEESE_SPRITE_10X = pygame.transform.scale_by(SUB_CHEESE_SPRITE, 6)
TUB_CHEESE_SPRITE = pygame.image.load(RESOURCES_DIR / "sprites" / "tub_cheese.png")
TUB_CHEESE_SPRITE_3X = pygame.transform.scale_by(TUB_CHEESE_SPRITE, 3)
TICKET_CHEESE_SPRITE = pygame.image.load(RESOURCES_DIR / "sprites" / "ticket_cheese.png")
TICKET_CHEESE_SPRITE = pygame.image.load(
RESOURCES_DIR / "sprites" / "ticket_cheese.png"
)
TICKET_CHEESE_SPRITE_3X = pygame.transform.scale_by(TICKET_CHEESE_SPRITE, 2)
@ -68,7 +74,9 @@ TICKET_TOP_SPRITE_3X = pygame.transform.scale_by(TICKET_TOP_SPRITE, 3)
SUB_BOTTOM_SPRITE = pygame.image.load(RESOURCES_DIR / "sprites" / "sub_bottom.png")
SUB_BOTTOM_SPRITE_10X = pygame.transform.scale_by(SUB_BOTTOM_SPRITE, 10)
TICKET_BOTTOM_SPRITE = pygame.image.load(RESOURCES_DIR / "sprites" / "ticket_bottom.png")
TICKET_BOTTOM_SPRITE = pygame.image.load(
RESOURCES_DIR / "sprites" / "ticket_bottom.png"
)
TICKET_BOTTOM_SPRITE_3X = pygame.transform.scale_by(TICKET_BOTTOM_SPRITE, 3)
CHARACTER_TORSOS = [
@ -89,7 +97,9 @@ CHARACTER_GLASSES = [
pygame.image.load(RESOURCES_DIR / "sprites" / "character_glasses_1.png"),
pygame.image.load(RESOURCES_DIR / "sprites" / "character_glasses_2.png"),
pygame.image.load(RESOURCES_DIR / "sprites" / "character_glasses_3.png"),
pygame.image.load(RESOURCES_DIR / "sprites" / "character_glasses_4.png"), # the blank one
pygame.image.load(
RESOURCES_DIR / "sprites" / "character_glasses_4.png"
), # the blank one
pygame.image.load(RESOURCES_DIR / "sprites" / "character_glasses_5.png"),
]
@ -97,12 +107,25 @@ CHARACTER_HAIR = [
pygame.image.load(RESOURCES_DIR / "sprites" / "character_hair_1.png"),
pygame.image.load(RESOURCES_DIR / "sprites" / "character_hair_2.png"),
pygame.image.load(RESOURCES_DIR / "sprites" / "character_hair_3.png"),
pygame.image.load(RESOURCES_DIR / "sprites" / "character_hair_4.png"), # the bald one
pygame.image.load(
RESOURCES_DIR / "sprites" / "character_hair_4.png"
), # the bald one
]
EMPTY_NOTE = pygame.image.load(RESOURCES_DIR / "empty_note.png")
SCRIPT_ASK_SANDWICH = open(RESOURCES_DIR / "scripts" / "ask-sandwich.txt").read().strip().splitlines()
SCRIPT_TAKE_SANDWICH = open(RESOURCES_DIR / "scripts" / "take-sandwich.txt").read().strip().splitlines()
SCRIPT_TAKE_SANDWICH_RUDE = open(RESOURCES_DIR / "scripts" / "take-sandwich-rude.txt").read().strip().splitlines()
SCRIPT_GAME_OVER = open(RESOURCES_DIR / "scripts" / "game-over.txt").read().strip().splitlines()
SCRIPT_ASK_SANDWICH = (
open(RESOURCES_DIR / "scripts" / "ask-sandwich.txt").read().strip().splitlines()
)
SCRIPT_TAKE_SANDWICH = (
open(RESOURCES_DIR / "scripts" / "take-sandwich.txt").read().strip().splitlines()
)
SCRIPT_TAKE_SANDWICH_RUDE = (
open(RESOURCES_DIR / "scripts" / "take-sandwich-rude.txt")
.read()
.strip()
.splitlines()
)
SCRIPT_GAME_OVER = (
open(RESOURCES_DIR / "scripts" / "game-over.txt").read().strip().splitlines()
)

View file

@ -8,7 +8,9 @@ def add_coord(a: tuple[int, int], b: tuple[int, int]) -> tuple[int, int]:
)
def center_within(canvas_size: tuple[int, int], thing_size: tuple[int, int]) -> tuple[int, int]:
def center_within(
canvas_size: tuple[int, int], thing_size: tuple[int, int]
) -> tuple[int, int]:
return (
int((canvas_size[0] - thing_size[0]) / 2),
int((canvas_size[1] - thing_size[1]) / 2),
@ -19,10 +21,7 @@ TRANSITION_EVENT_TYPE = 50123
def make_transition_event(to: str) -> pygame.event.Event:
return pygame.event.Event(
TRANSITION_EVENT_TYPE,
{"to": to}
)
return pygame.event.Event(TRANSITION_EVENT_TYPE, {"to": to})
def render_text_centred_at(text, font, colour, x, y, screen, allowed_width):
@ -36,12 +35,12 @@ def render_text_centred_at(text, font, colour, x, y, screen, allowed_width):
line_words = []
while len(words) > 0:
line_words.append(words.pop(0))
fw, fh = font.size(' '.join(line_words + words[:1]))
fw, fh = font.size(" ".join(line_words + words[:1]))
if fw > allowed_width:
break
# add a line consisting of those words
line = ' '.join(line_words)
line = " ".join(line_words)
lines.append(line)
# now we've split our text into lines that fit into the width, actually
@ -60,4 +59,4 @@ def render_text_centred_at(text, font, colour, x, y, screen, allowed_width):
font_surface = font.render(line, True, colour)
screen.blit(font_surface, (tx, ty))
y_offset += fh
y_offset += fh