AttributeError: 'Ship' object has no attribute' update' appears in Python

when adding ship.update () "Ship" object has no attribute" update", code to alien_invasion.py is written according to the book, ask the boss to solve the problem description of the error

"Python programming from introduction to practice" the source of game writing topics for Chinese and foreign invaders and their own ideas

-sharp-sharp-sharp  alien_invasion.py

import pygame

import sys

from settings import Settings

from ship import Ship

import game_functions as gf

def run_game():
    -sharp 
    pygame.init()
    ai_settings = Settings()
    screen = pygame.display.set_mode((ai_settings.screen_width,ai_settings.screen_height))
    pygame.display.set_caption("Alien Invasion")

    -sharp 
    ship = Ship(screen)

    -sharp 
    while True:
        gf.check_events(ship)
        ship.update()
        gf.update_screen(ai_settings,screen,ship)

        -sharp 
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()

run_game()


ship.py

import pygame

class Ship():

    def __init__(self,screen):
        """"""
        self.screen = screen

        -sharp 
        self.image = pygame.image.load("images/ship.bmp")
        self.rect = self.image.get_rect()
        self.screen_rect = screen.get_rect()

        -sharp 
        self.rect.centerx = self.screen_rect.centerx
        self.rect.bpttom = self.screen_rect.bottom

        -sharp 
        self.moving_right = False
        self.moving_right = False

    def update(self):
        """"""
        if self.moving_right:
            self.rect.centerx += 1
        if self.moving_left:
            self.rect.centerx -= 1

    def blitme(self):
        """"""
        self.screen.blit(self.image,self.rect)
        
 settings.py       
        
 class Settings():
    """"""

    def __init__(self):
        """"""
        -sharp 
        self.screen_width = 1200
        self.screen_height = 800
        self.bg_color = (0,128,255)       

import sys

import pygame

def check_events(ship):
    """"""
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()

        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_RIGHT:
                ship.moving_right = True
            elif event.key == pygame.K_LEFT:
                ship.moving_left = True

        elif event.type == pygame.KEYUP:
            if event.key == pygame.K_RIGHT:
                ship.moving_right = False
            elif event.key == pygame.K_LEFT:
                ship.moving_left = False


game_functions.py
def update_screen(ai_settings,screen,ship):
    """"""
    -sharp 
    screen.fill(ai_settings.bg_color)
    ship.blitme()


    -sharp 
    pygame.display.flip()

the above is all the code.

Ps: I also have another question, that is, why there is no import Ship, in game_functions.py, but I try to call the method in Ship, but there is no error. On the contrary, alien_invasion.py import Ship, will report an error when calling update, asking for boss

.
Nov.19,2021

Why the update attribute is missing, there are several possibilities

  • you have a ship.py file elsewhere, but you actually import another file
  • your * .pyc is old and needs to be deleted. If it is python 3, delete the _ _ pycache__ directory directly

Why there is no import Ship, in game_functions.py, but I tried to call the method in Ship myself, but did not report an error

because what you pass in is an object, and the related methods belong to the object, there is no need to import
Menu