gpt4 book ai didi

python - 当我点击 Space Pygame 时,如何让我的小鸟继续跳跃?

转载 作者:行者123 更新时间:2023-12-04 09:33:58 25 4
gpt4 key购买 nike

我正在尝试重新制作飞扬的鸟我想知道如何让我的鸟继续跳跃?当我点击空格时
video
这是我现在的跳跃


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

    关于python - 当我点击 Space Pygame 时,如何让我的小鸟继续跳跃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62683039/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com