-1

I thought that importing pygame and the sys module would suffice but it does not. I'm aware that self is not defined in my code. I'm using the a book for this project and the instructions are as follows:

import pygame
import sys

class AlienInvasion:
    """Overall class to manage assets and behavior"""

def __init__(self):
    """Initiaze the game, and create the game resources."""

pygame.init()
self.screen = pygame.display.set_mode((1200, 800))

when i run my code, I get a self not defined error. find below my code:

import pygame
import sys

class AlienInvasion:
    """Overall class to manage assets and behavior"""

def __init__(self):
    """Initiaze the game, and create the game resources."""

pygame.init()
self.screen = pygame.display.set_mode((1200, 800))

this is the error I get:

enter image description here

testingman
  • 11
  • 1
  • Please update your question with the full error traceback. – quamrana May 18 '21 at 15:45
  • 1
    you need to indent `def __init__(self)` to be inside class AlienInvasion. spacing in python matters. See [answer](https://stackoverflow.com/a/7024476/3462319) – depperm May 18 '21 at 15:45
  • 3
    You need to copy the indentation from the book, not just the text. If that's actually how the code appears indented in the book, then *throw the book away* - it's useless. – jasonharper May 18 '21 at 15:47
  • One of the first things you should have learned about Python is that indentation is part of the syntax. It takes the place of `{}` for marking code blocks in languages like C, JavaScript, and PHP. – Barmar May 18 '21 at 15:53
  • This code never uses the class or function that it defines. – Barmar May 18 '21 at 15:55

1 Answers1

1

you made a mistake in class structure

import pygame
import sys

class AlienInvasion:
    """Overall class to manage assets and behavior"""
   def __init__(self):
       """Initiaze the game, and create the game resources."""
       self.screen = pygame.display.set_mode((1200, 800))

Test:

AlienInvasion()
top talent
  • 615
  • 4
  • 17