所以我试图用pygame写一个小模拟器的东西,但我卡住了
我正在尝试制造掉落并沉淀的粒子,但我希望它们在撞墙时堆叠并停止
这可能听起来令人困惑,所以这里有一个例子:
http://www.coolmath-games.com/0-sugar-sugar/
我想在上面的游戏中制作类似糖的粒子:
我开始尝试:
if pygame.sprite.spritecollideany(self, self.game.block_list):
self.rect.x += 0
self.rect.y += 0
然后颗粒停止,它们不会滚下倾斜的表面
我还需要知道如何检查精灵是否与其组中的任何其他精灵碰撞
所以,如果有人知道如何复制这种液体,就像在pygame中的粒子运动一样,那将是非常棒的!
谢谢!
答案 0 :(得分:2)
正如我上面所说,我试图用pygame编写完全相同的游戏但是没有完成。
首先,我不希望将这些粒子存储为不同的对象。相反,我使用字典来存储他们的坐标。我是这样做的,因为它们有数百个,你必须检查每次碰撞〜每次50次。如果你试图让它们成为不同的物体,它可能会在某些时候失控
在检测到碰撞后,为了使它们向下滚动倾斜表面,让它们对角移动。首先检查下面的单元格,如果该单元格不为空,请检查粒子左下角和右下角的单元格
顺便说一下,我找到了移动粒子的函数,但它实际上并不是可读。
def spawnSugar(spawnPoint) :
global sugarList,mapDict
mapDict[spawnPoint] = 1
sugarList.append(spawnPoint)
def moveAll() :
global mapDict,sugarList
sugarListTmp = sugarList
sugarList = []
for sugarX,sugarY in sugarListTmp :
# m stands for the vertical movement (1 for down, 0 for staying still)
# k stands for horizontal movement (-1 for left, +1 for right)
m = 1
if mapDict[( sugarX , (sugarY+1)%mapSizeY )]==0:
# checks whether the coordinate below this particle is empty
k = randint( -(mapDict[((sugarX-1)%mapSizeX , (sugarY+1)%mapSizeY)]==0) , mapDict[((sugarX+1)%mapSizeX , (sugarY+1)%mapSizeY)]==0 )
# If it is empty; randomly chooses 1 of the 3 coordinates below the particle (1 of them is just below and the other 2 are diagonally below)
elif mapDict[((sugarX-1)%mapSizeX,(sugarY+1)%mapSizeY)]==0 and mapDict[((sugarX-1)%mapSizeX,(sugarY)%mapSizeY)]==0 and mapDict[((sugarX+1)%mapSizeX,(sugarY+1)%mapSizeY)]==0 and mapDict[((sugarX+1)%mapSizeX,(sugarY)%mapSizeY)]==0:
# If the coordinate below the particle is not empty but other 2 diagonals are empty
k = -1 if randint(0,1) else 1 #chooses 1 of them randomly
else : # If at most 1 of these 2 diagonal coordinates are empty
k = (mapDict[((sugarX+1)%mapSizeX,(sugarY+1)%mapSizeY)]==0 and mapDict[((sugarX+1)%mapSizeX,(sugarY)%mapSizeY)]==0) or -(mapDict[((sugarX-1)%mapSizeX,(sugarY+1)%mapSizeY)]==0 and mapDict[((sugarX-1)%mapSizeX,(sugarY)%mapSizeY)]==0)
if not k: # If none of them are empty
m = 0
mapDict[(sugarX,sugarY)] = 0
mapDict[((sugarX+k)%mapSizeX,(sugarY+m)%mapSizeY)] = 1
sugarList.append(((sugarX+k)%mapSizeX,(sugarY+m)%mapSizeY))
# Values to assign before entering the main loop
mapDict = {}
sugarList = []
for x in range(mapSizeX):
for y in range(mapSizeY):
mapDict[(x,y)] = 0