Pygame速度和碰撞麻烦

时间:2018-05-14 18:33:29

标签: python python-3.x pygame pygame-surface

我正在为我的考试做最后的项目,我正在制作一个平台游戏。实际上我是碰撞而且它有效。但实际的问题是,如果我跳上一个平台,我让我的播放器静止1秒,播放器就会消失,这很奇怪。你能帮我解决这个问题吗?我有一个提示,这个问题与速度有关(calles vitesse_x和vitesse_y)。

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):
        super().__init__()
        self.image = pygame.Surface((50,50))
        self.rect = self.image.get_rect(bottomleft=(0,740))
        self.image.fill((255, 0, 0))
        self.doit_monter = False
        self.vitesse_x = 0
        self.vitesse_y = 0

    def update(self):
        self.rect.x += self.vitesse_x
        self.rect.y += self.vitesse_y
        touche = pygame.key.get_pressed()
        if touche[pygame.K_RIGHT] and self.rect.x < 920:
            self.vitesse_x += 1
        if touche[pygame.K_LEFT] and self.rect.x > 0:
            self.vitesse_x -= 1
        collision = pygame.sprite.spritecollide(self, blocs, False)
        for bloc in collision:
            if self.vitesse_y >= 0:
                self.rect.bottom = bloc.rect.top
            elif self.vitesse_y < 0:
                self.rect.top = bloc.rect.bottom

perso = Perso()

class Bloc(pygame.sprite.Sprite): #pour creer un obstacle et éléments du jeu
    def __init__(self, x, y, w, h):
        super().__init__()
        self.image = pygame.Surface((w,h))
        self.image.fill((0, 255, 0))
        self.rect = self.image.get_rect(topleft=(x, y))

all_sprites = pygame.sprite.Group()
blocs = pygame.sprite.Group()

all_sprites.add(perso)

b1 = Bloc(200,500,250,50)
b2 = Bloc(500,600,200,50)
b3 = Bloc(0,740,1024,30)
blocs.add(b1,b2,b3)
all_sprites.add(b1,b2,b3)

new_y = 0

continuer = True
while continuer:
    pygame.time.Clock().tick(60)
    for event in pygame.event.get():  
        if event.type == QUIT:
            continuer = False
        if event.type == KEYDOWN:
            if event.key == pygame.K_SPACE:
                perso.doit_monter = True
                new_y = perso.rect.y - 100

        if event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT and perso.vitesse_x < 0:
                    perso.vitesse_x = 0
                    perso.vitesse_y = 0
                if event.key == pygame.K_RIGHT and perso.vitesse_x > 0:
                    perso.vitesse_x = 0
                    perso.vitesse_y = 0
                if event.key == pygame.K_SPACE and perso.vitesse_y < 0:
                    perso.vitesse_y = 0

    if perso.doit_monter == True:
        if perso.rect.y > new_y and perso.rect.y > 0:
            perso.vitesse_y -= 5
        else:
            perso.doit_monter = False
    else:
        perso.vitesse_y += 1
    all_sprites.update()

    fenetre.fill((0,0,0))
    all_sprites.draw(fenetre)
    pygame.display.flip()

pygame.quit()

1 个答案:

答案 0 :(得分:1)

简单,问题,简单的解决方案。如果你在地上,你必须重置y速度。

Perso.update

    for bloc in collision:
        if self.vitesse_y >= 0:
            self.vitesse_y = 0 # reset velocity, so it isn't increasing forever
            self.rect.bottom = bloc.rect.top
        elif self.vitesse_y < 0:
            self.rect.top = bloc.rect.bottom

如果你不这样做,速度会增加,直到它高到足以一次跳到地板上而不会与停止盒碰撞