试图制作一个图形骰子滚轮,我不知道为什么'形状'是不确定的

时间:2018-01-12 15:36:13

标签: 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()

我使用python 3.6.3请帮助我的编码老师似乎无能为力,似乎并不知道如何编程。

1 个答案:

答案 0 :(得分:0)

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()