1

私は単純な pygame プロジェクトに取り組んでおり、現在、画面の上部から画面の下部に向かう爆弾が落ちています。プレイヤーが爆弾に当たると、プレイヤーは死亡します。この時点まで、すべてがうまく機能します。問題は、爆弾がプレイヤーを通り過ぎても、まだ画面を離れていない場合でも、プレイヤーを殺してしまうことです。つまり、爆弾はプレイヤーの下部を通過しますが、交差すると、画面の下部を通過する前に死亡します。彼は私のコードです:

   if player.rect.y < thing_starty + thing_height:
        if player.rect.x > thing_startx and player.rect.x < thing_startx + thing_width or player.rect.x  + 28 > thing_startx and player.rect.x  + 28 < thing_startx + thing_width: 
            gameOver = True

値は次のとおりです。

thing_startx = random.randrange(0, S_WIDTH)
thing_starty = -300
thing_speed = 3
thing_width = 128
thing_height = 128

player.rect.x の値は、プレーヤーが画面上のどこにいるかによって 120 から 500 の範囲になります。(画面も左から右にスクロールします。) 28 は文字イメージの幅に由来します。

落下オブジェクトのコードは次のとおりです。

if thing_starty > S_HEIGHT:
        pygame.mixer.Sound.play(bomb_sound)
        thing_starty = 0 - thing_height
        thing_startx = random.randrange(0, S_WIDTH)
        dodged += 1
        thing_speed += .5

私はこれに約1週間取り組んでいますが、進歩はありません。助けてくれてありがとう。

4

2 に答える 2

1

Neal が既に言ったように、y 値がプレーヤーの y 値よりも大きいことを確認するだけです。

しかし、私のアドバイスは、次のようなコードの使用をやめることです。

 if player.rect.y < thing_starty + thing_height:
    if player.rect.x > thing_startx and player.rect.x < thing_startx + thing_width or player.rect.x  + 28 > thing_startx and player.rect.x  + 28 < thing_startx + thing_width: 
        gameOver = True

クラスのドキュメントをRect見て、 のような便利な関数をたくさん見つけてcolliderectください。

a を使用しRectて爆弾*の位置を表すこともできます ( for のようにplayer)。次のようなコードを使用できます。

if player.rect.colliderect(thing.rect):
    gameOVer = True

* から継承された独自のクラスを持っているはずSpriteですが、それは別のトピックです

于 2016-11-02T09:30:37.627 に答える