i am currentely making my final project for my exam and i'm making a platform game. Actually i'm stuck at the collisions because as you know in a platform game i need a different reaction for the upper, right and left side. I'd like to make position +10 if it collides on the left of the block or -10 if it's the right side. I tried a code but it only works on the top of the block so that's why i need your help! Could you help me to solve this issue?
import pygame
from pygame.locals import *
pygame.init()
fenetre = pygame.display.set_mode((1024,768))
pygame.display.set_caption("Portal Escape")
class Perso(pygame.sprite.Sprite):
def __init__(self):
self.image = pygame.image.load("perso.png").convert_alpha()
self.position = pygame.Rect((3,660),(100,100))
self.rect = self.image.get_rect()
self.doit_monter = False
def monte(self):
self.position.y -= 5
def descend(self):
self.position.y += 1
perso = Perso()
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
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:
pygame.time.Clock().tick(30)
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)
new_y = 0
noir=pygame.draw.rect(fenetre,(0, 0, 0),(200,500,250,300))
bleu=pygame.draw.rect(fenetre,(0, 0, 255),(500,600,200,200))
portal=pygame.draw.rect(fenetre,(0, 255, 0),(250,700,50,120))
sol=pygame.draw.rect(fenetre,(50, 50, 50),(0,757,1024,20))
bloc = [noir, bleu]
while continuer:
for event in pygame.event.get(): #Attente des événements
if event.type == QUIT:
continuer = 0
if event.type == KEYDOWN:
if perso.position.collidelist(bloc) != -1:
if perso.position.right == noir.left:
perso.position.x -= 10
print("collide")
perso.position.collidelist(bloc) = -1
elif perso.position.left == noir.right:
perso.position.x += 10
print("collide")
perso.position.collidelist(bloc) = -1
elif perso.position.bottom == noir.top:
perso.position.collidelist(bloc) = -1
print("collide")
if perso.position.collidelist(bloc) == -1:
if event.key == K_DOWN and perso.position.y < 660:
perso.position.y += 3
if event.key == K_UP:
perso.doit_monter = True
new_y = perso.position.y - 100
if event.key == K_RIGHT and perso.position.x < 920:
perso.position.x += 10
if event.key == K_LEFT and perso.position.x > 3:
perso.position.x -= 10
if event.key == K_SPACE:
fenetre.blit(pygame.transform.flip(fond, True, False))
if perso.doit_monter == True:
if perso.position.y > new_y:
perso.monte()
else:
perso.doit_monter = False
else:
if perso.position.collidelist(bloc) == -1:
if perso.position.y < 660:
perso.descend()
fenetre.blit(fond, (0,0))
noir=pygame.draw.rect(fenetre,(0, 0, 0),(200,500,250,300))
bleu=pygame.draw.rect(fenetre,(0, 0, 255),(500,600,200,200))
portal=pygame.draw.rect(fenetre,(0, 255, 0),(250,700,50,120))
sol=pygame.draw.rect(fenetre,(50, 50, 50),(0,757,1024,20))
fenetre.blit(perso.image, perso.position)
pygame.display.flip()
pygame.quit()