【问题标题】:How do I add jumping to my game [duplicate]如何在我的游戏中添加跳跃[重复]
【发布时间】:2018-05-31 03:41:21
【问题描述】:
import pygame
import time
pygame.init()

display_width = (1000)
display_height = (480)


black = (255,50,0)
white  = (0,60,7)
blue = (0,205,205)

kled1_width = 30
kled1_height = 45

ground1_height = 300

screen = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption(" kled ")
clock = pygame.time.Clock()

gameDisplay = screen

kled1IMG = pygame.image.load("IMG.png")

def kled1(x,y):
    gameDisplay.blit(kled1IMG,(x,y))


def game_loop():

    x = (1)
    y = (340)


    x_change = 0

    y_change = 0


    gameExit = False

    while not gameExit:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                gameExit = True

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    x_change = -5

                elif event.key == pygame.K_RIGHT:
                    x_change =  5

                elif event.key == pygame.K_UP:
                    y_change = -15

                elif event.key == pygame.K_DOWN:
                    y_change = 5

            if event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT or event.key == pygame.K_LEFT and pygame.K_r or event.key == pygame.K_RIGHT and pygame.K_2:
                    x_change = 0
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
                    y_change = 0


        x += x_change
        y += y_change

        gameDisplay.fill(blue)

        pygame.draw.rect(gameDisplay, white, [1, 400, 1000,ground1_height])

        kled1(x,y)

#prevents from going into ground
        if y > ground1_height:
            y_change = 0

        pygame.display.update()
        clock.tick(30)

game_loop()
pygame.quit()
quit()

我将如何编写我的代码,以便如果我按一次 ,图像从图像的当前位置向上移动 15 个空格,然后在大约 0.5 秒后向下移动 15 个空格?本质上,它是一款像马里奥一样的跳跃游戏,我会在其中添加平台和其他东西。另外,我想知道如何确保你不能在空中进行二段跳!对不起,我不是一个好的程序员,我只是需要这个游戏的帮助。

我正在使用 Python 2.7

【问题讨论】:

标签: python python-2.7 pygame


【解决方案1】:

添加一个GRAVITY 常量,您将其添加到每帧的y_change 以加速播放器向下。当玩家接触地面时,将on_ground 变量设置为True,当用户跳跃时检查他/她是否为on_ground,然后将其设置为False 以防止双跳。

import pygame

pygame.init()
screen = pygame.display.set_mode((1000, 480))
clock = pygame.time.Clock()

GREEN = (110, 190, 27)
BLUE = (0, 185, 225)

PLAYER_IMG = pygame.Surface((30, 45))
PLAYER_IMG.fill((250, 120, 0))

GRAVITY = 1


def game_loop():
    x = 1
    y = 340
    x_change = 0
    y_change = 0
    on_ground = True
    ground_height = 400

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                return
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    x_change = -5
                elif event.key == pygame.K_RIGHT:
                    x_change =  5
                elif event.key == pygame.K_UP:
                    if on_ground:
                        # Set the y-velocity to a negative
                        # value to jump.
                        y_change = -20
                        on_ground = False
                elif event.key == pygame.K_DOWN:
                    y_change = 5
            elif event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT and x_change < 0:
                    x_change = 0
                elif event.key == pygame.K_RIGHT and x_change > 0:
                    x_change = 0

        # Adjust the y-velocity by adding the gravity each frame.
        y_change += GRAVITY
        # Move the player.
        x += x_change
        y += y_change
        # Stop the player on the ground.
        if y > ground_height:
            # Stop the movement along the y-axis.
            y_change = 0
            y = ground_height
            on_ground = True

        screen.fill(BLUE)
        pygame.draw.rect(screen, GREEN, [0, 445, 1000, 35])
        screen.blit(PLAYER_IMG, (x, y))

        pygame.display.update()
        clock.tick(60)

game_loop()
pygame.quit()

如果您想查看一个最小的、完整的平台游戏示例,请查看this answer

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多