Skip to content

Commit 0d9dc7d

Browse files
authored
Solitaire update (#141)
* fixed issue with card not added to the controls list * fixed issue with card update * fixed card update in solitaire-game-rules * game-rules example fixed * click method for card * fixed card update in final part
1 parent fa74f05 commit 0d9dc7d

File tree

8 files changed

+146
-142
lines changed

8 files changed

+146
-142
lines changed

python/tutorials/solitaire/solitaire-fanned-piles/card.py

Lines changed: 36 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,87 +5,90 @@
55

66
import flet as ft
77

8+
89
class Card(ft.GestureDetector):
910
def __init__(self, solitaire, color):
1011
super().__init__()
11-
self.mouse_cursor=ft.MouseCursor.MOVE
12-
self.drag_interval=5
13-
self.on_pan_start=self.start_drag
14-
self.on_pan_update=self.drag
15-
self.on_pan_end=self.drop
16-
self.left=None
17-
self.top=None
12+
self.mouse_cursor = ft.MouseCursor.MOVE
13+
self.drag_interval = 5
14+
self.on_pan_start = self.start_drag
15+
self.on_pan_update = self.drag
16+
self.on_pan_end = self.drop
17+
self.left = None
18+
self.top = None
1819
self.solitaire = solitaire
1920
self.slot = None
2021
self.card_offset = CARD_OFFSET
2122
self.color = color
22-
self.content=ft.Container(bgcolor=self.color, width=CARD_WIDTH, height=CARD_HEIGTH)
23+
self.content = ft.Container(
24+
bgcolor=self.color, width=CARD_WIDTH, height=CARD_HEIGTH
25+
)
26+
self.draggable_pile = [self]
2327

2428
def move_on_top(self):
2529
"""Brings draggable card pile to the top of the stack"""
2630

27-
for card in self.get_draggable_pile():
31+
# for card in self.get_draggable_pile():
32+
for card in self.draggable_pile:
2833
self.solitaire.controls.remove(card)
2934
self.solitaire.controls.append(card)
3035
self.solitaire.update()
3136

3237
def bounce_back(self):
3338
"""Returns draggable pile to its original position"""
34-
draggable_pile = self.get_draggable_pile()
35-
for card in draggable_pile:
39+
for card in self.draggable_pile:
3640
card.top = card.slot.top + card.slot.pile.index(card) * CARD_OFFSET
3741
card.left = card.slot.left
3842
self.solitaire.update()
3943

4044
def place(self, slot):
4145
"""Place draggable pile to the slot"""
42-
43-
draggable_pile = self.get_draggable_pile()
44-
45-
for card in draggable_pile:
46+
for card in self.draggable_pile:
4647
card.top = slot.top + len(slot.pile) * CARD_OFFSET
4748
card.left = slot.left
4849

49-
# remove card from it's original slot, if exists
50+
# remove card from it's original slot, if it exists
5051
if card.slot is not None:
5152
card.slot.pile.remove(card)
52-
53+
5354
# change card's slot to a new slot
5455
card.slot = slot
5556

5657
# add card to the new slot's pile
5758
slot.pile.append(card)
58-
59+
5960
self.solitaire.update()
6061

6162
def get_draggable_pile(self):
6263
"""returns list of cards that will be dragged together, starting with the current card"""
6364
if self.slot is not None:
64-
return self.slot.pile[self.slot.pile.index(self):]
65-
return [self]
65+
self.draggable_pile = self.slot.pile[self.slot.pile.index(self) :]
66+
else: # slot == None when the cards are dealed and need to be place in slot for the first time
67+
self.draggable_pile = [self]
6668

6769
def start_drag(self, e: ft.DragStartEvent):
70+
self.get_draggable_pile()
6871
self.move_on_top()
69-
self.update()
72+
self.solitaire.update()
7073

71-
7274
def drag(self, e: ft.DragUpdateEvent):
73-
draggable_pile = self.get_draggable_pile()
74-
for card in draggable_pile:
75-
card.top = max(0, self.top + e.delta_y) + draggable_pile.index(card) * CARD_OFFSET
75+
for card in self.draggable_pile:
76+
card.top = (
77+
max(0, self.top + e.delta_y)
78+
+ self.draggable_pile.index(card) * CARD_OFFSET
79+
)
7680
card.left = max(0, self.left + e.delta_x)
77-
card.update()
78-
81+
self.solitaire.update()
7982

8083
def drop(self, e: ft.DragEndEvent):
8184
for slot in self.solitaire.slots:
8285
if (
83-
abs(self.top - (slot.top + len(slot.pile) * CARD_OFFSET))< DROP_PROXIMITY
84-
and abs(self.left - slot.left) < DROP_PROXIMITY
85-
):
86+
abs(self.top - (slot.top + len(slot.pile) * CARD_OFFSET))
87+
< DROP_PROXIMITY
88+
and abs(self.left - slot.left) < DROP_PROXIMITY
89+
):
8690
self.place(slot)
87-
self.update()
91+
self.solitaire.update()
8892
return
89-
93+
9094
self.bounce_back()
91-
self.update()

