class Viewer:
def __init__(self, width, height, arm_info, point_info, point_l):
self.list_x = [1,2,3,4]
self.list_y = [5,6,7,8]
self.i = 0
self.arm_info = arm_info
self.point_info = point_info
def on_key_press(self,symbol,modifiers):
if symbol == pyglet.window.key.Z:
self.point_info[:] = [self.list_x[self.i],self.list_y[self.i]]
self.i += 1
这里更新point_info [:]我每次都要按'Z',我只想按Z一次,每1秒更新一次point_info [:]。
def on_key_press(self,symbol,modifiers):
if symbol == pyglet.window.key.Z:
for i in range(0,4):
self.point_info[:] = [self.list_x[i],self.list_y[i]]
time.sleep(1)
我上面已经尝试但它不起作用。我怎么能这样做?
这是完整的代码,从另一个模块调用render方法。
class Viewer(pyglet.window.Window):
def __init__(self, width, height, arm_info, point_info, point_l, mouse_in):
self.list_x = [100,150,200,300,400]
self.list_y = [100,150,200,300,400]
#initialisation of variables
def render(self):
pyglet.clock.tick()
self._update_arm()
self.switch_to()
self.dispatch_events()
self.dispatch_event('on_draw')
self.flip()
def on_draw(self):
#draws on the screen
def _update_arm(self):
#updates the coordinates of the arm , as it moves
def on_key_press(self, symbol, modifiers):
#HERE LIES THE PROBLEM
if symbol == pyglet.window.key.S:
for j in range(0,4):
self.point_info[:] = [self.list_x[j],self.list_y[j]]
print(self.point_info)
#below 2 lines are for drawing on the screen.
self.clear()
self.batch.draw()
j=+1
time.sleep(1)
答案 0 :(得分:1)
我创建了一个小的可运行的示例代码,关于如何实现这一点。关键是,sleep()阻止了程序流程。使用pyglet,您可以使用pyglet.clock.schedule *方法安排将来的执行。您可以使用它来将来调用特定功能。
注意:实际上,仔猪正在其框架中运行一个主循环。如果你把程序保存在某个位置(就像你使用sleep()那样),同时不能再执行代码,因此,如果pyglet需要在绘制调用周围调用一些必需的方法,则不会发生绘图。我想,你不应该自己调用on_draw()方法。
import pyglet
from pyglet.window import key
from pyglet import clock
import random
window = pyglet.window.Window()
label = pyglet.text.Label('Hello, world',
font_name='Times New Roman',
font_size=36,
x=window.width//2, y=window.height//2,
anchor_x='center', anchor_y='center')
def updatePoint(dt):
label.x = random.random() * window.width//2
label.y = random.random() * window.height//2
@window.event
def on_key_press(symbol, modifiers):
if symbol == key.S:
print('The "S" key was pressed.')
for j in range(0,4):
clock.schedule_once(updatePoint, j)
@window.event
def on_draw():
window.clear()
label.draw()
if __name__ == '__main__':
pyglet.app.run()