Reformatted the code.
Formatting sequence: Imports Classes Functions Variables Start
This commit is contained in:
parent
08b117baf9
commit
37b36f2d49
1 changed files with 94 additions and 69 deletions
163
cowyeet.py
163
cowyeet.py
|
@ -13,12 +13,11 @@ FPS = 60
|
|||
SAVE_SETTINGS_ON_EXIT = True
|
||||
|
||||
|
||||
# some initializing
|
||||
pygame.init()
|
||||
pygame.init() # pygame initialization
|
||||
screen = pygame.display.set_mode(DEFAULT_WINDOW_SIZE)
|
||||
pygame.display.set_caption("Cowyeet 2.0")
|
||||
|
||||
if os.path.isfile("settings.txt"):
|
||||
if os.path.isfile("settings.txt"): # If the settings exist, load them into a dict. Else create the settings with the default values.
|
||||
settings = FileDict("settings.txt")
|
||||
|
||||
else:
|
||||
|
@ -35,38 +34,13 @@ 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()
|
||||
# /loading screen
|
||||
|
||||
|
||||
# variables
|
||||
nero = (40, 40, 40) # colors (color names by https://www.color-blindness.com/color-name-hue/)
|
||||
dim_gray = (100, 100, 100)
|
||||
summer_sky = (50, 200, 220)
|
||||
white_smoke = (240, 240, 240)
|
||||
# functions
|
||||
|
||||
buttons = [] # misc
|
||||
text_buttons = []
|
||||
active_buttons = []
|
||||
last_frame_mouse_pressed = False
|
||||
page = "main_menu"
|
||||
level = None
|
||||
level_data = None
|
||||
lvl_width = None
|
||||
level_surface = None
|
||||
|
||||
|
||||
# pygame objects
|
||||
clock = pygame.time.Clock() # misc
|
||||
mouse = pygame.mouse
|
||||
|
||||
bigger_default_font = pygame.font.SysFont("ubuntu", 32) # fonts
|
||||
|
||||
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
|
||||
# images
|
||||
def load_texture(path: str):
|
||||
if os.path.isfile(path):
|
||||
return pygame.image.load(path)
|
||||
|
||||
|
@ -78,18 +52,7 @@ 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"]))
|
||||
|
||||
|
||||
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))
|
||||
catapult_frame_texture = load_texture("textures/catapult/frame.png")
|
||||
catapult_frame_texture = pygame.transform.scale(catapult_frame_texture, (66 * 5 * settings["level_size_multiplier"], 31 * 5 * 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")
|
||||
|
||||
# /images
|
||||
|
||||
|
||||
# coordinate calculations
|
||||
|
@ -105,10 +68,21 @@ def center(size):
|
|||
width, height = size
|
||||
|
||||
return center_x(width), center_y(height)
|
||||
|
||||
# other functions that are needed in button definition
|
||||
# /coordinate calculations
|
||||
|
||||
|
||||
# image calculations
|
||||
def set_rot_point(img, pos):
|
||||
w, h = img.get_size()
|
||||
w, h = w * 2, h * 2
|
||||
|
||||
img2 = pygame.Surface((w, h), pygame.SRCALPHA)
|
||||
img2.blit(img, (w / 2 - pos[0], h / 2 - pos[1]))
|
||||
return img2, (w, h)
|
||||
# / image calculations
|
||||
|
||||
|
||||
# game logic
|
||||
def start_level(lvl: int):
|
||||
global level
|
||||
|
||||
|
@ -118,27 +92,6 @@ def start_level(lvl: int):
|
|||
load_level(lvl)
|
||||
|
||||
|
||||
# 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):
|
||||
global level_data
|
||||
global lvl_width
|
||||
|
@ -182,8 +135,10 @@ def blit_block(block, position: tuple):
|
|||
|
||||
else:
|
||||
level_surface.blit(texture_not_found, position)
|
||||
# /game logic
|
||||
|
||||
|
||||
# page system
|
||||
def main_menu_page():
|
||||
screen.blit(full_icon_texture, (center_x(full_icon_texture.get_width()), 128))
|
||||
buttons[start_button].blit()
|
||||
|
@ -199,11 +154,14 @@ def ingame_page():
|
|||
screen.fill(summer_sky)
|
||||
screen.blit(level_surface, (0, screen.get_height() - level_surface.get_height()))
|
||||
|
||||
lvl_size = settings["level_size_multiplier"]
|
||||
|
||||
cx, cy = level_data.catapult_pos
|
||||
cx = cx * 40 * settings["level_size_multiplier"]
|
||||
cy = screen.get_height() - level_surface.get_height() + cy * 40 * settings["level_size_multiplier"] - catapult_frame_texture.get_height()
|
||||
cx = cx * 40 * lvl_size
|
||||
cy = screen.get_height() - level_surface.get_height() + cy * 40 * lvl_size - catapult_frame_texture.get_height()
|
||||
|
||||
screen.blit(catapult_frame_texture, (cx, cy))
|
||||
screen.blit(catapult_arm_texture, (cx + 27 * 5 * lvl_size, cy - 35 * 5 * lvl_size))
|
||||
|
||||
|
||||
def page_selector():
|
||||
|
@ -238,6 +196,19 @@ def page_switch(new_page: str=None):
|
|||
#
|
||||
# for button in active_buttons:
|
||||
# button.active = True
|
||||
# /page system
|
||||
|
||||
|
||||
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 get_events():
|
||||
|
@ -284,6 +255,60 @@ def loop():
|
|||
clock.tick(FPS)
|
||||
|
||||
|
||||
# variables
|
||||
nero = (40, 40, 40) # colors (color names by https://www.color-blindness.com/color-name-hue/)
|
||||
dim_gray = (100, 100, 100)
|
||||
summer_sky = (50, 200, 220)
|
||||
white_smoke = (240, 240, 240)
|
||||
|
||||
buttons = [] # misc
|
||||
text_buttons = []
|
||||
active_buttons = []
|
||||
last_frame_mouse_pressed = False
|
||||
page = "main_menu"
|
||||
level = None
|
||||
level_data = None
|
||||
lvl_width = None
|
||||
level_surface = None
|
||||
|
||||
|
||||
# pygame objects
|
||||
clock = pygame.time.Clock() # misc
|
||||
mouse = pygame.mouse
|
||||
|
||||
bigger_default_font = pygame.font.SysFont("ubuntu", 32) # fonts
|
||||
|
||||
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"]))
|
||||
|
||||
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))
|
||||
catapult_frame_texture = load_texture("textures/catapult/frame.png")
|
||||
catapult_frame_texture = pygame.transform.scale(catapult_frame_texture, (66 * 5 * settings["level_size_multiplier"], 31 * 5 * settings["level_size_multiplier"]))
|
||||
catapult_arm_texture = load_texture("textures/catapult/arm.png")
|
||||
catapult_arm_texture = pygame.transform.scale(catapult_arm_texture, (25 * 5 * settings["level_size_multiplier"], 53 * 5 * 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")
|
||||
|
||||
|
||||
|
||||
# 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)
|
||||
# /buttons
|
||||
|
||||
# /variables
|
||||
|
||||
|
||||
# loading completed
|
||||
|
||||
screen = pygame.display.set_mode(settings["win_size"], flags=pygame.RESIZABLE)
|
||||
|
|
Loading…
Reference in a new issue