Skip to content

Commit

Permalink
solitaire: fix bouncing and animation.
Browse files Browse the repository at this point in the history
  • Loading branch information
mfitzp committed Feb 19, 2018
1 parent 6553479 commit 7ca39a4
Showing 1 changed file with 16 additions and 15 deletions.
31 changes: 16 additions & 15 deletions solitaire/solitaire.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
SIDE_FACE = 0
SIDE_BACK = 1

SLIPPING_OFFSET = 20
DEAL_N = 3
BOUNCE_ENERGY = 0.8

# We store cards as numbers 1-13, since we only need
# to know their order for solitaire.
Expand Down Expand Up @@ -187,14 +186,6 @@ def remove_all_cards(self):
card.stack = None
self.cards = []

def take_top_card(self):
try:
card = self.cards[-1]
self.remove_card(card)
return card
except IndexError:
pass

def is_valid_drop(self, card):
return True

Expand Down Expand Up @@ -239,6 +230,14 @@ def restack(self, fromstack):
self.add_card(card)
card.turn_back_up()

def take_top_card(self):
try:
card = self.cards[-1]
self.remove_card(card)
return card
except IndexError:
pass

def set_color(self, color):
color = QColor(color)
color.setAlpha(50)
Expand Down Expand Up @@ -636,18 +635,20 @@ def check_win_condition(self):
def win_animation(self):
# Start off a new card
for drop in self.drops:
card = drop.take_top_card()
if card and card.vector is None:
card.vector = QPoint(-random.randint(1, 10), 0)
break
if drop.cards:
card = drop.cards.pop()
if card.vector is None:
card.vector = QPoint(-random.randint(1, 10), 0)
break

for card in self.deck:
if card.vector is not None:
card.setPos(card.pos() + card.vector)
card.vector += QPoint(0, 1) # Gravity

if card.pos().y() > WINDOW_SIZE[1] - CARD_DIMENSIONS.height():
card.vector = QPoint(card.vector.x(), -card.vector.y() )
# Bounce the card, losing some energy.
card.vector = QPoint(card.vector.x(), -int(card.vector.y() * BOUNCE_ENERGY) )

if card.pos().x() < - CARD_DIMENSIONS.width():
card.vector = None
Expand Down

0 comments on commit 7ca39a4

Please sign in to comment.