Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
(Python速成班)外星人入侵项目_Python_Python 3.x_Pygame - Fatal编程技术网

(Python速成班)外星人入侵项目

(Python速成班)外星人入侵项目,python,python-3.x,pygame,Python,Python 3.x,Pygame,如果有人能告诉我如何修复此错误,我将不胜感激: AttributeError: 'Ship' object has no attribute 'bullet_width' 我正在开始学习Python,我不确定解决这个问题的方法是什么。当我按下空格键时,这种情况总是发生。 以下是本项目中的模块: alien_.py: import sys import pygame from settings import Settings from ship import Sh

如果有人能告诉我如何修复此错误,我将不胜感激:

AttributeError: 'Ship' object has no attribute 'bullet_width'
我正在开始学习Python,我不确定解决这个问题的方法是什么。当我按下空格键时,这种情况总是发生。 以下是本项目中的模块:

alien_.py:

    import sys
    import pygame
    from settings import Settings
    from ship import Ship
    import game_functions as gf
    from pygame.sprite import Group

    def run_game():
        pygame.init()
        ai_settings = Settings()
        screen = pygame.display.set_mode(
            (ai_settings.screen_width, ai_settings.screen_height))
        screen = pygame.display.set_mode((1100, 600))
        pygame.display.set_caption("Alien Invasion")
        bg_color = (230, 230, 230)
        ship = Ship(screen, ai_settings)
        bullets = Group()
        while True:
            gf.check_events(ship, screen, ai_settings, bullets)
            ship.update()
            bullets.update()
            gf.update_screen(ai_settings, screen, ship, bullets)
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                     sys.exit()
            screen.fill(ai_settings.bg_color)
            ship.blitme()
            screen.fill(bg_color)

    run_game()
ship.py:

    import pygame
    class Ship():
        def __init__(self, screen, ai_settings):
            self.screen = screen
            self.ai_settings = ai_settings
            self.image = pygame.image.load('images/spaceship.png')
            self.rect = self.image.get_rect()
            self.screen_rect = screen.get_rect()
            self.rect.centerx = self.screen_rect.centerx
            self.rect.bottom = self.screen_rect.bottom
            self.center = float(self.rect.centerx)
            self.moving_right = False
            self.moving_left = False
        def update(self):
            if self.moving_right and self.rect.right < self.screen_rect.right:
                self.center += self.ai_settings.ship_speed_factor
            if self.moving_left and self.rect.left > 0:
                self.center -= self.ai_settings.ship_speed_factor
            self.rect.centerx = self.center

        def blitme(self):
            self.screen.blit(self.image, self.rect)
game_functions.py

    import sys
    import pygame
    from bullet import Bullet

    def check_keydown_events(event, ai_settings, screen, ship, bullets):
        if event.key == pygame.K_RIGHT:
            ship.moving_right = True
        elif event.key == pygame.K_LEFT:
            ship.moving_left = True
        elif event.key == pygame.K_SPACE:
            new_bullet = Bullet(ai_settings, screen, ship)
            bullets.add(new_bullet)
    def check_keyup_events(event, ship):
        if event.key == pygame.K_RIGHT:
            ship.moving_right = False
        elif event.key == pygame.K_LEFT:
            ship.moving_left = False
    def check_events(ai_settings, screen, ship, bullets):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            elif event.type == pygame.KEYDOWN:
                check_keydown_events(event, ai_settings, screen, ship, bullets)
            elif event.type == pygame.KEYUP:
                check_keyup_events(event, ship)


    def update_screen(ai_settings, screen, ship, bullets):
        screen.fill(ai_settings.bg_color)
        for bullet in bullets.sprites():
            bullet.draw_bullet()
        ship.blitme()
        pygame.display.flip()
settings.py:

    class Settings():
        def __init__(self):
            self.screen_width = 1200
            self.screen_height = 800
            self.bg_color = (230, 230, 230)
            self.ship_speed_factor = 1.5

            self.bullet_speed_factor = 1
            self.bullet_width = 3
            self.bullet_height = 15
            self.bullet_color = 60, 60, 60

调用
check\u events
时,参数的顺序错误:

检查事件(发货、屏幕、ai设置、项目符号)

检查事件(ai设置、屏幕、发货、项目符号)

参数的顺序很重要,除了。

调用
检查事件时,参数的顺序是错误的:

检查事件(发货、屏幕、ai设置、项目符号)

检查事件(ai设置、屏幕、发货、项目符号)

参数的顺序很重要,除了。

看起来由于某种原因,
ai_设置
变成了
ship
(可能是超级调用),但这并不是真正可以解决的。你需要启动一个调试器,一步一步地进行,直到找到错误发生的时刻。
检查事件(发货、屏幕、ai设置、项目符号)
->
检查事件(ai设置、屏幕、发货、项目符号)
我刚刚更改了它,我很高兴它可以工作!但是为什么顺序很重要?@JackBrown请学习基本知识。参数的顺序很重要,除了。在所有语言中,参数的顺序都很重要(关键字参数除外)。语言不关心如何命名变量-函数外和函数内可能有不同的名称-它们关心参数的位置这样的问题出于某种原因
ai_设置
变成
ship
(可能是超级调用),但这并不是真正可以解决的。你需要启动一个调试器,一步一步地进行,直到找到错误发生的时刻。
检查事件(发货、屏幕、ai设置、项目符号)
->
检查事件(ai设置、屏幕、发货、项目符号)
我刚刚更改了它,我很高兴它可以工作!但是为什么顺序很重要?@JackBrown请学习基本知识。参数的顺序很重要,除了。在所有语言中,参数的顺序都很重要(关键字参数除外)。语言不关心如何命名变量——函数外部和函数内部可能有不同的名称——它们关心参数的位置
    class Settings():
        def __init__(self):
            self.screen_width = 1200
            self.screen_height = 800
            self.bg_color = (230, 230, 230)
            self.ship_speed_factor = 1.5

            self.bullet_speed_factor = 1
            self.bullet_width = 3
            self.bullet_height = 15
            self.bullet_color = 60, 60, 60