0

I'm making a game with pygame where the player has to kill some enemies and if they touch your character it dies, but I don't know how to add collision. I have tried with get_rect() but I can't make it work, because I don't really understand how it works and how I can link it with the images.

(slime is the playable character and bads and bads 2 the enemies)

from pygame import transform
from pygame.display import flip
import random
import pygame.freetype
from pygame.sprite import Sprite
from pygame.rect import Rect


pygame.init()
screen = pygame.display.set_mode((1200,800))
screen.fill("light blue")
clock = pygame.time.Clock()



pygame.display.set_caption("Game")


sky = pygame.Surface((1200,800))
sky.fill("light blue")

ground = pygame.image.load(r"img\suelo.png").convert_alpha()
nube1 = pygame.image.load(r"img\nubes.png").convert_alpha()

posx = -300
posy = 80

nube2 = pygame.image.load(r"img\nubes2.png").convert_alpha()


slime = pygame.image.load(r"img\slime2.png").convert_alpha()

rectx = 320
recty = 520

#enemy

bads = pygame.image.load(r"img\bads.png").convert_alpha()

rect1x = -500
rect1y = 520


bads2 = pygame.image.load(r"img\bads2.png").convert_alpha()

rect2x = 1300
rect2y = 520



jump = False
grav = 8



mainLoop = True

while mainLoop:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()

    screen.blit(sky,(0,0))
    screen.blit(ground,(-60,500))
    screen.blit(nube1,(-300,80))
    screen.blit(nube2,(250,10))
    screen.blit(bads,(rect1x,rect1y))
    screen.blit(bads2,(rect2x,rect2y))


    screen.blit(slime,(rectx,recty))

    step = 4.2
    step2 = 2.5
   

    
    
   
    keys = pygame.key.get_pressed()

    if keys[pygame.K_ESCAPE]:
        pygame.quit()
        exit()
    
    if keys[pygame.K_a] and rectx > -200:
        rectx -= step
        slime = pygame.image.load(r"img\slime3.png")

       
    if keys[pygame.K_d] and rectx < 890:
        rectx += step
        slime = pygame.image.load(r"img\slime2.png")


    if jump == False:
        if keys[pygame.K_SPACE]:
            jump = True
           
    else:
        if grav >= -8:
            recty -= grav * 3
            grav -= 0.5
        
        else:
            grav = 8 
            jump = False

    


    rect1x += step2 
    rect2x -= step2

    

    if rect1x > 1090:
        rect1x = random.randrange(-1000,-400)

    if rect2x < -310:   
        rect2x = random.randrange(1100,1800) 

 


        
    
    pygame.display.update()
    clock.tick(70)
    

thks :)

0 Answers0