首页 > 解决方案 > Pygame“继续前进”IF 语句

问题描述

我是编码和 pygame 的新手,我正在尝试创建一个简单的游戏。我对此有点挣扎:

if player.collidrect(enemy): #If player collides with enemy
    if screen.blit(image1, (110, 0)): #check if this image is on the screen
        screen.blit(image2, (110, 0)) #If it is, put this image on top

(ps不要担心定义这些东西,我的问题与此无关)

我想要发生的是当玩家与敌人发生碰撞时,检查屏幕上是否有“image1”,如果是,则将“image2”放在屏幕上。问题是,它仅在玩家与敌人碰撞时显示“image2”。一旦玩家停止碰撞,图像就会消失。但希望它留在那里。如果你明白,你能帮我找到让'image2'在玩家停止与敌人碰撞的情况下保持在那里的命令吗?谢谢

标签: pythonpygame

解决方案


screen.blit(image1, (110, 0))如果图像在屏幕上不返回,它返回图像在屏幕上的矩形,例如,如果image1是 100x100,它将返回 (110, 0, 100, 100)。您需要找到另一种方法来查看图像是否在屏幕上,当您将图像放在屏幕上时,可能有一个布尔值并将其设置为 True

show_image1 = True
show_image2 = False

if show_image1:
    screen.blit(image1, (110, 0))
if show_image2:
    screen.blit(image2, (110, 0))

if player.collidrect(enemy): #If player collides with enemy
    if show_image1: #check if this image is on the screen
        show_image2 = True #If it is, put this image on top

推荐阅读