如何让图像只在地面上跳跃?
How to make image jump only on the ground?
我正在尝试使图像跳转。当我按下向上箭头时,它会上升,直到它到达 Y 轴上的某个位置并开始下降。但我有一个问题,图像能够在空中无限跳跃,有点像飞扬的鸟。我希望它只有在到达跳跃点时才能跳跃。
这是我现在的代码:
#Variables
display_width = 900
display_height = 800
gameName = 'Slash'
#Settings
gameTitle = pygame.display.set_caption(gameName)
wn = pygame.display.set_mode((display_width, display_height))
clock = pygame.time.Clock()
#Game Loop
def gameLoop():
#stick man
manImg = pygame.image.load('blueStickman.png')
man_width = 58
man_height = 146
Xchange = 0
Ychange = 0
x = (display_width/2) - (man_width/2) + 1
y = display_height - man_height
def man(x,y):
wn.blit(manImg, (x,y))
while True:
wn.fill(white)
man(x,y)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_x:
pygame.display.quit()
pygame.quit()
if event.key == pygame.K_UP:
Ychange = -5
if event.key == pygame.K_LEFT:
Xchange = -4
if event.key == pygame.K_RIGHT:
Xchange = 4
if event.type == pygame.KEYUP:
if event.key == pygame.K_UP:
Ychange = -5
if event.key == pygame.K_LEFT:
Xchange = 0
if event.key == pygame.K_RIGHT:
Xchange = 0
x += Xchange
y += Ychange
if y <= 385:
Ychange = 5
if y >= display_height - man_height:
Ychange = 0
clock.tick(60)
gameLoop()
希望您能帮忙,如果可以的话,谢谢!
首先我推荐玩家的左右移动使用pygame.key.get_pressed()
,而不是KEYDOWN
和KEYUP
事件:
keys = pygame.key.get_pressed()
Xchange = 0
if keys[pygame.K_LEFT]:
Xchange -= 4
if keys[pygame.K_RIGHT]:
Xchange += 4
定义付款人的地面等级:
y = display_height - man_height
ground_y = y
在主应用程序循环中评估玩家是否在地面上,如果玩家不在地面上则丢弃跳跃:
while True:
# [...]
on_ground = y == ground_y
for event in pygame.event.get():
# [...]
if event.type == pygame.KEYDOWN:
# [...]
if event.key == pygame.K_UP and on_ground:
Ychange = -5
看例子:
import pygame
white = (255, 255, 255)
#Variables
display_width = 900
display_height = 800
gameName = 'Slash'
#Settings
gameTitle = pygame.display.set_caption(gameName)
wn = pygame.display.set_mode((display_width, display_height))
clock = pygame.time.Clock()
#Game Loop
def gameLoop():
#stick man
manImg = pygame.image.load('blueStickman.png')
man_width, man_height = 58, 156
Xchange, Ychange = 0, 0
x = (display_width/2) - (man_width/2) + 1
y = display_height - man_height
ground_y = y
def man(x,y):
wn.blit(manImg, (x,y))
while True:
wn.fill(white)
man(x,y)
pygame.display.update()
on_ground = y == ground_y
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_x:
pygame.display.quit()
pygame.quit()
if event.key == pygame.K_UP and on_ground:
Ychange = -5
keys = pygame.key.get_pressed()
Xchange = 0
if keys[pygame.K_LEFT]:
Xchange -= 4
if keys[pygame.K_RIGHT]:
Xchange += 4
x += Xchange
y += Ychange
if y <= 385:
Ychange = 5
if y >= display_height - man_height:
Ychange = 0
clock.tick(60)
gameLoop()
我正在尝试使图像跳转。当我按下向上箭头时,它会上升,直到它到达 Y 轴上的某个位置并开始下降。但我有一个问题,图像能够在空中无限跳跃,有点像飞扬的鸟。我希望它只有在到达跳跃点时才能跳跃。 这是我现在的代码:
#Variables
display_width = 900
display_height = 800
gameName = 'Slash'
#Settings
gameTitle = pygame.display.set_caption(gameName)
wn = pygame.display.set_mode((display_width, display_height))
clock = pygame.time.Clock()
#Game Loop
def gameLoop():
#stick man
manImg = pygame.image.load('blueStickman.png')
man_width = 58
man_height = 146
Xchange = 0
Ychange = 0
x = (display_width/2) - (man_width/2) + 1
y = display_height - man_height
def man(x,y):
wn.blit(manImg, (x,y))
while True:
wn.fill(white)
man(x,y)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_x:
pygame.display.quit()
pygame.quit()
if event.key == pygame.K_UP:
Ychange = -5
if event.key == pygame.K_LEFT:
Xchange = -4
if event.key == pygame.K_RIGHT:
Xchange = 4
if event.type == pygame.KEYUP:
if event.key == pygame.K_UP:
Ychange = -5
if event.key == pygame.K_LEFT:
Xchange = 0
if event.key == pygame.K_RIGHT:
Xchange = 0
x += Xchange
y += Ychange
if y <= 385:
Ychange = 5
if y >= display_height - man_height:
Ychange = 0
clock.tick(60)
gameLoop()
希望您能帮忙,如果可以的话,谢谢!
首先我推荐玩家的左右移动使用pygame.key.get_pressed()
,而不是KEYDOWN
和KEYUP
事件:
keys = pygame.key.get_pressed()
Xchange = 0
if keys[pygame.K_LEFT]:
Xchange -= 4
if keys[pygame.K_RIGHT]:
Xchange += 4
定义付款人的地面等级:
y = display_height - man_height
ground_y = y
在主应用程序循环中评估玩家是否在地面上,如果玩家不在地面上则丢弃跳跃:
while True:
# [...]
on_ground = y == ground_y
for event in pygame.event.get():
# [...]
if event.type == pygame.KEYDOWN:
# [...]
if event.key == pygame.K_UP and on_ground:
Ychange = -5
看例子:
import pygame
white = (255, 255, 255)
#Variables
display_width = 900
display_height = 800
gameName = 'Slash'
#Settings
gameTitle = pygame.display.set_caption(gameName)
wn = pygame.display.set_mode((display_width, display_height))
clock = pygame.time.Clock()
#Game Loop
def gameLoop():
#stick man
manImg = pygame.image.load('blueStickman.png')
man_width, man_height = 58, 156
Xchange, Ychange = 0, 0
x = (display_width/2) - (man_width/2) + 1
y = display_height - man_height
ground_y = y
def man(x,y):
wn.blit(manImg, (x,y))
while True:
wn.fill(white)
man(x,y)
pygame.display.update()
on_ground = y == ground_y
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_x:
pygame.display.quit()
pygame.quit()
if event.key == pygame.K_UP and on_ground:
Ychange = -5
keys = pygame.key.get_pressed()
Xchange = 0
if keys[pygame.K_LEFT]:
Xchange -= 4
if keys[pygame.K_RIGHT]:
Xchange += 4
x += Xchange
y += Ychange
if y <= 385:
Ychange = 5
if y >= display_height - man_height:
Ychange = 0
clock.tick(60)
gameLoop()