Я новичок в pygame и дизайне игр в целом. Я делаю игру, в которой мяч следует за курсором. Я хочу, чтобы у мяча была какая-то физика, такая как инерция и гравитация, чтобы, когда я бросаю мяч вокруг, он не просто проходил по прямой линии к курсору и останавливался. Как мне это сделать ? (Как добавить инерцию и гравитацию) Мой спрайт:
class Ball(pg.sprite.Sprite):
def __init__(self, radius=30):
super(Ball, self).__init__()
self.height, self.width = 60, 60
self.surf = pg.Surface((self.width, self.height))
self.surf.fill(black)
self.surf.set_colorkey(black)
self.rect = self.surf.get_rect()
self.center = (self.width//2, self.height//2)
pg.draw.circle(self.surf, white, self.center, radius)
# Special varis :
self.velocity = 0
def update(self, mousepos):
if mousepos[0] > self.rect.x:
self.velocity += 1
if mousepos[0] < self.rect.x:
self.velocity -= 1
self.move(self.velocity)
def move(self, v):
self.rect.move_ip(v, 0)
if self.velocity > 0:
self.velocity -= 0.1
else:
self.velocity += 0.1
# collision detection for edge of screen
if self.rect.x + self.width > width:
self.rect.x = width - self.width
self.velocity = 0
elif self.rect.x < 0:
self.rect.x = 0
self.velocity = 0
if self.rect.y > height:
self.rect.y = height
elif self.rect.y - self.height < 0:
self.rect.y = self.height
Я не понимаю вопроса. Не могли бы вы объяснить немного больше о том, как мяч должен двигаться, пожалуйста.
fling the ball around it doesn't just make a straight line to the cursor and stop
--> codemyui.com/stuff-following-mouse-pointer |ball to have some physics like inertia and gravity
(ошибаетесь... сколько гравитации?) --> howtoconstructdemos.com/… | имхо, я не понялa ball follows the cursor
как цель. Если вы укажете что-то вроде:i want the ball to follow the cursor like the ice iced in water following the spoon, (but the view is upsidedown)
илиreact like billard ball
, это гораздо лучший игровой интерфейс/цель ux.. /(^_^)