python - Pygame 具有类似流体的特征

标签 python pygame physics game-physics

所以我试图用 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

但是粒子就停止了,它们不会滚下倾斜的表面

我还需要知道如何检查 Sprite 是否与其组中的任何其他 Sprite 发生碰撞

因此,如果有人知道如何在 pygame 中复制这种液体(如粒子运动),那就太棒了!

谢谢!

最佳答案

正如我上面所说,我也尝试用 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

关于python - Pygame 具有类似流体的特征,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20086067/

相关文章:

python - pygame.key.get_pressed()-不起作用-pygame.error : video system not initialized

c - -1.#IND0 在 C 中使用 Runge-Kutta 方法时出错

python - 如何在 Python 中将变量打印到文本文件?

python-3.x - Pygame 水物理无法按预期工作

Python 3.5 "ImportError: cannot import name ' 某个名字'

python - 用 Python 在屏幕上显示文本的简单方法?

algorithm - 流体模拟如何集成到刚体 phisix 引擎中?

python - 在 sympy 中,定义 kets 上算子的已知 Action 并用它来简化

python - 如果 GPIO 输入改变,倒计时线程 RPi 停止

python - PyGears 元组和元组之间的区别