从游戏切换到菜单然后返回创建滞后

时间:2015-02-05 16:32:57

标签: menu pygame restart lag

游戏开始以正确的速度运行。但是,当我按下逃生并返回主菜单,然后返回游戏时,游戏会产生一点滞后(当我在屏幕上游戏然后返回主菜单并开始游戏时)。当我一遍又一遍地这样做时,它会造成越来越多的滞后。

更多"编码"术语,当我按下菜单上的Start时,它会调用reset(),它会重置所有精灵的位置。当游戏结束时,它会在屏幕上显示游戏,然后用户按下escape以返回菜单。

menuOn = 1
Rooms = []
running = True #Flags game as on
screen = None
(x1, y1) = (0, 0)
(x2, y2) = (0, -screenHeight)
allsprites = pygame.sprite.Group()
clock = pygame.time.Clock()

def placeholder1(screen):
    Rooms = []
    Rooms.append(Room())
    global menuOn
    menuOn = 0
    pass

class Menu(object):
    def __init__(self,screen):
        self.screen = screen
        self.title = startMenu
        self.menu = genmenu(['START', lambda: placeholder1(screen)],['INFO', lambda: placeholder2(screen)], ['QUIT', lambda: quit()])
        self.clock = pygame.time.Clock()
        event = pygame.event.get()
        self.menu.create(self.screen)
        self.menu.choose(event)
        self.main_loop()

    def main_loop(self):
        while menuOn == 1:
            self.clock.tick(60)
            events = pygame.event.get()
            self.menu.choose(events)
            self.screen.blit(self.title, (0, 0))
            self.menu.create(self.screen)
            pygame.display.flip()

class Room():
    def __init__(self):
        global wall, player, redFish, redfishes, greenFish, greenfishes, sharks, shark
        for x in range(29):
            wall = Wall()
            wall.rect.topleft = (x*32,0) #top walls
            walls.append(wall)
        for x in range(29):
            wall = Wall()
            wall.rect.topleft = (x*32,screenHeight-32) #bottom walls
            walls.append(wall)
        for y in range(17):
            wall = Wall()
            wall.rect.topleft = (0, (y*32)+32) #left walls
            walls.append(wall)
        for y in range(17):
            wall = Wall()
            wall.rect.topleft = (screenWidth-32, (y*32)+32) #right walls
            walls.append(wall)
        reset()
def reset():
    global playerpos
    playerpos = [screenWidth/2, screenHeight/2]
    player.rect.topleft = (playerpos[0], playerpos[1])
    for greenFish in greenfishes:
        greenFish.image = images["spr_greenfish"]
        greenScoreList[greenfishes.index(greenFish)] = 0
        greenFish.rect.topleft = random.randint(35,screenWidth-80),random.randint(35,screenHeight-80)
    for redFish in redfishes:
        redFish.rect.topleft = random.randint(35,screenWidth-80),random.randint(35,screenHeight-80)
    sharks[0].rect.topleft = (100, 500)
    sharks[1].rect.topleft = (300, 500)
    sharks[2].rect.topleft = (500, 500)
    sharks[3].rect.topleft = (700, 500)
while running:
    clock.tick(60)
    if menuOn == 1:
        Menu(screen)
    elif menuOn == 2:
        GameOver(screen)
    for event in pygame.event.get():
        if event.type == QUIT:
            running = False
        if event.type == pygame.KEYDOWN:
            if event.key == K_ESCAPE:
                updateScoreToScreen(score)
                menuOn = 1
                reset()
    allsprites.update()
    screen.blit(ground, (0,screenHeight-100))
    allsprites.draw(screen)
    pygame.display.update()

1 个答案:

答案 0 :(得分:0)

感谢评论,解决方案是:     全球墙     墙= [] 在函数placeholder1中。谢谢!