python/tutorials/solitaire/solitaire-fanned-piles/solitaire.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
1-
#CARD_OFFSET = 20
1+
# CARD_OFFSET = 20
22
SOLITAIRE_WIDTH = 1000
33
SOLITAIRE_HEIGHT = 500
44

55
import flet as ft
6-
from slot import Slot
76
from card import Card
7+
from slot import Slot
8+
89

910
class Solitaire(ft.Stack):
1011
def __init__(self):
1112
super().__init__()
12-
#self.start_top = 0
13-
#self.start_left = 0
13+
# self.start_top = 0
14+
# self.start_left = 0
1415
self.controls = []
1516
self.slots = []
16-
#self.card_offset = CARD_OFFSET
17+
# self.card_offset = CARD_OFFSET
1718
self.width = SOLITAIRE_WIDTH
1819
self.height = SOLITAIRE_HEIGHT
1920

@@ -26,7 +27,8 @@ def create_card_deck(self):
2627
card1 = Card(self, color="GREEN")
2728
card2 = Card(self, color="YELLOW")
2829
card3 = Card(self, color="RED")
29-
self.cards = [card1, card2, card3]
30+
card4 = Card(self, color="BLUE")
31+
self.cards = [card1, card2, card3, card4]
3032

3133
def create_slots(self):
3234
self.slots.append(Slot(top=0, left=0))
@@ -37,6 +39,7 @@ def create_slots(self):
3739

3840
def deal_cards(self):
3941
self.controls.extend(self.cards)
42+
4043
for card in self.cards:
4144
card.place(self.slots[0])
42-
self.update()
45+
self.update()

python/tutorials/solitaire/solitaire-final-part1/card.py

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -29,31 +29,31 @@ def __init__(self, solitaire, suite, rank):
2929
border_radius=ft.border_radius.all(6),
3030
content=ft.Image(src="/images/card_back.png"),
3131
)
32+
self.draggable_pile = [self]
3233

3334
def turn_face_up(self):
3435
"""Reveals card"""
3536
self.face_up = True
3637
self.content.content.src = f"/images/{self.rank.name}_{self.suite.name}.svg"
37-
self.update()
38+
self.solitaire.update()
3839

3940
def turn_face_down(self):
4041
"""Hides card"""
4142
self.face_up = False
4243
self.content.content.src = "/images/card_back.png"
43-
self.update()
44+
self.solitaire.update()
4445

4546
def move_on_top(self):
4647
"""Brings draggable card pile to the top of the stack"""
4748

48-
for card in self.get_draggable_pile():
49+
for card in self.draggable_pile:
4950
self.solitaire.controls.remove(card)
5051
self.solitaire.controls.append(card)
5152
self.solitaire.update()
5253

