0

i'm currentely making a platform game and i'm blocked for the collisions... Can you help me ? I actually added some blocs to test the collisions, created a player under the class Perso. I really need to make work the collision to make after that portal and also have to elaborate a gravity to make the player jump. Here is my code: import pygame from pygame.locals import * pygame.init()

#pour créer une fenêtre fenetre = pygame.display.set_mode((1024,768)) pygame.display.set_caption("Portal Escape") #pour créer un perso class Perso(pygame.sprite.Sprite): def __init__(self): #self.image = pygame.Surface((100,100)) self.image = pygame.image.load("perso.png").convert_alpha() self.position = pygame.Rect((3,660),(100,100)) self.rect = self.image.get_rect() perso = Perso() #pour creer un obstacle et éléments du jeu def bloc(): bloc=pygame.Surface((200,200)) bloc_rect = bloc.get_rect() bloc.fill((0, 0, 255)) fenetre.blit(bloc, (500,600)) red=pygame.Surface((200,500)) red_rect = red.get_rect() red.fill((255, 0, 0)) fenetre.blit(red, (250,300)) portal=pygame.Surface((20,120)) #création d'un bloc portail portal_rect = portal.get_rect() portal.fill((0, 255, 0)) fenetre.blit(portal, (250,650)) blocx = [bloc,red,portal] #Boucle du menu continuer = 1 while continuer: accueil = pygame.image.load("menu.jpg").convert() fenetre.blit(accueil, (0,0)) pygame.display.flip() continuer_jeu = 1 continuer_accueil = 1 choix = 1 #Boucle de l'accueil while continuer_accueil: pygame.time.Clock().tick(30) #limite les frames de la boucle for event in pygame.event.get(): if event.type == QUIT: #quitter le jeu continuer_accueil = 0 continuer_jeu = 0 continuer = 0 elif event.type == KEYDOWN: #choix du menu if event.key == K_F1: choix = 1 continuer_accueil = 0 #Boucle du choix if choix != 0: fond = pygame.image.load("fond.jpg").convert() fenetre.blit(fond, (0,0)) fenetre.blit(perso.image, perso.position) pygame.display.flip() pygame.key.set_repeat(400, 30) #accelérer le deplacement quand la touche est enfoncée bloc() while continuer: for event in pygame.event.get(): #Attente des événements if event.type == QUIT: continuer = 0 if event.type == KEYDOWN: if event.key == K_DOWN and perso.position.y < 660: fenetre.blit(perso.image, perso.position) if perso.position.colliderect(blocx): print("collide") else: perso.position.y += 3 if event.key == K_UP: perso.position.y -= 3 if event.key == K_RIGHT and perso.position.x < 920: perso.position.x += 3 if event.key == K_LEFT and perso.position.x > 3: perso.position.x -= 3 #on appelle les fonctions du jeu, le fond et les perso fenetre.blit(fond, (0,0)) fenetre.blit(perso.image, perso.position) bloc() pygame.display.flip() 

pygame.quit()

1

1 Answer 1

0

This line:

if perso.position.colliderect(blocx): 

Should take the Rect of the surface:

if perso.position.colliderect(bloc.get_rect()): 
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks the code worked but not dispaying "collide" in the console when touching the "bloc". Is it normal?
Ah yes, it is possible that you will need to call bloc.get_rect() at the time of collision. I have updated my answer accordingly. See if that fixes it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.