0

私は現在、Pygame、Python 3 を使用してゲームを作成しています。バグの 1 つは、ショットの移動方法です。このゲームは 2D のトップダウン シューティング ゲームであり、プレイヤーのシューティング メカニックのコードは次のとおりです。

(player_rectはプレーヤーの Rect で、 bullet_speed は事前定義されたint)

if pygame.mouse.get_pressed()[0]:
    dx = mouse_pos[0]-player_rect.centerx
    dy = mouse_pos[1]-player_rect.centery
    x_speed = bullet_speed/(math.sqrt(1+((dy**2)/(dx**2))))
    y_speed = bullet_speed/(math.sqrt(1+((dx**2)/(dy**2))))
    if dx < 0:
        x_speed *= -1
    if dy < 0:
        y_speed *= -1
    #surface, rect, x-speed, y-speed
    player_shots.append([player_shot_image, player_shot_image.get_rect(centerx=player_rect.centerx, centery=player_rect.centery), x_speed, y_speed])

ループの後半には、コードの次の部分があります。

for player_shot_counter in range(len(player_shots)):
    player_shots[player_shot_counter][1][0] += player_shots[player_shot_counter][2]
    player_shots[player_shot_counter][1][1] += player_shots[player_shot_counter][3]

このメカニズムは、1 つの大きなバグを除いて、ほとんど問題なく動作します。ショットが遅くなるほど、精度が低下し、pygame.Rect[0]整数pygame.Rect[1]値しか使用できなくなります。たとえば、player_rect.center(0, 0)、マウスの位置が(100, 115)、 bullet_speed が10の場合、x_speed自動的に7y_speedに丸め8られ、弾丸は最終的にポイントを通過します(98, 112)。ただし、 bullet_speed が5の場合、弾丸はポイントを通過します(99, 132)

pygameでこれを回避する方法はありますか?

助けてくれてありがとう!

4

1 に答える 1

1

Pygame についてはよくわかりませんが、位置を非整数値として保存し、表示される整数にのみ変換することを考えたことはありますか? 内部表現は、ユーザーに表示されるものよりも正確な場合があります。

于 2016-07-22T20:51:16.340 に答える