我正在为我的考试做最后的项目,我正在制作一个平台游戏。实际上我已经陷入了碰撞,因为正如你在平台游戏中所知道的那样,我需要对上侧,右侧和左侧做出不同的反应。如果它碰撞在块的左边,我想使位置+10或者如果它是右侧则为-10。我尝试了一个代码,但它只能在块的顶部工作,这就是我需要你帮助的原因!你能帮我解决这个问题吗?
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()