-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPyramid.3.01.py
executable file
·344 lines (300 loc) · 11.6 KB
/
Pyramid.3.01.py
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
import random
import datetime
import time
import sys
from tkinter import *
from PIL import Image, ImageTk
class Card:
__values = ("01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13")
__suits = ("c", "s", "d", "h")
def __init__(self, cardImage, value):
self.__value = value
self.__cardImage = cardImage
@property
def value(self):
return self.__value
@property
def cardImage(self):
return self.__cardImage
@staticmethod
def fresh_deck():
cards = []
for v in Card.__values:
for s in Card.__suits:
cards.append(Card(ImageTk.PhotoImage(Image.open("cardset-standard/"+v+s+".gif")), v))
random.shuffle(cards)
return cards
class Deck:
def __init__(self):
self.__deck = Card.fresh_deck()
@property
def deck(self):
return self.__deck
class Drawpile:
def __init__(self, pile, root):
self.k = ImageTk.PhotoImage(Image.open("cardset-standard/"+"back192.gif"))
self.doh_pic = ImageTk.PhotoImage(Image.open("cardset-standard/"+"doh_pic.gif"))
self.pile = pile
def doh(self,root) :
if self.py[7] != [] :
self.py[7][0]["state"] = DISABLED
self.py[7].insert(0,Checkbutton(self.root, text = -100, \
image = self.doh_pic, command = lambda: self._sum(7,0) ,selectimage=self.b))
self.py[7][0].deselect()
self.py[7][0].place(x =250 , y =500)
@property
def get(self):
if len(self.pile) == 1:
self.d.destroy()
return self.pile.pop()
else :
return self.pile.pop()
class Pyramid(Drawpile):
def __init__(self, deck, root, difficulty):
self.count = 2 - difficulty
self.root = root
super().__init__(deck.deck, root)
self.b = ImageTk.PhotoImage(Image.open("cardset-standard/"+"back192.gif"))
self.total = 0
self.score = 0
self.score_plus(0)
self.cod = []
self.__py = []
self.j = Button(root, command=self.count_joker, image=self.b)
self.j.image = self.b
self.joker()
self.d = Button(self.root, command = lambda: self.open_drawing(), image=self.b)
self.d.image = self.b
self.d.place(x =140, y=500)
self.newpy()
def game_clear(self):
if self.__py[0][0] == None:
self.score += self.count * 500
Reader.the_end(self.root,2,self.score)
def score_plus(self,plus_score):
self.score += plus_score
Label(text = "점수: " + str(self.score),font = ("",20), \
bg = "Darkgreen", fg = "White").place(x = 525, y = 25)
def count_joker(self):
self.count-=1
self.joker()
return self.doh(self.root)
def newpy(self):
self.__py = []
for i in range(7):
self.__py.append([])
for j in range(i+1):
p = self.get
if i != 6:
self.__py[i].append(Checkbutton(self.root, text = p.value, image=p.cardImage,
selectimage=self.b))
self.nn(self.__py,i,j)
self.__py[i][j]["state"] = DISABLED
self.__py[i][j].select()
else:
self.__py[i].append(Checkbutton(self.root, text = p.value, image=p.cardImage,
selectimage=self.b))
self.nn(self.__py,i,j)
self.__py[i][j].deselect()
self.__py[i][j].image = p.cardImage
self.__py[i][j].selectimage = self.b
self.__py.append([])
self.open_drawing()
def _sum(self, i, j):
self.cod.append((i,j))
if self.total != 0:
self.total += int(self.py[i][j]["text"])
if self.total == 13 or self.total < 0:
self.total = 0
for x in self.cod:
self.remove(x[0],x[1])
self.score_plus(150)
self.cod = []
self.isfail()
self.game_clear()
else:
self.total = 0
for x in self.cod:
self.__py[x[0]][x[1]].deselect()
self.cod = []
else:
self.total += int(self.py[i][j]["text"])
if self.total == 13:
self.total = 0
for x in self.cod:
self.score_plus(150)
self.remove(x[0],x[1])
self.cod = []
self.isfail()
self.game_clear()
def nn(self, ll, i,j):
ll[i][j]["command"] = lambda: self._sum(i,j)
def open_drawing(self):
tmp = self.get
if tmp != None:
self.py[7].insert(0,Checkbutton(self.root, text = tmp.value, \
image=tmp.cardImage, command = lambda: self._sum(7,0) ,selectimage=self.b))
self.py[7][0].image = tmp.cardImage
self.py[7][0].deselect()
self.py[7][0].place(x =250 , y =500)
self.draw()
self.isfail()
@property
def py(self):
return self.__py
def open(self):
for i in range(0,7):
for j in range(i+1):
if i != 6 and self.isselect(i, j):
self.__py[i][j]["state"] = NORMAL
self.__py[i][j].deselect()
if self.py[7] != []:
self.__py[7][0]["state"] = NORMAL
def print_py(self):
for i in range(7):
x = (6-i) * 80 / 2
y = i * 98 / 2
for j in range(i+1):
if self.__py[i][j] != None:
self.__py[i][j].place(x = x + 80*j + 40, y = 49 + y)
def isselect(self, x, y):
if 0 <= x <= 7 and 0 <= y <= x and self.py[x][y] != None:
if x in [6,7]:
return True
return self.py[x+1][y] == None and self.py[x+1][y+1] == None
else:
return False
def iscorrect(self, x1, y1, x2, y2):
if self.__py[x1][y1]["text"] == "0" or self.__py[x2][y2]["text"] == "0":
return True
else:
return int(self.__py[x1][y1]["text"]) + int(self.__py[x2][y2]["text"]) == 13
# 리팩토링 필요
def remove(self, x, y):
self.py[x][y].destroy()
if x == 7:
if len(self.py[7]) == 1:
self.open_drawing()
self.__py[7] = self.__py[7][:1]
else:
self.py[7] = self.__py[7][1:]
else:
self.__py[x][y] = None
self.open()
self.print_py()
def draw(self):
if self.pile != []:
self.d.place(x =140, y=500)
Label(text =str(len(self.pile)).center(4),font = ("",15), \
bg = "Darkgreen", fg = "White").place(x = 100, y = 580)
def joker(self):
if self.count <= 0:
self.j.destroy()
else:
self.j.place(x =420, y=500)
Label(text =str(self.count),font = ("",15), \
bg = "Darkgreen", fg = "White").place(x = 510, y = 580)
def isfail(self):
open_card_list = []
if self.py[7] != []:
open_card_list = [self.py[7][0]]
check_list = []
if self.count == 0 and self.pile == []:
pyramid_card = []
for i in range(7):
pyramid_card.append(self.py[i])
for i in pyramid_card:
for j in i:
if j != None and j["state"] == NORMAL :
open_card_list.append(j)
for i in open_card_list :
if int(i["text"]) == 13 :
return False
for j in open_card_list :
check_list.append(int(i["text"]) + int(j["text"]))
if 13 in check_list :
return False
else :
Reader.the_end(self.root,1,self.score)
else:
return False
class PyramidController():
def __init__(self, difficulty, root):
"""
컨트롤러가 하는일이 없음...... 이것도 리팩토링 대상
"""
self.root = root
self.difficulty = difficulty
self.canvas = Canvas(root, width=100, height=100, bg='DarkGreen', \
bd=0, highlightthickness=0, relief='ridge')
self.canvas.grid()
self.nowhour = int(time.strftime('%H'))
self.nowminate = int(time.strftime('%M'))
self.nowsecond = int(time.strftime('%S'))
self.Pyramid = Pyramid(Deck(), root, self.difficulty)
self.animate()
self.Pyramid.print_py()
def animate(self):
self.canvas.delete(ALL)
timer = 120 - ((int(time.strftime('%H')) * 3600 + int(time.strftime('%M')) * \
60 + int(time.strftime('%S'))) - (self.nowhour * 3600 + self.nowminate * 60 + self.nowsecond))
if timer == 0:
Reader.the_end(self.root,0,self.Pyramid.score)
else:
self.canvas.after(10, self.animate)
if timer > 50:
self.canvas.create_text(50, 40,text=timer,font = ("",20),fill="White")
elif timer > 10:
self.canvas.create_text(50, 40,text=timer,font = ("",20),fill="Yellow")
else:
self.canvas.create_text(50, 40,text=timer,font = ("",20),fill="Red")
class Reader:
@staticmethod
def difficulty_widgets(root):
frame = Frame(root)
Label(frame, text ='난이도',font = ("",15)).grid(row=0,column=1,pady=6, sticky=S)
difficulty = IntVar()
b1=Radiobutton(frame, text=' 쉬움',
variable = difficulty, value = 0).grid(row = 1, column = 0, padx = 10, pady = 3)
b2=Radiobutton(frame, text=' 보통',
variable = difficulty, value = 1).grid(row = 1, column = 1, padx = 10)
b3=Radiobutton(frame, text='어려움',
variable = difficulty, value = 2).grid(row = 1, column = 2, padx = 10)
Button(frame, text="게임 시작",command = lambda : Reader.closed(root, frame, difficulty.get())
).grid(row=2, column=1, pady=3)
frame.pack(pady=270)
@staticmethod
def the_end(root,n,total_score):
frame = Frame(root)
if n == 0:
Label(frame, text="시간 초과!").grid(row=0,column=1,padx=50,pady=3)
Label(frame, text="당신의 점수는 " + str(total_score) + "점 입니다.") \
.grid(row=1,column=1,padx=50,pady=3)
if n == 1:
Label(frame, text="더 이상 합이 13이 되는 카드가 없습니다.").grid(row=0,column=1,padx=50,pady=3)
Label(frame, text="당신의 점수는 " + str(total_score) + "점 입니다.") \
.grid(row=1,column=1,padx=50,pady=3)
if n == 2:
Label(frame, text="클리어!").grid(row=0,column=1,padx=50,pady=3)
Label(frame, text="당신의 점수는 " + str(total_score) + "점 입니다.") \
.grid(row=1,column=1,padx=50,pady=3)
Button(frame, command = quit, text="OK!").grid(row=2, column=1,padx=10,pady=3)
frame.pack(pady=280)
@staticmethod
def closed(root, frame, num):
frame.pack_forget()
PyramidController(num, root)
@staticmethod
def score(root,total_score):
frame = Frame(root)
Label(frame, text="당신의 점수는 " + str(total_score) + "점 입니다.").grid(row=0,column=1,padx=50,pady=3)
Button(frame, command = quit, text="OK!").grid(row=1, column=1,padx=10,pady=3)
frame.pack(pady=280)
def main():
root = Tk()
root.title("Pyramid Solitaire")
root.geometry("650x650")
root.configure(bg='darkgreen')
Reader.difficulty_widgets(root)
root.mainloop()
main()