-1
from graphics import*
import time

def moveAll(shapeList,dx,dy):
    for shape in shapeList:
        shape.move(dx,dy)

def moveAllOnLine(shapeList,dx,dy,repititions,delay):
    for i in range(repititions):
        moveAll(shapeList,dx,dy)
        time.sleep(delay)

def main():
    winWidth=300
    winHeight=300
    win=GraphWin('bacnd forth.',winWidth,winHeight)
    win.setCoords(0,0,winWidth,winHeight)
    rect=Rectangle(Point(200,90),Point(220,100))
    rect.setFill("blue")
    rect.draw(win)
    head=Circle(Point(40,100),25)
    head.setFill("blue")
    head.draw(win)
    eye1=Circle(Point(30,105),5)
    eye1.setFill('blue')
    eye1.draw(win)
    eye2=Circle(Point(45,105),Point(55,105))
    eye2.setWidth(3)
    eye2.draw(win)
    mouth=Oval(Point(30,90),Point(50,85))
    mouth.setFill("red")
    eye2.draw(win)
    faceList=[head,eye1,eye2,mouth]
    cir2=Circle(Point(150,125),25)
    cir2.setFill("red")
    cir2.draw(win)
    moveAllOnLine(faceList,5,0,46,.05)
    moveAllOnLine(faceList,-5,0,46,.05)
    Text(Point(winWidth/2,20),'click here to quit').draw(win)
    win.getMouse()
    win.close()

main()
4

1 回答 1

0

此代码未在 IDLE 中执行

这段代码也没有在 IDLE 之外执行,因为它有问题。你没有看到TypeError: unsupported operand type(s) for -: 'float' and 'Point'由于这条线:

eye2=Circle(Point(45,105),Point(55,105))

看来您将 an 切换Oval为 aCircle但没有解决参数。此外,由于复制粘贴错误,您从不这样做mouth.draw(win),而是可能会做两次。eye2.draw(win)

这是在 IDLE 内外运行的程​​序的修改版本:

import time
from graphics import *

def moveAll(shapeList, dx, dy):
    for shape in shapeList:
        shape.move(dx, dy)

def moveAllOnLine(shapeList, dx, dy, repetitions, delay):
    for _ in range(repetitions):
        moveAll(shapeList, dx, dy)
        time.sleep(delay)

def main():
    winWidth, winHeight = 300, 300
    win = GraphWin('bacnd forth.', winWidth, winHeight)
    win.setCoords(0, 0, winWidth, winHeight)

    rect = Rectangle(Point(200, 90), Point(220, 100))
    rect.setFill("blue")
    rect.draw(win)

    head = Circle(Point(40, 100), 25)
    head.setFill("blue")
    head.draw(win)

    eye1 = Circle(Point(30, 105), 5)
    eye1.setFill('blue')
    eye1.draw(win)

    eye2 = Circle(Point(45, 105), 5)
    eye2.setWidth(3)
    eye2.draw(win)

    mouth = Oval(Point(30, 90), Point(50, 85))
    mouth.setFill("red")
    mouth.draw(win)

    faceList = [head, eye1, eye2, mouth]

    cir2 = Circle(Point(150, 125), 25)
    cir2.setFill("red")
    cir2.draw(win)

    moveAllOnLine(faceList, 5, 0, 46, 0.05)
    moveAllOnLine(faceList, -5, 0, 46, 0.05)

    Text(Point(winWidth / 2, 20), 'click here to quit').draw(win)

    win.getMouse()
    win.close()

main()

请注意,图形窗口出现在 IDLE 控制台窗口的后面(至少在我的系统上),因此您需要将控制台移开。

于 2017-07-12T18:31:55.673 回答