简体   繁体   English

如何在pyglet侧滚动器中创建随机的无限地形?

[英]How to make random unlimited terrain in a pyglet side scroller?

I am trying to make a side scroller game using pyglet and I have managed to draw a background, a character and some terrain. 我正在尝试使用pyglet制作侧面滚动游戏,并且设法绘制了背景,角色和某些地形。 (I am pretty new to GUI stuff) The problem is that when the player moves the character, the terrain is meant to generate itself (like games like terraria) but I can't find a way to do it without making 1000 copies of a sprite. (我对GUI的东西还很陌生)问题是,当玩家移动角色时,地形是要自行生成的(例如terraria之类的游戏),但是如果不制作1000份副本,我就找不到办法雪碧。 Do you have any ideas on how I would go about this? 您对我将如何处理有任何想法吗? Feel free to ask questions if I haven't made myself clear :) 如果我还不清楚,请随时提出问题:)

EDIT: What I've been doing so far is creating a variable for each terrain like this: 编辑:到目前为止,我一直在为每个地形创建一个变量,如下所示:

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)

I also created a class for seeing whether the block had a tree on it: 我还创建了一个类来查看块上是否有树:

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)

But I was wondering if I had to create ground2, ground3, ground4... for all the terrain I want or if there is a simpler way of generating it (possibly unlimited) 但是我想知道是否必须为我想要的所有地形创建ground2,ground3,ground4 ... ,或者是否有一种更简单的生成方式(可能是无限的)

Sounds like you should be able to store all these ground objects in a list rather than storing them. 听起来您应该能够将所有这些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

So you'd iterated through this grounds_list when you need to find more stuff. 因此,当您需要查找更多内容时,可以遍历此grounds_list。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM