我有一个敌方角色的代码,其命中框为 20,20 保存到矩形对象“pursrect”中。 我还有详细描述一个 200 像素框的代码,该框直接在其上方不断跟随玩家。 我一直在尝试创造一种情况,一旦敌方角色“pursrect”接触到200 x 200的盒子,就会触发一个半透明的图像列表在屏幕上循环,一旦追击者就停止退出框。 尝试编写的盒子的代码如下:
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 比较陌生,尝试这里有关使图像半透明的其他帖子似乎不起作用。
pygame.Surface.set_alpha
返回None
。您必须在额外的代码行中设置 alpha 值:
glitchingimg = pygame.image.load(f"{cur_dir}/GlitchingImg{random.randint(1, 5)}.png")
glitchingimg.set_alpha(150)
glitcheringimg = pygame.transform.scale(glitchingimg, (800, 800))