-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcardgame.py
123 lines (107 loc) · 3.38 KB
/
cardgame.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
#https://www.lesswrong.com/posts/iJDrAuhByc8jJiFCE/constraints-and-slackness-reasoning-exercises
try:
import tkinter as tk
except:
import Tkinter as tk
from random import *
root=tk.Tk()
c = tk.Canvas(root)
c.pack(fill=tk.BOTH,expand=1)
xsz=root.wm_maxsize()[0]//2
pics={text:tk.PhotoImage(file=text+".png") for text in ("attack","defend","draw","energy")}
genemya=lambda:choice([0,0,0,0,1,1,2,3,4,6])
gprops=lambda :[3,0,0,0,10,10,genemya()]#Mana, Attack, Defence, deck pos, health, enemy health,enemy attack
props=gprops()#Mana, Attack, Defence, deck pos, health, enemy health,enemy attack
txtstr="Mana = %s Attack = %s Defence = %s\nDeck = %s Health = %s Enemy Health = %s Enemy Attack = %s"
manatxt=c.create_text(xsz-200,100,text=txtstr%tuple(props),font=("sans",20))
freeze=False
class Card:
cid=0
def __init__(self,t):
self.t=t
self.img=pics[t]
self.cid=Card.cid
Card.cid+=1
self.d=[]
self.vis=False
def show(self,x,y,w,h):
self.d=[c.create_image(x,y,image=self.img)]#[c.create_rectangle(x,y,w,h),
c.tag_bind(self.d[0],"<Button-1>",self.use)
def hide(self):
for i in self.d:
c.delete(i)
def use(self,e=None):
if freeze: return
if props[0]==0:
return #no more mana
props[0]-=1
if self.t=="attack":
props[1]+=1
#print(self.t,self.cid)
elif self.t=="defend":
props[2]+=2
elif self.t=="energy":
props[0]+=2
elif self.t=="draw":
newval=max(props[3]-2,0)
for i in range(newval,props[3]):
cards[i].vis=True
props[3]=newval
self.vis=False
show()
cards=[]#
for i,j in (("attack",6),("defend",6),("draw",4),("energy",4)):
for k in range(j):
cards.append(Card(i))
#Card("attack"),Card("attack"),Card("attack"),Card("defend"),Card("defend"),Card("defend"),Card("draw"),Card("draw")]
#shuffle(cards)
#cards[-1].vis=cards[-2].vis=cards[-3].vis=True
#props[3]=len(cards)-len(#number of starting cards
visCards=lambda :[i for i in cards if i.vis]
def show():
for i in cards:
i.hide()
visC=visCards()
fpos=xsz-len(visC)*40
for i,j in enumerate(visC):
j.show(fpos+i*80,300,0,0)
c.itemconfig(manatxt,text=txtstr%tuple(props))
if len(visC)==0 or props[0]==0:
global freeze
freeze=True
print("reseting")
root.after(800,rejig)
def reset():
shuffle(cards)
for i in cards:
i.vis=False
for i in cards[-5:]:
i.vis=True
#cards[-1].vis=cards[-2].vis=cards[-3].vis=True
props[3]=len(cards)-len(visCards())#number of starting cards
show()
def rejig():
global freeze
freeze=False
props[5]=props[5]-props[1]
props[4]-=max(props[6]-props[2],0)
props[6]=genemya()
props[0:3]=[3,0,0]
rep=None
if props[4]<=0:
rep=tk.messagebox.askyesno("You lose","You lose\nWant to play again?")
if props[5]<=0:
rep=tk.messagebox.askyesno("You win","You win\nWant to play again?")
if rep is True:
global props
props=gprops()
if rep is False:
root.destroy()
return
reset()
reset()
tk.mainloop()
#show()
#card=Card("attack")
#card.show(100,100,2,2)
#c.create_image(200,450,image=pics["draw"])