我正在做一个关于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()