2016-01-08 84 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並傳遞新老項目,因爲它們是唯一正在改變的東西。

您還應該將項目存儲爲曲面或精靈,而不是每次都通過循環重新計算它們。