如何使用类在pyglet中加载图像?

时间:2014-12-10 00:14:21

标签: python class pyglet

由于我的代码中充满了类,因此我创建游戏窗口的实例属于一个类。因此,我没有代码说" @ game_window.event"。有没有办法使用类将图像作为精灵加载?

编辑: 我从pyglet文档中获取了这段代码。但是当我运行游戏时它没有做任何事情,屏幕上没有画出图像。

brick_image = image.load('Brick.png')
brick_image.blit(100, 100,100)

1 个答案:

答案 0 :(得分:0)

class MySprite(pyglet.sprite.Sprite):
    def __init__(self, texture=None, x=None, y=None):
        self.texture = pyglet.image.load(texture)
        super(MySprite, self).__init__(self.texture)

        self.scale = 1.0    
        self.x = x
        self.y = y

    def move(self, x, y):
        self.x += x
        self.y += y

    def _draw(self):
        # Here you can do special draw scenarios
        # before you render your stuff, if you want.
        self.draw()

在您的主要课程中,只需使用您的图片位置'Brick.png'并在您的主课程中启动此操作,然后在渲染功能中调用该句柄上的._draw(),无论(您是什么)在这里缺少很多东西,所以我假设了几件事情。