首页 > 解决方案 > Python usig pygame中的错误(AttributeError:'builtin_function_or_method'对象没有属性'right')

问题描述

我一直在使用 python 制作游戏,按照我买的书(2019 版)的说明,但是在大约 300-400 行代码之后,我的游戏遇到了一个错误,我终生无法修复。错误如下:“AttributeError:'builtin_function_or_method'对象没有属性'right'”。错误代码如下:

def prep_score(self):

    """Turn the score into a rendered image."""
    score_str = str(self.stats.score)
    self.score_image= self.font.render(score_str, True,
        self.text_color, self.settings.bg_color)

    # Display the score at the top right of the screen.
    self.score_rect = self.score_image.get_rect()
    self.score_rect.right = self.screen_rect.right - 20  <----- error line
    self.score_rect.top = 20

我把它和书比较了大约一个小时,当我只运行这个模块(scoreboard.py)时,它似乎没有任何错误,但是我运行了游戏模块(alien_invasion.py),我明白了终端错误。作者还有一个包含本章确切代码的资源包,当我运行该代码时,它运行良好,但我的版本没有,这没有意义,因为模块“scoreboard.py”是相同的,但“alien_invasion.py”模块是不同的,但因为它有部分代码出现在本书后面,我不知道是什么代码有所作为。

这是包含此内容的 alien_invasion.py 代码:

from scoreboard import Scoreboard
(...)

def __init__(self):

    pygame.init()
    self.settings = Settings()

    self.screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
    self.settings.screen_width = self.screen.get_rect().width
    self.settings.screen_height = self.screen.get_rect().height
    pygame.display.set_caption("Alien Invasion")

    # Create an instance to store game statistics.
    self.stats = GameStats(self)

    self.ship = Ship(self)
    self.bullets = pygame.sprite.Group()
    self.aliens = pygame.sprite.Group()

    self._create_fleet()

    # Make the Play button.
    self.play_button = Button(self, "Play")
    self.sb = Scoreboard(self)

(...)

def _update_screen(self):


    # Redraw the screen during each pass through the loop.
    self.screen.fill(self.settings.bg_color)
    self.ship.blitme()
    for bullet in self.bullets.sprites():
        bullet.draw_bullet()
    self.aliens.draw(self.screen)

    # Draw the score information.
    self.sb.show_score()    

    # Draw the play button if the game is inactive.
    if not self.stats.game_active:
        self.play_button.draw_button()

    # Make the most recently drawn screen visible
    pygame.display.flip()

两者之间有更多代码,但这是唯一实际涉及此的代码。

谢谢你,如果你可以请尝试给我一些关于他的提示,我真的很绝望(第一次在论坛上)。

标签: pythonpygame

解决方案


推荐阅读