Pygame 砖在碰撞时消失但在几秒钟内重新出现

问题描述

更新:

我正在制作砖块突破游戏。但是,当球碰到砖块时,砖块消失但立即重新出现。这是与问题相关的代码

def brick_manager():
    bricks_positions = [] # brick list
    # adding the bricks' coordinates to the list
    for row in range(10):
        for col in range(7):
            bricks_positions.append((6 + col*75,44 + row*20))
    for pos in bricks_positions :
        # drawing the bricks from the coordinates in the list
        pygame.draw.rect(screen1,brick_colors[7],(pos[0],pos[1],70,12),border_radius=2)
        if pos[0] <= ball_PX <= pos[0] + 62 and pos[1] -15 <= ball_PY <= pos[1] + 5:
            bricks_positions.remove(pos) 
        

修复:

所以我为解决这个问题所做的是将 brick_manager() 函数的第一部分移到它之外和 while 循环之前。然后一切顺利。事实上,问题在于每次调用函数时,砖块的列表都会重置为空,并且砖块本身会重新绘制,这就是它们在碰撞后重新出现的原因。这是我解决问题后的代码

pygame.init()
...
bricks_positions = [] # brick list
for row in range(10):
    for col in range(7):
        bricks_positions.append((6 + col*75,44 + row*20))

def brick_manager():
    for pos in bricks_positions :
        # drawing the bricks from the coordinates in the list
        pygame.draw.rect(screen1,border_radius=2)
        if pos[0] <= ball_PX <= pos[0] + 62 and pos[1] -15 <= ball_PY <= pos[1] + 5:
        bricks_positions.remove(pos)

...
while running:
    ...
    brick_manager()
    ...
pygame.quit()

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)