在pygame中使用colorkey对表面进行blitting有多高效?

时间:2014-01-04 18:34:02

标签: python pygame

我注意到有关pygame中blitting项目的一些非常奇怪的事情。如果您有一个要在屏幕上显示的项目列表,如果屏幕没有颜色键,那么没有问题,帧速率保持水平。但是当有一个颜色键时,对于每个对象都是blitted,每个项目的帧速率会有很大的损失

请使用以下代码:

import pygame
import copy

pygame.init()

d=786,684
screen=pygame.display.set_mode(d)
screen.set_colorkey((255,255,255)) #anything white is transparent

canv=pygame.image.load('canvas.png')
screen.blit(canv,(0,0))
canvas=screen.copy() #main canvas, first object in the list of blitted items

tmpcanv=copy.copy(canvas) #this is the surface to which all the lines are drawn
                          #this surface is then added to the blitlist once the mouse
                          #comes up.

blitlist=[canvas,tmpcanv] #the list of items to be blitted
mp=[] #the list of mouse points for the draw tool

timer=pygame.time.Clock()
fpsLim=120

drawing=False

def display(): #blits the items to the screen in order
    for item in blitlist:
        r=item.get_rect()
        screen.blit(item,(r[0],r[1]))

def getmouse_pos():
    mp.append((mx,my))
    if len(mp)>2:
        del mp[0]

def add_drawing(surf):
    if not surf is blitlist[-1]:
        blitlist.append(surf)

def reinit_tmpcanv():
    tmpcanv=copy.copy(canvas)

display()

running=True
while running:
    for evt in pygame.event.get():
        if evt.type==pygame.QUIT:
            running=False
        elif evt.type==pygame.MOUSEBUTTONDOWN:
            drawing=True
            add_drawing(tmpcanv)
        elif evt.type==pygame.MOUSEBUTTONUP:
            drawing=False
            tmpcanv=copy.copy(canvas)

    timer.tick(fpsLim)

    if drawing:
        s=mp[0]; e=mp[1]
        pygame.draw.aaline(tmpcanv,(0,0,0),s,e)

    mx,my=pygame.mouse.get_pos()
    getmouse_pos()

    print(timer.get_fps())

    display()

    pygame.display.update()
pygame.quit()

请注意,帧速率限制为120 fps。在blitlist中有30个或更多图纸,帧率降至 8 fps 。它也将继续从那里下降。我已经把它归结为 0.5 fps (尽管在blitlist中有200多张图纸)。

所以我的问题;我的代码是否有什么问题导致帧速率大幅度下降,或者当涉及到一个颜色键时,pygame是否真的无效地使表面无效?

0 个答案:

没有答案