私は現在、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
自動的に7
とy_speed
に丸め8
られ、弾丸は最終的にポイントを通過します(98, 112)
。ただし、 bullet_speed が5
の場合、弾丸はポイントを通過します(99, 132)
。
pygameでこれを回避する方法はありますか?
助けてくれてありがとう!