让角色从平台上掉下来或跳下来,并到达他们开始时的相同y坐标?



我正在通过学习Pygame制作一个简单的游戏。下面是我的代码演示:

我在平台和角色周围都放了一个点击框,因为我使用的是图像,但在我的例子中,我简化了它,这样我就可以得到帮助的问题。我需要让角色跳上平台。如果他们在平台的正上方,他们需要能够跳跃并降落在平台上。如果他们从平台的一边走下去或跳下去,他们需要落在他们跳起来的y坐标。

import pygame
pygame.init()
win = pygame.display.set_mode((1200, 600))
clock = pygame.time.Clock()
class Player(object):
def __init__(self, x, y, width, height):
self.x = x
self.y = y
self.width = width
self.height = height
self.vel = 5
self.left = False
self.right = False
self.walk_count = 0
self.is_jumping = False
self.jump_count = 10
self.standing = False
self.idle = True
self.hitbox = (self.x + 10, self.y + 15, 50, 47)
def draw(self, win):
self.hitbox = (self.x + 10, self.y + 15, 50, 47)
pygame.draw.rect(win, (255,0,0), self.hitbox, 2)
def land(self):
self.y = stick.hitbox[0] - self.hitbox[0]
self.is_jumping = False
self.jump_count = 10
self.x = 800
self.y = 410 - self.height + 4
class Platform(object):
def __init__(self, x, y):
self.x = x
self.y = y
self.hitbox = (self.x, self.y, 104, 5, 2)
def draw(self, win):
pygame.draw.rect(win, (255, 255, 255), (self.x, self.y, 104, 5))
self.hitbox = (self.x, self.y, 104, 2)

# DRAW FUNCTION
def redraw_game_window():
win.fill((0, 0, 0))
stick.draw(win)
george.draw(win)
pygame.display.update()

# MAIN LOOP
font = pygame.font.SysFont('arial', 32, True)
stick = Platform(800, 410)
george = Player(50, 525, 64, 64)
running = True
while running:
clock.tick(27)
if george.is_jumping:
if george.hitbox[0] + george.hitbox[2] < stick.hitbox[0] + stick.hitbox[2] and george.hitbox[0] + george.hitbox[2] > stick.hitbox[0]:
if george.hitbox[1] + george.hitbox[3] < stick.hitbox[1] and george.hitbox[1] + george.hitbox[3] > 390:
george.land()
print('land')
if george.jump_count == 10 and george.y == 410 - george.height + 4:
if george.hitbox[0] + george.hitbox[2] < stick.hitbox[0] or george.hitbox[0] > stick.hitbox[0] + stick.hitbox[2]:
george.is_jumping = True
george.jump_count = -10
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and george.x > george.vel:
george.x -= george.vel
george.left = True
george.right = False
george.standing = False
george.idle = False
elif keys[pygame.K_RIGHT] and george.x < 1200 - (george.width + george.vel):
george.x += george.vel
george.left = False
george.right = True
george.standing = False
george.idle = False
else:
george.walk_count = 0
george.standing = True
if not(george.is_jumping):
if keys[pygame.K_UP]:
george.is_jumping = True
george.walk_count = 0
george.idle = False
else:
if george.jump_count >= -10:
george.y -= (george.jump_count * abs(george.jump_count)) * 0.5
george.jump_count -= 1
george.standing = False
else:
george.is_jumping = False
george.jump_count = 10
redraw_game_window()
pygame.quit()

我能够想出如何让角色降落在平台上,感觉我快要让他们掉下来或跳下来了,但他们不会一直掉下去,也不能回到平台上。

简单,只需阅读以下代码(提供注释)


# set the following variables
# the jump height is, you know the jump height
JUMP_HEIGHT = 20
# gravity, affects jump height and speed of downfall
GRAVITY = 1
# the number subtracted every frame, equal to jump height as of now
Y_VELOCITY = JUMP_HEIGHT
# the trigger for jump, initially false
isJump = False
# set the isJump variable to True when jump key is pressed, nothing else
# add this function to mainloop, just call this before blitting stuff
def jump():
# if jump is triggered, do this
if isJump:
# subtract the y_velocity from player y every frame
player_y -= Y_VELOCITY
# also make the gravity affect yvelocity for realistic effect and free fall (in negative)
Y_VELOCITY -= GRAVITY
# if player reached the initial spot, do this
if Y_VELOCITY <= -JUMP_HEIGHT - GRAVITY:
# cancel the trigger
isJump = False
# reset the variables
Y_VELOCITY = JUMP_HEIGHT

如果上面的代码没有按预期工作,

# replace this line
if Y_VELOCITY <= -JUMP_HEIGHT - GRAVITY:
# with this
if Y_VELOCITY < -JUMP_HEIGHT - GRAVITY:

我不擅长解释东西