Please could someone be of assistance.. probably not a difficult one but i sure am baffled :/!

So basically this exercise is not actually part of a fcc exercise but from the python crash couse book… Im working on a game that works like space-invaders, my issue arises when i try to create the bullets, because the ship and the bullets are connected were supposed to use the pygame.sprite module and import Sprite (I think ) I have done everything by the book but i am still getting a “ImportError: cannot import name ‘sprite’ from ‘pygame.sprite’”… if anyone could help me out with this error i would be greatly appreciative… Here is the module for the bullets:

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 ships current location"""
        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.
        self.rect = pygame.Rect(0, 0, self.settings.bullet_width,
            self.settings.bullet_height)
        self.rect.midtop = ai_game.ship.rect.midtop

        #Store the bullets value as a decimal value.
        self.y = float(self.rect.y)

    def update(self):
        """Move bullet up the screen"""
        #update decimal value of the bullet.
        self.y -= self.settings.bullet_speed
        #update rect position.
        self.rect.y = self.y

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

(please let me know if i need to add more of my code in order to be assisted…)

Can you paste the complete error? Is the sprite in it lowercase? Because in the pasted code it’s Sprite, with first letter uppercase.

Hi @jacobplw99 ,

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

Ahh I see… thank you very much.

The complete error is:

ImportError: cannot import name 'sprite' from 'pygame.sprite' (C:\Users\jacob\AppData\Roaming\Python\Python39\site-packages\pygame\sprite.py)

I meant the whole traceback, with the line that’s encounters the error.

ygame 2.0.1 (SDL 2.0.14, Python 3.9.6)
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
  File "C:\Users\jacob\OneDrive\Desktop\Alien_Invasion\alien_i.py", line 5, in <module>
    from bullet import Bullet
  File "C:\Users\jacob\OneDrive\Desktop\Alien_Invasion\bullet.py", line 2, in <module>
    from pygame.sprite import sprite
ImportError: cannot import name 'sprite' from 'pygame.sprite' (C:\Users\jacob\AppData\Roaming\Python\Python39\site-packages\pygame\sprite.py)
from pygame.sprite import sprite

The second sprite should be capitalized here, as in the code you pasted in first post.

yeah, I cant see why its not capitalised there because in the actual program it is…

The only thing that comes to mind is that maybe file with it capitalized isn’t saved.

The traceback names two files. Are you sure your file naming is correct?
Like, maybe you got an old bullet.py but are currently working on bullet_i.py or so - and the old file has the error?

Yes the file naming is correct, the traceback contains two file names because there are multiple modules which i am running from the main program (alien_i.py)…Here is the code from the main program if it helps:

`import sys
import pygame
from settings import Settings
from ship import Ship
from bullet import Bullet

class AlienInvasion:
“”“Overall class to manage game assests and behaviour.”""

def __init__(self):
    """Initialise game, and create game resources."""
    pygame.init()
    self.settings = Settings()

    self.screen = pygame.display.set_mode(
        (self.settings.screen_width, self.settings.screen_height))
    pygame.display.set_caption("Alien Invasion")

    self.ship = Ship(self)
    self.bullets = pygame.sprite.Group()


def run_game(self):
    """start the main loop for the game."""
    while True:
        self._check_events()
        self.ship.update()
        self.bullets.update()
        self._update_screen()

def _check_events(self):
    """Respond to keypresses and mouse events."""
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        elif event.type == pygame.KEYDOWN:
            self.check_keydown_events(event)
        elif event.type == pygame.KEYUP:
            self.check_keyup_events(event)

def check_keydown_events(self,event):
    """Respond to keypresses"""
    if event.key == pygame.K_RIGHT:
        self.ship.moving_right = True
    elif event.key == pygame.K_LEFT:
        self.ship.moving_left = True

    elif self.key == pygame.K_SPACE:
        self.fire_bullet()

    elif event.key == pygame.K_q:
        sys.exit()

def check_keyup_events(self,event):
    if event.key == pygame.K_RIGHT:
        self.ship.moving_right = False
    elif event.key == pygame.K_LEFT:
        self.ship.moving_left = False

def _fire_bullet(self):
    """Create a new bullet and add it to the bullet group"""
    new_bulet = Bullet(self)
    self.bullets.add(new_bulet)

def _update_screen(self):
    """Update images on the screen and flip to the new screen"""
    #Redraw the screen for each pass through the loop.
    self.screen.fill(self.settings.bg_color)
    self.ship.blitme()
    for bullet in self.bullets.sprites():
        self.draw_bullet()

    pygame.display.flip()

if name == ‘main’:
ai= AlienInvasion()
ai.run_game()
`

hm… weird… if the files are correct, there is no reason for the traceback to have different capitalization than your code…
You are only using Sprite once in the code, right? Then maybe comment out the import and just use pygame.sprite.Sprite instead and see if this causes another error or works out.

You were right ! As it turns out I randomly happened to have another bullet.py saved somewhere on my computer, after deleting that file it started working… thank you very much !

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.