Added Level selection.

This commit is contained in:
The Wobbler 2023-11-12 15:38:08 +01:00
parent a9e4b1921b
commit 44c8ba0231

View file

@ -45,6 +45,7 @@ text_buttons = []
active_buttons = []
last_frame_mouse_pressed = False
page = "main_menu"
level = None
# pygame objects
@ -53,6 +54,9 @@ 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):
@ -68,11 +72,23 @@ def center(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
# buttons
buttons.append(pg.TextButton("Start", center, screen, lambda: page_switch("game"), text_color=white_smoke, bg_color=dim_gray, font=bigger_default_font))
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():
@ -88,13 +104,30 @@ def window_size_reload(new_size):
def main_menu_page():
buttons[start_button].blit(screen)
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
@ -103,9 +136,6 @@ def page_switch(new_page: str=None):
if not new_page is None:
page = new_page
else:
print("Error: Page not found.")
# for button in buttons:
# button.active = False
#
@ -127,7 +157,7 @@ def get_events():
if not last_frame_mouse_pressed:
for button in active_buttons:
button.check(pos, pressed)
buttons[button].check(pos, pressed)
last_frame_mouse_pressed = True