具有Pygam的流动特性

2024-09-30 01:25:32 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我想用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中复制这种类似液体的粒子运动,那就太棒了!在

谢谢你!在


Tags: rectselfcomhttp游戏ifwww粒子
1条回答
网友
1楼 · 发布于 2024-09-30 01:25:32

如上所述,我试图用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

相关问题 更多 >

    热门问题