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

58
main.py
View File

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