Pygame仅显示部分精灵以获得更高的帧频

时间:2019-04-12 20:16:57

标签: python pygame render

我正在测试RenderUpdates Sprite组,以有效地更新屏幕(稍后将用于更复杂的游戏)。我正在用一个简单的球在屏幕上弹跳的情况进行测试。但是当我使用它时,屏幕似乎只显示了球的一部分。前缘是正方形。但是,暂停动画时会显示整个球。我将其与清除整个屏幕然后更新整个屏幕的时间进行了比较。这样可以正确显示整个球(请参见下图)。起初我以为这是RenderUpdates的问题,但现在我看到它是与帧速率有关的图形问题,因为当我降低帧速率时,它显示了整个问题。关于正在发生的事情以及如何解决这个问题的任何想法?

我对第一张图片表示歉意。我必须用手机拍摄照片,因为任何屏幕捕捉应用程序都会降低帧频,从而使其整体显示。

更高的帧速率

较低的帧速率

以下是参考代码:

# import libraries
import pygame, sys
import numpy as np

# Define colors
BLACK = (0,0,0)
WHITE = (255,255,255)

# Screen parameters
SCALE = 2 # how much bigger the game will be compared to the original pong dim.
SCREEN_HEIGHT = 256*SCALE
SCREEN_WIDTH = 2*SCREEN_HEIGHT
FPS = 30*SCALE # how fast the screen updates (frames per second)

# Ball parameters
DIAMETER = 12*SCALE
BALL_VELOCITY = int(round(SCREEN_WIDTH/(2.5*FPS))) # crosses screen in 2.5 sec

# --- Ball class ---------------------------------------
class Ball(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self, self.containers)

        # create the physical surface of the ball
        self.image = pygame.Surface([DIAMETER,DIAMETER],pygame.SRCALPHA)
        pygame.draw.circle(self.image,WHITE, \
                (int(DIAMETER/2),int(DIAMETER/2)),int(DIAMETER/2))
        self.rect = self.image.get_rect()

        # velocity of ball
        self.velx = BALL_VELOCITY
        self.vely = BALL_VELOCITY

    # update is called every frame (moves the ball)
    def update(self):
        # Check if ball hits wall
        self.hit_wall()
        self.rect.x += self.velx
        self.rect.y += self.vely

    def hit_wall(self):
        # Check if ball hits left or right walls
        if self.rect.right + self.velx > SCREEN_WIDTH \
            or self.rect.left + self.velx < 0:
            self.velx = -self.velx # switches direction ball moves

        # Check if ball hits top or bottom walls
        if self.rect.bottom + self.vely > SCREEN_HEIGHT \
            or self.rect.top + self.vely < 0:
            self.vely = -self.vely # switches direction ball moves

# close the window and quits
def terminate():
    pygame.quit()
    sys.exit()

# --- Main Program -----------------------------------------------------
def main():
    # --- Setup --------------------------------------------
    pygame.init()

    # Set the width and heigth of the screen
    screen = pygame.display.set_mode([SCREEN_WIDTH,SCREEN_HEIGHT])

    # define background
    background = pygame.Surface([SCREEN_WIDTH,SCREEN_HEIGHT])
    background.fill(BLACK)

    # Write game title at the top of the screen
    pygame.display.set_caption("Pong")

    # Create group that contails all the sprites in the game
    all_sprites = pygame.sprite.RenderUpdates()

    # Containers for sprites
    Ball.containers = all_sprites

    # add the ball sprite using the Ball class above
    ball = Ball()

    # Create a clock to manage how fast the screen updates
    clock = pygame.time.Clock()

    # --- Main Program Loop ----------------------------------------------
    while True:
        # --- Event Processing
        for event in pygame.event.get():
            # user closed the screen (pressing x in top-right corner)
            if event.type == pygame.QUIT:
                terminate()

        # --- Update everything -------------------------------------------
        all_sprites.update()

        # --- Drawing code -----------------------------------------------
        # Clear screen
        all_sprites.clear(screen,background)

        # Draw all sprites
        dirty = all_sprites.draw(screen)

        # Draw the updates
        pygame.display.update(dirty)

        # Frames per second
        clock.tick(FPS)

# Begin Progam
if __name__ == '__main__':
    main()
else:
    terminate()

0 个答案:

没有答案