pygame - 使用箭头键进行火柴人跳跃,但不超过10个单位
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了pygame - 使用箭头键进行火柴人跳跃,但不超过10个单位相关的知识,希望对你有一定的参考价值。
我所拥有的火柴人能够使用箭头键移动所有,但是,当按下向上箭头键时,我希望他每次按下按键时只能运行10个单位,无论多长时间这是迫切的。换句话说,我希望他跳,并限制他能跳多高。我尝试了几件事,但没有任何效果。
import pygame
def drawMan(screen,x,y):
#head
pygame.draw.ellipse(screen,BLACK,[0+x,0+y,10,10], 0)
#body
pygame.draw.line(screen,BLACK,[4+x,17+y],[4+x,7+y], 2)
#legs
pygame.draw.line(screen,BLACK,[4+x,17+y],[9+x,27+y], 2)
pygame.draw.line(screen,BLACK,[4+x,17+y],[-1+x,27+y], 2)
#arms
pygame.draw.line(screen,BLACK,[4+x,7+y],[8+x,17+y], 2)
pygame.draw.line(screen,BLACK,[4+x,7+y],[0+x,17+y], 2)
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
BORDER = (100,100,100)
pygame.init()
size = (800, 500)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Jump")
done = False
clock = pygame.time.Clock()
pygame.mouse.set_visible(1)
xCoord = 11
yCoord = 463
xSpeed = 0
ySpeed = 0
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
xSpeed = -3
if event.key == pygame.K_RIGHT:
xSpeed = 3
if event.key == pygame.K_UP:
ySpeed = -3
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
xSpeed = 0
if event.key == pygame.K_RIGHT:
xSpeed = 0
if event.key == pygame.K_UP:
ySpeed = 3
if xCoord >= 780:
xSpeed = 0
xCoord -= 1
elif xCoord <= 13:
xSpeed = 0
xCoord += 1
elif yCoord > 465:
ySpeed = 0
yCoord -= 1
elif yCoord <= 13:
ySpeed = 0
yCoord += 1
else:
xCoord += xSpeed
yCoord += ySpeed
screen.fill(WHITE)
pygame.draw.line(screen, BORDER, [0,0],[800,0], 20)
pygame.draw.line(screen, BORDER, [0,0],[0,500], 20)
pygame.draw.line(screen, BORDER, [0,500],[800,500], 20)
pygame.draw.line(screen, BORDER, [800,500],[800,0], 20)
drawMan(screen,xCoord,yCoord)
pygame.display.flip()
clock.tick(60)
pygame.quit()
答案
设置一个允许他跳跃的变量。如果变量不是True,则跳转键不执行任何操作。
当他跳跃时,将变量切换为False。在他再次撞到地面之前不要重置它。
伪代码:
IF INPUT = "jump" AND can_jump == True THEN
can_jump = False
player.jump()
END IF
IF player.y == 0 and can_jump == False THEN
can_jump = True
END IF
以上是关于pygame - 使用箭头键进行火柴人跳跃,但不超过10个单位的主要内容,如果未能解决你的问题,请参考以下文章
向量使用1:pygame编写篮球游戏-火柴人运球避开防守跳起投篮(向量法处理防守者逼近投篮者前进数据)