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