pygame平板游戏中的跳跃问题

时间:2018-09-25 15:33:40

标签: pygame

在我的pygame平台游戏中,跳跃高度不一致,这意味着玩家有时跳跃得比我希望其跳跃的高度低而高。我还想根据空格键按下的时间来确定玩家跳到的高度。有没有一种方法可以解决跳跃问题并使跳跃高度取决于空格键保持的时间?游戏的3个文件的代码可以在下面找到。请告诉我为什么玩家没有以固定的高度跳跃,以及如何根据按键的持续时间来设定跳跃高度。

Main.py

import pygame
import random
from settings import *
from obj import *

pygame.init()
pygame.mixer.init()
pygame.font.init()

pygame.display.set_caption(TITLE)
screen = pygame.display.set_mode([WIDTH,HEIGHT])

clock = pygame.time.Clock()

me = Player()
all_sprites.add(me)

platforms = []

pf = Wall(100,40,500,400, 0)
pf2 = Wall(WIDTH,40, 400,500, 0)
platforms.append(pf)
platforms.append(pf2)

for i in platforms:
    wall_sprites.add(i)

running = True

while running:
    clock.tick(FPS)

    all_sprites.update()
    wall_sprites.update()

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
            me.jump()

    screen.fill(GREY)
    all_sprites.draw(screen)
    wall_sprites.draw(screen)
    pygame.display.update()

pygame.quit()

obj.py

import pygame
import math
from settings import *

class Player(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((40,40))
        self.image.fill(BLACK)
        self.rect = self.image.get_rect()
        self.rect.x = WIDTH / 2
        self.rect.y = 20
        self.vx = 0
        self.vy = 0
        self.SW = False  # Can you screen wrap?
        self.player_states = ["Standing","Falling"]
        self.state = self.player_states[1]

    def jump(self):
        if self.state == self.player_states[0]:
            self.vy -= JUMP_SPEED
            self.state = self.player_states[1]        

    def update(self):
        self.vx = 0  # X speed set to 0 if no input is received
        self.vy += GRAVITY  # Gravity 

        keys = pygame.key.get_pressed()
        if keys[pygame.K_LEFT]:
            self.vx = -SPEED
        if keys[pygame.K_RIGHT]:
            self.vx = SPEED

        self.rect.left += self.vx  # X and Y positions are updated
        self.collide(self.vx, 0, wall_sprites)  # Collision is checked. Second param is 0 b/c we aren't checking for vertical collision here
        self.rect.top += self.vy
        self.collide(0, self.vy, wall_sprites)


        if self.SW:
            if self.rect.left > WIDTH:
                self.rect.right = 0
            if self.rect.right < 0:
                self.rect.left = WIDTH

            if self.rect.top > HEIGHT:
                self.rect.bottom = 0
            if self.rect.bottom < 0:
                self.rect.top = HEIGHT

    def collide(self, xDif, yDif, platform_list):
        for i in platform_list:                      # Shuffle through list of platforms
            if pygame.sprite.collide_rect(self, i):  # If there is a collision between the player and a platform...
                if xDif > 0:                         # And our x (horizontal) speed is greater than 0...
                    self.rect.right = i.rect.left    # That means that we are moving right, 
                if xDif < 0:                         # So our right bounding box becomes equal to the left bounding box of all platforms and we don't collide    
                    self.rect.left = i.rect.right
                if yDif > 0:
                    self.rect.bottom = i.rect.top
                    self.state = self.player_states[0]
                    self.vy = 0
                if yDif < 0:
                    self.rect.top = i.rect.bottom
                    self.vy = 0


class Wall(pygame.sprite.Sprite):  # Collision is added for platforms just in case that they are moving. If they move to you, they push you
    def __init__(self, width, height, xpos, ypos, speed):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((width,height))
        self.image.fill(BLUE)
        self.rect = self.image.get_rect()
        self.rect.centerx = xpos
        self.rect.centery = ypos
        self.speed = speed

    def update(self):
        self.rect.left += self.speed
        self.collide(self.speed, all_sprites)  # Collision only for platforms moving left and right. Not up and down yet

    def collide(self, xDif, player_list): 
        for i in player_list:                      
            if pygame.sprite.collide_rect(self, i):

                if xDif > 0:                         # If the platform is moving right... (has positive speed)
                    i.rect.left += self.speed        # Platform pushes player
                    self.rect.right = i.rect.left    # Player sticks to the wall and is pushed

                if xDif < 0:
                    i.rect.right -= self.speed
                    self.rect.left = i.rect.right

settings.py

import pygame

FPS = 60
WIDTH = 800
HEIGHT = 600
TITLE = "Perfect collision"

GREY = (150,150,150)
BLACK = (0,0,0)
BLUE = (0,0,255)

SPEED = 5
JUMP_SPEED = 9
GRAVITY = 0.3

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

1 个答案:

答案 0 :(得分:0)

打印self.vy,即使玩家站立,您也会看到该值一直在变化,因此,根据当前的vy,您将获得不同的跳跃高度。您可以在self.vy方法中将JUMP_SPEED设置为负数jump,以解决此问题:self.vy = -JUMP_SPEED