5354
def bounce_back(self):
5455
"""Returns draggable pile to its original position"""
55-
draggable_pile = self.get_draggable_pile()
56-
for card in draggable_pile:
56+
for card in self.draggable_pile:
5757
if card.slot in self.solitaire.tableau:
5858
card.top = card.slot.top + card.slot.pile.index(card) * CARD_OFFSET
5959
else:
@@ -64,9 +64,7 @@ def bounce_back(self):
6464
def place(self, slot):
6565
"""Place draggable pile to the slot"""
6666

67-
draggable_pile = self.get_draggable_pile()
68-
69-
for card in draggable_pile:
67+
for card in self.draggable_pile:
7068
if slot in self.solitaire.tableau:
7169
card.top = slot.top + len(slot.pile) * CARD_OFFSET
7270
else:
@@ -90,29 +88,30 @@ def place(self, slot):
9088

9189
def get_draggable_pile(self):
9290
"""returns list of cards that will be dragged together, starting with the current card"""
91+
9392
if (
9493
self.slot is not None
9594
and self.slot != self.solitaire.stock
9695
and self.slot != self.solitaire.waste
9796
):
98-
return self.slot.pile[self.slot.pile.index(self) :]
99-
return [self]
97+
self.draggable_pile = self.slot.pile[self.slot.pile.index(self) :]
98+
else: # slot == None when the cards are dealed and need to be place in slot for the first time
99+
self.draggable_pile = [self]
100100

101101
def start_drag(self, e: ft.DragStartEvent):
102102
if self.face_up:
103+
self.get_draggable_pile()
103104
self.move_on_top()
104-
self.update()
105105

106106
def drag(self, e: ft.DragUpdateEvent):
107107
if self.face_up:
108-
draggable_pile = self.get_draggable_pile()
109-
for card in draggable_pile:
108+
for card in self.draggable_pile:
110109
card.top = (
111110
max(0, self.top + e.delta_y)
112-
+ draggable_pile.index(card) * CARD_OFFSET
111+
+ self.draggable_pile.index(card) * CARD_OFFSET
113112
)
114113
card.left = max(0, self.left + e.delta_x)
115-
card.update()
114+
self.solitaire.update()
116115

117116
def drop(self, e: ft.DragEndEvent):
118117
if self.face_up:
@@ -123,38 +122,35 @@ def drop(self, e: ft.DragEndEvent):
123122
and abs(self.left - slot.left) < DROP_PROXIMITY
124123
) and self.solitaire.check_tableau_rules(self, slot):
125124
self.place(slot)
126-
self.update()
127125
return
128126

129-
if len(self.get_draggable_pile()) == 1:
127+
if len(self.draggable_pile) == 1:
130128
for slot in self.solitaire.foundations:
131129
if (
132130
abs(self.top - slot.top) < DROP_PROXIMITY
133131
and abs(self.left - slot.left) < DROP_PROXIMITY
134132
) and self.solitaire.check_foundations_rules(self, slot):
135133
self.place(slot)
136-
self.update()
137134
return
138135

139136
self.bounce_back()
140-
self.update()
141137

142138
def click(self, e):
139+
140+
self.get_draggable_pile()
143141
if self.slot in self.solitaire.tableau:
144-
if not self.face_up and self == self.slot.get_top_card():
142+
if not self.face_up and len(self.draggable_pile) == 1:
145143
self.turn_face_up()
146-
self.update()
147144
elif self.slot == self.solitaire.stock:
148145
self.move_on_top()
149146
self.place(self.solitaire.waste)
150147
self.turn_face_up()
151-
self.solitaire.update()
152148

153149
def doubleclick(self, e):
154-
if self.face_up:
150+
self.get_draggable_pile()
151+
if self.face_up and len(self.draggable_pile) == 1:
155152
self.move_on_top()
156153
for slot in self.solitaire.foundations:
157154
if self.solitaire.check_foundations_rules(self, slot):
158155
self.place(slot)
159-
self.page.update()
160156
return

0 commit comments

Comments
 (0)