首页 > 解决方案 > Pygame 速度和碰撞问题

问题描述

我目前正在为我的考试制作我的期末项目,我正在制作一个平台游戏。实际上我已经发生了碰撞并且它有效。但实际的问题是,如果我跳上一个平台并让我的播放器保持静止 1 秒钟,播放器就会消失,这很奇怪。你能帮我解决这个问题吗?我暗示这个问题与速度有关(称为 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()

标签: pythonpython-3.xpygamepygame-surface

解决方案


好的,简单,问题,简单的解决方案。如果您在地面上,则必须重置 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

如果你不这样做,速度会增加,直到它足够高,可以一口气跳过地板而不会撞到停止箱


推荐阅读