属性错误:侧向射击对象没有属性!Python速成课程第2版

属性错误:侧向射击对象没有属性!Python速成课程第2版,python,pygame,Python,Pygame,我是Python新手,已经让自己难堪了几个小时。我正试图按照《Python速成课程》一书中的例子进行学习。我在尝试让子弹穿过屏幕射击的最后一部分,但我在尝试定义游戏屏幕的限制时,使用了“\u update\u bullets”方法,在删除子弹之前,子弹可以移动。我尝试过使用self.screen\u rect,但这也会产生AttributeError import sys import pygame from gun import Gun from bullets import Bullet

我是Python新手,已经让自己难堪了几个小时。我正试图按照《Python速成课程》一书中的例子进行学习。我在尝试让子弹穿过屏幕射击的最后一部分,但我在尝试定义游戏屏幕的限制时,使用了“\u update\u bullets”方法,在删除子弹之前,子弹可以移动。我尝试过使用
self.screen\u rect
,但这也会产生
AttributeError

import sys
import pygame
from gun import Gun
from bullets import Bullet

class SidewaysShooter:
    #Game board

    def __init__(self):
        #Initializes game and resources.
        pygame.init()


        self.screen = pygame.display.set_mode((1200, 500))
        pygame.display.set_caption("Sideways Shooter")


        self.gun = Gun(self)
        self.bullets = pygame.sprite.Group()

        #set background color
        self.bg_color = (153, 72, 141)

    def run_game(self):
        #Start the main loop for the game
        while True:
            self._check_events()
            self.gun.update ()
            self._update_bullets()
            self._update_screen()

    def _check_events(self):
        #Watch for keyboard and mounse events.
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                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):
        if event.key == pygame.K_UP:
            self.gun.moving_up = True
        elif event.key == pygame.K_DOWN:
            self.gun.moving_down = True
        elif event.key == pygame.K_SPACE:
            self._fire_bullet()
        elif event.key == pygame.K_q:
            pygame.quit()
            sys.exit()

    def _check_keyup_events(self, event):
        if event.key == pygame.K_UP:
            self.gun.moving_up = False
        if event.key == pygame.K_DOWN:
            self.gun.moving_down = False

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

    def _update_bullets(self):
        """Update the position of the bullets and get rid of old bullets."""
        #Update bullets position
        self.bullets.update()

        #Get rid of bullets that have disappeared.
        for bullet in self.bullets.copy():
            if bullet.rect.left >= self.screen_right:
                self.bullets.remove(bullet)


    def _update_screen(self):
        #Update images on a screen and flip to a new screen
        self.screen.fill(self.bg_color)
        self.gun.blitme()
        for bullet in self.bullets.sprites():
            bullet.draw_bullet()


        pygame.display.flip()

if __name__== '__main__':
    #MAke a game instance, and run the game.
    ai = SidewaysShooter()
    ai.run_game()

“”“

您得到属性错误,因为没有定义“屏幕右键””属性(在您的示例中)。 您需要将其序列化为Int,以便在“\uuuu init\uuuuu(self)”函数中所需的所有对象函数中访问它

例如:

def __init__(self):
    #Initializes game and resources.
    pygame.init()
    self.display_width = 1200  # The size of the screen should be defined
    self.display_height = 500  # Now you can use it when you create the screen.
    self.screen = pygame.display.set_mode((self.display_width, self.display_height))
    pygame.display.set_caption("Sideways Shooter")
    self.gun = Gun(self)
    self.bullets = pygame.sprite.Group()
    #set background color
    self.bg_color = (153, 72, 141)
    self.screen_right = self.display_width  # This is the position of the right side of the screen, 0 would be the left side

请添加完整的错误消息(如果堆栈很长,您可以省略堆栈跟踪)。''回溯(最后一次调用):文件“C:\Users\Antho\Desktop\python\u work\python37\shooter\sideways\u shooter.py”,ai.run\u game()文件“C:\Users\Antho\Desktop\python\u work\python37\shooter\sideways\u shooter.py”第89行,在run\u game self中的第29行。\更新\u子弹()文件“C:\Users\Antho\Desktop\python\u work\python37\shooter\sideways\u shooter.py”,第72行,如果bullet.rect.left>=self.screen\u right:AttributeError:“SidewaysShooter”对象没有属性“screen\u right”