到目前为止,我有一个玩家类,对于每个更新方法都没有重力作用,现在我所有的重力方法都是检查玩家是否与地面碰撞,如果没有碰撞,那么玩家 yVel += 1 ,但从不超过 13(每帧下落不超过 13 像素),但问题是如果我的播放器正好在地面上方并且掉到地面 (13) 像素,他会卡在地面中间并且不能跳回来。有什么办法可以解决这个问题,还是我需要完全重写我的播放器类中的所有内容?
import pygame
import time # lint:ok
from pygame.locals import *
char = 'ball.png'
char_jump = 'ball_jump.png'
ball = pygame.image.load(char)
ballJump = pygame.image.load(char_jump)
class Player(pygame.sprite.Sprite):
def __init__(self, screen, image, xPos, yPos, xVel, yVel, checkGroup):
pygame.sprite.Sprite.__init__(self)
self.xPos = xPos
self.yPos = yPos
self.xVel = xVel
self.yVel = yVel
self.image = image
self.screen = screen
self.rect = self.image.get_rect()
self.isInAir = True
self.checkGroup = checkGroup
def draw(self, screen):
screen.blit(self.image, self.rect)
def update(self):
self.gravity()
self.xPos += self.xVel # updates x and y based on velocities
self.yPos += self.yVel # updates rect
self.rect.topleft = (self.xPos, self.yPos) # updates sprite rectangle
if self.xPos > 440: # keeps player from going to far right and left
self.xPos = 440
if self.xPos < -3: # #########
self.xPos = -3
def gravity(self):
if self.checkCollision(self.checkGroup) is True:
self.yVel = 0
elif self.checkCollision(self.checkGroup) is False:
self.yVel = 50
def jump(self):
if self.isInAir is False:
print('jump')
self.yVel -= 20
self.image = ballJump
def moveRight(self):
self.xVel = 3
def moveLeft(self):
self.xVel = -3
def stopLeft(self):
self.xVel = 0
self.image = ball
def stopRight(self):
self.xVel = 0
self.image = ball
def stopJump(self):
self.image = ball
if self.yVel < 0: # if player is still jumping up
self.yVel = 1 # make y Velocity positive (fall down)
def checkCollision(self, group):
if pygame.sprite.spritecollideany(self, group):
return True
elif not pygame.sprite.spritecollideany(self, group):
return False