Cowyeet/cowyeet.py
2023-11-12 16:58:39 +01:00

214 lines
4.5 KiB
Python

#!/usr/bin/python3
import os
import pygame
import numpy
from tools import pg # if you import pg from tools, you dont need to init pygame.
from tools.file_dict import FileDict
# 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)
pygame.display.set_caption("Cowyeet 2.0")
if os.path.isfile("settings.txt"):
settings = FileDict("settings.txt")
else:
settings = FileDict()
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()
# variables
nero = (40, 40, 40) # colors (color names by https://www.color-blindness.com/color-name-hue/)
dim_gray = (100, 100, 100)
white_smoke = (240, 240, 240)
buttons = [] # misc
text_buttons = []
active_buttons = []
last_frame_mouse_pressed = False
page = "main_menu"
level = None
# pygame objects
clock = pygame.time.Clock() # misc
mouse = pygame.mouse
bigger_default_font = pygame.font.SysFont("ubuntu", 32) # fonts
# texts
choose_level_text = bigger_default_font.render("Choose a level:", True, white_smoke)
# 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
return center_x(width), center_y(height)
# other functions that are needed in button definition
def start_level(lvl: int):
global level
page_switch("ingame")
level = lvl
load_level(1)
# buttons
buttons.append(pg.TextButton("Start", center, screen, lambda: page_switch("level_selector"), text_color=white_smoke, bg_color=dim_gray, font=bigger_default_font))
start_button = len(buttons) - 1
text_buttons.append(start_button)
buttons.append(pg.TextButton("1", (128, 128), screen, lambda: start_level(1), text_color=white_smoke, bg_color=dim_gray, font=bigger_default_font, padding=(17, 8)))
lvl_one_button = len(buttons) - 1
text_buttons.append(lvl_one_button)
def close():
global running
running = False
def window_size_reload(new_size):
for button in text_buttons:
buttons[button].update()
settings["win_size"] = new_size
def load_level(lvl):
level_data = __import__("data.levels.1")
def main_menu_page():
global active_buttons
active_buttons = [start_button]
buttons[start_button].blit()
def level_selector_page():
global active_buttons
active_buttons = [lvl_one_button]
buttons[lvl_one_button].blit()
screen.blit(choose_level_text, (screen.get_width() / 2 - choose_level_text.get_width() / 2, 16))
def page_selector():
if page == "main_menu":
main_menu_page()
elif page == "level_selector":
level_selector_page()
def page_switch(new_page: str=None):
global active_buttons
global page
if not new_page is None:
page = new_page
# for button in buttons:
# button.active = False
#
# for button in active_buttons:
# button.active = True
def get_events():
global last_frame_mouse_pressed
for event in pygame.event.get():
if event.type == pygame.QUIT:
close()
return
if event.type == pygame.MOUSEBUTTONDOWN:
pressed = mouse.get_pressed()
pos = mouse.get_pos()
if not last_frame_mouse_pressed:
for button in active_buttons:
buttons[button].check(pos, pressed)
last_frame_mouse_pressed = True
if event.type == pygame.VIDEORESIZE:
window_size_reload(event.size)
def loop():
global last_frame_mouse_pressed
screen.fill(nero)
get_events()
if not running:
return
pressed = mouse.get_pressed()
if not pressed[0] and not pressed[1] and not pressed[2]:
last_frame_mouse_pressed = False
page_selector()
pygame.display.update()
clock.tick(FPS)
# loading completed
screen = pygame.display.set_mode(settings["win_size"], flags=pygame.RESIZABLE)
window_size_reload(settings["win_size"])
screen.fill(nero)
pygame.display.update()
page_switch("main_menu")
running = True
while running:
loop()
pygame.quit()
if SAVE_SETTINGS_ON_EXIT:
settings.save()