Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/311.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上做一个Tamagotchi项目,在这个早期阶段,程序运行非常缓慢。你有什么关于为什么的提示吗? 还有,有没有办法加快速度 这是我目前的代码: import pygame import time pygame.init() def draw_grid(grid): for i in range(0,len(grid)): for j in range(0,len(grid[i])): area = (j*10, i*10, 8,

我正在pygame上做一个Tamagotchi项目,在这个早期阶段,程序运行非常缓慢。你有什么关于为什么的提示吗? 还有,有没有办法加快速度

这是我目前的代码:

import pygame
import time

pygame.init()
def draw_grid(grid):
    for i in range(0,len(grid)):
        for j in range(0,len(grid[i])):
            area = (j*10, i*10, 8, 8)
            if grid[i][j] == 0:
                fill = True
                color = 0,0,0
            elif grid[i][j] == 1:
                fill = False
                color = 0,0,0
            elif grid[i][j] == 2:
                fill = False
                color = 255,0,0
            square = pygame.draw.rect(screen, color, area, fill)

def make_empty_grid(width,height):
    grid = []
    for i in range(height):
        zeros = []
        for j in range(width):
            zeros.append(0)
        grid.append(zeros)
    return grid

def draw_item(item, x, y):
    for i in range(0, len(item)):
        for j in range(0, len(item[i])):
            grid[i + x][j + y] = item[i][j]

size = width, height = 600, 400
#rects = 
screen = pygame.display.set_mode(size)

running = True

#beige background
background = 213, 230, 172 
black = 0, 0, 0
red = 255, 0, 0

image1 = [
         [1,0,1,1,1,0,1],
         [0,1,0,0,0,1,0],
         [1,0,1,0,1,0,1],
         [1,0,0,0,0,0,1],
         [1,0,1,1,1,0,1],
         [1,0,0,0,0,0,1],
         [0,1,1,1,1,1,0]
         ]

image2 = [
         [1,0,1,1,1,0,1],
         [0,1,0,0,0,1,0],
         [1,0,1,0,1,0,1],
         [1,0,0,0,0,0,1],
         [1,0,0,1,0,0,1],
         [1,0,0,0,0,0,1],
         [0,1,1,1,1,1,0]
         ]

bars = [
       [2,2,2,2,2,2,2,2,2,2],
       [2,2,2,2,2,2,2,2,2,2]
       ]


tamaPosX = 27
tamaPosY = 8

curImage = image1
while running:
    # Get the window input
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                tamaPosX -= 1
            if event.key == pygame.K_RIGHT:
                tamaPosX += 1
            if event.key == pygame.K_UP:
                tamaPosY -= 1
            if event.key == pygame.K_DOWN:
                tamaPosY += 1

    screen.fill(background)

    grid = make_empty_grid(width,height)

    if curImage is image1:
        curImage = image2
    else:
        curImage = image1

    draw_item(curImage, tamaPosY, tamaPosX)
    draw_item(bars, 35, 1)

    draw_grid(grid)

    pygame.display.flip()

您可以尝试分析代码:

主要是因为有很多嵌套for循环。其中有些似乎没有必要。嵌套for循环在Python中并不快速

您的宽度和高度是恒定的,您实际上不需要在每个刻度处创建一个全新的空网格

import copy

empty_grid = [[0] * width for _ in range(height)]
def make_empty_grid():
    return copy.deepcopy(empty_grid)
与其将项目复制到网格中,然后绘制网格,为什么不直接调用pygame.draw?实际上,您在整个网格上迭代了两次—一次是将项目放入网格,一次是绘制网格


编辑:这可能是一个不好的建议,我看到即使是值为0的网格元素也绘制了与背景不同的颜色。我不确定你在做什么。

你不需要每次都重新计算你的空网格。将其作为“背景图像”存储在曲面中,然后每次只需将其
blit
显示在屏幕上,而无需清除它、重新计算它,然后对其进行blit。此外,当绘制项目时,只绘制项目,而不是整个屏幕。您可以使用display.update并传递项目rect old和new,因为它们是唯一正在更改的内容


您还应该将项目存储为曲面或精灵,而不是在循环中每次都重新计算它们。

这看起来像是一个问题。这可能是一个代码审查问题,只要:(a)代码有效(B)它在任何方面都不是假设的或不完整的。如果你选择代码审查,请在发帖前阅读。我终于找到了一个解决方案。在make_empty_grid()函数中,我必须更改for循环中的宽度和高度变量。从600和400分别减少到60和40。我将在周一之前有一个更简洁的程序版本,这样如果有人需要,我可以上传到这里。