0

I'm new to pygame and this is a simple game I'm making. I've been encountering this problem where I can go up to level 2 but not further.

This is main.py:

import pygame
import sys
from Button import Button
from level_maps import *
from level import Level
pygame.init()
pygame.display.set_caption("My Platformer Game")
background_image = pygame.image.load("owl.png")

background_image = pygame.transform.scale(background_image, (SCREEN_WIDTH, SCREEN_HEIGHT))
background_rect = background_image.get_rect()
font = pygame.font.Font(None, 36)

level_no = 0
game_start = False

screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
screen.fill("black")
start_button = Button(SCREEN_WIDTH // 2 - 100, SCREEN_HEIGHT // 2, 200, 50, "Start Game")

level = Level(map[0], screen)

clock = pygame.time.Clock()
run = True
while True:
    screen.blit(background_image, background_rect)
    start_button.draw(screen)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        elif event.type == pygame.MOUSEBUTTONDOWN:
            if start_button.rect.collidepoint(event.pos):
                game_start = True


    if game_start:
        screen.fill("Black")
        level.run()
        #level_no = level.level

        if level_no == 2:
            screen.fill("black")
            level = Level(map[1], screen)
            level.run()
        level_no = level.level
        if level_no == 3:
            screen.fill("black")
            level = Level(map[2], screen)
            level.run()
        if level_no == 4:
            screen.fill("black")
            level = Level(map[3], screen)
            level.run()

    pygame.display.flip()
    clock.tick(60)

My level maps are in level_maps.py which looks like this:

map = [[
    '            ',
    '            ',
    '            ',
    '            ',
    '            ',
    '            ',
    '            ',
    '        KKK ',
    '            ',
    '  P KKK         ',
    '              C ',
    'XXXXXXXXXXXXXXXX',],
    [
    'KKKKKKKKKKKKKKKKK',
    'B            ',
    'B            ',
    'B            ',
    'B   KKKK   KKKK      ',
    'B   B         B',
    'B   B         B ',
    'B   B    K    B',
    'B   B    B    B',
    '    B    B    B',
    'P   B    B    B',
    'XXXXXXXXXXXXXXXXX',
],
[
    'KKKKKKKKKKKKKKKK',
    '              ',
    '              ',
    '              ',
    '              ',
    '              ',
    '              ',
    '        KKK   ',
    '              ',
    '  P KKK         ',
    '                ',
    'XXXXXXXXXXXXXXXXX',
]]

And also in the level.py where I have the Level class, I have a function to increment level.

def check_end(self):
    player = self.player_surf.sprite
    if player.rect.x > 200:
    self.level += 1
    print(self.level)

This is the output to printing self.level

Even though it prints out 3, the level map does not load, the second map loads over and over again, it does not go further. How do I fix this?

Robert
  • 7,394
  • 40
  • 45
  • 64
  • Welcome to SO! This is a lot of code, can you run us through it? For instance, what is "Level" – Lexpj Apr 25 '23 at 15:55
  • Level is a class which I use to place things on the map, it also has functions that handle collision and also checks the end of a level. – Learning Kid Apr 26 '23 at 06:20

1 Answers1

-1

Welcome to SO. Are you updating the player x position back to 0 when you finish the level? If you're not, it may think that you're passing the level over and over again, resulting in that behavior where it loops, outputting this (https://i.stack.imgur.com/kDU2U.png) over and over again. It may be difficult to determine the source of the problem without more code, but I strongly suspect that this is what's happening.

Brock Brown
  • 956
  • 5
  • 11
  • 1
    Thanks! I think I figured out what the problem is. Since the level no is in level class and I had set it such that: self.level=0, everytime a new level loads it starts from the beginning all over again. – Learning Kid Apr 25 '23 at 17:17