Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to avoid "SyntaxError expected an indented block"?

I’ve checked that I have 4 spaces. But the error still shows up. I’m using classes for the first time. I do not know why, but when I copy-paste working code, the error still shows up.
Here’s the code:

import pygame, sys, random, time
from pygame.locals import *
pygame.init()

s=600

BLUE  = (0, 0, 255)
RED   = (255, 0, 0)
GREEN = (0, 255, 0)
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)

disp=pygame.display.set_mode((600,600))
disp.fill(BLACK)
pygame.display.set_caption("Game")

class Apple(pygame.sprite.Sprite):
    def _init_(self):
        super()._init_()
        self.image=pygame.image.load("Leaf.png")
        self.rect=self.image.get_rect()
        self.rect.center=(random.randint(10,s-10),(10,s-10))
    def ate(self):
        
class Player(pygame.sprite.Sprite):
    def _init_(self):
        super().__init__() 
        self.image = pygame.image.load("Ant.png")
        self.rect = self.image.get_rect()
        self.rect.center = (160, 520)
    def move(self):
        pressed_keys=pygame.key.get_pressed()
        if self.rect.up > 0:
            if pressed_keys[K_UP]:
                self.rect.move_ip(0, -5)
        if self.rect.down<s:
            if pressed_keys[K_DOWN]:
                self.rect.move_ip(0,5)
        
        if self.rect.left > 0:
              if pressed_keys[K_LEFT]:
                  self.rect.move_ip(-5, 0)
        if self.rect.right < s:        
              if pressed_keys[K_RIGHT]:
                  self.rect.move_ip(5, 0)
P1=Player()
A1=Apple()

The error is in the line class Player(pygame.sprite.Sprite):.

I tried 4 spaces and tab. It still didn’t work. I do not know how to fix the code. I need help.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

A function definition must contain a body, even if it’s a trivial body like pass.

class Apple(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__()
        self.image = pygame.image.load("Leaf.png")
        self.rect = self.image.get_rect()
        self.rect.center = (random.randint(10, s-10), (10, s-10))

    def ate(self):
        pass

Without pass following the :, the parser is looking for the body, but the next text it finds is the unindented definition of Player, resulting in the observed error.

See Why does python allow an empty function (with doc-string) body without a "pass" statement? for more information about what can serve as a "minimal" function body aside from the pass statement.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading