Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 当网格显示在pygame中时,角色移动缓慢_Python_Pygame - Fatal编程技术网

Python 当网格显示在pygame中时,角色移动缓慢

Python 当网格显示在pygame中时,角色移动缓慢,python,pygame,Python,Pygame,我正在用pygame做一个蛇游戏,我注意到了一个古怪的东西。 每当我显示网格时,我的角色运行缓慢。 这是我的程序的主要功能。 我刚刚开始学习pygame 以下是样本的图片 我认为重新绘制没有任何问题。重新绘制的项目数量较少 但是,我可以看到,在draw函数中,您正在为行和列中的每个像素绘制线。如果在每一帧中都执行此操作,则成本非常高,而且不必要地浪费CPU电源,因为并非所有线条都位于视口中。请尝试执行以下操作(在范围中用BLOCKSIZE划分窗口宽度)函数: 绘制像素的速度很慢。抽签和大量的像

我正在用pygame做一个蛇游戏,我注意到了一个古怪的东西。 每当我显示网格时,我的角色运行缓慢。
这是我的程序的主要功能。
我刚刚开始学习pygame

以下是样本的图片


我认为重新绘制没有任何问题。重新绘制的项目数量较少

但是,我可以看到,在draw函数中,您正在为行和列中的每个像素绘制线。如果在每一帧中都执行此操作,则成本非常高,而且不必要地浪费CPU电源,因为并非所有线条都位于视口中。请尝试执行以下操作(在
范围中用BLOCKSIZE划分窗口宽度)
函数:


绘制像素的速度很慢。抽签和大量的像素将更慢。只需重新画出你必须抹掉的蛇。
def main():
    global SCREEN, CLOCK
    pygame.init()
    CLOCK = pygame.time.Clock()
    SCREEN.fill(BLACK)

    x = 0
    y = 0
    velocity = 20
    x_change = 0
    y_change = 0

    while True:
        drawGrid()
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_UP:
                    y_change = -velocity
                    x_change = 0
                if event.key == pygame.K_DOWN:
                    y_change = velocity
                    x_change = 0
                if event.key == pygame.K_LEFT:
                    x_change = -velocity
                    y_change = 0
                if event.key == pygame.K_RIGHT:
                    x_change = velocity
                    y_change = 0

        x += x_change
        y += y_change

        snake(x, y)
        pygame.display.update()
        SCREEN.fill(BLACK)
        CLOCK.tick(60)

def snake(x, y):
    head_rect = pygame.Rect(x, y, BLOCKSIZE, BLOCKSIZE)
    pygame.draw.rect(SCREEN, GREEN, head_rect)


def drawGrid():
    for x in range(WINDOW_WIDTH):
        for y in range(WINDOW_HEIGHT):
            rect = pygame.Rect(x*BLOCKSIZE, y*BLOCKSIZE,
                               BLOCKSIZE, BLOCKSIZE)
            pygame.draw.rect(SCREEN, WHITE, rect, 1)
def drawGrid():
    for x in range(WINDOW_WIDTH // BLOCKSIZE):
        for y in range(WINDOW_HEIGHT // BLOCKSIZE):
            rect = pygame.Rect(x*BLOCKSIZE, y*BLOCKSIZE,
                               BLOCKSIZE, BLOCKSIZE)
            pygame.draw.rect(SCREEN, WHITE, rect, 1)