Python 有人能帮我做一个Yahtzee游戏吗?
在我的计算机科学课上,我正在做一个年终项目,我选择用Python和Tkinter创建一个Yahtzee游戏。我已经开始编写代码了,但到目前为止我所做的都不起作用,我不知道为什么,有人能帮我吗 “滚动1”、“滚动2”等后面的代码是为五个骰子上的每个滚动可能性创建点。它们存储在我不太了解的定义中。复选框用于确定玩家想要掷哪个骰子,但是当我测试掷骰按钮是否可以掷1时,它一直不起作用。我还希望复选框水平排列,但由于某些原因,它们是垂直排列的。有人请帮忙Python 有人能帮我做一个Yahtzee游戏吗?,python,checkbox,tkinter,definitions,Python,Checkbox,Tkinter,Definitions,在我的计算机科学课上,我正在做一个年终项目,我选择用Python和Tkinter创建一个Yahtzee游戏。我已经开始编写代码了,但到目前为止我所做的都不起作用,我不知道为什么,有人能帮我吗 “滚动1”、“滚动2”等后面的代码是为五个骰子上的每个滚动可能性创建点。它们存储在我不太了解的定义中。复选框用于确定玩家想要掷哪个骰子,但是当我测试掷骰按钮是否可以掷1时,它一直不起作用。我还希望复选框水平排列,但由于某些原因,它们是垂直排列的。有人请帮忙 from Tkinter import * imp
from Tkinter import *
import random
root = Tk()
drawpad = Canvas(root, width=600, height=600, background='white')
dice1 = drawpad.create_rectangle(10, 10, 110, 110, fill="white")
dice2 = drawpad.create_rectangle(130, 10, 230, 110, fill="white")
dice3 = drawpad.create_rectangle(250, 10, 350, 110, fill="white")
dice4 = drawpad.create_rectangle(370, 10, 470, 110, fill="white")
dice5 = drawpad.create_rectangle(490, 10, 590, 110, fill="white")
check1 = False
check2 = False
check3 = False
check4 = False
check5 = False
# roll 1
roll1 = []
for i in range(1, 6, 1):
x = (120 * i) - 65
y = x + 10
roll1.append(drawpad.create_oval(x, 55, y, 65, fill="red", state=HIDDEN))
# roll 2
roll2 = {}
for i in range(1, 6, 1):
x = (120 * i) - 98
y = x + 10
x2 = (120 * i) - 33
y2 = x2 + 10
roll2[i] = [drawpad.create_oval(x, 23, y, 33, fill="red", state=HIDDEN), drawpad.create_oval(
x2, 87, y2, 97, fill="red", state=HIDDEN)]
# roll3
roll3 = {}
for i in range(1, 6, 1):
x = (120 * i) - 65
y = x + 10
x2 = (120 * i) - 98
y2 = x2 + 10
x3 = (120 * i) - 33
y3 = x3 + 10
roll3[i] = [drawpad.create_oval(x, 55, y, 65, fill="red", state=HIDDEN), drawpad.create_oval(
x2, 23, y2, 33, fill="red", state=HIDDEN), drawpad.create_oval(x3, 87, y3, 97, fill="red", state=HIDDEN)]
# roll4
roll4 = {}
for i in range(1, 6, 1):
x = (120 * i) - 98
y = x + 10
x2 = (120 * i) - 33
y2 = x2 + 10
roll4[i] = [drawpad.create_oval(x, 23, y, 33, fill="red", state=HIDDEN), drawpad.create_oval(x2, 23, y2, 33, fill="red", state=HIDDEN), drawpad.create_oval(
x2, 87, y2, 97, fill="red", state=HIDDEN), drawpad.create_oval(x, 87, y, 97, fill="red", state=HIDDEN)]
# roll5
roll5 = {}
for i in range(1, 6, 1):
x = (120 * i) - 98
y = x + 10
x2 = (120 * i) - 33
y2 = x2 + 10
x3 = (120 * i) - 65
y3 = x3 + 10
roll5[i] = [drawpad.create_oval(x, 23, y, 33, fill="red", state=HIDDEN), drawpad.create_oval(x2, 23, y2, 33, fill="red", state=HIDDEN), drawpad.create_oval(
x2, 87, y2, 97, fill="red", state=HIDDEN), drawpad.create_oval(x, 87, y, 97, fill="red", state=HIDDEN), drawpad.create_oval(x3, 55, y3, 65, fill="red", state=HIDDEN)]
# roll6
roll6 = {}
for i in range(1, 6, 1):
x = (120 * i) - 98
y = x + 10
x2 = (120 * i) - 33
y2 = x2 + 10
roll6[i] = [drawpad.create_oval(x, 23, y, 33, fill="red", state=HIDDEN), drawpad.create_oval(x2, 23, y2, 33, fill="red", state=HIDDEN), drawpad.create_oval(x2, 87, y2, 97, fill="red", state=HIDDEN), drawpad.create_oval(
x, 87, y, 97, fill="red", state=HIDDEN), drawpad.create_oval(x, 55, y, 65, fill="red", state=HIDDEN), drawpad.create_oval(x2, 55, y2, 65, fill="red", state=HIDDEN)]
class MyApp(object):
def __init__(self, parent):
global drawpad
self.myParent = parent
self.myContainer1 = Frame(parent)
self.myContainer1.pack()
# Roll Button
self.rollButton = Button(self.myContainer1)
self.rollButton.configure(text="Roll", background= "green")
self.rollButton.grid(row=0,column=0)
# Stop Button
self.stop = Button(self.myContainer1)
self.stop.configure(text="End Turn", background= "green")
self.stop.grid(row=0,column=1)
# Dice Checkboxes
self.var1 = IntVar()
c = Checkbutton(root, text="Dice 1",variable = self.var1,command=self.cb)
c.grid(row=1,column=0)
self.var2 = IntVar()
c2 = Checkbutton(root, text="Dice 2",variable = self.var2,command=self.cb2)
c2.grid(row=1,column=1)
self.var3 = IntVar()
c3 = Checkbutton(root, text="Dice 3",variable = self.var3,command=self.cb3)
c3.grid(row=1,column=2)
self.var4 = IntVar()
c4 = Checkbutton(root, text="Dice 4",variable = self.var4,command=self.cb4)
c4.grid(row=1,column=3)
self.var5 = IntVar()
c5 = Checkbutton(root, text="Dice 5",variable = self.var5,command=self.cb5)
c5.grid(row=1,column=4)
self.rollButton.bind("<Button-1>", self.rollButtonClick)
c.pack()
c2.pack()
c3.pack()
c4.pack()
c5.pack()
drawpad.pack()
def cb(self):
global check1
if(self.var1.get() == 1):
check1 = True
else:
check1 = False
def cb2(self):
global check2
if(self.var2.get() == 1):
check2 = True
else:
check2 = False
def cb3(self):
global check3
if(self.var3.get() == 1):
check3 = True
else:
check3 = False
def cb4(self):
global check4
if(self.var4.get() == 1):
check4 = True
else:
check4 = False
def cb5(self):
global check5
if(self.var5.get() == 1):
check5 = True
else:
check5 = False
def rollButtonClick(self, event):
global drawpad
global roll1
global check
global check2
global check3
global check4
global check5
dice1=0
dice2=0
dice3=0
dice4=0
dice5=0
diceValues = [dice1,dice2,dice3,dice4,dice5]
if(check1==True):
dice1 = random.randint(1,6)
if(check2==True):
dice2 = random.randint(1,6)
if(check3==True):
dice3 = random.randint(1,6)
if(check4==True):
dice4 = random.randint(1,6)
if(check5==True):
dice5 = random.randint(1,6)
for i in (0,4,1):
if(diceValues[i]==1):
drawpad.itemconfig(roll1[i],state=NORMAL)
app = MyApp(root)
root.mainloop()
从Tkinter导入*
随机输入
root=Tk()
绘图板=画布(根,宽=600,高=600,背景为白色)
dice1=绘图板。创建_矩形(10、10、110、110,fill=“白色”)
dice2=绘图板。创建_矩形(130、10、230、110,fill=“白色”)
dice3=绘图板。创建_矩形(250、10、350、110,fill=“白色”)
dice4=绘图板。创建_矩形(370、10、470、110,fill=“白色”)
dice5=绘图板。创建_矩形(490、10590、110,fill=“白色”)
检查1=错误
检查2=错误
检查3=错误
check4=False
检查5=错误
#第一卷
roll1=[]
对于范围(1,6,1)内的i:
x=(120*i)-65
y=x+10
roll1.append(drawpad.create_oval(x,55,y,65,fill=“red”,state=HIDDEN))
#第二卷
roll2={}
对于范围(1,6,1)内的i:
x=(120*i)-98
y=x+10
x2=(120*i)-33
y2=x2+10
roll2[i]=[drawpad.create_oval(x,23,y,33,fill=“red”,state=HIDDEN),drawpad.create_oval(
x2,87,y2,97,fill=“红色”,state=隐藏)]
#滚轴3
roll3={}
对于范围(1,6,1)内的i:
x=(120*i)-65
y=x+10
x2=(120*i)-98
y2=x2+10
x3=(120*i)-33
y3=x3+10
roll3[i]=[drawpad.create_oval(x,55,y,65,fill=“red”,state=HIDDEN),drawpad.create_oval(
x2,23,y2,33,fill=“红色”,state=隐藏),绘图板。创建椭圆(x3,87,y3,97,fill=“红色”,state=隐藏)]
#滚轴4
roll4={}
对于范围(1,6,1)内的i:
x=(120*i)-98
y=x+10
x2=(120*i)-33
y2=x2+10
roll4[i]=[drawpad.create_oval(x,23,y,33,fill=“red”,state=隐藏),drawpad.create_oval(x2,23,y2,33,fill=“red”,state=隐藏),drawpad.create_oval(
x2,87,y2,97,fill=“红色”,state=隐藏),绘图板。创建椭圆(x,87,y,97,fill=“红色”,state=隐藏)]
#滚5
roll5={}
对于范围(1,6,1)内的i:
x=(120*i)-98
y=x+10
x2=(120*i)-33
y2=x2+10
x3=(120*i)-65
y3=x3+10
roll5[i]=[drawpad.create_oval(x,23,y,33,fill=“red”,state=隐藏),drawpad.create_oval(x2,23,y2,33,fill=“red”,state=隐藏),drawpad.create_oval(
x2,87,y2,97,fill=“红色”,state=隐藏),绘图板。创建椭圆(x,87,y,97,fill=“红色”,state=隐藏),绘图板。创建椭圆(x3,55,y3,65,fill=“红色”,state=隐藏)]
#滚6
roll6={}
对于范围(1,6,1)内的i:
x=(120*i)-98
y=x+10
x2=(120*i)-33
y2=x2+10
滚动6[i]=[drawpad.创建椭圆(x,23,y,33,fill=“red”,state=隐藏),drawpad.创建椭圆(x2,23,y2,33,fill=“red”,state=隐藏),drawpad.创建椭圆(x2,87,y2,97,fill=“red”,state=隐藏),drawpad.创建椭圆(
x、 87,y,97,fill=“red”,state=隐藏),绘图板。创建椭圆(x,55,y,65,fill=“red”,state=隐藏),绘图板。创建椭圆(x2,55,y2,65,fill=“red”,state=隐藏)]
类MyApp(对象):
定义初始化(自身,父级):
全球绘图板
self.myParent=parent
self.myContainer1=帧(父级)
self.myContainer1.pack()
#滚动按钮
self.rollButton=按钮(self.myContainer1)
self.rollButton.configure(text=“Roll”,background=“green”)
self.rollButton.grid(行=0,列=0)
#停止按钮
self.stop=按钮(self.myContainer1)
自动停止配置(text=“结束转弯”,background=“绿色”)
self.stop.grid(行=0,列=1)
#骰子复选框
self.var1=IntVar()
c=检查按钮(root,text=“Dice 1”,变量=self.var1,命令=self.cb)
c、 网格(行=1,列=0)
self.var2=IntVar()
c2=检查按钮(root,text=“Dice 2”,变量=self.var2,命令=self.cb2)
c2.网格(行=1,列=1)
self.var3=IntVar()
c3=检查按钮(root,text=“Dice 3”,变量=self.var3,命令=self.cb3)
c3.网格(行=1,列=2)
self.var4=IntVar()
c4=检查按钮(root,text=“Dice 4”,变量=self.var4,命令=self.cb4)
c4.网格(行=1,列=3)
self.var5=IntVar()
c5=检查按钮(root,text=“Dice 5”,变量=self.var5,命令=self.cb5)
c5.网格(行=1,列=4)
self.rollButton.bind(“,self.rollButton单击)
c、 包()
c2.pack()
c3.pack()
c4.pack()
c5.包装()
drawpad.pack()
def cb(自我):
全局检查1
如果(self.var1.get()==1):
check1=True
其他:
检查1=错误
def cb2(自身):
全球检查2
如果(self.var2.get()==1):
check2=True
其他:
检查2=错误
def cb3(自身):
全球检查3
如果(self.var3.get()==1):
检查3=正确
其他:
检查3=错误
def cb4(自身):
全球检查4
如果(self.var4.get()==1):
check4=True
其他:
check4=False
def cb5(自身):
全球检查5
如果(self.var5.get()==1):
检查5=正确
其他:
检查5=错误
def ROLL按钮单击(自身,事件):
全球绘图板
全局滚动1
全局检查
全球检查2
全球检查3
全球检查4
全球检查5
1=0
2=0
3=0
dice4=0
5=0
diceValues=[dice1,dice2,dice3,dice4,dic
<import the Die class, or define it here>
root = Tk()
drawpad = Canvas(...)
die = Die(drawpad, x, y) # draw a die at coordinate (x,y)
die.roll() # roll the die, and redraw it with the value
print(die.value()) # print out the value
from Tkinter import *
import random
class OneDie():
def __init__(self, drawpad, x1, x2, color):
self.drawpad=drawpad
self.x1=x1
self.x2=x2
self.color=color
self.dots_location={1:[[0.5, 0.5]],
2:[[0.25, 0.25], [0.75, 0.75]],
3:[[0.25, 0.25], [0.5, 0.5], [0.75, 0.75]],
4:[[0.25, 0.25], [0.25, 0.75], [0.75, 0.25], [0.75, 0.75]],
5:[[0.5, 0.5], [0.25, 0.25], [0.25, 0.75], [0.75, 0.25], [0.75, 0.75]],
6:[[0.25, 0.25], [0.25, 0.75], [0.75, 0.25], [0.75, 0.75], [0.50, 0.25], [0.50, 0.75]]}
self.die = self.drawpad.create_rectangle(x1, 10, x2, 110, fill="red")
self.update_this_die(6) ## initialize every die to 6
def roll_this_die(self):
num=random.randint(1, 6)
self.update_this_die(num)
def update_this_die(self, num):
self.drawpad.delete(self.die)
self.die = self.drawpad.create_rectangle(self.x1, 10, self.x2, 110, fill="red")
location_list = self.dots_location[num]
for ctr in range(len(location_list)):
multiplier_x_y = location_list[ctr]
x=(self.x2-self.x1)*multiplier_x_y[0]+self.x1-10 ## 10 is 1/2 of circle size
y=(110-10)*multiplier_x_y[1]
self.drawpad.create_oval(x, y, x+20, y+20, fill=self.color, outline="red")
root = Tk()
drawpad = Canvas(root, width=600, height=300)
drawpad.grid()
Button(root, text="Exit", command=root.quit, bg="orange").grid(row=100)
instance_list=[]
x1=10
x2=110
colors=["white", "lightblue", "yellow", "black", "green"] ## identify each die while testing
for ctr in range(5):
OD=OneDie(drawpad, x1, x2, colors[ctr])
x1 += 120
x2 += 120
instance_list.append(OD)
## roll each die once
for instance in instance_list:
instance.roll_this_die()
root.mainloop()