如何让我的鸟在点击太空 Pygame 时继续跳跃?



我正在尝试重现飞扬的鸟 我想知道如何让我的鸟继续跳跃? 当我单击空格时 视频 这是我现在的跳跃


if collide:
if keys[pygame.K_SPACE]:
bird1.isJump = True
bird1.fall = 0

else:
if bird1.JumpCount > 0:
bird1.y -= (bird1.JumpCount*abs(bird1.JumpCount))*0.4
bird1.JumpCount -= 1
else:
bird1.JumpCount = 10
bird1.isJump = False

我的完整代码都是矩形,如果你想测试它

import pygame
pygame.init()
#this is screem height
window = pygame.display.set_mode((500,500))
#know we put screem name
pygame.display.set_caption("Noobs Flappy Bird Game")
#player class
class bird:
def __init__(self,x,y,width,height,color):
self.x = x
self.y = y
self.width = width
self.height = height
self.color = color
self.speed = 5
self.isJump = False
self.JumpCount = 10
self.fall = 0
self.speed = 5
self.rect = pygame.Rect(x,y,height,width)
def draw(self):
self.rect.topleft = (self.x,self.y)
pygame.draw.rect(window,self.color,self.rect)
class platform:
def __init__(self,x,y,height,width,color):
self.x = x
self.y = y
self.height = height
self.width = width
self.color = color
self.rect = pygame.Rect(x,y,height,width)
def draw(self):
self.rect.topleft=(self.x,self.y)
pygame.draw.rect(window,self.color,self.rect)

#player and enemy
white = (255,255,255)
bird1 = bird(0,400,40,40,white)
red = (255,48,48)
platform1 = platform(100,400,60,20,red)

platforms = [platform1]
#window
def redrawwindow():
window.fill((0,0,0))
#player draw
bird1.draw()
for platform in platforms:
platform.draw()
fps = (30)
clock = pygame.time.Clock()
run = True
while run:
clock.tick(fps)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False

keys = pygame.key.get_pressed()
# bird moving
bird1.x += bird1.speed
if not bird1.isJump:
bird1.y += bird1.fall
bird1.fall += 1
bird1.isJump = False


# this part lets you jump on platform
collide = False
for platform in platforms:
if bird1.rect.colliderect(platform.rect):
collide = True
bird1.isJump = False
bird1.y = platform.rect.top - bird1.height + 1
if bird1.rect.right > platform.rect.left and bird1.rect.left - bird1.width:
bird1.x = platform.rect.left - player1.width
if bird1.rect.left < platform.rect.right and bird1.rect.right + bird1.width:
bird1.x = platform.rect.right

if bird1.rect.bottom >= 500:
collide = True
bird1.isJump = False
bird1.JumpCount = 10
bird1.y = 500 - bird1.height
if collide:
if keys[pygame.K_SPACE]:
bird1.isJump = True
bird1.fall = 0

else:
if bird1.JumpCount > 0:
bird1.y -= (bird1.JumpCount*abs(bird1.JumpCount))*0.4
bird1.JumpCount -= 1
else:
bird1.JumpCount = 10
bird1.isJump = False



redrawwindow()
pygame.display.update()
pygame.quit()

你必须改变两件事:

  • 即使鸟不与地面碰撞,也可以跳跃。
  • 保持状态bird1.isJump,如果鸟开始坠落时空间仍然被按下。
while run:
# [...]
keys = pygame.key.get_pressed()
# bird moving
bird1.x += bird1.speed
if not bird1.isJump:
# [...]
# the bird is allowed to jump even if it is not colliding:
if keys[pygame.K_SPACE]:
bird1.isJump = True
if collide:
bird1.fall = 0
else:
if bird1.JumpCount > 0:
bird1.y -= (bird1.JumpCount*abs(bird1.JumpCount))*0.4
bird1.JumpCount -= 1
else:
bird1.JumpCount = 10

# if K_SPACE is pressed, then the bird keeps jumping
if not keys[pygame.K_SPACE]:
bird1.isJump = False

最新更新