Added terrain of first level.

This commit is contained in:
The Wobbler 2023-11-12 18:15:57 +01:00
parent f8aa5bd3d9
commit d4f6235bb8

View file

@ -25,6 +25,7 @@ else:
settings = FileDict() settings = FileDict()
settings.path = "settings.txt" settings.path = "settings.txt"
settings["win_size"] = DEFAULT_WINDOW_SIZE settings["win_size"] = DEFAULT_WINDOW_SIZE
settings["level_size_multiplier"] = 1
settings.save() settings.save()
# loading screen # loading screen
@ -39,6 +40,7 @@ pygame.display.update()
# variables # variables
nero = (40, 40, 40) # colors (color names by https://www.color-blindness.com/color-name-hue/) nero = (40, 40, 40) # colors (color names by https://www.color-blindness.com/color-name-hue/)
dim_gray = (100, 100, 100) dim_gray = (100, 100, 100)
summer_sky = (50, 200, 220)
white_smoke = (240, 240, 240) white_smoke = (240, 240, 240)
buttons = [] # misc buttons = [] # misc
@ -47,6 +49,9 @@ active_buttons = []
last_frame_mouse_pressed = False last_frame_mouse_pressed = False
page = "main_menu" page = "main_menu"
level = None level = None
level_data = None
lvl_width = None
level_surface = None
# pygame objects # pygame objects
@ -55,8 +60,30 @@ mouse = pygame.mouse
bigger_default_font = pygame.font.SysFont("ubuntu", 32) # fonts bigger_default_font = pygame.font.SysFont("ubuntu", 32) # fonts
# texts choose_level_text = bigger_default_font.render("Choose a level:", True, white_smoke) # texts
choose_level_text = bigger_default_font.render("Choose a level:", True, white_smoke)
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"]))
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")
# coordinate calculations # coordinate calculations
@ -82,7 +109,7 @@ def start_level(lvl: int):
page_switch("ingame") page_switch("ingame")
level = lvl level = lvl
load_level(1) load_level(lvl)
# buttons # buttons
@ -107,27 +134,67 @@ def window_size_reload(new_size):
def load_level(lvl): def load_level(lvl):
global level_data
global lvl_width
global level_surface
level_data = __import__("data.levels." + str(lvl), fromlist="data.levels") level_data = __import__("data.levels." + str(lvl), fromlist="data.levels")
lvl_width, lvl_height = level_data.level_size
level_surface = pygame.Surface((lvl_width * 40 * settings["level_size_multiplier"], lvl_height * 40 * settings["level_size_multiplier"]))
level_surface.fill(summer_sky)
x = 0
y = 0
for row in level_data.data_array:
x = 0
for block in row:
rx = x * 40 * settings["level_size_multiplier"]
ry = y * 40 * settings["level_size_multiplier"]
print(block)
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)
elif block == 2:
level_surface.blit(dirt_block_texture, position)
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(): def main_menu_page():
global active_buttons
active_buttons = [start_button]
buttons[start_button].blit() buttons[start_button].blit()
def level_selector_page(): def level_selector_page():
global active_buttons
active_buttons = [lvl_one_button]
buttons[lvl_one_button].blit() buttons[lvl_one_button].blit()
screen.blit(choose_level_text, (screen.get_width() / 2 - choose_level_text.get_width() / 2, 16)) screen.blit(choose_level_text, (screen.get_width() / 2 - choose_level_text.get_width() / 2, 16))
def ingame_page():
screen.blit(level_surface, (0, screen.get_height() - level_surface.get_height()))
def page_selector(): def page_selector():
if page == "main_menu": if page == "main_menu":
main_menu_page() main_menu_page()
@ -135,6 +202,9 @@ def page_selector():
elif page == "level_selector": elif page == "level_selector":
level_selector_page() level_selector_page()
elif page == "ingame":
ingame_page()
def page_switch(new_page: str=None): def page_switch(new_page: str=None):
global active_buttons global active_buttons
@ -143,6 +213,15 @@ def page_switch(new_page: str=None):
if not new_page is None: if not new_page is None:
page = new_page page = new_page
if page == "main_menu":
active_buttons = [start_button]
elif page == "level_selector":
active_buttons = [lvl_one_button]
elif page == "ingame":
active_buttons = []
# for button in buttons: # for button in buttons:
# button.active = False # button.active = False
# #