0

我试图制作一个程序,您可以在其中输入速度(每秒像素数),因此窗口中的一个点将以该精确速度在 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

4

2 回答 2

0

返回的秒数time.clock()是一个浮点数。它碰巧等于的可能性t+1很低,以至于您的点很少移动。而不是使用==,使用>=

if time >= t + 1:
    t += 1
    point.move(speed1, 0)
于 2016-03-13T17:27:05.800 回答
0

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

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

timeis not ==, nor >=,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 像素的亮绿色点在移动。

于 2017-02-01T23:37:19.577 回答