JimJam
JimJam

Reputation: 3

Working through Python Crash Course: NameError: 'self' is not defined

import pygame
from pygame.sprite import Sprite

class Bullet(Sprite):
    '''A class to manage bullets fired from the ship.'''

    def __init__(self, ai_game):
        '''Create a bullet object at the ship's current position'''
        super().__init__()
        self.screen = ai_game.screen
        self.settings = ai_game.settings
        self.color = self.settings.bullet_color

    # Create a bullet rect at (0, 0) and then set correct position. THIS LINE IS THE ISSUE. I GET TRACEBACK TO THIS LINE AND SAYS SELF NOT DEFINED.
    self.rect = pygame.Rect(0, 0, self.settings.bullet_height, self.settings.bullet_width)
    self.rect.midtop = ai_game.ship.rect.midtop

    # Store the bullet's position as a decimal value.
    self.x = float(self.rect.x)

    def update(self):
        '''Move the bullet up the screen.'''
        # Update the decmimal position of the bullet
        self.x -= self.settings.bullet_speed
        # Update the rect position.
        self.rect.x = self.x

    def draw_bullet(self):
        '''Draw the bullet to the screen'''
        pygame.draw.rect(self.screen, self.color, self.rect)

Any help would be greatly appreciated.

Upvotes: 0

Views: 130

Answers (2)

Dallan
Dallan

Reputation: 516

Since Python doesn't have {} like in other popular languages, indentation is important:

import pygame
from pygame.sprite import Sprite

class Bullet(Sprite):
    '''A class to manage bullets fired from the ship.'''

    def __init__(self, ai_game):
        '''Create a bullet object at the ship's current position'''
        super().__init__()
        self.screen = ai_game.screen
        self.settings = ai_game.settings
        self.color = self.settings.bullet_color

        # Create a bullet rect at (0, 0) and then set correct position. THIS LINE IS THE ISSUE. I GET TRACEBACK TO THIS LINE AND SAYS SELF NOT DEFINED.
        self.rect = pygame.Rect(0, 0, self.settings.bullet_height, self.settings.bullet_width)
        self.rect.midtop = ai_game.ship.rect.midtop

        # Store the bullet's position as a decimal value.
        self.x = float(self.rect.x)

    def update(self):
        '''Move the bullet up the screen.'''
        # Update the decmimal position of the bullet
        self.x -= self.settings.bullet_speed
        # Update the rect position.
        self.rect.x = self.x

    def draw_bullet(self):
        '''Draw the bullet to the screen'''
        pygame.draw.rect(self.screen, self.color, self.rect)

Upvotes: 1

Unsigned_Arduino
Unsigned_Arduino

Reputation: 366

You forgot some indents so some variables were initialized outside if __init__()

python pygame
import pygame
from pygame.sprite import Sprite

class Bullet(Sprite):
    '''A class to manage bullets fired from the ship.'''

    def __init__(self, ai_game):
        '''Create a bullet object at the ship's current position'''
        super().__init__()
        self.screen = ai_game.screen
        self.settings = ai_game.settings
        self.color = self.settings.bullet_color

        # Create a bullet rect at (0, 0) and then set correct position. THIS LINE IS THE ISSUE. I GET TRACEBACK TO THIS LINE AND SAYS SELF NOT DEFINED.
        self.rect = pygame.Rect(0, 0, self.settings.bullet_height, self.settings.bullet_width)
        self.rect.midtop = ai_game.ship.rect.midtop

        # Store the bullet's position as a decimal value.
        self.x = float(self.rect.x)

    def update(self):
        '''Move the bullet up the screen.'''
        # Update the decmimal position of the bullet
        self.x -= self.settings.bullet_speed
        # Update the rect position.
        self.rect.x = self.x

    def draw_bullet(self):
        '''Draw the bullet to the screen'''
        pygame.draw.rect(self.screen, self.color, self.rect)

Upvotes: 0

Related Questions