-
Notifications
You must be signed in to change notification settings - Fork 653
Expand file tree
/
Copy pathcard.py
More file actions
173 lines (151 loc) · 6.54 KB
/
card.py
File metadata and controls
173 lines (151 loc) · 6.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import flet as ft
class Card(ft.GestureDetector):
def __init__(self, solitaire, suite, rank):
super().__init__()
self.solitaire = solitaire
self.suite = suite
self.rank = rank
self.face_up = False
self.slot = None
self.mouse_cursor = ft.MouseCursor.MOVE
self.drag_interval = 5
self.on_pan_update = self.drag
self.on_pan_start = self.start_drag
self.on_pan_end = self.drop
self.on_tap = self.click
self.on_double_tap = self.doubleclick
self.content = ft.Container(
width=70,
height=100,
border_radius=ft.border_radius.all(6),
# content=ft.Image(src=f"/images/card_back.svg"),
content=ft.Image(src=self.solitaire.settings.card_back),
)
def turn_face_up(self):
self.face_up = True
self.content.content.src = f"/images/{self.rank.name}_{self.suite.name}.svg"
self.solitaire.update()
def turn_face_down(self):
self.face_up = False
# self.content.content.src=f"/images/card_back.svg"
self.content.content.src = self.solitaire.settings.card_back
self.solitaire.update()
def can_be_moved(self):
if self.face_up and self.slot.type != "waste":
return True
if self.slot.type == "waste" and len(
self.solitaire.waste.pile
) - 1 == self.solitaire.waste.pile.index(self):
return True
return False
def start_drag(self, e: ft.DragStartEvent):
# if e.control.face_up:
if self.can_be_moved():
cards_to_drag = self.get_cards_to_move()
self.solitaire.move_on_top(cards_to_drag)
# remember card original position to return it back if needed
self.solitaire.current_top = e.control.top
self.solitaire.current_left = e.control.left
self.solitaire.update()
def drag(self, e: ft.DragUpdateEvent):
if self.can_be_moved():
i = 0
for card in self.get_cards_to_move():
card.top = max(0, self.top + e.local_delta.y)
if card.slot.type == "tableau":
card.top += i * self.solitaire.card_offset
card.left = max(0, self.left + e.local_delta.x)
i += 1
self.solitaire.update()
def drop(self, e: ft.DragEndEvent):
if self.can_be_moved():
cards_to_drag = self.get_cards_to_move()
slots = self.solitaire.tableau + self.solitaire.foundation
# check if card is close to any of the tableau or foundation slots
for slot in slots:
# compare with top and left position of the top card in the slot pile
if (
abs(self.top - slot.upper_card_top()) < 40
and abs(self.left - slot.left) < 40
):
if (
slot.type == "tableau"
and self.solitaire.check_tableau_rules(
self, slot.get_top_card()
)
) or (
slot.type == "foundation"
and len(cards_to_drag) == 1
and self.solitaire.check_foundation_rules(
self, slot.get_top_card()
)
):
old_slot = self.slot
for card in cards_to_drag:
card.place(slot)
# reveal top card in old tableau slot if exists
if len(old_slot.pile) > 0 and old_slot.type == "tableau":
old_slot.get_top_card().turn_face_up()
elif old_slot.type == "waste":
self.solitaire.display_waste()
self.solitaire.update()
return
# return card to original position
self.solitaire.bounce_back(cards_to_drag)
self.solitaire.update()
def doubleclick(self, e):
if self.slot.type in ("waste", "tableau"):
if self.face_up:
# self.move_on_top(self.solitaire.controls, [self])
self.solitaire.move_on_top([self])
old_slot = self.slot
for slot in self.solitaire.foundation:
if self.solitaire.check_foundation_rules(self, slot.get_top_card()):
# if True:
self.place(slot)
# if len(old_slot.pile) > 0:
# old_slot.get_top_card().turn_face_up()
# self.solitaire.display_waste()
self.solitaire.update()
return
def click(self, e):
if self.slot.type == "stock":
# first, set the current top 3 cards to invisible
for card in self.solitaire.waste.get_top_three_cards():
card.visible = False
for i in range(
min(self.solitaire.settings.waste_size, len(self.solitaire.stock.pile))
):
top_card = self.solitaire.stock.pile[-1]
# self.move_on_top(self.solitaire.controls, [top_card])
# self.solitaire.move_on_top([top_card])
top_card.place(self.solitaire.waste)
top_card.turn_face_up()
self.solitaire.display_waste()
self.solitaire.update()
if self.slot.type == "tableau":
if self.face_up == False and len(
self.slot.pile
) - 1 == self.slot.pile.index(self):
self.turn_face_up()
def place(self, slot):
self.top = slot.top
self.left = slot.left
if slot.type == "tableau":
self.top += self.solitaire.card_offset * len(slot.pile)
# remove the card form the old slot's pile if exists
if self.slot is not None:
self.slot.pile.remove(self)
# set card's slot as new slot
self.slot = slot
# add the card to the new slot's pile
slot.pile.append(self)
self.solitaire.move_on_top([self])
if self.solitaire.check_if_you_won():
self.solitaire.on_win()
self.solitaire.update()
def get_cards_to_move(self):
"""returns list of cards that will be dragged together, starting with the current card"""
if self.slot is not None:
return self.slot.pile[self.slot.pile.index(self) :]
return [self]