So I’m trying to recreate the game Ballz in python (using pygame), and in that game, you have to shoot a ball. You get a few ‘aiming balls’ in front to aim your shot. I’m trying to make the shooting ball go in the direction of the last aiming ball, but I’m not sure how to do it. I tried to use vectors, but I didn’t really understand them.
This is the class for the ball that you shoot:
class shooting_ball():
def __init__(self):
self.movement = pygame.math.Vector2(0, 0)
self.speed = 8
def movement_data(self):
start = pygame.math.Vector2(shooting_ball.sprites()[shooting_ball_pos])
end = pygame.math.Vector2(shooting_ball.sprites()[circle4.sprites()])
return (end - start).normalize()
def update(self):
shooting_ball_pos = self.move_direction * self.speed
These are some of the things you found in the above code that may have questions about:
shooting_ball_pos = pygame.math.Vector2(400, 600), and
circle4 = pygame.draw.circle(window, gray, [circle_positions_x[3], circle_positions_y[3]], 8, 0) (last aiming circle which I shoot the shooting ball
towards.
Also, I‘m making it so that you shoot when you click the space bar, and here is the code for that:
if keys[pygame.K_SPACE]:
shooting_ball.sprite() = self.movement_data()