【问题标题】:Pygame blitting many images lags gamePygame blitting 许多图像滞后于游戏
【发布时间】:2016-06-10 15:27:10
【问题描述】:

所以我有一个基于图块的游戏,大小为 216X384 单位。 cell_array 中的每个方块都是草,我用 convert_alpha() 预加载了草。每次绘制时(我的 fps 为 30),它会循环遍历 cell_array 并将其传送到屏幕上。如果 cell_array 为空,则执行此操作和其他操作大约需要 0.02 秒,因此 30 fps 可以正常工作。但是当它充满草时,需要0.08-0.1秒,使其滞后很多。有任何提示、技巧或想法可以提供帮助吗?

这是 cell_array 代码的样子。

cell_array = []

width_of_cells = 10
height_of_cells = 10
cell_Rows = 216
cell_Cols = 384

def initialize_empty_cell_board(rows, cols):
    for x in range(cell_Cols):
        new = []
        for y in range(cell_Rows):
            new.append(["NULL", False, True])

        cell_array.append(new)

def add_tile(board, tile, cx, cy):
    for i in range(tile.c_width):
        for q in range(tile.c_height):
            if cx+i < cell_Cols and cy+i < cell_Rows:
                board[cx+i][cy+i] = [tile, False, tile.is_passible]
    board[cx][cy] = [tile, True, tile.is_passible]

initialize_empty_cell_board(cell_Rows, cell_Cols)

这里是blitting代码:

cx = -1
    cy = -1
    for x in cell_array:
        cy = -1
        cx += 1
        for y in x:
            cy += 1
            if y[0] != "NULL" and y[1] == True:
                screen.blit(y[0].img.name, (cx*cells.width_of_cells, cy*cells.height_of_cells))

图像代码如下所示:

class Image(object):

    def __init__(self, src):
        self.name = pygame.image.load(src).convert_alpha()

【问题讨论】:

  • 最好的选择可能是使用 convert() 而不是 convert_alpha()。这应该已经大大提高了你的 blitting 速度。
  • @xXliolauXx 当它是空的并且它只是字符时,它会有所帮助。但是 alpha 变成了丑陋的黑色,并且由于草没有 alpha,它只是让它以 0.07 秒而不是 0.08 秒运行,这不足以证明它的丑陋。不过好主意!
  • 好吧,它不会比在 Windows 机器上好多少(在没有硬件加速的情况下,pygame 在 Windows 上似乎不会超过 20 FPS)。如果 fps 在您的游戏中非常关键,您可能需要研究 PyOpenGL,它对 Pygame 有支持。如果你愿意,你可以把你的源代码发给我,我会让它在我的 Linux 机器上运行,这似乎可以通过 pygame 获得不错的最大 FPS。如果它的运行速度比你的快,那就是典型的 pygame-fps-issue-thingie。

标签: python arrays pygame lag


【解决方案1】:

如果游戏不必每帧都对它们进行 blit,那么处理起来会容易得多。我假设这个草砖是地板或背景。这意味着所有的草都可以预先blitted到一个表面,并且每帧只需要对这个表面进行blit。

这个例子:

import pygame as py

py.init()

window = (400,400)
screen = py.display.set_mode(window)

font = py.font.Font(None,36)

clock = py.time.Clock()

def draw_onto_screen():
    for i in xrange(0,200):
        for j in xrange(0,200):
            py.draw.rect(screen,(0,255,0),(i,j,5,5))
    text = font.render(str(clock.get_fps()),1,(10,10,10))
    screen.blit(text,(0,0))

done = False
while not done:
    for event in py.event.get():
        if event.type == py.QUIT:
            done = True
    draw_onto_screen()
    py.display.flip()
    clock.tick(60)

py.quit()

每帧绘制 40,000 个方格,并且在我的机器上平均显示 11fps(你可以自己运行它并查看左上角的 fps)。

但是,如果我们先将所有方块绘制到另一个表面上(我们称之为背景),然后我们得到简单的 blit 该表面:

import pygame as py

py.init()

window = (400,400)
screen = py.display.set_mode(window)

font = py.font.Font(None,36)

clock = py.time.Clock()

def draw_onto_screen():
    for i in xrange(0,200):
        for j in xrange(0,200):
            py.draw.rect(background,(0,255,0),(i,j,5,5))

background = py.Surface(window)
draw_onto_screen() #### only called once

done = False
while not done:
    for event in py.event.get():
        if event.type == py.QUIT:
            done = True
    screen.blit(background,(0,0)) #### blit the background onto screen
    text = font.render(str(clock.get_fps()),1,(10,10,10))
    screen.blit(text,(0,0))
    py.display.flip()
    clock.tick(60)

py.quit()

绘制完全相同的内容,但以 62.5fps 的速度巡航。现在,当您想要移动背景时,只需将其 blit 到不同的位置(当前为 (0,0))

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-19
    • 2023-01-03
    • 1970-01-01
    • 2020-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多