0

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()
  • 4
    Where are you defining your Kimchi object? It looks like you might just be passing a Kimchi object instead of a Ship object. – SuperTetelman Mar 07 '17 at 03:13
  • Not sure if my noob brain can follow yet, but I'm editing to add the code where I create my ship and kimchi objects in the hopes that will help. – Ben Harvey Mar 08 '17 at 00:54
  • Show us the `create_kimchi`, `instantiate_kimchi` functions and the `Ship` class and everything else that's relevant. Also, read this: [mcve](http://stackoverflow.com/help/mcve). – skrx Mar 08 '17 at 08:59
  • As @skrx said you should probably read that post. If you read a little bit closer into your stack trace you will clear see that one of the last line of your code that errored was the create_kimchi function. When posting code/asking for help you should make sure people can read through the relevant code in your trace. – SuperTetelman Mar 10 '17 at 22:22
  • I actually got this resolved elsewhere, but learned about how to post the relevant code for my question which will be helpful next time. Thanks all! – Ben Harvey Mar 12 '17 at 18:03

0 Answers0