简体   繁体   中英

"IndentationError: expected an indented block" from a tutorial for Pygame

I followed a tutorial for Pygame just to get a feel for it. I am new to coding and instead of copying and pasting the code I typed them out myself. It worked fine in the video and I have no idea why a certain line is supposed to be indented when it wasn't in the video. I've gotten the error for line 51 and 54.

def show_go_screen(self):
  ^
IndentationError: expected an indented block

Tried to get the error code in correctly, this is my first time posting on here.

import pygame as pg
import random
import os
from settings import *

class Game:
    def __init__(self):
        # initialize window
        pg.init()
        pg.mixer.init()
        screen = pg.display.set_mode((WIDTH, HEIGHT))
        pg.display.set_caption(TITLE)
        clock = pg.time.Clock()
        self.running = True

    def new(self):
        # Start New Game
        self.all_sprites = pg.sprite.Group()

    def run(self):
        # Game Loop
        self.playing = True
        while self.playing:
            self.clock.tick(FPS)
            self.events()
            self.update()
            self.draw()

    def update(self):
        # Game Loop Update
        self.all_sprites.update()

    def events(self):
        # Game Loop Events
        for event in pg.event.quit():
            if event.type == pg.QUIT:
                if self.playing:
                    self.playing = False
                self.running = False

    def draw(self):
        # Game Loop Draw
        self.screen.fill(BLACK)
        self.all_sprites.draw(self.screen)

        pg.display.flip()

    def show_start_screen(self):
        # game start screen

    def show_go_screen(self):
        # game over/continue
    
g = Game()
g.show_start_screen()
while g.running:
    g.new()
    g.run()
    g.show_go_screen()

pg.quit
    

The problem is here:

def show_start_screen(self):
    # game start screen

def show_go_screen(self):
    # game over/continue

You can't just leave a function blank like in C or Java, you need to use pass .

def show_start_screen(self):
    # game start screen
    pass

def show_go_screen(self):
    # game over/continue
    pass

This should fix the indentation problem but I don't think the code will work as expected because the functions aren't there for no reason... Why would a blank function get called for no reason? You certainly missed something.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM