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, 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()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值