Вы должны найти касательную к окружности. Если у вас есть angle
, тогда вектор к точке на окружности будет:
vx, vy = cos(angle), sin(angle)
Касательной к окружности будет вектор, повернутый на 90 °:
tx, ty = -vy, vy
Добавить тангенс, умноженный на скорость до точки (x, y) в каждом кадре:
x -= math.sin(angle*math.pi/180)*vel
y += math.cos(angle*math.pi/180)*vel
angle += 3
См. пример:
import pygame
import sys
import math
pygame.init()
width = 640
height = 480
screen = pygame.display.set_mode((width,height))
clock = pygame.time.Clock()
img = pygame.Surface((50,50))
img.set_colorkey((255,0,0))
angle = 0
c_list = []
x, y = 300, 200
vel = 5
def draw_line(surface, color, pos1, pos2):
pygame.draw.line(surface, color, pos1, pos2)
start = False
while True:
screen.fill((122,122,122))
keys = pygame.key.get_pressed()
x -= math.sin(angle*math.pi/180)*vel
y += math.cos(angle*math.pi/180)*vel
angle += 3
if (x,y) not in c_list:
c_list.append((x,y))
for i in range(len(c_list)-1):
draw_line(screen,(0,0,0),c_list[i],c_list[i+1])
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit(-1)
img_copy = pygame.transform.rotate(img, -angle)
screen.blit(img_copy,(x-int(img_copy.get_width()/2),y-int(img_copy.get_width()/2)))
pygame.display.flip()