2023-11-05 19:44:35 +01:00
|
|
|
#!/usr/bin/python3
|
|
|
|
|
|
|
|
import os
|
|
|
|
import pygame
|
2023-11-12 16:58:39 +01:00
|
|
|
import numpy
|
2023-11-05 19:44:35 +01:00
|
|
|
from tools import pg # if you import pg from tools, you dont need to init pygame.
|
2023-11-11 20:28:00 +01:00
|
|
|
from tools.file_dict import FileDict
|
2023-11-05 19:44:35 +01:00
|
|
|
|
|
|
|
|
|
|
|
# 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"):
|
2023-11-11 20:28:00 +01:00
|
|
|
settings = FileDict("settings.txt")
|
2023-11-05 19:44:35 +01:00
|
|
|
|
|
|
|
else:
|
2023-11-11 20:28:00 +01:00
|
|
|
settings = FileDict()
|
2023-11-05 19:44:35 +01:00
|
|
|
settings.path = "settings.txt"
|
|
|
|
settings["win_size"] = DEFAULT_WINDOW_SIZE
|
2023-11-12 18:15:57 +01:00
|
|
|
settings["level_size_multiplier"] = 1
|
2023-11-05 19:44:35 +01:00
|
|
|
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)
|
2023-11-12 18:15:57 +01:00
|
|
|
summer_sky = (50, 200, 220)
|
2023-11-05 19:44:35 +01:00
|
|
|
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-12 15:38:08 +01:00
|
|
|
level = None
|
2023-11-12 18:15:57 +01:00
|
|
|
level_data = None
|
|
|
|
lvl_width = None
|
|
|
|
level_surface = None
|
2023-11-09 17:20:51 +01:00
|
|
|
|
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
|
|
|
|
|
2023-11-12 18:15:57 +01:00
|
|
|
choose_level_text = bigger_default_font.render("Choose a level:", True, white_smoke) # texts
|
|
|
|
|
|
|
|
texture_not_found = pygame.image.load("textures/texture_not_found.png")
|
|
|
|
texture_not_found = pygame.transform.scale(texture_not_found, (40 * settings["level_size_multiplier"], 40 * settings["level_size_multiplier"]))
|
|
|
|
|
|
|
|
|
|
|
|
def load_texture(path: str): # images
|
|
|
|
if os.path.isfile(path):
|
|
|
|
return pygame.image.load(path)
|
|
|
|
|
|
|
|
else:
|
|
|
|
return texture_not_found
|
|
|
|
|
|
|
|
|
|
|
|
def load_block_texture(path: str):
|
|
|
|
texture = load_texture(path)
|
|
|
|
|
|
|
|
return pygame.transform.scale(texture, (40 * settings["level_size_multiplier"], 40 * settings["level_size_multiplier"]))
|
|
|
|
|
|
|
|
|
2023-11-19 14:31:16 +01:00
|
|
|
icon_texture = load_texture("textures/icon.png")
|
|
|
|
full_icon_texture = load_texture("textures/icon_full.png")
|
|
|
|
full_icon_texture = pygame.transform.scale(full_icon_texture, (260, 90))
|
2023-11-12 18:15:57 +01:00
|
|
|
stone_block_texture = load_block_texture("textures/terrain/stone_01.png")
|
|
|
|
dirt_block_texture = load_block_texture("textures/terrain/dirt_01.png")
|
|
|
|
grass_block_texture = load_block_texture("textures/terrain/grass_01.png")
|
|
|
|
rock_block_texture = load_block_texture("textures/terrain/rock_01.png")
|
2023-11-19 14:31:16 +01:00
|
|
|
|
2023-11-12 15:38:08 +01:00
|
|
|
|
2023-11-09 17:20:51 +01:00
|
|
|
|
|
|
|
# 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
|
|
|
|
2023-11-12 15:38:08 +01:00
|
|
|
# other functions that are needed in button definition
|
|
|
|
|
|
|
|
|
|
|
|
def start_level(lvl: int):
|
|
|
|
global level
|
|
|
|
|
|
|
|
page_switch("ingame")
|
|
|
|
level = lvl
|
|
|
|
|
2023-11-12 18:15:57 +01:00
|
|
|
load_level(lvl)
|
2023-11-12 16:58:39 +01:00
|
|
|
|
2023-11-05 19:44:35 +01:00
|
|
|
|
|
|
|
# buttons
|
2023-11-12 15:38:08 +01:00
|
|
|
buttons.append(pg.TextButton("Start", center, screen, lambda: page_switch("level_selector"), text_color=white_smoke, bg_color=dim_gray, font=bigger_default_font))
|
2023-11-11 20:28:00 +01:00
|
|
|
start_button = len(buttons) - 1
|
2023-11-09 17:20:51 +01:00
|
|
|
text_buttons.append(start_button)
|
2023-11-12 15:38:08 +01:00
|
|
|
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)
|
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:
|
2023-11-11 20:28:00 +01:00
|
|
|
buttons[button].update()
|
|
|
|
|
|
|
|
settings["win_size"] = new_size
|
2023-11-09 17:20:51 +01:00
|
|
|
|
|
|
|
|
2023-11-12 16:58:39 +01:00
|
|
|
def load_level(lvl):
|
2023-11-12 18:15:57 +01:00
|
|
|
global level_data
|
|
|
|
global lvl_width
|
|
|
|
global level_surface
|
|
|
|
|
2023-11-12 17:13:08 +01:00
|
|
|
level_data = __import__("data.levels." + str(lvl), fromlist="data.levels")
|
2023-11-12 16:58:39 +01:00
|
|
|
|
2023-11-12 18:15:57 +01:00
|
|
|
lvl_width, lvl_height = level_data.level_size
|
2023-11-12 16:58:39 +01:00
|
|
|
|
2023-11-18 13:01:55 +01:00
|
|
|
level_surface = pygame.Surface((lvl_width * 40 * settings["level_size_multiplier"], lvl_height * 40 * settings["level_size_multiplier"]), pygame.SRCALPHA, 32)
|
2023-11-12 15:38:08 +01:00
|
|
|
|
2023-11-12 18:15:57 +01:00
|
|
|
x = 0
|
|
|
|
y = 0
|
2023-11-12 15:38:08 +01:00
|
|
|
|
2023-11-12 18:15:57 +01:00
|
|
|
for row in level_data.data_array:
|
|
|
|
x = 0
|
2023-11-12 15:38:08 +01:00
|
|
|
|
2023-11-12 18:15:57 +01:00
|
|
|
for block in row:
|
|
|
|
rx = x * 40 * settings["level_size_multiplier"]
|
|
|
|
ry = y * 40 * settings["level_size_multiplier"]
|
|
|
|
print(block)
|
2023-11-12 15:38:08 +01:00
|
|
|
|
2023-11-12 18:15:57 +01:00
|
|
|
blit_block(block, (rx, ry))
|
|
|
|
x += 1
|
|
|
|
|
|
|
|
y += 1
|
|
|
|
|
|
|
|
|
|
|
|
def blit_block(block, position: tuple):
|
|
|
|
if not block == 0:
|
|
|
|
if block == 1:
|
|
|
|
level_surface.blit(stone_block_texture, position)
|
2023-11-12 15:38:08 +01:00
|
|
|
|
2023-11-12 18:15:57 +01:00
|
|
|
elif block == 2:
|
|
|
|
level_surface.blit(dirt_block_texture, position)
|
2023-11-12 15:38:08 +01:00
|
|
|
|
2023-11-12 18:15:57 +01:00
|
|
|
elif block == 3:
|
|
|
|
level_surface.blit(grass_block_texture, position)
|
|
|
|
|
|
|
|
elif block == 4:
|
|
|
|
level_surface.blit(rock_block_texture, position)
|
|
|
|
|
|
|
|
else:
|
|
|
|
level_surface.blit(texture_not_found, position)
|
|
|
|
|
|
|
|
|
|
|
|
def main_menu_page():
|
2023-11-19 14:31:16 +01:00
|
|
|
screen.blit(full_icon_texture, (center_x(full_icon_texture.get_width()), 128))
|
2023-11-12 18:15:57 +01:00
|
|
|
buttons[start_button].blit()
|
|
|
|
|
|
|
|
|
|
|
|
def level_selector_page():
|
2023-11-12 15:38:08 +01:00
|
|
|
buttons[lvl_one_button].blit()
|
|
|
|
|
|
|
|
screen.blit(choose_level_text, (screen.get_width() / 2 - choose_level_text.get_width() / 2, 16))
|
2023-11-09 17:20:51 +01:00
|
|
|
|
|
|
|
|
2023-11-12 18:15:57 +01:00
|
|
|
def ingame_page():
|
2023-11-18 13:01:55 +01:00
|
|
|
screen.fill(summer_sky)
|
2023-11-12 18:15:57 +01:00
|
|
|
screen.blit(level_surface, (0, screen.get_height() - level_surface.get_height()))
|
|
|
|
|
|
|
|
|
2023-11-09 17:20:51 +01:00
|
|
|
def page_selector():
|
|
|
|
if page == "main_menu":
|
|
|
|
main_menu_page()
|
|
|
|
|
2023-11-12 15:38:08 +01:00
|
|
|
elif page == "level_selector":
|
|
|
|
level_selector_page()
|
|
|
|
|
2023-11-12 18:15:57 +01:00
|
|
|
elif page == "ingame":
|
|
|
|
ingame_page()
|
|
|
|
|
2023-11-09 17:20:51 +01:00
|
|
|
|
|
|
|
def page_switch(new_page: str=None):
|
|
|
|
global active_buttons
|
|
|
|
global page
|
|
|
|
|
|
|
|
if not new_page is None:
|
|
|
|
page = new_page
|
|
|
|
|
2023-11-12 18:15:57 +01:00
|
|
|
if page == "main_menu":
|
|
|
|
active_buttons = [start_button]
|
|
|
|
|
|
|
|
elif page == "level_selector":
|
|
|
|
active_buttons = [lvl_one_button]
|
|
|
|
|
|
|
|
elif page == "ingame":
|
|
|
|
active_buttons = []
|
|
|
|
|
2023-11-09 17:20:51 +01:00
|
|
|
# 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:
|
2023-11-12 15:38:08 +01:00
|
|
|
buttons[button].check(pos, pressed)
|
2023-11-09 17:20:51 +01:00
|
|
|
|
|
|
|
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-11 20:28:00 +01:00
|
|
|
window_size_reload(settings["win_size"])
|
2023-11-18 13:01:55 +01:00
|
|
|
pygame.display.set_icon(icon_texture)
|
2023-11-09 17:20:51 +01:00
|
|
|
screen.fill(nero)
|
|
|
|
pygame.display.update()
|
|
|
|
|
2023-11-11 20:28:00 +01:00
|
|
|
page_switch("main_menu")
|
2023-11-05 19:44:35 +01:00
|
|
|
|
|
|
|
running = True
|
|
|
|
|
|
|
|
while running:
|
|
|
|
loop()
|
|
|
|
|
|
|
|
pygame.quit()
|
|
|
|
|
|
|
|
if SAVE_SETTINGS_ON_EXIT:
|
|
|
|
settings.save()
|