I have this code for an enemy character that has a hitbox of 20, 20 saved to the Rect
obj pursrect
.
I also have code detailing a 200 pixel box that constantly follows the player around directly on top of them.
I have been trying to create a situation in which once the enemy character pursrect
comes into contact with the 200 x 200 box, it will trigger a semi-transparent list of images to cycle over the screen, and stop as soon as the pursuer exits the box.
The code for the box that tried to write is as follows:
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
All of this code is contained within the main game loop
(The variables bg1, bg2, and bg3 are outside variables that represent the background of the screen so that the rect obj appears invisible to the player)
(cur_dir
was just a copy paste of the filepath so that I didn't have to constantly write it)
When I enter this code, I get this syntax error:
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
I also tried switching the set_alpha(150) line to convert_alpha(), but that just doesn't print anything at all.
I am relatively new to pygame, and trying the other posts here about making images semi-transparent didn't seem to work.
pygame.Surface.set_alpha
returns None
. You have to set the alpha value in an extra line of code:
glitchingimg = pygame.image.load(f"{cur_dir}/GlitchingImg{random.randint(1, 5)}.png")
glitchingimg.set_alpha(150)
glitcheringimg = pygame.transform.scale(glitchingimg, (800, 800))