如何添加跳跃到我的游戏

时间:2017-12-17 17:18:39

标签: python python-2.7 pygame

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个空格,然后在约.5后向下移动15个空格。秒?从本质上讲,它是一个像马里奥这样的跳跃游戏,我将添加平台和其他东西。另外,我想知道如何确保你不能在空中跳跃!对不起,我不是一个好的程序员,我只需要帮助这个游戏。

我正在使用Python 2.7

1 个答案:

答案 0 :(得分:1)

添加with my friends常量,每个帧添加到GRAVITY以加速播放器向下。当玩家触地时将y_change变量设置为on_ground,当用户跳过时检查他/她是True然后将其设置为on_ground以防止双跳。

False

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