Search code examples
menupygamerestartlag

Switching from game to menu then back creates lag


The game starts out working at the right speed. However, when I press escape and go back to the main menu, and then go back to the game, the game creates a little lag (same with when I get game over screen and then go back to main menu and start the game). When I keep doing that over and over again, it creates more and more lag.

In more "coding" terms, when I press Start on the menu, it calls reset(), which resets the positions of all the sprites. When there is a Game over, it shows the game over screen, and then the user presses escape to go back to the menu.

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

Solution

  • Thanks to the comment, the solution is to put: global walls walls = [] in the function placeholder1. Thank you!