如何解决Pygame中重复出现的跳转问题?

时间:2019-04-25 16:38:24

标签: python pygame

我最近选择了Pygame,希望学习游戏开发并希望能够以图形方式显示模拟和测试。我尝试过制作一个具有移动和跳跃功能的简单矩形。但是,此代码使可控对象无限长地跳跃。

我尝试过移动pl.jump()函数,以及向jump()函数添加参数,但是问题仍然存在。

   ...
   class Player:
       def __init__(self, x, y):
            self.x = x
            self.y = y
            self.isJump = False
            self.jumpCount = 10
            self.vel = 4

        def draw(self):
            pygame.draw.rect(screen, red, (self.x, self.y, 20, 40))

        def jump(self):
            if self.jumpCount >= -10:
                self.y -= (self.jumpCount * abs(self.jumpCount)) * 0.2
                self.jumpCount -= 1
            else:
                self.jumpCount = 10
                self.isJump = False

        def move(self):
            if k[K_a] and self.x > 0:
                self.x -= self.vel
            if k[K_d] and self.x < (sWidth - 20):
                self.x += self.vel
            if k[K_w] and self.y > 0:
                self.y -= self.vel
            if k[K_s] and self.y < (sHeight - 40):
                self.y += self.vel

    pl = Player(400, 300)

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_SPACE:
                    pl.isJump = True

        k = pygame.key.get_pressed()
        clock.tick(framerate)
        screen.fill(black)
        pl.draw()
        pl.move()
        pl.jump()

        pygame.display.update()

据我所知,实际代码中没有缩进问题。非常感谢所有帮助! 预先感谢!

0 个答案:

没有答案