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?