Python:从Pygame proj中的模块导入变量

2024-06-07 14:08:11 发布

您现在位置:Python中文网/ 问答频道 /正文

首先,全面道歉:这是我的第一个问题。你知道吗

我在做一个Pygame项目,有两个类代表游戏中的对象,每个类都在自己的模块中。一个由玩家控制(发货.py). 我在试着做另一个(泡菜.py)在屏幕周围跟随ship对象。为此,我需要将船的centerx和centery值传递到kimchi模块,这就是我要解决的问题。我正在使用'从船进口船',然后试图使用发货.centerx访问变量,但这不起作用。你知道吗

以下是我的完整错误信息:

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'

以及泡菜.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---

下面是我创建船和泡菜对象的模块:

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()

Tags: frompyimportselfgamebssettingsscreen

热门问题