2016-01-08 85 views
-1

我在pygame上做了一个Tamagotchi项目,在这个早期阶段,程序运行非常慢。你有什么提示为什么? 另外,有什么方法可以加快速度?Pygame程序运行缓慢。为什么?

这是我到目前为止的代码:

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() 
+0

它看起来像[code review](http://codereview.stackexchange.com)的问题。 – GingerPlusPlus

+1

只要:**(A)**代码有效**(B)**这可能是Code Review的问题**它不是任何假设或不完整的。 如果您选择进入Code Review,请在发布之前阅读[主题指南](http://codereview.stackexchange.com/help/on-topic)。 – Quill

+0

我终于找到了一个解决方案。 在make_empty_grid()函数中,我必须更改for循环中的宽度和高度变量。从600和400分别他们必须减少到60和40. –

回答

0

你可以尝试剖析代码:

https://docs.python.org/2/library/profile.html

主要是我希望那是因为你有很多嵌套的循环。其中一些看起来没有必要。 Python中嵌套for循环不是很快。

你的宽度和高度是恒定的,你并不需要在每个刻度上创建一个全新的空格。

import copy 

empty_grid = [[0] * width for _ in range(height)] 
def make_empty_grid(): 
    return copy.deepcopy(empty_grid) 

而是复制你的项目进入电网,然后绘制网格的,为什么不能有draw_item直接调用pygame.draw?因为它是在整个网格上迭代两次 - 一次是将网格中的项目放入网格中,一次是绘制网格。

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

+0

什么都没有。我会及时通知你的! –

0

您不需要每次重新计算您的空网格。将它作为“背景图像”存储在表面中,然后每次只能将其屏幕显示到屏幕上,而不是清除它,重新计算它,然后对其进行分块。另外,当绘制项目时,只是绘制项目不是整个屏幕。您可以使用display.update并传递新老项目,因为它们是唯一正在改变的东西。

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