【问题标题】:PyGame is ChoppyPyGame 不稳定
【发布时间】:2020-07-07 16:45:46
【问题描述】:

我是 pygame 的新手,我正在看一些关于如何开始使用它的教程,我编写了这个基本代码,它只有运动和跳跃“功能”,但它真的很不稳定,我不认为它是我的硬件( MacBook Pro 2018)。有人知道发生了什么吗?

代码如下:

import pygame
pygame.init()
win_width = 500
win_height = 500
win = pygame.display.set_mode((win_width,win_height))

    pygame.display.set_caption("First PyGame")

    width = 40
    height = 60
    x = win_width/2
    y = win_height/2
    vel = 10

    isJump = False
    jumpCount = 10

    run = True
    while run:
        pygame.time.delay(100)

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                print("adios putito")
                run = False

        keys = pygame.key.get_pressed()
        if keys[pygame.K_LEFT] and x > vel:
            x -= vel
        if keys[pygame.K_RIGHT] and x < win_width - width - vel:
            x+= vel
        if not (isJump):
            if keys[pygame.K_UP] and y > vel:
                y -= vel
            if keys[pygame.K_DOWN]and y < win_height - height - vel:
                y += vel
            if keys[pygame.K_SPACE]:
                isJump = True
        else:
            if jumpCount >= -10:
                neg = 1
                if jumpCount < 0:
                    neg = -1
                y -= (jumpCount ** 2) * 0.5 * neg
                jumpCount -= 1
            else:
                isJump = False
                jumpCount = 10

        win.fill((0,0,0))
        pygame.draw.rect(win,(255,255,255), (x,y,width,height))
        pygame.display.update()

    pygame.quit()

【问题讨论】:

  • 我对 pygame 了解不多,但乍一看 pygame.time.delay(100) 看起来很可疑。
  • 在视频中只是说它就像“游戏时钟”,但我不知道为什么,但我的代码与教程中的人完全一样,但我的代码很不稳定:/
  • @Phix 你是对的,我刚刚删除了那行,一切正常!谢谢!
  • @AbrahamEsquivel 这是什么教程?这不是你使用游戏时钟的方式。您应该在程序开始时使用clock = pygame.time.Clock() 创建一个时钟,然后在您拥有pygame.time.delay(100) 的地方使用clock.tick(60)
  • 确保将您的答案标记为已接受。

标签: python macos animation pygame


【解决方案1】:

线

pygame.time.delay(100)

将您的脚本延迟几毫秒,删除该行,它应该可以正常工作!

结果:

import pygame
pygame.init()
win_width = 500
win_height = 500
win = pygame.display.set_mode((win_width,win_height))

    pygame.display.set_caption("First PyGame")

    width = 40
    height = 60
    x = win_width/2
    y = win_height/2
    vel = 10

    isJump = False
    jumpCount = 10

    run = True
    while run:

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                print("adios putito")
                run = False

        keys = pygame.key.get_pressed()
        if keys[pygame.K_LEFT] and x > vel:
            x -= vel
        if keys[pygame.K_RIGHT] and x < win_width - width - vel:
            x+= vel
        if not (isJump):
            if keys[pygame.K_UP] and y > vel:
                y -= vel
            if keys[pygame.K_DOWN]and y < win_height - height - vel:
                y += vel
            if keys[pygame.K_SPACE]:
                isJump = True
        else:
            if jumpCount >= -10:
                neg = 1
                if jumpCount < 0:
                    neg = -1
                y -= (jumpCount ** 2) * 0.5 * neg
                jumpCount -= 1
            else:
                isJump = False
                jumpCount = 10

        win.fill((0,0,0))
        pygame.draw.rect(win,(255,255,255), (x,y,width,height))
        pygame.display.update()

    pygame.quit()

【讨论】:

    【解决方案2】:

    拥有一个足以延迟您的程序以保持稳定帧速率的游戏时钟仍然是一个好主意。你可以使用 pygame 的 pygame.time.Clock 对象并使用它的 tick() 方法来延迟你的游戏。 tick 方法采用一个整数,表示您希望游戏限制的 FPS。如果您的游戏运行速度低于您输入的 FPS 值,则不会发生延迟。

    import pygame
    pygame.init()
    win_width = 500
    win_height = 500
    win = pygame.display.set_mode((win_width,win_height))
    
        pygame.display.set_caption("First PyGame")
        clock = pygame.time.Clock()
    
        width = 40
        height = 60
        x = win_width/2
        y = win_height/2
        vel = 10
    
        isJump = False
        jumpCount = 10
    
        run = True
        while run:
            # Delay your game to try and keep 60 FPS.
            clock.tick(60)
    
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    print("adios putito")
                    run = False
    
            keys = pygame.key.get_pressed()
            if keys[pygame.K_LEFT] and x > vel:
                x -= vel
            if keys[pygame.K_RIGHT] and x < win_width - width - vel:
                x+= vel
            if not (isJump):
                if keys[pygame.K_UP] and y > vel:
                    y -= vel
                if keys[pygame.K_DOWN]and y < win_height - height - vel:
                    y += vel
                if keys[pygame.K_SPACE]:
                    isJump = True
            else:
                if jumpCount >= -10:
                    neg = 1
                    if jumpCount < 0:
                        neg = -1
                    y -= (jumpCount ** 2) * 0.5 * neg
                    jumpCount -= 1
                else:
                    isJump = False
                    jumpCount = 10
    
            win.fill((0,0,0))
            pygame.draw.rect(win,(255,255,255), (x,y,width,height))
            pygame.display.update()
    
        pygame.quit()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-05-11
      • 2011-01-20
      • 2018-11-26
      • 2016-10-18
      • 2011-01-16
      • 2021-01-10
      • 1970-01-01
      相关资源
      最近更新 更多