试图做一个图形骰子辊,我不知道为什么'形状'是未定义的

2024-10-03 06:19:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图给它的外观滚动通过使模具出现在屏幕上等待一点,然后取消绘制它,使另一个可以出现。我一直收到一个错误,说“形状”没有定义。刚开始编写代码,所以不要对我太苛刻。你知道吗

from graphics import *
win = GraphWin ("Rolling Dice", 200,200)
from random import *
import time

win.yUp

def One():
    shape = Rectangle(Point(150,150), Point(60,60))
    shape.draw(win)
    middleDot = Circle(Point(105,105), 8)
    middleDot.setFill('black')
    middleDot.draw(win)


def Two():
    shape = Rectangle(Point(150,150), Point(60,60))
    shape.draw(win)
    RightDot = Circle(Point(125,125), 8)
    RightDot.setFill('black')
    RightDot.draw(win)
    LeftDot = Circle(Point(85,85), 8)
    LeftDot.setFill('black')
    LeftDot.draw(win)


def Three():
    shape = Rectangle(Point(150,150), Point(60,60))
    shape.draw(win)
    midDot = Circle(Point(105,108), 8)
    midDot.setFill('black')
    midDot.draw(win)
    topDot = Circle(Point(105,80),8)
    topDot.setFill('black')
    topDot.draw(win)
    botDot = Circle(Point(105,135), 8)
    botDot.setFill('black')
    botDot.draw(win)

def Four():
    shape = Rectangle(Point(150,150), Point(60,60))
    shape.draw(win)
    topleft = Circle(Point(85,85),8)
    topleft.setFill('black')
    topleft.draw(win)
    topright = Circle(Point(125,85),8)
    topright.setFill('black')
    topright.draw(win)
    botleft = Circle(Point(85,125),8)
    botleft.setFill('black')
    botleft.draw(win)
    botright = Circle(Point(125,125),8)
    botright.setFill('black')
    botright.draw(win)

def Five():
    shape = Rectangle(Point(150,150), Point(60,60))
    shape.draw(win)
    topleft = Circle(Point(85,85),8)
    topleft.setFill('black')
    topleft.draw(win)
    topright = Circle(Point(125,85),8)
    topright.setFill('black')
    topright.draw(win)
    botleft = Circle(Point(85,125),8)
    botleft.setFill('black')
    botleft.draw(win)
    botright = Circle(Point(125,125),8)
    botright.setFill('black')
    botright.draw(win)
    middleDot = Circle(Point(105,105), 8)
    middleDot.setFill('black')
    middleDot.draw(win)


 def Six():
    shape = Rectangle(Point(150,150), Point(60,60))
    shape.draw(win)
    topleft = Circle(Point(85,85),8)
    topleft.setFill('black')
    topleft.draw(win)
    topright = Circle(Point(125,85),8)
    topright.setFill('black')
    topright.draw(win)
    botleft = Circle(Point(85,125),8)
    botleft.setFill('black')
    botleft.draw(win)
    botright = Circle(Point(125,125),8)
    botright.setFill('black')
    botright.draw(win)
    left = Circle(Point(85,105),8)
    left.setFill('black')
    left.draw(win)
    right = Circle(Point(125,105),8)
    right.setFill('black')
    right.draw(win)








Dice = [One,Two,Three,Four,Five,Six]

for x in range(1):
     x = randint(1,1)
    if x == 1:
        One()
        time.sleep(.2)
        shape.undraw()

我使用的是python3.6.3,请帮助我的编码老师,他似乎不太会编程。你知道吗


Tags: importdefwinpointblackdrawshapecircle
1条回答
网友
1楼 · 发布于 2024-10-03 06:19:36

for循环中的shape变量不是全局变量,因此需要将其设为全局变量,或者可以将其放入类中:

from graphics import *
win = GraphWin ("Rolling Dice", 200,200)
from random import *
import time

win.yUp
shape = Rectangle(Point(0,0), Point(1,1))

def One():
    global shape
    shape = Rectangle(Point(150,150), Point(60,60))
    shape.draw(win)
    middleDot = Circle(Point(105,105), 8)
    middleDot.setFill('black')
    middleDot.draw(win)


def Two():
    global shape
    shape = Rectangle(Point(150,150), Point(60,60))
    shape.draw(win)
    RightDot = Circle(Point(125,125), 8)
    RightDot.setFill('black')
    RightDot.draw(win)
    LeftDot = Circle(Point(85,85), 8)
    LeftDot.setFill('black')
    LeftDot.draw(win)


def Three():
    global shape
    shape = Rectangle(Point(150,150), Point(60,60))
    shape.draw(win)
    midDot = Circle(Point(105,108), 8)
    midDot.setFill('black')
    midDot.draw(win)
    topDot = Circle(Point(105,80),8)
    topDot.setFill('black')
    topDot.draw(win)
    botDot = Circle(Point(105,135), 8)
    botDot.setFill('black')
    botDot.draw(win)

def Four():
    global shape
    shape = Rectangle(Point(150,150), Point(60,60))
    shape.draw(win)
    topleft = Circle(Point(85,85),8)
    topleft.setFill('black')
    topleft.draw(win)
    topright = Circle(Point(125,85),8)
    topright.setFill('black')
    topright.draw(win)
    botleft = Circle(Point(85,125),8)
    botleft.setFill('black')
    botleft.draw(win)
    botright = Circle(Point(125,125),8)
    botright.setFill('black')
    botright.draw(win)

def Five():
    global shape
    shape = Rectangle(Point(150,150), Point(60,60))
    shape.draw(win)
    topleft = Circle(Point(85,85),8)
    topleft.setFill('black')
    topleft.draw(win)
    topright = Circle(Point(125,85),8)
    topright.setFill('black')
    topright.draw(win)
    botleft = Circle(Point(85,125),8)
    botleft.setFill('black')
    botleft.draw(win)
    botright = Circle(Point(125,125),8)
    botright.setFill('black')
    botright.draw(win)
    middleDot = Circle(Point(105,105), 8)
    middleDot.setFill('black')
    middleDot.draw(win)


 def Six():
    global shape
    shape = Rectangle(Point(150,150), Point(60,60))
    shape.draw(win)
    topleft = Circle(Point(85,85),8)
    topleft.setFill('black')
    topleft.draw(win)
    topright = Circle(Point(125,85),8)
    topright.setFill('black')
    topright.draw(win)
    botleft = Circle(Point(85,125),8)
    botleft.setFill('black')
    botleft.draw(win)
    botright = Circle(Point(125,125),8)
    botright.setFill('black')
    botright.draw(win)
    left = Circle(Point(85,105),8)
    left.setFill('black')
    left.draw(win)
    right = Circle(Point(125,105),8)
    right.setFill('black')
    right.draw(win)

Dice = [One,Two,Three,Four,Five,Six]

for x in range(1):
     x = randint(1,1)
    if x == 1:
        One()
        time.sleep(.2)
        shape.undraw()

相关问题 更多 >