微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决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 举报,一经查实,本站将立刻删除。