'AttributeError: 'pygame.Surface' object has no attribute 'ship_img'

So, I was doing a project by the help of a YouTube tutorial and suddenly I got this error. I searched the entire internet reviewed it again and again, but nothing worked.I don't understand what is happening, Can you please help me? This is the error: AttributeError: 'pygame.Surface' object has no attribute 'ship_img'

import pygame
import os
pygame.font.init()

# screen
wth, hht = 800, 600
window = pygame.display.set_mode((wth, hht))
pygame.display.set_caption("Space Invaders")

# load images
red_space_ship = pygame.image.load(os.path.join("F:/shaan/python/pixel_ship_red_small.png"))
green_space_ship = pygame.image.load(os.path.join("F:/shaan/python/pixel_ship_green_small.png"))
blue_space_ship = pygame.image.load(os.path.join("F:/shaan/python/pixel_ship_blue_small.png"))

# player ship
yellow_space_ship = pygame.image.load(os.path.join("F:/shaan/python/pixel_ship_yellow.png"))

# lasers
red_laser = pygame.image.load(os.path.join("F:/shaan/python/pixel_laser_red.png"))
green_laser = pygame.image.load(os.path.join("F:/shaan/python/pixel_laser_green.png"))
blue_laser = pygame.image.load(os.path.join("F:/shaan/python/pixel_laser_blue.png"))
yellow_laser = pygame.image.load(os.path.join("F:/shaan/python/pixel_laser_yellow.png"))

# Background
BG = pygame.transform.scale(pygame.image.load(os.path.join("F:/shaan/python/oackground-black.png")), (wth, hht))


class Ship:
    def __init__(self, x, y, health=100):
        self.x = x
        self.y = y
        self.health = health
        self.ship_img = None
        self.laser_img = None
        self.lasers = []
        self.cool_down_counter = 0

    def get_width(self):
        return self.ship_img.get_width()

    def get_height(self):
        return self.ship_img.get_height()

    def draw(self, windows):
        windows.blit(self.ship_img, (self.x, self.y))


class Player(Ship):
    def __init__(self, x, y, health=100):
        super().__init__(x, y, health)
        self.ship_img = yellow_space_ship
        self.laser_img = yellow_laser
        self.mask = pygame.mask.from_surface(self.ship_img)
        self.max_health = health


def main():
    run = True
    fps = 60
    level = 1
    lives = 5
    main_font = pygame.font.SysFont("comicsans", 20)

    player_vel = 5

    clock = pygame.time.Clock()

    def redraw_window(windows):
        window.blit(BG, (0, 0))
        # text
        lives_label = main_font.render(f"Lives: {lives}", True, (255, 255, 255))
        level_label = main_font.render(f"Level: {level}", True, (255, 255, 255))

        window.blit(lives_label, (10, 10))
        window.blit(lives_label, (wth - level_label.get_width() - 10, 10))

        Ship.draw(window, windows)

        pygame.display.update()

    while run:
        clock.tick(fps)
        redraw_window(window)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False

        keys = pygame.key.get_pressed()
        if keys[pygame.K_a] and Player.x - player_vel > 0:
            Player.x -= player_vel
        if keys[pygame.K_d] and Player.x + player_vel + Player.get_width() < wth:
            Player.x += player_vel
        if keys[pygame.K_w] and Player.y - player_vel > 0:
            Player.y -= player_vel
        if keys[pygame.K_s] and Player.y + player_vel + Player.get_height() < hht:
            Player.y += player_vel


main()

The whole error:

Traceback (most recent call last):
  File "F:\shaan\python\Main.py", line 99, in <module>
    main()
  File "F:\shaan\python\Main.py", line 83, in main
    redraw_window(window)
  File "F:\shaan\python\Main.py", line 77, in redraw_window
    Ship.draw(window, windows)
  File "F:\shaan\python\Main.py", line 45, in draw
    windows.blit(self.ship_img, (self.x, self.y))
AttributeError: 'pygame.Surface' object has no attribute 'ship_img'


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source