Compare commits

..

No commits in common. "converting-to-oop" and "main" have entirely different histories.

View file

@ -3,8 +3,8 @@
import os import os
import pygame import pygame
import numpy import numpy
from wobbl_tools import pg # if you import pg from tools, you dont need to init pygame. from tools import pg # if you import pg from tools, you dont need to init pygame.
from wobbl_tools.data_file import DictFile from tools.file_dict import FileDict
from physics.parabelfunc import berechneflugbahn from physics.parabelfunc import berechneflugbahn
@ -14,337 +14,386 @@ FPS = 60
SAVE_SETTINGS_ON_EXIT = True SAVE_SETTINGS_ON_EXIT = True
class Cowyeet: pygame.init() # pygame initialization
def __init__(self): screen = pygame.display.set_mode(DEFAULT_WINDOW_SIZE)
self.DEFAULT_WINDOW_SIZE = DEFAULT_WINDOW_SIZE pygame.display.set_caption("Cowyeet 2.0")
self.FPS = FPS
self.SAVE_SETTINGS_ON_EXIT = SAVE_SETTINGS_ON_EXIT
pygame.init() # pygame initialization if os.path.isfile("settings.txt"): # If the settings exist, load them into a dict. Else create the settings with the default values.
self.screen = pygame.display.set_mode(DEFAULT_WINDOW_SIZE) settings = FileDict("settings.txt")
pygame.display.set_caption("Cowyeet 2.0")
self.settings = self.load_settings() else:
settings = FileDict()
settings.path = "settings.txt"
settings["win_size"] = DEFAULT_WINDOW_SIZE
settings["level_size_multiplier"] = 1
settings.save()
self.draw_loading_screen() # loading screen
default_font = pygame.font.SysFont("ubuntu", 16)
loading_text = default_font.render("Loading...", True, (240, 240, 240))
# variables screen.fill((40, 40, 40))
self.nero = (40, 40, 40) # colors (color names by https://www.color-blindness.com/color-name-hue/) screen.blit(loading_text, (400 - loading_text.get_width() / 2, 300 - loading_text.get_height() / 2))
self.dim_gray = (100, 100, 100) pygame.display.update()
self.summer_sky = (50, 200, 220) # /loading screen
self.white_smoke = (240, 240, 240)
self.buttons = [] # misc
self.text_buttons = []
self.active_buttons = []
self.last_frame_mouse_pressed = False
self.page = "main_menu"
self.level = None
self.level_data = None
self.lvl_width = None
self.level_surface = None
self.cow_throwable = False
self.cow_flying = False
self.cow_position = (0, 0)
self.cow_flight_path = []
self.cow_flight_step = 0
self.pressed_keys = []
self.pressed_special_keys = pygame.key.get_mods()
# pygame objects # functions
self.clock = pygame.time.Clock() # misc
self.mouse = pygame.mouse
self.keyboard = pygame.key
self.bigger_default_font = pygame.font.SysFont("ubuntu", 32) # fonts # images
def load_texture(path: str):
if os.path.isfile(path):
return pygame.image.load(path)
self.choose_level_text = self.bigger_default_font.render("Choose a level:", True, self.white_smoke) # texts else:
return texture_not_found
self.texture_not_found = pygame.image.load("textures/texture_not_found.png")
self.texture_not_found = pygame.transform.scale(self.texture_not_found, (40 * self.settings["level_size_multiplier"], 40 * self.settings["level_size_multiplier"]))
self.icon_texture = self.load_texture("textures/icon.png") def load_block_texture(path: str):
self.full_icon_texture = self.load_texture("textures/icon_full.png") texture = load_texture(path)
self.full_icon_texture = pygame.transform.scale(self.full_icon_texture, (260, 90))
self.catapult_frame_texture = self.load_texture("textures/catapult/frame.png")
self.catapult_frame_texture = pygame.transform.scale(self.catapult_frame_texture, (66 * 5 * self.settings["level_size_multiplier"], 31 * 5 * self.settings["level_size_multiplier"]))
self.catapult_arm_texture = self.load_texture("textures/catapult/arm.png")
self.catapult_arm_texture = pygame.transform.scale(self.catapult_arm_texture, (25 * 5 * self.settings["level_size_multiplier"], 53 * 5 * self.settings["level_size_multiplier"]))
self.cow = self.load_texture("textures/cow/head.png")
self.cow = pygame.transform.scale(self.cow, (64 * 5 * self.settings["level_size_multiplier"], 64 * 5 * self.settings["level_size_multiplier"]))
self.stone_block_texture = self.load_block_texture("textures/terrain/stone_01.png")
self.dirt_block_texture = self.load_block_texture("textures/terrain/dirt_01.png")
self.grass_block_texture = self.load_block_texture("textures/terrain/grass_01.png")
self.rock_block_texture = self.load_block_texture("textures/terrain/rock_01.png")
# buttons return pygame.transform.scale(texture, (40 * settings["level_size_multiplier"], 40 * settings["level_size_multiplier"]))
self.buttons.append( # /images
pg.TextButton("Start", self.center, self.screen, lambda: self.page_switch("level_selector"), text_color=self.white_smoke, bg_color=self.dim_gray, font=self.bigger_default_font))
self.start_button = len(self.buttons) - 1
self.text_buttons.append(self.start_button)
self.buttons.append(
pg.TextButton("1", (128, 128), self.screen, lambda: self.start_level(1), text_color=self.white_smoke, bg_color=self.dim_gray, font=self.bigger_default_font, padding=(17, 8)))
self.lvl_one_button = len(self.buttons) - 1
self.text_buttons.append(self.lvl_one_button)
# /buttons
# /variables # coordinate calculations
def center_x(width: int):
return screen.get_width() / 2 - width / 2
# loading completed
self.screen = pygame.display.set_mode(self.settings["win_size"], flags=pygame.RESIZABLE, vsync=1) def center_y(height: int):
self.window_size_reload(self.settings["win_size"]) return screen.get_height() / 2 - height / 2
pygame.display.set_icon(self.icon_texture)
self.screen.fill(self.nero)
pygame.display.update()
self.page_switch("main_menu")
self.running = True def center(size):
width, height = size
def load_settings(self): return center_x(width), center_y(height)
if os.path.isfile("settings.txt"): # If the settings exist, load them into a dict. Else create the settings with the default values. # /coordinate calculations
settings = DictFile("settings.txt")
else:
settings = DictFile()
settings.path = "settings.txt"
settings["win_size"] = DEFAULT_WINDOW_SIZE
settings["level_size_multiplier"] = 1
settings.save()
return settings # image calculations
def set_rot_point(img, pos):
w, h = img.get_size()
w, h = w * 2, h * 2
def draw_loading_screen(self): img2 = pygame.Surface((w, h), pygame.SRCALPHA)
default_font = pygame.font.SysFont("ubuntu", 16) img2.blit(img, (w / 2 - pos[0], h / 2 - pos[1]))
loading_text = default_font.render("Loading...", True, (240, 240, 240)) return img2, (w, h)
# / image calculations
self.screen.fill((40, 40, 40))
self.screen.blit(loading_text, (400 - loading_text.get_width() / 2, 300 - loading_text.get_height() / 2))
pygame.display.update()
def load_texture(self, path: str): # game logic
if os.path.isfile(path): def start_level(lvl: int):
return pygame.image.load(path) global level
global cow_throwable
else: page_switch("ingame")
return self.texture_not_found level = lvl
def load_block_texture(self, path: str): load_level(lvl)
texture = self.load_texture(path)
return pygame.transform.scale(texture, (40 * self.settings["level_size_multiplier"], 40 * self.settings["level_size_multiplier"])) cow_throwable = True
def center_x(self, width: int):
return self.screen.get_width() / 2 - width / 2
def center_y(self, height: int): def load_level(lvl):
return self.screen.get_height() / 2 - height / 2 global level_data
global lvl_width
global level_surface
def center(self, size): level_data = __import__("data.levels." + str(lvl), fromlist="data.levels")
width, height = size
return self.center_x(width), self.center_y(height) lvl_width, lvl_height = level_data.level_size
def set_rot_point(self, img, pos): level_surface = pygame.Surface((lvl_width * 40 * settings["level_size_multiplier"], lvl_height * 40 * settings["level_size_multiplier"]), pygame.SRCALPHA, 32)
w, h = img.get_size()
w, h = w * 2, h * 2
img2 = pygame.Surface((w, h), pygame.SRCALPHA) x = 0
img2.blit(img, (w / 2 - pos[0], h / 2 - pos[1])) y = 0
return img2, (w, h)
def start_level(self, lvl: int):
self.page_switch("ingame")
self.level = lvl
self.load_level(lvl)
self.cow_throwable = True
def load_level(self, lvl):
self.level_data = __import__("data.levels." + str(lvl), fromlist="data.levels")
self.lvl_width, lvl_height = self.level_data.level_size
self.level_surface = pygame.Surface((self.lvl_width * 40 * self.settings["level_size_multiplier"], lvl_height * 40 * self.settings["level_size_multiplier"]), pygame.SRCALPHA, 32)
for row in level_data.data_array:
x = 0 x = 0
y = 0
for row in self.level_data.data_array: for block in row:
x = 0 rx = x * 40 * settings["level_size_multiplier"]
ry = y * 40 * settings["level_size_multiplier"]
for block in row: blit_block(block, (rx, ry))
rx = x * 40 * self.settings["level_size_multiplier"] x += 1
ry = y * 40 * self.settings["level_size_multiplier"]
self.blit_block(block, (rx, ry)) y += 1
x += 1
y += 1
def blit_block(self, block, position: tuple): def blit_block(block, position: tuple):
if not block == 0: if not block == 0:
if block == 1: if block == 1:
self.level_surface.blit(self.stone_block_texture, position) level_surface.blit(stone_block_texture, position)
elif block == 2: elif block == 2:
self.level_surface.blit(self.dirt_block_texture, position) level_surface.blit(dirt_block_texture, position)
elif block == 3: elif block == 3:
self.level_surface.blit(self.grass_block_texture, position) level_surface.blit(grass_block_texture, position)
elif block == 4: elif block == 4:
self.level_surface.blit(self.rock_block_texture, position) level_surface.blit(rock_block_texture, position)
else: else:
self.level_surface.blit(self.texture_not_found, position) level_surface.blit(texture_not_found, position)
def yeet_cow(self):
if self.cow_throwable:
self.cow_throwable = False
lvl_x, lvl_y = self.level_data.level_size def yeet_cow():
lvl_x *= 40 global cow_throwable
lvl_x *= self.settings["level_size_multiplier"] global cow_flying
lvl_y *= 40 global cow_flight_path
lvl_y *= self.settings["level_size_multiplier"] global cow_flight_step
self.cow_flight_path = berechneflugbahn(lvl_x, lvl_y, lvl_x + 100, 34, 128, xstep=10) if cow_throwable:
cow_throwable = False
self.cow_flying = True lvl_x, lvl_y = level_data.level_size
self.cow_flight_step = 0 lvl_x *= 40
lvl_x *= settings["level_size_multiplier"]
lvl_y *= 40
lvl_y *= settings["level_size_multiplier"]
def main_menu_page(self): cow_flight_path = berechneflugbahn(lvl_x, lvl_y, lvl_x + 100, 34, 128, xstep=10)
self.screen.blit(self.full_icon_texture, (self.center_x(self.full_icon_texture.get_width()), 128))
self.buttons[self.start_button].blit()
def level_selector_page(self): cow_flying = True
self.buttons[self.lvl_one_button].blit() cow_flight_step = 0
# /game logic
self.screen.blit(self.choose_level_text, (self.screen.get_width() / 2 - self.choose_level_text.get_width() / 2, 16))
def ingame_page(self): # page system
self.screen.fill(self.summer_sky) def main_menu_page():
self.screen.blit(self.level_surface, (0, self.screen.get_height() - self.level_surface.get_height())) screen.blit(full_icon_texture, (center_x(full_icon_texture.get_width()), 128))
buttons[start_button].blit()
lvl_size = self.settings["level_size_multiplier"]
cx, cy = self.level_data.catapult_pos def level_selector_page():
cx = cx * 40 * lvl_size buttons[lvl_one_button].blit()
cy = self.screen.get_height() - self.level_surface.get_height() + cy * 40 * lvl_size - self.catapult_frame_texture.get_height()
self.screen.blit(self.catapult_frame_texture, (cx, cy)) screen.blit(choose_level_text, (screen.get_width() / 2 - choose_level_text.get_width() / 2, 16))
self.screen.blit(self.catapult_arm_texture, (cx + 27 * 5 * lvl_size, cy - 35 * 5 * lvl_size))
if self.cow_flying:
self.cow_flight_step += 1
x = self.cow_flight_path[self.cow_flight_step * 2] def ingame_page():
y = self.cow_flight_path[self.cow_flight_step * 2 + 1] global cow_flight_step
self.cow_position = (x, self.screen.get_height() - y - 64 * 5 * self.settings["level_size_multiplier"]) global cow_position
global cow_flying
global cow_throwable
if self.cow_flight_step * 2 == len(self.cow_flight_path) - 2: screen.fill(summer_sky)
self.cow_flying = False screen.blit(level_surface, (0, screen.get_height() - level_surface.get_height()))
self.cow_throwable = True
self.screen.blit(self.cow, self.cow_position) lvl_size = settings["level_size_multiplier"]
def page_selector(self): cx, cy = level_data.catapult_pos
if self.page == "main_menu": cx = cx * 40 * lvl_size
self.main_menu_page() cy = screen.get_height() - level_surface.get_height() + cy * 40 * lvl_size - catapult_frame_texture.get_height()
elif self.page == "level_selector": screen.blit(catapult_frame_texture, (cx, cy))
self.level_selector_page() screen.blit(catapult_arm_texture, (cx + 27 * 5 * lvl_size, cy - 35 * 5 * lvl_size))
elif self.page == "ingame": if cow_flying:
self.ingame_page() cow_flight_step += 1
def page_switch(self, new_page: str=None): x = cow_flight_path[cow_flight_step * 2]
if not new_page is None: y = cow_flight_path[cow_flight_step * 2 + 1]
self.page = new_page cow_position = (x, screen.get_height() - y - 64 * 5 * settings["level_size_multiplier"])
if self.page == "main_menu": if cow_flight_step * 2 == len(cow_flight_path) - 2:
self.active_buttons = [self.start_button] cow_flying = False
cow_throwable = True
elif self.page == "level_selector": screen.blit(cow, cow_position)
self.active_buttons = [self.lvl_one_button]
elif self.page == "ingame":
self.active_buttons = []
# for button in buttons: def page_selector():
# button.active = False if page == "main_menu":
# main_menu_page()
# for button in active_buttons:
# button.active = True
def close(self): elif page == "level_selector":
self.running = False level_selector_page()
def window_size_reload(self, new_size): elif page == "ingame":
for button in self.text_buttons: ingame_page()
self.buttons[button].update()
self.settings["win_size"] = new_size
def get_events(self): def page_switch(new_page: str=None):
for event in pygame.event.get(): global active_buttons
if event.type == pygame.QUIT: global page
self.close()
return
if event.type == pygame.MOUSEBUTTONDOWN: if not new_page is None:
pressed = self.mouse.get_pressed() page = new_page
pos = self.mouse.get_pos()
for button in self.active_buttons: if page == "main_menu":
self.buttons[button].check(pos, pressed) active_buttons = [start_button]
self.last_frame_mouse_pressed = True elif page == "level_selector":
active_buttons = [lvl_one_button]
elif event.type == pygame.VIDEORESIZE: elif page == "ingame":
active_buttons = []
self.window_size_reload(event.size) # for button in buttons:
# button.active = False
#
# for button in active_buttons:
# button.active = True
# /page system
elif event.type == pygame.KEYDOWN:
key = event.key
if key == pygame.K_c and self.pressed_special_keys & pygame.KMOD_CTRL: def close():
self.close() global running
running = False
elif key == pygame.K_SPACE:
if self.page == "ingame":
self.yeet_cow()
def loop(self): def window_size_reload(new_size):
self.screen.fill(self.nero) for button in text_buttons:
buttons[button].update()
self.pressed_keys = self.keyboard.get_pressed() settings["win_size"] = new_size
self.pressed_special_keys = self.keyboard.get_mods()
self.get_events()
if not self.running:
def get_events():
global last_frame_mouse_pressed
for event in pygame.event.get():
if event.type == pygame.QUIT:
close()
return return
self.page_selector() if event.type == pygame.MOUSEBUTTONDOWN:
pressed = mouse.get_pressed()
pos = mouse.get_pos()
pygame.display.update() if not last_frame_mouse_pressed:
for button in active_buttons:
buttons[button].check(pos, pressed)
self.clock.tick(self.FPS) last_frame_mouse_pressed = True
elif event.type == pygame.VIDEORESIZE:
window_size_reload(event.size)
elif event.type == pygame.KEYDOWN:
key = event.key
if key == pygame.K_c and pressed_special_keys & pygame.KMOD_CTRL:
close()
elif key == pygame.K_SPACE:
if page == "ingame":
yeet_cow()
if __name__ == "__main__": def loop():
game = Cowyeet() global last_frame_mouse_pressed
global pressed_keys
global pressed_special_keys
while game.running: screen.fill(nero)
game.loop()
pygame.quit() pressed_keys = keyboard.get_pressed()
pressed_special_keys = keyboard.get_mods()
get_events()
if game.SAVE_SETTINGS_ON_EXIT: if not running:
game.settings.save() return
print("Bye!") 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)
# 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
cow_throwable = False
cow_flying = False
cow_position = (0, 0)
cow_flight_path = []
cow_flight_step = 0
pressed_keys = []
pressed_special_keys = pygame.key.get_mods()
# pygame objects
clock = pygame.time.Clock() # misc
mouse = pygame.mouse
keyboard = pygame.key
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"]))
cow = load_texture("textures/cow/head.png")
cow = pygame.transform.scale(cow, (64 * 5 * settings["level_size_multiplier"], 64 * 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, vsync=1)
window_size_reload(settings["win_size"])
pygame.display.set_icon(icon_texture)
screen.fill(nero)
pygame.display.update()
page_switch("main_menu")
running = True
while running:
loop()
pygame.quit()
if SAVE_SETTINGS_ON_EXIT:
settings.save()
print("Bye!")