Sublime Forum

Help me please i need it for my code

#1

i cant run a pygame window from this errror “IndentationError: unindent does not match any outer indentation level” and i think all of my code is correct

import pygame
from sprites import *
from config import *
import sys

class Game:
      def _init_(self):
         pygame.init()
         self.screen = pygame.display.set_mode((WIN_WIDTH, WIN_HEIGHT))
         self.clock = pygame.time.Clock()
         self.font = pygame.font.Font('Arial', 32)
         self.running = True

       def new(self):
           # a new game starts
           self.playing = True

           self.all_sprites = pygame.sprite.LayeredUpdates()
           self.blocks = pygame.sprite.LayeredUpdates()
           self.enemies = pygame.sprite.LayeredUpdates()
           self.attacks = pygame.sprite.LayeredUpdates

           self.player = Player(self, 1, 2)

           def events(self):
               # game loop evets
               for event in pygame.event.get():
                  if event.type == pygame.QUIT:
                     self.playing = False
                     self.running = False

        def update(self):
        #game loop updates
         self.all_sprites.update()

        def draw(self):
        # game loop draw
        self.screen.fill(BLACK)
        self.all_sprites.draw(self.screen)
        self.clock.tick(FPS)
        pygame.display.update()

        def main(self):
        # game loop
        while self.playing
              self.events()
              self.update()
              self.draw()
            self.running = False

        def game_over(self):
            pass

        def intro_screen(self):
            pass

g = Game()
g.intro_screen()
g.new()
while g.running():
    g.main()
    g.game_over()

pygame.quit()
sys.exit()
0 Likes

#2

The technical support section (and the Sublime forum in general) is more related to problems with Sublime specifically and not questions on coding in general; for those you are probably better off finding a forum specific for that kind of thing.

That said, the error message that Python generates tells you specifically what line the error was seen on:

   
  File "main.py", line 14
    def new(self):
                 ^
IndentationError: unindent does not match any outer indentation level

If you examine your code, none of your indentation is consistent:

image

  • _init_ is indented 6 spaces
  • new is indented 7 spaces
  • The body of _init_ is indented 3 spaces
  • The body of new is indented 4 spaces

Python uses Indentation to know what code goes in what block, and as the error suggests, that needs to be consistent across the whole file.

It may also be worth mentioning that your code is not explicitly calling _init_ anywhere, so it’s not going to do anything. I think you mean to use __init__ there instead.

0 Likes

#3

so how do i make it consistent

0 Likes

#4

@Leo1 https://www.prepbytes.com/blog/python/indentation-in-python/#:~:text=A%3A%20The%20Python%20style%20guide,for%20each%20level%20of%20indentation.

0 Likes

#5

i still do not understand because i am a beginner

0 Likes

#6

i still dont understand

0 Likes

#7

i need help

0 Likes

#8

Your problem is unrelated to Sublime Text. I suggest asking on forums/discords for beginner programmers.

0 Likes