elukjanovica 2024-02-27 17:19:30 +02:00
parent da9e428202
commit a28063da25
1 changed files with 68 additions and 39 deletions

107
main.py
View File

@ -12,7 +12,7 @@ pygame.init()
FPS = 60
MAIN_PATH = "C:/Users/User/Documents/Coding/Picture Puzzle/"
#or MAIN_PATH = "C:/Users/RVKG/Documents/My Palettes/Picture Puzzle/"
MAIN_PATH = "C:/Users/RVKG/Documents/My Palettes/Picture Puzzle/"
IMAGES_PATH = "/images/albums/"
DATA_FILE = "main_data.json"
@ -487,8 +487,7 @@ def save_puzzle_state():
with open(PUZZLE_FILE, 'w') as puzzle_file:
json.dump(puzzle_data, puzzle_file)
def update_puzzle_data(completed, sorted, current_time, current_moves):
print("saving2")
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]]
for component in path_components[1:]:
@ -498,30 +497,24 @@ def update_puzzle_data(completed, sorted, current_time, current_moves):
print("Something went wrong while updating puzzle data")
return
print("current_data:", current_data)
initial_chosen, initial_completed, initial_sorted, initial_moves, initial_time, _, initial_current_moves = current_data
print(current_data)
initial_chosen, initial_completed, initial_sorted, initial_moves, initial_time, _, _ = current_data
initial_completed = int(completed)
initial_sorted = int(sorted)
initial_moves += current_moves
initial_time_seconds = sum(int(x) * 60**i for i, x in enumerate(reversed(initial_time.split(":"))))
current_time_seconds = sum(int(x) * 60**i for i, x in enumerate(reversed(current_time.split(":"))))
total_time_seconds = initial_time_seconds + current_time_seconds
total_time_seconds = initial_time_seconds + real_time
hours, remainder = divmod(total_time_seconds, 3600)
minutes, seconds = divmod(remainder, 60)
formatted_time = f"{hours:02d}:{minutes:02d}:{seconds:02d}"
current_data = [initial_chosen, initial_completed, initial_sorted, initial_moves, formatted_time, current_time, initial_current_moves]
current_data = [initial_chosen, initial_completed, initial_sorted, initial_moves, formatted_time, current_time, current_moves]
print(current_data)
storage[path_components[0]][path_components[1]][path_components[2]] = current_data
with open(DATA_FILE, 'w') as data_file:
data_file.write(json.dumps(storage, indent=3))
def fade_in_out(rect, duration):
fade_surface = pygame.Surface((rect.width, rect.height))
fade_surface.fill((0, 0, 0))
fade_surface.set_alpha(0)
# SETTINGS MODE
def play_music():
music_file = settings_data["selected_music"]
@ -623,6 +616,7 @@ for component in path_components[1:]:
completed = current_data[1]
not_shuffled = current_data[2]
timer_text = current_data[5]
current_timer_text = timer_text
current_moves = current_data[6]
if completed:
@ -638,15 +632,12 @@ 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, timer_text, current_moves
not_shuffled = current_data[2]
timer_text = current_data[5]
current_moves = current_data[6]
global completed, not_shuffled, current_timer_text, current_moves, real_time
do_not_save = False
if pressed_button == close_button:
run = False
save_puzzle_state()
update_puzzle_data(completed, not_shuffled, timer_text, current_moves)
update_puzzle_data(completed, not_shuffled, current_timer_text, current_moves, real_time)
reload_chosen()
elif pressed_button == settings_button:
current_mode = "settings"
@ -667,7 +658,7 @@ def button_check(pressed_button, run, current_mode, timer_running):
quick_game()
current_mode = "game"
not_shuffled = 1
timer_text = "00:00:00"
current_timer_text = "00:00:00"
current_moves = 0
do_not_save = True
elif pressed_button in [not_finished_button, finished_button]:
@ -678,7 +669,7 @@ def button_check(pressed_button, run, current_mode, timer_running):
elif pressed_button == stats_button:
stats()
return run, current_mode, timer_running, not_shuffled, timer_text, current_moves, do_not_save
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)
@ -719,10 +710,12 @@ clicked_image = False
image_display = None
# Puzzle-related stuff
last_shuffle_time = 0
user_win = False
# Timer-related stuff
timer_running = False
timer_start_time = 0
elapsed_time = 0
real_time = elapsed_time
total_paused_time = 0
timer_button.disable()
@ -755,16 +748,10 @@ while run:
selected_image.render(screen)
for event in pygame.event.get():
if event.type == pygame.QUIT:
if current_mode == "game":
if event.type == pygame.QUIT or event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
if current_mode == "game": #is this needed?
save_puzzle_state()
update_puzzle_data(completed, not_shuffled, timer_text, current_moves)
reload_chosen()
run = False
elif event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
if current_mode == "game":
save_puzzle_state()
update_puzzle_data(completed, not_shuffled, timer_text, current_moves)
update_puzzle_data(completed, not_shuffled, current_timer_text, current_moves, real_time)
reload_chosen()
run = False
elif event.type == pygame.MOUSEBUTTONDOWN:
@ -802,7 +789,7 @@ while run:
for button in current_buttons:
if button.rect.collidepoint(start_pos):
pressed_button = button
run, current_mode, timer_running, not_shuffled, timer_text, current_moves, do_not_save = button_check(pressed_button, run, current_mode, timer_running)
run, current_mode, timer_running, do_not_save = button_check(pressed_button, run, current_mode, timer_running)
if settings_data["sound"] == SOUND_ON[0] and current_mode == "game" and non_movable_area.collidepoint(event.pos):
play_sound()
@ -823,18 +810,24 @@ while run:
if timer_running:
current_time = pygame.time.get_ticks() - timer_start_time
elapsed_time = (current_time + total_paused_time) // 1000
timer_text = timer(elapsed_time)
render_text(timer_text, (timer_area.x, timer_area.y), screen, timer_color)
real_time = elapsed_time
hours, minutes, seconds = map(int, timer_text.split(':'))
total_seconds = hours * 3600 + minutes * 60 + seconds
elapsed_time += total_seconds
current_timer_text = timer(elapsed_time)
render_text(current_timer_text, (timer_area.x, timer_area.y), screen, timer_color)
# Puzzle
if pressed_button == shuffle_button and current_time - last_shuffle_time > 500:
if not_shuffled:
np.copyto(puzzle_matrix, original_matrix)
save_puzzle_state()
update_directions()
user_win = False
not_shuffled = 0
else:
shuffle_pieces(puzzle_pieces)
not_shuffled = 0
not_shuffled = 1
save_puzzle_state()
update_directions()
last_shuffle_time = current_time
for row in range(len(puzzle_matrix)):
@ -852,15 +845,51 @@ while run:
if 0 <= clicked_row < len(puzzle_matrix) and 0 <= clicked_col < len(puzzle_matrix[0]):
if puzzle_matrix[clicked_row, clicked_col] != 0:
user_win = True
current_moves += 1
move_pieces(clicked_row, clicked_col)
if pressed_button == hint_button:
screen.blit(original_image, non_movable_area)
if number_of_rows == 4 and puzzle_data[f'current {puzzle_version}']['matrix'] == puzzle_data[f'puzzle {puzzle_version}']:
render_text("You win !!", (win_area.x, win_area.y), screen, timer_color)
if number_of_rows == 4 and puzzle_data[f'current {puzzle_version}']['matrix'] == puzzle_data[f'puzzle {puzzle_version}'] and user_win is True:
completed = 1
not_shuffled = 1
user_win = False
timer_running = False
start_time = pygame.time.get_ticks()
fade_in_end_time = start_time + 2 * 1000
fade_out_start_time = start_time + 4 * 1000
end_time = start_time + 6 * 1000
alpha = 0
text_alpha = 0
fade_in_speed = 100 / (fade_in_end_time - start_time)
fade_out_speed = 100 / (fade_out_start_time - fade_in_end_time)
while pygame.time.get_ticks() < end_time:
surface = pygame.Surface((non_movable_area.width, non_movable_area.height), pygame.SRCALPHA)
surface.fill((0, 0, 0, alpha))
print(alpha)
screen.blit(surface, (non_movable_area.x, non_movable_area.y))
render_text("You win !!", (win_area.x, win_area.y), screen, (255, 255, 255, text_alpha))
if pygame.time.get_ticks() < fade_in_end_time:
alpha += fade_in_speed
text_alpha += fade_in_speed
elif fade_in_end_time <= pygame.time.get_ticks() < fade_out_start_time:
alpha = max(0, alpha)
text_alpha = max(0, text_alpha)
elif pygame.time.get_ticks() >= fade_out_start_time:
alpha -= fade_out_speed
text_alpha -= fade_out_speed
alpha = max(0, alpha)
text_alpha = max(0, text_alpha)
pygame.display.flip()
pygame.time.delay(16)
pygame.display.flip()
pygame.quit()