如何在我的游戏中添加跳跃
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 个空格?本质上,它是一个像马里奥一样的跳跃游戏,我会添加平台和其他东西.另外,我想知道如何确保您不能在空中进行双跳!对不起,我不是一个好的程序员,我只是需要这个游戏的帮助.
How would I make my code so that if I press ▲ once, the image moves up 15 spaces from the image's current position, and then goes down 15 spaces after about .5 seconds? Essentially, it is a jumping game like Mario that I will add platforms and other things to it. Also, I want to know how to make sure you can't double jump while in the air! Sorry that I am not a good programmer, I just need help for this game.
我使用的是 Python 2.7
I am using Python 2.7
添加 GRAVITY
常量,您可以将其添加到 y_change
每一帧以加速播放器向下.当玩家接触地面时,将 on_ground
变量设置为 True
,当用户跳跃时检查他/他是否是 on_ground
,然后将其设置为 False
防止双跳.
Add a GRAVITY
constant which you add to the y_change
each frame to accelerate the player downwards. When the player touches the ground set a on_ground
variable to True
and when the user jumps check if s/he's on_ground
and then set it to False
to prevent double jumps.
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()
如果您想查看一个最小的、完整的平台游戏示例,请查看这个答案.
If you want to see a minimal, complete platformer example, take a look at this answer.