First off, blanket apology: this is my first question on SO.
I'm working on a Pygame project with two classes that represent objects in the game, each in their own module. One is controlled by the player (ship.py). I'm trying to make the other (kimchi.py) follow the ship object around the screen. To do this, I need to pass the the centerx and centery values of the ship into the kimchi module, and that's where I'm stumped. I am using 'from ship import Ship', then trying to use ship.centerx to access the variable, but that's not working.
Here's my full error message:
Traceback (most recent call last):
File "bluesky.py", line 51, in <module>
run_game()
File "bluesky.py", line 36, in run_game
gf.create_kimchi(bs_settings, screen, ship, kimchis)
File "/Users/benjamin_harvey/alien_invasion/hot_dog/game_functions.py", line 137, in create_kimchi
instantiate_kimchi(bs_settings, screen, ship, kimchis)
File "/Users/benjamin_harvey/alien_invasion/hot_dog/game_functions.py", line 108, in instantiate_kimchi
kimchi = Kimchi(bs_settings, screen, ship)
File "/Users/benjamin_harvey/alien_invasion/hot_dog/kimchi.py", line 15, in __init__
self.ship_rect_x = ship.centerx
AttributeError: 'Kimchi' object has no attribute 'centerx'
And the relevant code for kimchi.py:
import pygame
from pygame.sprite import Sprite
import random
import math
from ship import Ship
class Kimchi(pygame.sprite.Sprite):
"""A class to represent kimchi."""
def __init__(self, bs_settings, screen, ship):
"""Initialize the kimchi and set its starting position."""
pygame.sprite.Sprite.__init__(self)
self.screen = screen
self.bs_settings = bs_settings
self.ship_rect_x = ship.centerx
self.ship_rect_y = ship.centery
---snip---
Here's the module where I create the ship and kimchi objects:
import pygame
from pygame.sprite import Group
from settings import Settings
from ship import Ship
import game_functions as gf
from hot_dog import Hotdog
from game_stats import GameStats
from button import Button
from kimchi import Kimchi
def run_game():
# Initialize pygame, settings, and screen object.
pygame.init()
# pygame.mixer.pre_init(44100, -16, 2, 2048)
bs_settings = Settings()
screen = pygame.display.set_mode(
(bs_settings.screen_width, bs_settings.screen_height))
pygame.display.set_caption("Blue Sky")
# Make the Play button
play_button = Button(bs_settings, screen, 'Play')
# Create an instance to store stats.
stats = GameStats(bs_settings)
# Make a ship, a group of bullets, and a group of hot_dogs.
ship = Ship(bs_settings, screen)
hot_dogs = Group()
kimchis = Group()
# Create the fleet of hot_dogs.
gf.create_fleet(bs_settings, screen, ship, hot_dogs)
# Create kimchi.
gf.create_kimchi(bs_settings, screen, ship, kimchis)
# Start the main loop for the game.
while True:
gf.check_events(bs_settings, screen, stats, play_button, ship, hot_dogs, kimchis)
if stats.game_active:
ship.update()
gf.update_hot_dogs(bs_settings, hot_dogs, ship, screen)
gf.update_kimchis(bs_settings, screen, ship, kimchis)
gf.update_screen(bs_settings, screen, stats, ship, hot_dogs, play_button, kimchis)
run_game()