首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在PyGame中绘制大图像导致低帧率

问在PyGame中绘制大图像导致低帧率
EN

Stack Overflow用户
提问于 2016-07-22 21:10:18
回答 1查看 1K关注 0票数 2

我正在尝试做一个‘跑步’风格的游戏在PyGame (如几何达什),那里的背景是不断移动。到目前为止,一切都很好,但是背景图像的渲染限制了帧速率超过每秒35帧。在添加无限/重复背景元素之前,它可以轻松地以60 fps运行。这两行代码负责(删除后,游戏可以在60+fps运行):

screen.blit(bg,(bg_x,0)) x screen.blit(bg,(bg_x2,0))

我能做些什么让比赛跑得更快吗?提前感谢!

简化源代码:

代码语言:javascript
复制
import pygame
pygame.init()

screen = pygame.display.set_mode((1000,650), 0, 32)
clock  = pygame.time.Clock()

def text(text, x, y, color=(0,0,0), size=30, font='Calibri'): # blits text to the screen
    text = str(text)

    font = pygame.font.SysFont(font, size)
    text = font.render(text, True, color)

    screen.blit(text, (x, y))

def game():
    bg = pygame.image.load('background.png')
    bg_x = 0 # stored positions for the background images
    bg_x2 = 1000

    pygame.time.set_timer(pygame.USEREVENT, 1000)
    frames = 0 # counts number of frames for every second
    fps = 0

    while True:
        frames += 1
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()
            if event.type == pygame.USEREVENT: # updates fps every second
                fps = frames
                frames = 0 # reset frame count

        bg_x -= 10 # move the background images
        bg_x2 -= 10

        if bg_x == -1000: # if the images go off the screen, move them to the other end to be 'reused'
            bg_x = 1000
        elif bg_x2 == -1000:
            bg_x2 = 1000

        screen.fill((0,0,0))
        screen.blit(bg, (bg_x, 0))
        screen.blit(bg, (bg_x2, 0))

        text(fps, 0, 0)

        pygame.display.update()
        #clock.tick(60)

game()

以下是背景图像:

​

​

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-07-22 22:38:34

你试过使用convert()吗?

代码语言:javascript
复制
    bg = pygame.image.load('background.png').convert()

来自文档

您通常希望调用没有参数的Surface.convert(),以创建一个在屏幕上绘制得更快的副本。 对于alpha透明性,就像在.png图像中一样,在加载后使用convert_alpha()方法,使图像具有每个像素的透明度。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38535283

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档