你必须找到圆的切线。如果你有一个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()