elukjanovica 2024-02-29 15:08:33 +02:00
parent 12c4d3ee15
commit 65dac764b6
1 changed files with 102 additions and 50 deletions

54
main.py
View File

@ -22,6 +22,8 @@ SETTINGS_FILE = "main_settings.json"
STATS_FILE = "main_stats.json" STATS_FILE = "main_stats.json"
# Data loading and saving # Data loading and saving
def load_data(data): def load_data(data):
try: try:
with open(data, "r") as file: with open(data, "r") as file:
@ -30,6 +32,7 @@ def load_data(data):
print("Something went wrong with file loading") print("Something went wrong with file loading")
return current_data return current_data
storage = load_data(DATA_FILE) storage = load_data(DATA_FILE)
puzzle_data = load_data(PUZZLE_FILE) puzzle_data = load_data(PUZZLE_FILE)
settings_data = load_data(SETTINGS_FILE) settings_data = load_data(SETTINGS_FILE)
@ -128,6 +131,8 @@ clock = pygame.time.Clock()
hwnd = pygame.display.get_wm_info()["window"] hwnd = pygame.display.get_wm_info()["window"]
# Sprites generation # Sprites generation
class SpriteSheet: class SpriteSheet:
def __init__(self, sheet_path): def __init__(self, sheet_path):
self.sheet = pygame.image.load(sheet_path).convert_alpha() self.sheet = pygame.image.load(sheet_path).convert_alpha()
@ -156,8 +161,10 @@ class SpriteSheet:
self.sheet.set_at((x, y), new_color) self.sheet.set_at((x, y), new_color)
sprite_sheet = SpriteSheet(SIZE_PATH + str(INT) + STYLE_PATH + "sprites.png") sprite_sheet = SpriteSheet(SIZE_PATH + str(INT) + STYLE_PATH + "sprites.png")
class Image: class Image:
def __init__(self, sprite_position, window_position, size): def __init__(self, sprite_position, window_position, size):
self.sprite_sheet = sprite_sheet self.sprite_sheet = sprite_sheet
@ -173,6 +180,7 @@ class Image:
def draw(self, screen): def draw(self, screen):
screen.blit(self.img, self.rect) screen.blit(self.img, self.rect)
class Slider: class Slider:
def __init__(self, line_size, button_size, position): def __init__(self, line_size, button_size, position):
self.line_size = line_size self.line_size = line_size
@ -202,6 +210,7 @@ class Slider:
self.value = int(((mouse_x - self.position[0]) / (self.line_size[0] - self.button_size[0])) * 360) self.value = int(((mouse_x - self.position[0]) / (self.line_size[0] - self.button_size[0])) * 360)
self.update() self.update()
class Button: class Button:
def __init__(self, sprite_position, window_position, size): def __init__(self, sprite_position, window_position, size):
self.sprite_sheet = sprite_sheet self.sprite_sheet = sprite_sheet
@ -244,6 +253,8 @@ class Button:
screen.blit(self.img, self.rect) screen.blit(self.img, self.rect)
# Ability to move window # Ability to move window
def move_win(coordinates): def move_win(coordinates):
hwnd = pygame.display.get_wm_info()["window"] hwnd = pygame.display.get_wm_info()["window"]
w, h = pygame.display.get_surface().get_size() w, h = pygame.display.get_surface().get_size()
@ -251,6 +262,8 @@ def move_win(coordinates):
set_shadow_style(hwnd) set_shadow_style(hwnd)
# Generating shadow # Generating shadow
def set_shadow_style(hwnd): def set_shadow_style(hwnd):
try: try:
DWMWA_NCRENDERING_POLICY = 2 DWMWA_NCRENDERING_POLICY = 2
@ -269,6 +282,8 @@ def set_shadow_style(hwnd):
print(e) print(e)
# Text render # Text render
def render_text(text, position, screen, color=text_color, centered=False): def render_text(text, position, screen, color=text_color, centered=False):
font = pygame.font.SysFont(FONT, FONT_SIZE) font = pygame.font.SysFont(FONT, FONT_SIZE)
text_render = font.render(text, True, color) text_render = font.render(text, True, color)
@ -280,6 +295,8 @@ def render_text(text, position, screen, color=text_color, centered=False):
screen.blit(text_render, position) screen.blit(text_render, position)
# MENU MODE # MENU MODE
def browse(): def browse():
root = Tk() root = Tk()
root.withdraw() root.withdraw()
@ -287,9 +304,11 @@ def browse():
root.destroy() root.destroy()
return file_path return file_path
quick_game_activated = False quick_game_activated = False
selected_file = None selected_file = None
def quick_game(): def quick_game():
global selected_file, puzzle_image, puzzle_pieces, shuffled_pieces, empty_position, quick_game_activated, original_image global selected_file, puzzle_image, puzzle_pieces, shuffled_pieces, empty_position, quick_game_activated, original_image
quick_game_activated = True quick_game_activated = True
@ -305,6 +324,7 @@ def quick_game():
except FileNotFoundError: except FileNotFoundError:
print("No file selected") print("No file selected")
def render_albums(): def render_albums():
global current_mode, album_name global current_mode, album_name
current_hovered_index = -1 current_hovered_index = -1
@ -336,15 +356,19 @@ def render_albums():
arrow_image = Image((cons[1], 0), (cons[2], size[1] + cons[0] * current_hovered_index + cons[3]), (cons[4], cons[4])) arrow_image = Image((cons[1], 0), (cons[2], size[1] + cons[0] * current_hovered_index + cons[3]), (cons[4], cons[4]))
arrow_image.draw(screen) arrow_image.draw(screen)
def album_deletion(): def album_deletion():
# Not finished yet # Not finished yet
pass pass
def album_creation(): def album_creation():
# Not finished yet # Not finished yet
pass pass
# SELECT MODE # SELECT MODE
class ImageRender: class ImageRender:
def __init__(self, album_name, initial_index=0): def __init__(self, album_name, initial_index=0):
self.album_name = album_name self.album_name = album_name
@ -368,7 +392,6 @@ class ImageRender:
self.num_images = len(storage["albums"][self.album_name]) self.num_images = len(storage["albums"][self.album_name])
self.current_text = f"{self.index+1} of {self.num_images}" self.current_text = f"{self.index+1} of {self.num_images}"
def render(self, screen): def render(self, screen):
screen.blit(self.scaled_image, (self.scaled_size[0], self.scaled_size[1])) screen.blit(self.scaled_image, (self.scaled_size[0], self.scaled_size[1]))
render_text(self.current_text, (area[0], area[1]), screen, oposite_color, True) render_text(self.current_text, (area[0], area[1]), screen, oposite_color, True)
@ -408,6 +431,7 @@ class ImageRender:
# Not finished yet # Not finished yet
pass pass
def reload_chosen(): def reload_chosen():
for album in storage["albums"].values(): for album in storage["albums"].values():
for image in album.values(): for image in album.values():
@ -416,6 +440,8 @@ def reload_chosen():
data_file.write(json.dumps(storage, indent=3)) data_file.write(json.dumps(storage, indent=3))
# GAME MODE # GAME MODE
def timer(elapsed_time): def timer(elapsed_time):
hours = elapsed_time // 3600 hours = elapsed_time // 3600
minutes = (elapsed_time % 3600) // 60 minutes = (elapsed_time % 3600) // 60
@ -424,6 +450,8 @@ def timer(elapsed_time):
return timer_text return timer_text
# Puzzle loading and running logic # Puzzle loading and running logic
def cut_image(image): def cut_image(image):
piece_width = puzzle_size[0] // len(puzzle_matrix[0]) piece_width = puzzle_size[0] // len(puzzle_matrix[0])
piece_height = puzzle_size[1] // len(puzzle_matrix) piece_height = puzzle_size[1] // len(puzzle_matrix)
@ -450,6 +478,7 @@ def cut_image(image):
return pieces return pieces
def load_puzzle(): def load_puzzle():
global puzzle_matrix, original_matrix, empty_position, puzzle_pieces, shuffled_pieces, puzzle_size, directions, cell_size, number_of_rows, original_image global puzzle_matrix, original_matrix, empty_position, puzzle_pieces, shuffled_pieces, puzzle_size, directions, cell_size, number_of_rows, original_image
@ -474,8 +503,10 @@ def load_puzzle():
shuffled_pieces = shuffle_pieces(puzzle_pieces) shuffled_pieces = shuffle_pieces(puzzle_pieces)
empty_position = (len(puzzle_matrix) - 1, len(puzzle_matrix[0]) - 1) empty_position = (len(puzzle_matrix) - 1, len(puzzle_matrix[0]) - 1)
load_puzzle() load_puzzle()
def shuffle_pieces(puzzle_pieces): def shuffle_pieces(puzzle_pieces):
global puzzle_matrix global puzzle_matrix
numbers = list(range(len(puzzle_pieces))) numbers = list(range(len(puzzle_pieces)))
@ -490,6 +521,7 @@ def shuffle_pieces(puzzle_pieces):
return puzzle_pieces return puzzle_pieces
def move_pieces(clicked_row, clicked_col): def move_pieces(clicked_row, clicked_col):
global puzzle_matrix global puzzle_matrix
@ -526,6 +558,7 @@ def move_pieces(clicked_row, clicked_col):
save_puzzle_state() save_puzzle_state()
update_directions() update_directions()
def update_directions(): def update_directions():
global directions, puzzle_matrix global directions, puzzle_matrix
zero_positions = np.nonzero(puzzle_matrix == 0) zero_positions = np.nonzero(puzzle_matrix == 0)
@ -538,14 +571,17 @@ def update_directions():
"right": puzzle_matrix[zero_row, :zero_col].flatten().tolist() if zero_col > 0 else [], "right": puzzle_matrix[zero_row, :zero_col].flatten().tolist() if zero_col > 0 else [],
} }
update_directions() update_directions()
def save_puzzle_state(): def save_puzzle_state():
puzzle_matrix_list = puzzle_matrix.tolist() puzzle_matrix_list = puzzle_matrix.tolist()
puzzle_data[f'current {puzzle_version}']['matrix'] = puzzle_matrix_list puzzle_data[f'current {puzzle_version}']['matrix'] = puzzle_matrix_list
with open(PUZZLE_FILE, 'w') as puzzle_file: with open(PUZZLE_FILE, 'w') as puzzle_file:
json.dump(puzzle_data, puzzle_file) json.dump(puzzle_data, puzzle_file)
def update_puzzle_data(completed, sorted, current_time, current_moves, real_time): def update_puzzle_data(completed, sorted, current_time, current_moves, real_time):
path_components = puzzle_data[f'current {puzzle_version}']['selected_image'].split('/')[2:] path_components = puzzle_data[f'current {puzzle_version}']['selected_image'].split('/')[2:]
current_data = storage[path_components[0]] current_data = storage[path_components[0]]
@ -575,6 +611,8 @@ def update_puzzle_data(completed, sorted, current_time, current_moves, real_time
data_file.write(json.dumps(storage, indent=3)) data_file.write(json.dumps(storage, indent=3))
# SETTINGS MODE # SETTINGS MODE
def play_music(): def play_music():
music_file = settings_data["music"]["version"] music_file = settings_data["music"]["version"]
volume = settings_data["music"]["volume"] volume = settings_data["music"]["volume"]
@ -582,9 +620,11 @@ def play_music():
pygame.mixer.music.set_volume(volume) pygame.mixer.music.set_volume(volume)
pygame.mixer.music.play(-1) pygame.mixer.music.play(-1)
if settings_data["music"]["on"] is True: if settings_data["music"]["on"] is True:
play_music() play_music()
def play_sound(): def play_sound():
sound_file = settings_data["sound"]["version"] sound_file = settings_data["sound"]["version"]
volume = settings_data["sound"]["volume"] volume = settings_data["sound"]["volume"]
@ -592,6 +632,7 @@ def play_sound():
sound.set_volume(volume) sound.set_volume(volume)
pygame.mixer.Sound.play(sound) pygame.mixer.Sound.play(sound)
def set_hue(): def set_hue():
global hue_value global hue_value
if settings_data["style"] == STYLE_CHOICES[0]: if settings_data["style"] == STYLE_CHOICES[0]:
@ -614,6 +655,7 @@ def set_hue():
with open(SETTINGS_FILE, "w") as settings_file: with open(SETTINGS_FILE, "w") as settings_file:
json.dump(settings_data, settings_file) json.dump(settings_data, settings_file)
def change_hue(image, hue): def change_hue(image, hue):
new_image = image.copy() new_image = image.copy()
@ -630,23 +672,28 @@ def change_hue(image, hue):
return new_image return new_image
def export(): def export():
# Export data function, not finished yet # Export data function, not finished yet
pass pass
def reset(): def reset():
# Reset all data function, not finished yet # Reset all data function, not finished yet
pass pass
def info(): def info():
# Not finished yet # Not finished yet
pass pass
# Stats # Stats
def stats(): def stats():
# Not finished yet # Not finished yet
pass pass
# Buttons # Buttons
if settings_data["size"] == SIZE_CHOICES[0]: if settings_data["size"] == SIZE_CHOICES[0]:
px12 = (12, 12) px12 = (12, 12)
@ -741,6 +788,7 @@ settings_buttons = [info_button, menu_button, stats_button, continue_button, clo
do_not_save = False do_not_save = False
def button_check(pressed_button, run, current_mode, timer_running): def button_check(pressed_button, run, current_mode, timer_running):
global completed, not_shuffled, current_timer_text, current_moves, real_time global completed, not_shuffled, current_timer_text, current_moves, real_time
do_not_save = False do_not_save = False
@ -781,12 +829,14 @@ def button_check(pressed_button, run, current_mode, timer_running):
return run, current_mode, timer_running, do_not_save return run, current_mode, timer_running, do_not_save
win32gui.SetWindowLong(hwnd, win32con.GWL_EXSTYLE, win32gui.SetWindowLong(hwnd, win32con.GWL_EXSTYLE,
win32gui.GetWindowLong(hwnd, win32con.GWL_EXSTYLE) | win32con.WS_EX_LAYERED) win32gui.GetWindowLong(hwnd, win32con.GWL_EXSTYLE) | win32con.WS_EX_LAYERED)
win32gui.SetLayeredWindowAttributes(hwnd, win32api.RGB(*(255, 0, 128)), 0, win32con.LWA_COLORKEY) win32gui.SetLayeredWindowAttributes(hwnd, win32api.RGB(*(255, 0, 128)), 0, win32con.LWA_COLORKEY)
set_shadow_style(hwnd) set_shadow_style(hwnd)
def data_check(current_mode): def data_check(current_mode):
if current_mode == "select": if current_mode == "select":
background_image = ver2_layer background_image = ver2_layer
@ -807,6 +857,8 @@ def data_check(current_mode):
return background_image, current_buttons return background_image, current_buttons
# Game state # Game state
run = True run = True
current_mode = "menu" current_mode = "menu"
# Dragging and mouse interaction # Dragging and mouse interaction