# coding=utf-8
import graphics
import time
import random
class Button:#按键类
def __init__(self,width=None,height=None,x=None,y=None,text=None):
"""
按键类
:param width:宽
:param height: 高
:param x: 中心坐标:x
:param y: 中心坐标:y
:param text: 文字
"""
self.width=width
self.height=height
self.x=x
self.y=y
self.text=text
self.p = graphics.Point(x, y)
d1 = self.p.clone()
d2 = self.p.clone()
d1.move(-self.width/2,-self.height/2)
d2.move(self.width / 2, self.height / 2)
self.ct=graphics.Rectangle(d1, d2)# 创建一个长方形对象,一个左上角点,一个右下角点
self.text_dx = graphics.Text(self.p, self.text) # 创建一个文本对象,位置为点对象
def day(self,win):#绘制到指定窗口
self.text_dx.draw(win)
self.ct.draw(win)
def dianj(self,p):#判断点击事件
p1=self.ct.getP1()
p2=self.ct.getP2()
if p.getX()>=p1.getX() and p.getX()<=p2.getX():
if p.getY()>=p1.getY() and p.getY()<=p2.getY():
return True
return False
class shaizi:
def __init__(self,width=None,x=None,y=None,size=None):
self.width = width
self.x = x
self.y = y
self.size=size
self.p = graphics.Point(x, y)
d1 = self.p.clone()
d2 = self.p.clone()
d1.move(-self.width / 2, -self.width / 2)
d2.move(self.width / 2, self.width / 2)
self.ct = graphics.Rectangle(d1, d2) # 创建一个长方形对象,一个左上角点,一个右下角点
#骰子点数
self.dian=[]
self.dict={}
#点数为1
list_a=[]
list_a.append(self.p)
self.dian.append(list_a)
#点数为二
list_b = []
b1=self.p.clone()
b2=self.p.clone()
b1.move(-self.width/4,-self.width/4)
b2.move(self.width / 4, self.width / 4)
list_b.append(b1)
list_b.append(b2)
self.dian.append(list_b)
# 点数为3
list_c = list_b.copy()
list_c.append(self.p)
self.dian.append(list_c)
# 点数为4
list_d = list_b.copy()
d1 = self.p.clone()
d2 = self.p.clone()
d1.move(-self.width / 4, self.width / 4)
d2.move(self.width / 4, -self.width / 4)
list_d.append(d1)
list_d.append(d2)
self.dian.append(list_d)
# 点数为5
list_e = list_d.copy()
list_e.append(self.p)
self.dian.append(list_e)
# 点数为6
list_f = []
f1 = self.p.clone()
f2 = self.p.clone()
f3 = self.p.clone()
f4 = self.p.clone()
f5 = self.p.clone()
f6 = self.p.clone()
f1.move(-self.width/3,-self.width/3)
f2.move(-self.width / 3, 0)
f3.move(-self.width / 3, self.width / 3)
f4.move(self.width / 3, -self.width / 3)
f5.move(self.width / 3, 0)
f6.move(self.width / 3, self.width / 3)
list_f=[f1,f2,f3,f4,f5,f6]
self.dian.append(list_f)
#点数半径
self.r=self.width/8
def day(self,win,s):#画点数
self.ct.draw(win)
si=0
for j in self.dian[s]:
self.dict[si] = graphics.Circle(j,self.r) # 创建一个圆对象,半径为r,位置为点对象
self.dict[si].setFill(graphics.color_rgb(0, 0, 0)) # 填充颜色设置颜色为黑色
self.dict[si].draw(win)
si+=1
def xch(self):#消除上一个点数
for i in self.dict:
self.dict[i].undraw()#从窗口中移除
self.ct.undraw()
self.dict.clear()
def xaoshiz(self,win):#摇色子
for i in range(10):
j=random.randint(0,5)
self.xch()
self.day(win,j)
time.sleep(0.1)
if __name__ == '__main__':
win=graphics.GraphWin("qin",800,600)
b1=Button(150,60,200,500,"退出")#退出按键
b1.day(win)#绘制到指定窗口
b2 = Button(150, 60, 600, 500, "摇骰子") # 摇骰子按键
b2.day(win) # 绘制到指定窗口
b3=Button(200, 200,400,300)#输出骰子的边框
b3.day(win)
shai=shaizi(100,400,300)#骰子对象
while(1):
i=win.getMouse()#获取鼠标按键
if b1.dianj(i):# 退出按键
break
elif b2.dianj(i):#摇骰子按键
shai.xaoshiz(win)
运行结果: