当我点击太空游戏时,如何让我的鸟继续跳跃?

2024-06-11 12:27:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我在试着重新创造一只扑翼鸟,我想知道我该如何让我的鸟继续跳跃?当我点击空格时 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()




Tags: rectselffalsetrueifwidthpygamecolor
1条回答
网友
1楼 · 发布于 2024-06-11 12:27:17

你必须改变两件事:

  • 即使鸟没有与地面相撞,也允许它跳跃
  • 如果鸟开始下落时仍按下空格,则保持状态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

相关问题 更多 >