在绘制完整的窗格后,PYGAME延迟 [英] Pygame lags after drawing full window grid
本文介绍了在绘制完整的窗格后,PYGAME延迟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我被编码为一个简单的电子游戏网格窗口。但在那之后,假游戏窗口开始延迟。
下面是简单代码👇
import pygame
import random
pygame.init()
pygame.font.init()
screen_width = 500
screen_height = screen_width
screen = pygame.display.set_mode((screen_width,screen_height))
pygame.display.set_caption("Snake GaMe By Akila")
def drawGrid():
grid_list = []
blockSize = 25
for x in range(screen_width):
for y in range(screen_height):
rect = pygame.Rect(x*blockSize, y*blockSize, blockSize, blockSize)
pygame.draw.rect(screen, (255,255,255), rect, 1)
running = True
while running:
screen.fill((0,0,0))
drawGrid()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.display.update()
我尝试更改drawGrid()
函数位置的调用代码段。
推荐答案
若要提高性能,请不要在每一帧中构造Grind。
创建具有窗口大小的pygame.Surface
,并在此表面上绘制网格:
grid_surf = pygame.Surface((screen_width,screen_height))
drawGrid(grid_surf)
该曲面是场景的背景。blit
每帧开头:
screen.blit(grid_surf, (0, 0))
示例代码:
import pygame
import random
pygame.init()
pygame.font.init()
screen_width = 500
screen_height = screen_width
screen = pygame.display.set_mode((screen_width,screen_height))
pygame.display.set_caption("Snake GaMe By Akila")
def drawGrid(surf):
grid_list = []
blockSize = 25
for x in range(screen_width):
for y in range(screen_height):
rect = pygame.Rect(x*blockSize, y*blockSize, blockSize, blockSize)
pygame.draw.rect(surf, (255,255,255), rect, 1)
grid_surf = pygame.Surface((screen_width,screen_height))
drawGrid(grid_surf)
running = True
while running:
screen.blit(grid_surf, (0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.display.update()
这篇关于在绘制完整的窗格后,PYGAME延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文