python怎样导入游戏库_python – 如何添加跳转到我的游戏

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

最佳答案

添加一个GRAVITY常量,您可以在每个帧中添加y_change以加速玩家向下移动.当玩家触摸地面时,将on_ground变量设置为True,当用户跳转时检查s /他是否为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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值