gpt4 book ai didi

python - Pyglet游戏 Action 落后

转载 作者:太空宇宙 更新时间:2023-11-03 21:08:04 26 4
gpt4 key购买 nike

所以,我的蛇做了连续的运动,但如果我按任意键,它就会及时倒退并前后滞后。这是视频:https://youtu.be/KCesu5bGiS8

我的猜测是更快地更新按键输入,但是当我这样做时,所有内容都会更新得更快,因此蛇会跑得更快等等。

代码(按文本形式要求):

import pyglet
import random

pyglet.resource.path = ["resources"]
pyglet.resource.reindex()
# sets the resource path


class Snake_Window(pyglet.window.Window):

a = 0
dtx = 160
dty = 200
# sets the basic direction and snake body x and y

def __init__(self):
super(Snake_Window, self).__init__(width=1280, height=720)
# sets resolution and inherits

self.key_handler = pyglet.window.key.KeyStateHandler()
self.push_handlers(self.key_handler)
# sets keys

self.set_caption("Wild Snake")
# gives it a name

self.background_image = pyglet.resource.image("background.png")
self.food_image = pyglet.resource.image("food.png")
self.snake_head_image = pyglet.resource.image("snake_head.png")
self.snake_body_image = pyglet.resource.image("snake_body.png")
# makes images usable

self.center_image(self.food_image)
self.center_image(self.snake_head_image)
self.center_image(self.snake_body_image)
# centers the images using center_image

self.snake_head = pyglet.sprite.Sprite(img=self.snake_head_image, x=200, y=200)
self.snake_head.scale = 0.1
self.snake_head.rotation = 270
# sets snake_head as a image on screen

self.snake_body = pyglet.sprite.Sprite(img=self.snake_body_image, x=self.dtx, y=self.dty)
self.snake_body.scale = 0.1
self.snake_body.rotation = 90
# sets snake_body as a image on screen

self.background = pyglet.sprite.Sprite(img=self.background_image, x=0, y=0)
# sets background as a image on screen

self.food = []
# sets food

pyglet.clock.schedule_interval(self.game_tick, 0.1)

def center_image(self, image):
# sets the center of the image to the actual center
image.anchor_x = image.width / 2
image.anchor_y = image.height / 2

def update_snake_head(self):
# makes the snake head go and sets the x and y for the body
if self.a == 0:
self.snake_head.x += 40
self.dtx = self.snake_head.x - 40
self.dty = self.snake_head.y
elif self.a == 1:
self.snake_head.x -= 40
self.dtx = self.snake_head.x + 40
self.dty = self.snake_head.y
elif self.a == 2:
self.snake_head.y += 40
self.dty = self.snake_head.y - 40
self.dtx = self.snake_head.x
elif self.a == 3:
self.snake_head.y -= 40
self.dty = self.snake_head.y + 40
self.dtx = self.snake_head.x

def update_snake_body(self, dtx, dty):
# makes the snakes body go
self.snake_body.x = dtx
self.snake_body.y = dty

def game_tick(self, dt):
# updates snakes head, snakes body, key presses and sets the background
self.update_snake_head()
self.update_snake_body(self.dtx, self.dty)
self.draw_elements()
self.key_press()
print(dt)

def draw_elements(self):
# draws everything in window
self.clear()
self.background.draw()
self.snake_head.draw()
self.snake_body.draw()

def key_press(self):
# sets direction of snake upon key press and rotates his head accordingly
if self.key_handler[pyglet.window.key.RIGHT]:
if self.a == 1:
pass
else:
self.a = 0
self.snake_head.rotation = 270
elif self.key_handler[pyglet.window.key.LEFT]:
if self.a == 0:
pass
else:
self.a = 1
self.snake_head.rotation = 90
elif self.key_handler[pyglet.window.key.UP]:
if self.a == 3:
pass
else:
self.a = 2
self.snake_head.rotation = 180
elif self.key_handler[pyglet.window.key.DOWN]:
if self.a == 2:
pass
else:
self.a = 3
self.snake_head.rotation = 0


game_window = Snake_Window()

pyglet.app.run()

最佳答案

将我的所有评论转换为答案。它不会完全解决你的问题。但由于时间不够,我会留下一些有用的东西,至少几乎可以解决这个问题。

造成这些的原因有两个。其中之一是您使用调度程序来渲染内容,而不是使用内置的 on_draw 事件。不能肯定地说,但一个好的猜测是,当您在侧链渲染函数中进行绘图和填充时,图形缓冲区会在 on_draw 中自动更新/翻转。因此,将所有渲染内容移至 on_draw 中是有意义的。

另一个问题是,您不会在实际按键时触发,而是需要在调度程序中的每个刻度上对按键进行计时 - 您也已将其混入渲染函数中。本质上,你是在一个函数的一个集群***中进行事件处理+渲染+更新+IO,呵呵。相反,您应该依赖 on_key_press 来处理键盘事件。

