如何使用pyglet库中的batch.draw()绘制精灵

时间:2019-10-22 13:17:36

标签: python graphics pyglet

这似乎是重复的,也许是重复的,但是我使用的是Windows 10,而sprite.py仅将v2i放在一个位置,并且对其进行更改不能解决问题。 也许我使用的批处理图错误,所以我提供了代码。通过on_draw()函数从游戏对象中调用draw()。

class Island:
    def __init__(self):
        self.island = [[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
                       [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
                       [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
                       [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
                       [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
                       [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
                       [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
                       [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
                       [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
                       [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
                       [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
                       [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
                       [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
                       [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
                       [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],]
        self.tile_width = 30
        self.offsetx = 350
        self.offsety = 100
        self.batch = pyglet.graphics.Batch()
        self.water_tile = pyglet.image.load("path")
        self.grass_tile = pyglet.image.load("path")
        self.render_phase = True
    def render(self):
        for i in range(len(self.island)):
            for j in range(len(self.island[i])):
                if self.island[i][j] == 0:
                    x = j*self.tile_width - i*self.tile_width + self.offsetx
                    y = int((j*self.tile_width+i*self.tile_width)/2+self.offsety)
                    pyglet.sprite.Sprite(self.water_tile, x, y, batch=self.batch)
                elif self.island[i][j] == 1:
                    x = j * self.tile_width - i * self.tile_width + self.offsetx
                    y = int((j * self.tile_width + i * self.tile_width) / 2 + self.offsety)
                    pyglet.sprite.Sprite(self.grass_tile, x, y, batch=self.batch)
    def draw(self):
        if self.render_phase:
            self.render()
            self.render_phase = False
        else:
            self.batch.draw()

1 个答案:

答案 0 :(得分:1)

问题是您没有将Sprites分配给全局变量或类变量,因此它可能会在Sprites结束时删除render()(作为本地对象)

def __init__(self):

    self.all_sprites = []

def render(self):

    s = pyglet.sprite.Sprite(self.water_tile, x, y, batch=self.batch)
    self.sprites.append(s)

完整示例-在有和没有self.sprites.append(s)的情况下尝试

import pyglet

class Island:
    def __init__(self):
        self.my_batch = pyglet.graphics.Batch()
        self.image = pyglet.image.load("image.jpg")
        self.all_sprites = []
        self.render()

    def render(self):
        for y in range(0, 100, 30):
            for x in range(0, 100, 30):
                s = pyglet.sprite.Sprite(self.image, x, y, batch=self.my_batch)
                self.all_sprites.append(s)  # <-- try without this line

    def draw(self):
        print('island.draw()')
        self.my_batch.draw()

window = pyglet.window.Window()
island = Island()

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

pyglet.app.run()