gpt4 book ai didi

python - pyglet 中的平滑运动

转载 作者:行者123 更新时间:2023-12-01 06:53:33 25 4
gpt4 key购买 nike

因此,在我们的游戏中使用 Tkinter 后,我们决定转向 Pyglet。但我遇到的问题之一是运动,在 Tkinter 中我能够制作一个平滑的运动系统,但在 pyglet 中我所能做的就是让角色以一种紧张的方式移动。

import pyglet
from pyglet.window import key, Window
from pyglet import clock
from threading import Timer

X = 5 #Speed of the player
w = 0 #Controls +y movement
a = 0 #Controls -x movement
s = 0 #Controls -y movement
d = 0 #Controls +x movement

window = Window(width = 500, height = 500) #Defines window

MainChar = pyglet.image.load("Mainchar.png") #Loads the Mainchar.png
Player = pyglet.sprite.Sprite(MainChar, x = 0, y = 0) #Makes MainChar into a sprite

@window.event
def on_key_press(symbol, modifiers): #Looks for a keypress
print("keypress")
if symbol == key.W:
global w
global a
global s
global d
w = True
elif symbol == key.A:
a = True
elif symbol == key.S:
s = True
elif symbol == key.D:
d = True

@window.event
def on_key_release(symbol, modifiers):
print("keyup")
if symbol == key.W:
w = False
elif symbol == key.A:
a = False
elif symbol == key.S:
s = False
elif symbol == key.D:
d = False
@window.event
def moveT():
print("cycle")
def moveD():
if w == True:
Player.y += 0.1
elif a == True:
Player.x -= 5
elif s == True:
Player.y -= 5
elif d == True:
Player.x += 5
moveT()
moveTimer = Timer(0.01, moveD)
moveTimer.start()

@window.event
def on_draw():
window.clear()
Player.draw()

moveT()
pyglet.app.run()

我将 w、a、s 和 d 设置为 bool 值的原因是,我可以通过按住按键而不是按键来移动角色。

任何帮助都会很棒,提前致谢!

最佳答案

问题是您使用 python threading 。这不会触发 pyglet 窗口被重绘。
你必须使用pyglet.clock并安排一个函数在每次时钟滴答时调用。这会导致 pyglet 窗口也更新,并触发 on_draw() 事件。例如:

@window.event
def moveT(dt):

if w == True:
Player.y += 0.1
elif s == True:
Player.y -= 5

if a == True:
Player.x -= 5
elif d == True:
Player.x += 5

pyglet.clock.schedule_interval(moveT, 1 / 60)

还必须声明更多 wasd globalon_key_release 中也是如此。

import pyglet
from pyglet.window import key, Window
from pyglet import clock

X = 5 #Speed of the player
w, a, s, d = False, False, False, False

window = Window(width = 500, height = 500) #Defines window

MainChar = pyglet.image.load("Mainchar.png")
Player = pyglet.sprite.Sprite(MainChar, x = 0, y = 0)

@window.event
def on_key_press(symbol, modifiers): #Looks for a keypress
global w, a, s, d
if symbol == key.W:
w = True
elif symbol == key.A:
a = True
elif symbol == key.S:
s = True
elif symbol == key.D:
d = True

@window.event
def on_key_release(symbol, modifiers):
global w, a, s, d
if symbol == key.W:
w = False
elif symbol == key.A:
a = False
elif symbol == key.S:
s = False
elif symbol == key.D:
d = False

@window.event
def moveT(dt):

if w == True:
Player.y += 0.1
elif s == True:
Player.y -= 5

if a == True:
Player.x -= 5
elif d == True:
Player.x += 5

@window.event
def on_draw():
window.clear()
Player.draw()

pyglet.clock.schedule_interval(moveT, 1 / 60)
pyglet.app.run()

关于python - pyglet 中的平滑运动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58900582/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com