import pygame
pygame.init()
screen = pygame.display.set_mode((1280,720))
clock = pygame.time.Clock()
running = True
class Ball:
def __init__(self,p,v,m) -> None:
self.p = p
self.m = m
self.v = v
self.a = [0,0]
def force(self, li):
self.a[0] = li[0] / self.m
self.a[1] = li[1] / self.m
self.v[0] += self.a[0]
self.v[1] += self.a[1]
def update(self):
self.p[0] = self.p[0] + self.v[0]
self.p[1] = self.p[1] + self.v[1]
def draw(self,surface:pygame.Surface):
pygame.draw.circle(surface,"red",(self.p[0],self.p[1]),8)
ball = Ball(
p = [640,360],
v = [0,0],
m = 100
)
while running:
clock.tick(120)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.MOUSEBUTTONDOWN:
j=pygame.mouse.get_pos()
xx = j[0] - ball.p[0]
yy = j[1] - ball.p[1]
li = [50*(xx/(xx**2+yy**2)**0.5),50*(yy/(xx**2+yy**2)**0.5)]
ball.force(li)
ball.update()
screen.fill('black')
ball.draw(screen)
pygame.display.update()
pygame.quit()
把速度分为x 轴和y 轴上的速度的矢量和,加速度也一样。