0
votes

My game opens up to a menu and whenever I select any of the options (Easy, Medium, Hard) it always opens the Easy level no matter what option I click. If someone could have a look over my code (mostly the option, menu, and main functions) and show me where I went wrong and how to fix it that would be greatly appreciated.

class Option:
    hovered = False
    def __init__(self, text, pos):
        self.text = text
        self.pos = pos
        self.set_rect()
        self.draw()
    def draw(self):
        self.set_rend()
        screen.blit(self.rend, self.rect)
    def set_rend(self):
        self.rend = menu_font.render(self.text, True, self.get_color())
    def get_color(self):
        if self.hovered:
            return (WHITE)
        else:
            return (GREY)
    def set_rect(self):
        self.set_rend()
        self.rect = self.rend.get_rect()
        self.rect.topleft = self.pos

options = [Option("Easy", (450, 205)), Option("Medium", (450, 305)),
           Option("Hard", (450, 405)), Option("Exit", (450, 505))]



levels = [[
    "WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW",
    "WF                                                             GW",
    "W                              P                                W",
    "W                                                               W",
    "WH                                                             IW",
    "WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW",
    ],
    [
    "WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW",
    "WF                                                             GW",
    "W                              P                                W",
    "W                      W                                        W",
    "W                       W                                       W",
    "W                        W                                      W",
    "W                         WW                                    W",
    "W                           W                                   W",
    "W                            W                                  W",
    "W                             W                                 W",
    "W                              W                                W",
    "W                                                               W",
    "W                                                               W",
    "W                                                               W",
    "W                                                               W",
    "WH                                                             IW",
    "WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW",
    ],
    [
    "WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW",
    "WF                                                             GW",
    "W                           W                                   W",
    "W                            W                                  W",
    "W                             W                                 W",
    "W                              W                                W",
    "W                               W                               W",
    "W                                W                              W",
    "W                                 W                             W",
    "W                                  W                            W",
    "W                                                               W",
    "W                                                               W",
    "W                                                               W",
    "W                                                               W",
    "W                                                               W",
    "W                              P                                W",
    "WH                                                             IW",
    "WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW",
    ]]

def load_level(level):
    walls = []
    players = []
    finishes = []
    x = y = 0
    for row in levels[level]:
        for col in row:
            if col == "W":
                walls.append(Wall((x, y)))
            if col == "P":
                players.append(Player((x, y)))
            if col == "F":
                finishes.append(Finish1((x, y)))
            if col == "G":
                finishes.append(Finish2((x, y)))
            if col == "H":
                finishes.append(Finish3((x, y)))
            if col == "I":
                finishes.append(Finish4((x, y)))
            x += 16
        y += 16
        x = 0
    return walls, players, finishes

walls, players, finishes = load_level(currentLevel)


def Menu():
    runnin = True
    while runnin:
        clock.tick(60)
        screen.fill(BLACK)
        mouseclick = pygame.mouse.get_pressed()
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit(0)
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    pygame.quit()
                    sys.exit(0)
        for option in options:
            if option.rect.collidepoint(pygame.mouse.get_pos()):
                option.hovered = True
                if mouseclick[0] == 1:
                    if option.text == "Easy":
                        walls, players, finishes = load_level(0)
                        global currentlevel
                        currentlevel = 0
                        main()
                    elif option.text == "Medium":
                        walls, players, finishes = load_level(1)
                        currentlevel = 1
                        main()
                    elif option.text == "Hard":
                        walls, players, finishes = load_level(2)
                        currentlevel = 2
                        main()
                    else:
                        runnin = False
            else:
                option.hovered = False
            option.draw()
        pygame.display.update()
    pygame.quit()
    sys.exit(0)

def main():
    pygame.display.update()
    rand = random.randint(1, 4)
    running = True
    while running:
        for option in options:
            if option.rect.collidepoint(pygame.mouse.get_pos()):
                option.hovered = True
            else:
                option.hovered = False
            option.draw()
        # Move the player if an arrow key is pressed
        key = pygame.key.get_pressed()
        if total_seconds > 0:
            if key[pygame.K_LEFT]:
                player.move(-2, 0)
            if key[pygame.K_RIGHT]:
                player.move(2, 0)
            if key[pygame.K_UP]:
                player.move(0, -2)
            if key[pygame.K_DOWN]:
                player.move(0, 2)

        for wall in walls:
            pygame.draw.rect(screen, (WHITE), wall.rect)
        for player in players:
            pygame.draw.rect(screen, (YELLOW), player.rect)
        pygame.draw.rect(screen, (GREEN), Finish1.rect)
        pygame.draw.rect(screen, (GREEN), Finish2.rect)
        pygame.draw.rect(screen, (GREEN), Finish3.rect)
        pygame.draw.rect(screen, (GREEN), Finish4.rect)

        if rand == 1:
            pygame.draw.rect(screen, (RED), Finish1.rect)
        elif rand == 2:
            pygame.draw.rect(screen, (RED), Finish2.rect)
        elif rand == 3:
            pygame.draw.rect(screen, (RED), Finish3.rect)
        else:
            pygame.draw.rect(screen, (RED), Finish4.rect)
        pygame.display.flip()
Menu()
1
Please give a minimal reproducible example, it's a bit much to ask people to trawl through all of that. - jonrsharpe

1 Answers

2
votes

Your new edited example isn't working because menu_font is no longer defined. Nevertheless your problem is caused by this line

walls, players, finishes = load_level(currentLevel)

It is always executed, currentLevel is set to 0 at the beginning, therefore your level is set to easy. In your Menu() function you overwrite the variables walls, players, finishes locally (i.e. in your function). Your function main() tries to access the the global variables. You can fix that simply by using by changing the if option.text block and commenting out the first load_level call, or better by passing the variables to your main function.

                if option.text == "Easy":
                    walls, players, finishes = load_level(0)
                    global currentlevel, walls, players, finishes
                    currentlevel = 0
                    main()
                elif option.text == "Medium":
                    walls, players, finishes = load_level(1)
                    global currentlevel, walls, players, finishes
                    currentlevel = 1
                    main()
                elif option.text == "Hard":
                    walls, players, finishes = load_level(2)
                    global currentlevel, walls, players, finishes
                    currentlevel = 2
                    main()
                else:
                    runnin = False

alternatively:

            if option.text == "Easy":
                walls, players, finishes = load_level(0)
                currentlevel = 0
                main(walls, players, finishes, currentlevel)
-------------
def main(walls, players, finishes, currentlevel):
[put your old code here]