所以我有一個基於瓷磚的遊戲是216X384單位大。 cell_array中的每個正方形都是草地,我預先裝入了convert_alpha()。每次繪製時(我的fps都是30),它在cell_array中循環並將其傳送到屏幕上。如果cell_array爲空,則需要大約0.02秒來完成此操作以及其他操作,因此30 fps工作得很好。但是當它充滿草時,需要0.08-0.1秒,使其滯後很多。任何提示,技巧或想法可以幫到您?Pygame blitting很多圖片滯後遊戲
以下是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)
這裏是塊傳輸代碼:
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()。這應該已經大大提高你的傳輸速度。 – xXliolauXx
@xXliolauXx當它爲空時,它只是字符,它有幫助。但阿爾法變成了一個醜陋的黑色,並且由於草沒有阿爾法,它只會讓它運行在0.07秒而不是0.08秒,幾乎不足以證明它的醜陋。好主意,但! – Demandooda
好吧,它不會比Windows機器上的好得多(在沒有硬件加速的情況下,pygame在Windows上似乎不會超過20FPS左右)。如果fps在你的遊戲中非常關鍵,你可能想看看PyGame支持的PyOpenGL。如果你想要的話,你可以給我發送你的源代碼,我會讓它在我的Linux機器上運行,這似乎可以通過pygame獲得體面的最大FPS。如果它比你的運行速度快,那就是典型的pygame-fps-issue-thingie。 – xXliolauXx