提问人:T3CHN0BL4ST 提问时间:10/21/2023 最后编辑:Rabbid76T3CHN0BL4ST 更新时间:10/22/2023 访问量:33
有没有办法仅在发生碰撞时将半透明图像传送到屏幕上?
Is there a way to blit a semi-transparent image to a screen only while collision is occurring?
问:
我有一个敌方角色的代码,该角色的命中框为 20、20 保存到 obj .
我还有一段代码,详细说明了一个 200 像素的盒子,该盒子会一直跟随玩家直接在它们上面走动。
我一直在尝试创造一种情况,一旦敌方角色接触到 200 x 200 的盒子,它就会触发一个半透明的图像列表在屏幕上循环,并在追击者离开盒子后立即停止。
尝试写入的框的代码如下:Rect
pursrect
pursrect
square_of_glitchiness = pygame.Rect(playerx - 100, playery - 100, 200, 200)
pygame.draw.rect(world, (bg1, bg2, bg3), square_of_glitchiness)
if pursrect.colliderect(square_of_glitchiness):
print("collision")
glitchingimg = pygame.image.load(f"{cur_dir}/GlitchingImg{random.randint(1, 5)}.png").set_alpha(150)
glitcheringimg = pygame.transform.scale(glitchingimg, (800, 800))
world.blit(glitcheringimg, ((1, 1)))
pygame.display.update()
if playerx - 100 != square_of_glitchiness.x:
square_of_glitchiness.x = playerx - 100
if playery - 100 != square_of_glitchiness.y:
square_of_glitchiness.y = playery - 100
所有这些代码都包含在主游戏循环
中(变量 bg1、bg2 和 bg3 是表示屏幕背景的外部变量,因此矩形 obj 对玩家来说是不可见的)(
只是文件路径的复制粘贴,这样我就不必经常写它)cur_dir
当我输入此代码时,我收到以下语法错误:
File "c:\School\IST\New folder\Pygame Horror Game\Pygame.py", line 365, in <module>
glitcheringimg = pygame.transform.scale(glitchingimg, (800, 800))
TypeError: argument 1 must be pygame.surface.Surface, not None
我还尝试将 set_alpha(150) 行切换到 convert_alpha(),但这根本没有打印任何东西。
我对 pygame 比较陌生,尝试这里关于使图像半透明的其他帖子似乎不起作用。
答:
2赞
Rabbid76
10/21/2023
#1
pygame。Surface.set_alpha
返回 。您必须在额外的代码行中设置 alpha 值:None
glitchingimg = pygame.image.load(f"{cur_dir}/GlitchingImg{random.randint(1, 5)}.png")
glitchingimg.set_alpha(150)
glitcheringimg = pygame.transform.scale(glitchingimg, (800, 800))
评论