繁体   English   中英

我的定时运动Zelle graphics.py程序错误

[英]Error in my timed motion Zelle graphics.py program

我试图制作一个程序,在其中输入速度(每秒像素数),因此窗口中的一个点将沿x轴以该精确速度移动。 我输入了速度,但是要点没有移动,IDLE也没有错误提示。

from graphics import *
import time
win=GraphWin("Time", 600, 600)
point=Point(50, 100)
point.setFill("green")
point.draw(win)
speed=Entry(Point(100,50), 15)
speed.setText("Pixels per second")
speed.draw(win)
win.getMouse()
speed1=speed.getText()

speed1=eval(speed1)
t=0.0
time=time.clock()

if time==t+1:
   t+=1
   point.move(speed1, 0)

有人可以告诉我我做错了什么吗? 我正在使用Python 3.4

time.clock()返回的time.clock()是浮点数。 它恰好等于t+1的可能性非常低,以至于您的点很少移动。 代替使用== ,使用>=

if time >= t + 1:
    t += 1
    point.move(speed1, 0)

它不会移动,因为这不是循环:

if time==t+1:
   t+=1
   point.move(speed1, 0)

time不是t+1 == ,也不是>= ,所以它过去了,程序结束了。 您需要的是:

import time
from graphics import *

WINDOW_WIDTH, WINDOW_HEIGHT = 600, 600

win = GraphWin("Time", WINDOW_WIDTH, WINDOW_HEIGHT)

circle = Circle(Point(50, 200), 10)
circle.setFill("green")
circle.draw(win)

speed = Entry(Point(100, 50), 15)
speed.setText("Pixels per second")
speed.draw(win)

win.getMouse()
velocity = float(speed.getText())

t = time.clock()

while circle.getCenter().x < WINDOW_WIDTH:
    if time.clock() >= t + 1:
        t += 1
        circle.move(velocity, 0)

我使用了一个较大的对象,因为很难看到1像素的亮绿色点在移动。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM