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

时间:2019-11-15 02:06:17

标签: python pygame

Python的新手,使自己陷入困境几个小时。我正在尝试遵循《 Python崩溃课程》一书中的示例。我在试图让子弹在屏幕上射击的最后一部分,但是在尝试通过“ _update_bullets”方法定义子弹在被删除之前可以传播的游戏屏幕限制时,我带着AttributeError来了。我尝试使用self.screen_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()

'''

1 个答案:

答案 0 :(得分:0)

由于没有定义“ screen_right ”-属性(在您的示例中),因此出现属性错误。 您需要将它初始化为Int,才能在它的所有函数中访问它,这些函数必须在您的“ __ init __(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