2023-11-05 19:44:35 +01:00
|
|
|
#!/usr/bin/python3
|
|
|
|
|
|
|
|
import os
|
|
|
|
import pygame
|
|
|
|
from tools import pg # if you import pg from tools, you dont need to init pygame.
|
|
|
|
from tools.settings import Settings
|
|
|
|
|
|
|
|
|
|
|
|
# integrated settings (I dont trust my own settings class.)
|
|
|
|
DEFAULT_WINDOW_SIZE = (800, 600)
|
|
|
|
FPS = 60
|
|
|
|
SAVE_SETTINGS_ON_EXIT = True
|
|
|
|
|
|
|
|
|
|
|
|
# some initializing
|
|
|
|
pygame.init()
|
|
|
|
screen = pygame.display.set_mode(DEFAULT_WINDOW_SIZE)
|
2023-11-09 17:20:51 +01:00
|
|
|
pygame.display.set_caption("Cowyeet 2.0")
|
2023-11-05 19:44:35 +01:00
|
|
|
|
|
|
|
if os.path.isfile("settings.txt"):
|
|
|
|
settings = Settings("settings.txt")
|
|
|
|
|
|
|
|
else:
|
|
|
|
settings = Settings()
|
|
|
|
settings.path = "settings.txt"
|
|
|
|
settings["win_size"] = DEFAULT_WINDOW_SIZE
|
|
|
|
settings.save()
|
|
|
|
|
|
|
|
# loading screen
|
|
|
|
default_font = pygame.font.SysFont("ubuntu", 16)
|
|
|
|
loading_text = default_font.render("Loading...", True, (240, 240, 240))
|
|
|
|
|
|
|
|
screen.fill((40, 40, 40))
|
|
|
|
screen.blit(loading_text, (400 - loading_text.get_width() / 2, 300 - loading_text.get_height() / 2))
|
|
|
|
pygame.display.update()
|
|
|
|
|
|
|
|
|
2023-11-09 17:20:51 +01:00
|
|
|
# variables
|
|
|
|
nero = (40, 40, 40) # colors (color names by https://www.color-blindness.com/color-name-hue/)
|
2023-11-05 19:44:35 +01:00
|
|
|
dim_gray = (100, 100, 100)
|
|
|
|
white_smoke = (240, 240, 240)
|
|
|
|
|
2023-11-09 17:20:51 +01:00
|
|
|
buttons = [] # misc
|
|
|
|
text_buttons = []
|
|
|
|
active_buttons = []
|
|
|
|
last_frame_mouse_pressed = False
|
|
|
|
page = "main_menu"
|
|
|
|
|
2023-11-05 19:44:35 +01:00
|
|
|
|
|
|
|
# pygame objects
|
2023-11-09 17:20:51 +01:00
|
|
|
clock = pygame.time.Clock() # misc
|
|
|
|
mouse = pygame.mouse
|
|
|
|
|
|
|
|
bigger_default_font = pygame.font.SysFont("ubuntu", 32) # fonts
|
|
|
|
|
|
|
|
|
|
|
|
# coordinate calculations
|
|
|
|
def center_x(width: int):
|
|
|
|
return screen.get_width() / 2 - width / 2
|
|
|
|
|
|
|
|
|
|
|
|
def center_y(height: int):
|
|
|
|
return screen.get_height() / 2 - height / 2
|
|
|
|
|
|
|
|
|
|
|
|
def center(size):
|
|
|
|
width, height = size
|
2023-11-05 19:44:35 +01:00
|
|
|
|
2023-11-09 17:20:51 +01:00
|
|
|
return center_x(width), center_y(height)
|
2023-11-05 19:44:35 +01:00
|
|
|
|
|
|
|
|
|
|
|
# buttons
|
2023-11-09 17:20:51 +01:00
|
|
|
start_button = pg.TextButton("Start", center, screen, lambda: page_switch("game"), text_color=white_smoke, bg_color=dim_gray, font=bigger_default_font)
|
|
|
|
buttons.append(start_button)
|
|
|
|
text_buttons.append(start_button)
|
2023-11-05 19:44:35 +01:00
|
|
|
|
|
|
|
|
|
|
|
def close():
|
|
|
|
global running
|
|
|
|
running = False
|
|
|
|
|
|
|
|
|
2023-11-09 17:20:51 +01:00
|
|
|
def window_size_reload(new_size):
|
|
|
|
for button in text_buttons:
|
|
|
|
button.update()
|
|
|
|
|
|
|
|
|
|
|
|
def main_menu_page():
|
|
|
|
start_button.blit(screen)
|
|
|
|
|
|
|
|
|
|
|
|
def page_selector():
|
|
|
|
if page == "main_menu":
|
|
|
|
main_menu_page()
|
|
|
|
|
|
|
|
|
|
|
|
def page_switch(new_page: str=None):
|
|
|
|
global active_buttons
|
|
|
|
global page
|
|
|
|
|
|
|
|
if not new_page is None:
|
|
|
|
page = new_page
|
|
|
|
|
|
|
|
if page == "main_menu":
|
|
|
|
active_buttons = [start_button]
|
|
|
|
|
|
|
|
else:
|
|
|
|
print("Error: Page not found.")
|
|
|
|
|
|
|
|
# for button in buttons:
|
|
|
|
# button.active = False
|
|
|
|
#
|
|
|
|
# for button in active_buttons:
|
|
|
|
# button.active = True
|
|
|
|
|
|
|
|
|
2023-11-05 19:44:35 +01:00
|
|
|
def get_events():
|
2023-11-09 17:20:51 +01:00
|
|
|
global last_frame_mouse_pressed
|
|
|
|
|
2023-11-05 19:44:35 +01:00
|
|
|
for event in pygame.event.get():
|
|
|
|
if event.type == pygame.QUIT:
|
|
|
|
close()
|
|
|
|
return
|
|
|
|
|
2023-11-09 17:20:51 +01:00
|
|
|
if event.type == pygame.MOUSEBUTTONDOWN:
|
|
|
|
pressed = mouse.get_pressed()
|
|
|
|
pos = mouse.get_pos()
|
|
|
|
|
|
|
|
if not last_frame_mouse_pressed:
|
|
|
|
for button in active_buttons:
|
|
|
|
button.check(pos, pressed)
|
|
|
|
|
|
|
|
last_frame_mouse_pressed = True
|
|
|
|
|
|
|
|
if event.type == pygame.VIDEORESIZE:
|
|
|
|
|
|
|
|
window_size_reload(event.size)
|
|
|
|
|
2023-11-05 19:44:35 +01:00
|
|
|
|
|
|
|
def loop():
|
2023-11-09 17:20:51 +01:00
|
|
|
global last_frame_mouse_pressed
|
|
|
|
|
2023-11-05 19:44:35 +01:00
|
|
|
screen.fill(nero)
|
|
|
|
|
|
|
|
get_events()
|
|
|
|
|
|
|
|
if not running:
|
|
|
|
return
|
|
|
|
|
2023-11-09 17:20:51 +01:00
|
|
|
pressed = mouse.get_pressed()
|
|
|
|
if not pressed[0] and not pressed[1] and not pressed[2]:
|
|
|
|
last_frame_mouse_pressed = False
|
|
|
|
|
|
|
|
page_selector()
|
2023-11-05 19:44:35 +01:00
|
|
|
|
|
|
|
pygame.display.update()
|
|
|
|
|
|
|
|
clock.tick(FPS)
|
|
|
|
|
|
|
|
|
|
|
|
# loading completed
|
|
|
|
|
|
|
|
screen = pygame.display.set_mode(settings["win_size"], flags=pygame.RESIZABLE)
|
2023-11-09 17:20:51 +01:00
|
|
|
screen.fill(nero)
|
|
|
|
pygame.display.update()
|
|
|
|
|
|
|
|
page_switch()
|
2023-11-05 19:44:35 +01:00
|
|
|
|
|
|
|
running = True
|
|
|
|
|
|
|
|
while running:
|
|
|
|
loop()
|
|
|
|
|
|
|
|
pygame.quit()
|
|
|
|
|
|
|
|
if SAVE_SETTINGS_ON_EXIT:
|
|
|
|
settings.save()
|