最后,您在各处进行数学运算 - 当您进行实际渲染时,其中任何一个都可能完成了一半。这就是为什么你可能会出现重影或奇怪的伪像(有些事情没有完全完成更新位置等)。

但这里有一个几乎可行的示例,其中说明了为更接近您想要的结果而采取的几个步骤。如果几天后没有人(包括您)解决了这个问题,我将继续重写您的大部分代码,并为您指出一些好的方向(批处理就是其中之一)。

import pyglet
import random

pyglet.resource.path = ["resources"]
pyglet.resource.reindex()
# sets the resource path


class Snake_Window(pyglet.window.Window):

a = 0
dtx = 160
dty = 200
# sets the basic direction and snake body x and y

def __init__(self):
super(Snake_Window, self).__init__(width=1280, height=720)
# sets resolution and inherits

self.key_handler = pyglet.window.key.KeyStateHandler()
self.push_handlers(self.key_handler)
# sets keys

self.set_caption("Wild Snake")
# gives it a name

self.background_image = pyglet.resource.image("background.png")
self.food_image = pyglet.resource.image("food.png")
self.snake_head_image = pyglet.resource.image("snake_head.png")
self.snake_body_image = pyglet.resource.image("snake_body.png")
# makes images usable

self.center_image(self.food_image)
self.center_image(self.snake_head_image)
self.center_image(self.snake_body_image)
# centers the images using center_image

self.snake_head = pyglet.sprite.Sprite(img=self.snake_head_image, x=200, y=200)
self.snake_head.rotation = 270
# sets snake_head as a image on screen

self.snake_body = pyglet.sprite.Sprite(img=self.snake_body_image, x=self.dtx, y=self.dty)
self.snake_body.scale = 0.1
self.snake_body.rotation = 90
# sets snake_body as a image on screen

self.background = pyglet.sprite.Sprite(img=self.background_image, x=0, y=0)
# sets background as a image on screen

self.food = []
# sets food

pyglet.clock.schedule_interval(self.game_tick, 0.1)

def on_draw(self):
self.draw_elements()

def center_image(self, image):
# sets the center of the image to the actual center
image.anchor_x = image.width / 2
image.anchor_y = image.height / 2

def update_snake_head(self):
# makes the snake head go and sets the x and y for the body
if self.a == 0:
self.snake_head.x += 40
self.dtx = self.snake_head.x - 40
self.dty = self.snake_head.y
elif self.a == 1:
self.snake_head.x -= 40
self.dtx = self.snake_head.x + 40
self.dty = self.snake_head.y
elif self.a == 2:
self.snake_head.y += 40
self.dty = self.snake_head.y - 40
self.dtx = self.snake_head.x
elif self.a == 3:
self.snake_head.y -= 40
self.dty = self.snake_head.y + 40
self.dtx = self.snake_head.x

def update_snake_body(self, dtx, dty):
# makes the snakes body go
self.snake_body.x = dtx
self.snake_body.y = dty

def game_tick(self, dt):
# updates snakes head, snakes body, key presses and sets the background
self.update_snake_head()
self.update_snake_body(self.dtx, self.dty)

def draw_elements(self):
# draws everything in window
self.clear()
self.background.draw()
print('Head:', self.snake_head.x, self.snake_head.y, {0:'Right', 1:'Left', 2: 'Up', 3:'Down'}[self.a])
self.snake_head.draw()
self.snake_body.draw()
self.flip()

def on_key_press(self, symbol, modifier):
# sets direction of snake upon key press and rotates his head accordingly
if symbol == pyglet.window.key.ESCAPE:
pyglet.app.exit()
if symbol == pyglet.window.key.SPACE:
print('Here')
if symbol == pyglet.window.key.RIGHT:
if self.a == 1:
pass
else:
self.a = 0
self.snake_head.rotation = 270
elif symbol == pyglet.window.key.LEFT:
if self.a == 0:
pass
else:
self.a = 1
self.snake_head.rotation = 90
elif symbol == pyglet.window.key.UP:
if self.a == 3:
pass
else:
self.a = 2
self.snake_head.rotation = 180
elif symbol == pyglet.window.key.DOWN:
if self.a == 2:
pass
else:
self.a = 3
self.snake_head.rotation = 0


game_window = Snake_Window()
pyglet.app.run()

我不仅会给您留下这个运行良好的代码,而且会给您一个很好的建议。
停止问这么多问题,并开始学习如何调试为什么事情会这样发生。您现在在黑暗中拍摄,提出问题,希望有人能为您解决问题,这样您就可以专注于有趣的事情 - 也就是游戏开发本身。
但你在以后的生活中会有很多用处 - 找到方法来调试、探索、理解和查明为什么事情会按照你想要的方式发生或没有按照你想要的方式发生。

到处放一些print("moo"),打印一些值,在各处添加日志记录/调试,直到你变得更明智。它并不总是有效,但它让我通过你的代码达到了这一点。

关于python - Pyglet游戏 Action 落后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55266542/

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