Python Pyglet没有打电话给你
我试图做一个简单的游戏,但我有问题 这是我的代码:Python Pyglet没有打电话给你,python,python-3.x,pyglet,Python,Python 3.x,Pyglet,我试图做一个简单的游戏,但我有问题 这是我的代码: from myvector import myVector from car import Car import pyglet width = 1000 height = 600 agent = None agent = Car(int(width/2), int(height/2)) window = pyglet.window.Window() window.set_size(width,height) @window.event
from myvector import myVector
from car import Car
import pyglet
width = 1000
height = 600
agent = None
agent = Car(int(width/2), int(height/2))
window = pyglet.window.Window()
window.set_size(width,height)
@window.event
def on_key_press(symbol, modifiers):
if symbol == 119: # w
agent.applyForce(myVector(-1, 0))
if symbol == 115: # s
agent.applyForce(myVector(1, 0))
if symbol == 97: # a
agent.applyForce(myVector(0, -1))
if symbol == 100: # d
agent.applyForce(myVector(0, 1))
@window.event
def on_draw():
window.clear()
agent.update()
agent.sprite.draw()
print(1)
if __name__ == "__main__":
pyglet.app.run()
问题是,只有当我在键盘上输入内容时,才会调用绘图上的
事件
我正在使用Python3.6和最新的pyglet包
我在网上什么也没找到
为什么会发生这种情况?这可能是decorator函数的问题 使用您自己对该函数的声明替换窗口对象的
on\u draw
函数,而不是装饰该函数上的:
请参见鼠标上的上的此示例,该示例会被自己的声明所回复
@window.event
def on_mouse_press(x, y, button, modifiers):
global state, image
if button == pyglet.window.mouse.LEFT:
print('mouse press')
if state:
state = False
else:
state = True
替换为
import pyglet
image = pyglet.resource.image('test.png')
image.anchor_x = image.width // 2
image.anchor_y = image.height // 2
state = True
def on_draw():
print('on_draw() called')
window.clear()
if state:
image.blit(window.width // 2, window.height // 2)
def on_mouse_press(x, y, button, modifiers):
global state
print('mouse pressed')
if state:
state = False
else:
state = True
window = pyglet.window.Window()
window.on_draw = on_draw
window.on_mouse_press = on_mouse_press
pyglet.app.run()
只有在事件发生时,Pyglet才会在绘图时调用。用于通过计时器连续调用函数。这会导致绘图时的也被触发:
@window.event
def on_draw():
window.clear()
agent.update()
agent.sprite.draw()
印刷品(1)
def更新(dt):
#更新对象
# [...]
通过
如果名称=“\uuuuu main\uuuuuuuu”:
pyglet.clock.schedule_间隔(更新,1/60)#每秒安排60次
pyglet.app.run()文件
您能详细说明您的问题吗?根据您的代码,绘图只应在用户输入时更新。您是否有其他需要间歇性更新的函数?draw函数不应该在循环上运行吗?我习惯于使用javascriptyes的P5js库,只有在您这样设置它的情况下。您可能需要查阅文档以了解如何配置您的项目。您可以建议一个链接吗?