我正在尝试使用pyglet制作一个侧滚动游戏,我设法绘制了一个背景,一个角色和一些地形。 (我对GUI的东西很新)问题是,当玩家移动角色时,地形意味着自己生成(就像像terraria这样的游戏),但是如果不制作1000份副本,我就找不到办法精灵。你有什么想法我会怎么做?如果我没有说清楚,请随意提问:)
编辑:到目前为止我一直在做的是为每个地形创建一个变量:
ground_1 = pyglet.sprite.Sprite(ground1, x=-100, y=200, batch = terrain)
ground = pyglet.sprite.Sprite(ground1, 0, y=200, batch = terrain)
ground2 = pyglet.sprite.Sprite(ground1, x=100, y = 200, batch = terrain)
ground3 = pyglet.sprite.Sprite(ground1, x=200, y = 200, batch = terrain)
ground4 = pyglet.sprite.Sprite(ground1, x=300, y = 200, batch = terrain)
ground5 = pyglet.sprite.Sprite(ground1, x=400, y = 200, batch = terrain)
ground6 = pyglet.sprite.Sprite(ground1, x=500, y = 200, batch = terrain)
ground7 = pyglet.sprite.Sprite(ground1, x=600, y = 200, batch = terrain)
ground8 = pyglet.sprite.Sprite(ground1, x=700, y = 200, batch = terrain)
ground9 = pyglet.sprite.Sprite(ground1, x=800, y = 200, batch = terrain)
ground10 = pyglet.sprite.Sprite(ground1, x=900, y = 200, batch = terrain)
我还创建了一个类,用于查看块上是否有树:
class block():
""" block """
def __init__(self,image,tree,grass,destroyed):
self.image = image
self.tree = tree
self.grass = grass
self.destroyed = destroyed
newGround1 = block(newGround,tree[treeR],grass[grassR],destroyed[destroyedR])
groundO = block(ground_1,False,False,False)
groundO1 = block(ground,True,False,False)
groundO2 = block(ground2,False,False,False)
groundO3 = block(ground3,False,False,False)
groundO4 = block(ground4,False,False,False)
groundO5 = block(ground5,False,False,False)
groundO6 = block(ground6,False,False,False)
groundO7 = block(ground7,False,False,False)
groundO8 = block(ground8,False,False,False)
groundO9 = block(ground9,False,False,False)
groundO10 = block(ground10,False,False,False)
但我想知道是否必须为我想要的所有地形创建 ground2,ground3,ground4 ...... ,或者是否有更简单的生成方式(可能无限制)
答案 0 :(得分:0)
听起来你应该能够将所有这些ground
对象存储在列表中而不是存储它们。
#pseudocode
grounds_list = []
for x in range(0, 100000, 100):
ground = pyglet.sprite.Sprite(ground1, x, y=200, batch = terrain) #x gets passed in here
groundO1 = block(ground,True,False,False)
grounds_list.append(ground) #store the objects you want to keep
所以当你需要找到更多的东西时,你会遍历这个grounds_list。