Pyglet游戏动作落后

时间:2019-03-20 17:10:17

标签: python class pyglet

因此,我的蛇会连续运动,但是如果我按任何键,它会及时返回并前后移动。这是一个视频: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()

1 个答案:

答案 0 :(得分:0)

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

这些的原因是一对。其中之一是您使用调度程序来呈现内容,而不是使用内置的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"),打印一些值,在各处添加日志记录/调试,直到您变得更聪明为止。它并不总是有效的,但是使用您的代码使我到了这一点。