Skip to content

Commit

Permalink
chore: fix card placement bug and celebrations
Browse files Browse the repository at this point in the history
Signed-off-by: Namit Arjaria <[email protected]>
  • Loading branch information
depleur committed Jul 27, 2024
1 parent 04c293b commit fe8eb0d
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 9 deletions.
33 changes: 26 additions & 7 deletions game.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
import pygame
from updater import Updater
from tkinter import messagebox
from win_celebration import create_win_celebration

CURRENT_VERSION = "v1.0.15-alpha"
CURRENT_VERSION = "v1.0.16-alpha"


class Card:
Expand Down Expand Up @@ -73,6 +74,8 @@ def __init__(self, master):
self.zoom_factor = self.rules_manager.get_zoom_factor()
self.apply_zoom()

self.win_celebration = create_win_celebration(self)

# Apply fullscreen preference
if self.rules_manager.get_is_fullscreen():
self.master.attributes("-fullscreen", True)
Expand Down Expand Up @@ -527,16 +530,14 @@ def update_game_state(self, dragged_item):
dragged_cards = self.drag_data["cards"]
x, y = self.game_canvas.coords(dragged_item)

target_house_index = min(9, max(0, int((x - 60) / (self.card_width + 20))))
target_house = self.houses[target_house_index]

source_house = self.drag_data["source_house"]
target_house, target_house_index = self.find_nearest_house(x, y)

if source_house != target_house and self.is_valid_move(
dragged_cards, target_house
):
self.move_card(dragged_cards, source_house, target_house)
self.update_move_count() # Add this line
self.update_move_count()

self.display_cards()

Expand All @@ -547,6 +548,23 @@ def update_game_state(self, dragged_item):
self.status_var.set("Game over. No more moves possible. Try again!")
self.deal_button.config(state=tk.NORMAL)

def find_nearest_house(self, x, y):
min_distance = float("inf")
nearest_house = None
nearest_index = -1

for i, house in enumerate(self.houses):
house_x = 60 + i * (self.card_width + 20)
house_y = 20
distance = ((x - house_x) ** 2 + (y - house_y) ** 2) ** 0.5

if distance < min_distance:
min_distance = distance
nearest_house = house
nearest_index = i

return nearest_house, nearest_index

def on_card_release(self, event):
if self.drag_data["item"] and self.drag_data["cards"]:
self.update_game_state(self.drag_data["item"])
Expand Down Expand Up @@ -651,8 +669,9 @@ def check_win(self):
):
valid_house_count += 1

if valid_house_count == 4:
return True
if valid_house_count == 4:
self.win_celebration.show_celebration(self.move_count)
return True

return False

Expand Down
4 changes: 2 additions & 2 deletions updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ def check_for_updates(self):
return False

def is_newer_version(self, latest_version):
current = [int(x) for x in self.current_version[1:].split(".")]
latest = [int(x) for x in latest_version[1:].split(".")]
current = [x for x in self.current_version[1:].split(".")]
latest = [x for x in latest_version[1:].split(".")]
return latest > current

def notify_update_available(self, new_version, download_url):
Expand Down
31 changes: 31 additions & 0 deletions win_celebration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import tkinter as tk
from tkinter import messagebox


class WinCelebration:
def __init__(self, game):
self.game = game

def show_celebration(self, move_count):
message = (
f"Excellent work! You have beaten Patience.\n\n"
f"You took {move_count} moves to complete this game.\n\n"
f"Click 'Redeal' if you would like to better your score, \n"
f"'Yay!' if you want to close this window, or\n"
f"'Clear Board' if you would like to start afresh."
)

result = messagebox.askquestion(
"Congratulations!", message, type="yesnocancel", icon="info", default="yes"
)

if result == "yes": # Redeal
self.game.redeal_cards()
elif result == "no": # Yay! (close window)
pass # Dialog will close automatically
else: # Clear Board
self.game.clear_board()


def create_win_celebration(game):
return WinCelebration(game)

0 comments on commit fe8eb0d

Please sign in to comment.