1

Python Programming For The Absolute Beginnerの特別な「pygame」と「livewires」を使用してゲームを作成していますが、ほとんどの場合、かなりうまく機能しています。ゲームをプレイできるようになると、タイマーが作動し、スパイクが当たると「Lose!」というテキストが表示されます。Time:' すると、いつでも表示されます。ただし、メッセージの有効期間が 300 であるにもかかわらず、画面が閉じて次のエラーが表示されるため、メッセージの持続時間は 1 秒未満です。

Traceback (most recent call last):
    File "run.py", line 105, in main
        games.screen.mainloop()
    File "C:\Python31\lib\site-packages\livewires\games.py, line 308, in mainloop


       object._tick()
    File "C:\Python31\lib\site-packages\livewires\games.py", live 506, in _tick
        self.update()
    File "run.py", line 27, in update
        self.check_collide()
    File "run.py", line 34, in check_collide
        spike.handle_collide()
AttributeError: 'Message' object has no attribute 'handle_collide'

うわー。関連するコードは次のとおりです(私の判断による):

class Player(games.Sprite):
    """The player that must dodge the spikes."""

    def __init__(self, image, x, y):
        super().__init__(image=image, x=x, y=y)
        """
        Timer
        """
        self.timer = games.Text(value = 0,
                            size = 40,
                            color = color.black,
                            x = 600,
                            y = 440)
        games.screen.add(self.timer)

    def update(self):
        """Move to the mouse."""
        self.x = games.mouse.x
        self.y = games.mouse.y
        self.check_collide()
        self.timer.value = int(time.clock())


    def check_collide(self):
        """Check for a collision with the spikes."""
        for spike in self.overlapping_sprites:
            spike.handle_collide()



class Spike(games.Sprite):
    """A spike!"""

    def update(self):
        """Move to the left!"""
        self.x -= random.randrange(3,12)
        if self.x == 0 or self.x == 1 or self.x == 2 or self.x == 3 or self.x == 4 or self.x == 5 or self.x == 6 or self.x < 10:
            self.x = 640
            self.y = random.randrange(games.screen.height)

    def handle_collide(self):
        """Destroy!"""

        timebomb = int(time.clock())
        man = ("Lose! Time:",timebomb)
        lost_message = games.Message(value = man,
                            size = 100,
                            color = color.red,
                            x = games.screen.width/2,
                            y = games.screen.height/2,
                            lifetime = 300,
                            after_death = games.screen.quit)
        games.screen.add(lost_message)

よくわかりません。メソッドhandle_collideはゲームを終了できるように呼び出されるはずですが、handle_collide はオブジェクト メッセージの属性のようです。これがばかげていると思われる場合は、申し訳ありません。なぜなら、私はこれで正直に最善を尽くしたからです。本の名前が示すように、私は初心者です。助けてください、事前に感謝します。

4

1 に答える 1

1

修正が見つかりました。

def check_collide(self):
    """Check for a collision with the spikes."""
    for spike in self.overlapping_sprites:
        spike.handle_collide()

for ループの「スパイク」は、プレイヤーと衝突するものの名​​前にすぎません。実際に起こっていることは、プレイヤーが「メッセージ」オブジェクトと衝突し、スプライトの衝突を処理するメッセージ オブジェクトの方法を尋ねていることです。メッセージ オブジェクトは準備されておらず、これを処理する ' handle_collide ' メソッドがありません。そのため、エラーがスローされます。これを修正するには、テキストをプレイヤーから遠ざけるか、「メッセージ」オブジェクトをいじって、for ループが期待するメソッドを作成します。

于 2015-06-05T23:44:01.810 に答える