如何减少此 python 代码中的延迟?

How do I reduce lag in this python code?

我正在研究 Breakout/Arkanoid 的克隆。我的代码似乎工作正常,但对于我添加的每个新级别,它变得越来越慢。它是用 pygame 模块写在 python 3.4.3 中的。抱歉没有评论,我对此很陌生。

import pygame
import math
import random
pygame.init()

# Global constants
white = (255,255,255)
black = (0,0,0)
red = (255,0,0)
orange = (255,128,0)
yellow = (255,255,0)
green = (0,255,0)
blue = (0,0,255)
indigo = (0,0,128)
violet = (255,0,255)

# global variables
PAD_WIDTH = 100
PAD_HI = 20
TARGET_WIDTH = 40
TARGET_HEIGHT = 20
WIDTH = 21
HEIGHT = 30
DISPLAY = pygame.display.set_mode((WIDTH*TARGET_WIDTH, HEIGHT*TARGET_HEIGHT))
BALL_START_POS= [WIDTH//2,HEIGHT-15]
clock = pygame.time.Clock()
pygame.display.update()
FPS = 60




def dist(p, q):
    return math.sqrt((p[0] - q[0]) ** 2 + (p[1] - q[1]) ** 2)


class Ball:

    def __init__(self):
        self.x = (WIDTH*TARGET_WIDTH)//2 - 10
        self.y = (HEIGHT*TARGET_HEIGHT) - 40
        self.width = 20
        self.height = 20
        self.vel = [0,0]
        self.color = white
        self.stuck = True

    def update(self):
        self.x += self.vel[0]
        self.y += self.vel[1]
        if self.x > (WIDTH*TARGET_WIDTH) - self.width or self.x < 0:
            self.vel[0] = -self.vel[0]
        if self.y < 0:
            self.vel[1] = -self.vel[1]
        if self.y > (HEIGHT*TARGET_HEIGHT) - self.height-10:
            self.stuck = True

    def fire_ball(self, vel):
        self.vel = vel
        self.stuck = False

    def collide(self, other_object):
        if self.rect.colliderect(other_object.rect):            
            self.vel[1] = -self.vel[1]

    def draw(self):
        pygame.draw.ellipse(DISPLAY, self.color, self.rect)

    @property
    def rect(self):
        return pygame.Rect(self.x, self.y, self.width, self.height)

class Paddle:

    def __init__(self):

        self.x = ((WIDTH*TARGET_WIDTH)//2)-(PAD_WIDTH//2)
        self.y = (HEIGHT*TARGET_HEIGHT)-PAD_HI        
        self.vel = [0,0]
        self.color = white
        self.width = PAD_WIDTH
        self.height = PAD_HI

    def update(self):
        self.x += self.vel[0]

    def collide(self, other_object):
        pass

    def draw(self):
        pygame.draw.rect(DISPLAY,white,self.rect)

    def get_center(self):
        return (self.x + PAD_WIDTH/2)-10

    @property
    def rect(self):
        return pygame.Rect(self.x, self.y, self.width, self.height)     

class Target:

    def __init__(self, x, y, color):

        self.x = x
        self.y = y
        self.center = [self.x/2,self.y/2]        
        self.color = color
        self.width = TARGET_WIDTH
        self.height = TARGET_HEIGHT

    def update(self):
        pass

    def collide(self, other_object):
        if self.rect.colliderect(other_object.rect):
            return True

    def draw(self):
        pygame.draw.rect(DISPLAY,white,self.rect)
        pygame.draw.rect(DISPLAY,red,self.rect,1)

    @property
    def rect(self):
        return pygame.Rect(self.x, self.y, self.width, self.height)

def key_handler():
    global stuck_var
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                pad.vel[0]-=1
            elif event.key == pygame.K_RIGHT:
                pad.vel[0]+=1
            elif event.key == pygame.K_SPACE:
                if ball.stuck == True:
                    ball.fire_ball([pad.vel[0]/2,-0.5])

        elif event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT:
                pad.vel[0]=0
            elif event.key == pygame.K_RIGHT:
                pad.vel[0]=0

def draw_handler():
    pad.draw()
    pad.update()
    ball.update()
    ball.draw()

    for target in targets:
        target.draw()

def collider(ball,thing):
    if ball.collide(pad):
        ball.vel[0] = -ball.vel[0]
        ball.vel[1] = -ball.vel[1]

def group_collider(ball,target_group):
    for target in set(target_group):
        if target.collide(ball):
            target_group.remove(target)
            for target in target_group:
                ball.collide(target)

def pad_wall():
    if pad.x <= 0:
        pad.x = 0        
    if pad.x >= (WIDTH*TARGET_WIDTH) - PAD_WIDTH:
        pad.x = (WIDTH*TARGET_WIDTH) - PAD_WIDTH

def stuck():
    if ball.stuck == True:
        ball.x = pad.get_center()
        ball.y = (HEIGHT*TARGET_HEIGHT) - 40

pad = Paddle()
ball = Ball()
targets = set([])
o = 'o'
t = 't'

levels = {1:[(o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o),
         (o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o),
         (o,o,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,o,o),
         (o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o),
         (o,o,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,o,o),
         (o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o),
         (o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o),
         (o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o),
         (o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o),
         (o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o),
         (o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o),
         (o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o),
         (o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o),
         (o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o),
         (o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o),
         (o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o),
         (o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o),
         (o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o),
         (o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o),
         (o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o),
         (o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o),
         (o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o),
         (o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o),
         (o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o),
         (o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o),
         (o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o),
         (o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o),
         (o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o),
         (o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o),
         (o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o),],

        2:[(o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o),
         (o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o),
         (o,o,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,o,o),
         (o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o),
         (o,o,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,o,o),
         (o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o),
         (o,o,t,o,t,o,t,o,t,o,t,o,t,o,t,o,t,o,t,o,o),
         (o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o),
         (o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o),
         (o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o),
         (o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o),
         (o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o),
         (o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o),
         (o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o),
         (o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o),
         (o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o),
         (o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o),
         (o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o),
         (o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o),
         (o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o),
         (o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o),
         (o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o),
         (o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o),
         (o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o),
         (o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o),
         (o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o),
         (o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o),
         (o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o),
         (o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o),
         (o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o),],

        3:[(o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o),
         (o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o),
         (o,o,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,o,o),
         (o,o,t,t,t,o,t,o,t,o,t,o,t,o,t,o,t,t,t,o,o),
         (o,o,o,o,t,t,t,t,t,t,t,t,t,t,t,t,t,o,o,o,o),
         (o,o,o,t,o,t,o,t,o,t,o,t,o,t,o,t,o,t,o,o,o),
         (o,o,t,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,t,o,o),
         (o,t,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,t,o),
         (t,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,t),
         (o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o),
         (o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o),
         (o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o),
         (o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o),
         (o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o),
         (o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o),
         (o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o),
         (o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o),
         (o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o),
         (o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o),
         (o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o),
         (o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o),
         (o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o),
         (o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o),
         (o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o),
         (o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o),
         (o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o),
         (o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o),
         (o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o),
         (o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o),
         (o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o),],

         4:[(o,o,o,o,o,o,o,t,o,t,o,t,o,t,o,o,o,o,o,o,o),
         (o,o,o,o,o,t,o,t,o,t,o,t,o,t,o,t,o,o,o,o,o),
         (o,o,o,o,o,t,o,o,o,o,o,o,o,o,o,t,o,o,o,o,o),
         (o,o,o,t,t,o,o,t,t,t,t,t,t,t,o,o,t,t,o,o,o),
         (o,o,o,o,o,o,t,t,t,t,t,t,t,t,t,o,o,o,o,o,o),
         (o,o,t,t,o,t,t,t,o,o,o,o,o,t,t,t,o,t,t,o,o),
         (o,o,o,o,o,t,t,o,o,o,o,o,o,o,t,t,o,o,o,o,o),
         (o,o,o,o,o,t,t,o,o,o,o,o,o,o,t,t,o,o,o,o,o),
         (o,o,t,t,o,t,t,t,o,o,o,o,o,t,t,t,o,t,t,o,o),
         (o,o,o,o,o,o,t,t,t,t,t,t,t,t,t,o,o,o,o,o,o),
         (o,o,o,t,t,o,o,t,t,t,t,t,t,t,o,o,t,t,o,o,o),
         (o,o,o,o,o,t,o,o,o,o,o,o,o,o,o,t,o,o,o,o,o),
         (o,o,o,o,o,t,o,t,o,t,o,t,o,t,o,t,o,o,o,o,o),
         (o,o,o,o,o,t,o,t,o,t,o,t,o,t,o,o,o,o,o,o,o),
         (o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o),
         (o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o),
         (o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o),
         (o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o),
         (o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o),
         (o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o),
         (o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o),
         (o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o),
         (o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o),
         (o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o),
         (o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o),
         (o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o),
         (o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o),
         (o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o),
         (o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o),
         (o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o),]
          }


level = 0
def level_handler():
    global level,targets
    level += 1
    if level == 5:
        level = 1

    for pos in levels[level]:
        for row in range(HEIGHT):
            for column in range(WIDTH):                    
                if levels[level][row][column]== t:
                    target = Target(column*TARGET_WIDTH,row*TARGET_HEIGHT, white)        
                    targets.add(target)

while True:

    if not targets:
        level_handler()


    pad_wall()
    stuck()
    key_handler()
    DISPLAY.fill(black)
    draw_handler()
    collider(ball,pad)
    group_collider(ball,targets)
    pygame.display.update()

好的,您的关卡是二维的(行和列)。然后,查看您的 level_handler,注意您如何以某种方式使用 3 个循环:

for pos in levels[level]:
    for row in range(HEIGHT):
        for column in range(WIDTH):    

因此,对于关卡中的每一行,您循环遍历每一行和每一列。

所以只需删除 for pos in levels[level]: 行。