python - 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'在玩家停止与敌人碰撞的情况下保持在那里的命令吗?谢谢
解决方案
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
推荐阅读
- python - 更改 Gurobi 中的下限
- html - 当我单击播放按钮时,它显示一条错误消息“无法读取 null 的属性 'play'”
- firebase - 如何计算 Firebase 事件中的“每位用户计数”值
- linux - 需要调试 MMAP 那么 MMAP 的目标文件或包含 MMAP() 的模块在哪里——如何在 linux 中找到?
- python - python csv,删除所有逗号,但每行的第一个和最后一个
- c# - 在 xamarin 中滚动 CollectionView 图片时,它的滞后,该怎么办?
- java - Java中不同方式调用变量的影响
- flutter - 颤振:RangeError(索引):无效值:不在包含范围内0..52:53即使有项目计数也会出错
- html - 在移动模式下,主要的 CSS 不必要的空间
- deck.gl - 在 pydeck 中加载本地 TileFile 层