You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

125 lines
4.3 KiB

#!/usr/bin/env python3
import sys
import pygame
class MenuButton:
'''Very nice buttons'''
def __init__(self, x, y, size, label, lpos):
self.color = (255,255,0)
self.x = x
self.y = y
self.size = size
# info about the label
self.font = pygame.font.SysFont("Sans Serif", 50)
self.label = self.font.render(label, 1, (0,0,0))
self.lpos = lpos
# button hitbox
self.rect = pygame.rect.Rect((self.x, self.y), self.size)
def get_pos(self):
return (self.x, self.y)
def render(self):
'''render button and return its surface'''
surface = pygame.Surface(self.size)
pygame.draw.rect(surface, (0,0,0), pygame.rect.Rect((0,0), self.size))
pygame.draw.rect(surface, self.color, pygame.rect.Rect((4,4), (self.size[0]-8, self.size[1]-8)))
surface.blit(self.label, self.lpos)
return surface
def main(screen, settings):
'''main menu with start and exit buttons'''
clock = pygame.time.Clock()
# images used in menu
bg_img = pygame.image.load("assets/menu/bg.png")
menu_logo = pygame.image.load("assets/logo.png")
# buttons
start_button = MenuButton(540, 300, (200, 80), "Start", (58,25))
exit_button = MenuButton(560, 400, (160, 60), "Exit", (46,12))
# draw stuff to screen (only once unlike in game where it is done every frame)
screen.blit(bg_img, (0,0))
screen.blit(menu_logo, (265, 100))
screen.blit(start_button.render(), start_button.get_pos())
screen.blit(exit_button.render(), exit_button.get_pos())
pygame.display.update()
# mainloop of menu
while True:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit(0)
elif event.type == pygame.KEYDOWN:
# start game when ENTER or SPACE are pressed
if event.key == pygame.K_RETURN or event.key == pygame.K_SPACE:
return
elif event.type == pygame.MOUSEBUTTONDOWN:
# check if a button was clicked
if start_button.rect.collidepoint(event.pos):
return
elif exit_button.rect.collidepoint(event.pos):
sys.exit(0)
def finish(screen, settings, results):
'''finish screen with results and buttons, background has last frame of game (without player)'''
clock = pygame.time.Clock()
# buttons
start_button = MenuButton(540, 400, (200, 80), "Restart", (45,25))
exit_button = MenuButton(560, 500, (160, 60), "Exit", (46,12))
# fonts and labels for results table
font = pygame.font.SysFont("Sans Serif", 50)
res_label = font.render("Results:", 1, (255, 255, 255))
small_font = pygame.font.SysFont("Sans Serif", 30)
satis_label = small_font.render("The teachers aren't satisfied.", 1, (255, 255, 255))
# render text rows for the table
rendered = []
table_size = 160
for row in results:
table_size += 40
rendered.append(small_font.render(row + " : " + str(results[row]), 1, (255, 255, 255)))
# create table
table = pygame.Surface((400, table_size))
table.fill((0,0,0))
pygame.draw.rect(table, (54, 52, 48), pygame.rect.Rect((4,4), (400-8, table_size-8)))
table.blit(res_label, (130, 30))
table.blit(satis_label, (70, table_size-50))
# blit rendered text to table
for i in range(len(rendered)):
table.blit(rendered[i], (10, i*40+80))
# blit stuff to screen (again, only once like in main menu)
screen.blit(table, (440,100))
screen.blit(start_button.render(), start_button.get_pos())
screen.blit(exit_button.render(), exit_button.get_pos())
pygame.display.update()
# mainloop of finish screen
while True:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit(0)
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_RETURN or event.key == pygame.K_SPACE:
return
elif event.type == pygame.MOUSEBUTTONDOWN:
if start_button.rect.collidepoint(event.pos):
return
elif exit_button.rect.collidepoint(event.pos):
sys.exit(0)