Skip to content

Commit dd8e314

Browse files
authored
Merge pull request #22 from fabi200123/improve-card-drop
Increase card drop zone + update winning screen
2 parents e758e7b + 328c6b2 commit dd8e314

File tree

2 files changed

+58
-7
lines changed

2 files changed

+58
-7
lines changed

dist/Solitaire.exe

-33.1 MB
Binary file not shown.

game/game.py

Lines changed: 58 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,12 @@ def on_key_press(self, symbol: int, modifiers: int):
131131
class WinningView(arcade.View):
132132
'''Winning Screen'''
133133

134+
def __init__(self, time_taken: int = 0, moves: int = 0):
135+
'''Initialize the view'''
136+
super().__init__()
137+
self.time_taken = time_taken
138+
self.moves = moves
139+
134140
def on_show_view(self):
135141
'''Called when view is activated'''
136142
# Reset the viewport, necessary if we have a scrolling game and we need
@@ -140,12 +146,29 @@ def on_show_view(self):
140146
def on_draw(self):
141147
'''Draw the view'''
142148
self.clear()
149+
# Draw the timer inside a gray rectangle
150+
minutes = int(self.time_taken // 60)
151+
seconds = int(self.time_taken % 60)
152+
timer_text = f"{minutes:02d}:{seconds:02d}"
153+
143154
arcade.set_background_color(arcade.color.GRAY)
144-
arcade.draw_text("You Won!", self.window.width / 2, self.window.height / 2,
145-
arcade.color.BLACK, font_size=50, anchor_x="center")
146-
arcade.draw_text("Press R to restart", self.window.width / 2, self.window.height / 2 - 75,
147-
arcade.color.BLACK, font_size=20, anchor_x="center")
148-
155+
arcade.draw_text("Somehow you finished the game...", self.window.width / 2, self.window.height / 2,
156+
arcade.color.BLACK, font_size=40, anchor_x="center")
157+
arcade.draw_text("You have successfully wasted...", self.window.width / 2, self.window.height / 2 - 50,
158+
arcade.color.BLACK, font_size=30, anchor_x="center")
159+
# Show the time taken to win the game
160+
arcade.draw_text("Time: ", self.window.width / 2, self.window.height / 2 - 100,
161+
arcade.color.BLACK, font_size=30, anchor_x="center")
162+
arcade.draw_text(timer_text, self.window.width / 2 + 140, self.window.height / 2 - 100,
163+
arcade.color.BLACK, font_size=30, anchor_x="center")
164+
# Show the number of moves to win the game
165+
arcade.draw_text("Moves: ", self.window.width / 2, self.window.height / 2 - 150,
166+
arcade.color.BLACK, font_size=30, anchor_x="center")
167+
arcade.draw_text(str(self.moves), self.window.width / 2 + 140, self.window.height / 2 - 150,
168+
arcade.color.BLACK, font_size=30, anchor_x="center")
169+
arcade.draw_text("Note: Press R to restart", self.window.width / 2, self.window.height / 2 - 350,
170+
arcade.color.LIGHT_GRAY, font_size=20, anchor_x="center")
171+
149172
def on_key_press(self, symbol: int, modifiers: int):
150173
""" If the user presses the mouse button, start the game. """
151174
if symbol == arcade.key.R:
@@ -479,7 +502,7 @@ def on_mouse_release(self, x, y, button, key_modifiers):
479502
pile, distance = arcade.get_closest_sprite(self.held_cards[0], self.pile_mat_list)
480503
reset_position = True
481504

482-
# Check if we are in contact with the closest mat
505+
# Check if we are in contact with the closest mat or the cards in it
483506
if arcade.check_for_collision(self.held_cards[0], pile):
484507

485508
# Get the index of the pile we are over
@@ -552,6 +575,32 @@ def on_mouse_release(self, x, y, button, key_modifiers):
552575
else:
553576
# Reset position of cards
554577
reset_position = True
578+
else:
579+
# Check if we are over a card pile
580+
for pile_index in range(PILE_COUNT):
581+
if len(self.piles[pile_index]) > 0:
582+
top_card = self.piles[pile_index][-1]
583+
if arcade.check_for_collision(self.held_cards[0], top_card):
584+
if pile_index >= PLAY_PILE_1 and pile_index <= PLAY_PILE_7:
585+
if top_card.colour != self.held_cards[0].colour and \
586+
CARD_VALUES.index(top_card.value) - 1 == CARD_VALUES.index(self.held_cards[0].value):
587+
# Move cards to proper position
588+
for i, dropped_card in enumerate(self.held_cards):
589+
dropped_card.position = top_card.center_x, \
590+
top_card.center_y - CARD_VERTICAL_OFFSET * (i + 1)
591+
reset_position = False
592+
self.moves += 1
593+
for card in self.held_cards:
594+
self.move_card_to_new_pile(card, pile_index)
595+
break
596+
elif pile_index >= TOP_PILE_1 and pile_index <= TOP_PILE_4 and len(self.held_cards) == 1:
597+
if top_card.suit == self.held_cards[0].suit and \
598+
CARD_VALUES.index(top_card.value) + 1 == CARD_VALUES.index(self.held_cards[0].value):
599+
self.move_card_to_new_pile(self.held_cards[0], pile_index)
600+
self.held_cards[0].position = top_card.position
601+
reset_position = False
602+
self.moves += 1
603+
break
555604
if reset_position:
556605
# Reset position of the cards
557606
for i, card in enumerate(self.held_cards):
@@ -563,7 +612,7 @@ def on_mouse_release(self, x, y, button, key_modifiers):
563612
# --- Win check
564613
if self.check_winning():
565614
# Show the winning window
566-
view = WinningView()
615+
view = WinningView(self.elapsed_time, self.moves)
567616
self.window.show_view(view)
568617

569618
def on_mouse_motion(self, x: float, y: float, dx: float, dy: float):
@@ -586,6 +635,8 @@ def on_key_press(self, symbol: int, modifiers: int):
586635

587636
def check_winning(self):
588637
'''Check if the player has won the game'''
638+
if len(self.piles[TOP_PILE_1]) > 0:
639+
return True
589640
for pile in self.piles[TOP_PILE_1:]:
590641
if len(pile) != 13:
591642
return False

0 commit comments

Comments
 (0)