我们如何让我们的玩家在游戏中跳跃

2024-03-28 13:06:55 发布

您现在位置:Python中文网/ 问答频道 /正文

import pygame
import sys
from pygame import *
from pygame.locals import RESIZABLE

pygame.init()

WINDOW_SIZE = (800, 600)
screen = pygame.display.set_mode(WINDOW_SIZE, RESIZABLE, 32)

player_img = pygame.image.load('ClipartKey_738895_adobespark.png')
player_X = 130
player_Y = 500
player_change_X=0

def player():
    screen.blit(player_img, (player_X, player_Y))

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
            sys.exit()

        if event.type == KEYDOWN:
            if event.key == K_RIGHT:
                player_change_X = 0.3
            if event.key == K_LEFT:
                player_change_X = -0.3
            if event.key == K_SPACE:
                player_Y += 40
        elif event.type == KEYUP:
            if event.key == K_RIGHT:
                player_change_X = 0
            if event.key == K_LEFT:
                player_change_X = 0

    screen.fill((0, 200, 255))
    player()
    player_X += player_change_X

    pygame.display.update()

我想让玩家跳跃大约4 Y,但无法做到。 请告诉我如何做到这一点,如果告诉任何函数,请告诉我它是什么,它是如何做到的,因为我是pygame的新手


Tags: keyfromimporteventsizeiftypesys
1条回答
网友
1楼 · 发布于 2024-03-28 13:06:55

您可以使用类似on_floor的变量。如果它在地板上(使用碰撞),那么它允许程序开始跳转(使用另一个变量,如y_speed。我通常让玩家这样跳转:

y_speed = 0
on_floor = False

# main loop
    if on_floor:
        if pygame.key.get_pressed()[K_SPACE]:
            y_speed = -40 # start the jump if space pressed
            # set to the value you used, but you should move according the the framerate
    if y_speed > -40 # speed limit
        y_speed += 1 # change the speed, to make a parabol-shape fall

    player.y += y_speed

另外,您可以使用this answer让玩家像这样跳跃,这也可以做类似的工作

相关问题 更多 >