[英]Error in my timed motion Zelle graphics.py program
I tried to make a program into which you enter a speed (pixels per second), so a point in the window will move at that exact speed on the x axis. 我试图制作一个程序,在其中输入速度(每秒像素数),因此窗口中的一个点将沿x轴以该精确速度移动。 I enter the speed, but the point isn't moving and IDLE does not complain with an error.
我输入了速度,但是要点没有移动,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)
Can someone tell me what I did wrong here? 有人可以告诉我我做错了什么吗? I'm using Python 3.4
我正在使用Python 3.4
The number of seconds returned by time.clock()
is a floating-point number. time.clock()
返回的time.clock()
是浮点数。 The likelihood that it will happen to be equal to t+1
is low enough that your point very rarely moves. 它恰好等于
t+1
的可能性非常低,以至于您的点很少移动。 Instead of using ==
, use >=
: 代替使用
==
,使用>=
:
if time >= t + 1:
t += 1
point.move(speed1, 0)
It doesn't move because this is not a loop: 它不会移动,因为这不是循环:
if time==t+1:
t+=1
point.move(speed1, 0)
time
isn't ==
, nor >=
, to t+1
so it goes past and the program is finished. time
不是t+1
==
,也不是>=
,所以它过去了,程序结束了。 What you need is: 您需要的是:
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)
I used a larger object as it was just too hard to see the 1 pixel bright green point moving. 我使用了一个较大的对象,因为很难看到1像素的亮绿色点在移动。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.