2023-06-17 01:33:16 -04:00

113 lines
3.4 KiB
Python

import pygame
import sys
# Colors
LIGHT_SKY_BLUE = (135, 206, 235)
BLACK = (0, 0, 0)
PEACH = (255, 218, 185)
# Screen dimensions
SCREEN_WIDTH = 1366
SCREEN_HEIGHT = 768
# Button dimensions
BUTTON_WIDTH = 200
BUTTON_HEIGHT = 100
# Initialize Pygame
pygame.init()
# Create the screen
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT), pygame.FULLSCREEN)
# Fonts
title_font = pygame.font.SysFont("sans-serif", 64)
subtitle_font = pygame.font.SysFont("serif", 32, italic=True)
button_font = pygame.font.SysFont("sans-serif", 24)
class Button:
def __init__(self, x, y, width, height, text, font, color, action=None):
self.x = x
self.y = y
self.width = width
self.height = height
self.text = text
self.font = font
self.color = color
self.action = action
def display(self):
pygame.draw.rect(screen, self.color, (self.x - self.width/2, self.y - self.height/2, self.width, self.height))
text_surface = self.font.render(self.text, True, BLACK)
text_rect = text_surface.get_rect(center=(self.x, self.y))
screen.blit(text_surface, text_rect)
def is_hovered(self):
mouse_pos = pygame.mouse.get_pos()
return pygame.Rect(self.x - self.width / 2, self.y - self.height / 2, self.width, self.height).collidepoint(mouse_pos)
def is_pressed(self):
return self.is_hovered() and pygame.mouse.get_pressed()[0]
def handle_event(self, event):
if event.type == pygame.MOUSEBUTTONUP and self.is_hovered():
if self.action:
self.action()
class Screen:
def __init__(self, buttons, background_color):
self.buttons = buttons
self.background_color = background_color
def handle_event(self, event):
for button in self.buttons:
button.handle_event(event)
def display(self):
screen.fill(self.background_color)
for button in self.buttons:
button.display()
# Define the buttons and screens
buttons_main_menu = [
Button(SCREEN_WIDTH // 3, SCREEN_HEIGHT // 2, BUTTON_WIDTH, BUTTON_HEIGHT, "create item", button_font, PEACH),
Button(2 * SCREEN_WIDTH // 3, SCREEN_HEIGHT // 2, BUTTON_WIDTH, BUTTON_HEIGHT, "lookup item", button_font, PEACH),
Button(50, 50, 50, 50, "H", button_font, PEACH)
]
buttons_create_item_screen = [
Button(SCREEN_WIDTH // 3, SCREEN_HEIGHT // 2, BUTTON_WIDTH, BUTTON_HEIGHT, "yes", button_font, PEACH),
Button(2 * SCREEN_WIDTH // 3, SCREEN_HEIGHT // 2, BUTTON_WIDTH, BUTTON_HEIGHT, "no", button_font, PEACH),
Button(50, SCREEN_HEIGHT - 50, 50, 50, "<", button_font, PEACH),
Button(50, 50, 50, 50, "H", button_font, PEACH)
]
main_menu_screen = Screen(buttons_main_menu, LIGHT_SKY_BLUE)
create_item_screen = Screen(buttons_create_item_screen, LIGHT_SKY_BLUE)
# Set the actions of the buttons
def go_to_create_item_screen():
global current_screen
current_screen = create_item_screen
buttons_main_menu[0].action = go_to_create_item_screen
# Game loop
clock = pygame.time.Clock()
running = True
current_screen = main_menu_screen
while running:
clock.tick(60) # Limit the frame rate to 60 FPS
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.quit()
sys.exit()
else:
current_screen.handle_event(event)
# Render screen
current_screen.display()
pygame.display.flip()