this might be a duplicate of a question but i don't know what to search to find the solution of this problem i want the bullet fired to perfectly coincide with mouse cursor at the time of firing here is my code
import pygame
import math
pygame.init()
window=pygame.display.set_mode((500,500))
clock=pygame.time.Clock()
class Bullet(pygame.sprite.Sprite):
def __init__(self,pos):
super().__init__()
self.pos=pos
self.image=pygame.surface.Surface((5,5))
self.image.fill("white")
self.rect=self.image.get_rect(center=pos)
self.mx,self.my=pygame.mouse.get_pos()
self.move()
def move(self):
self.dir=(self.mx-self.pos[0],self.my-self.pos[1])
length=math.hypot(*(self.dir))
self.x_dir=self.dir[0]/length
self.y_dir=self.dir[1]/length
def update(self):
self.rect.x+=self.x_dir*2
self.rect.y+=self.y_dir*2
bullets=pygame.sprite.Group()
while True:
for event in pygame.event.get():
if event.type==pygame.QUIT:
pygame.quit()
if event.type==pygame.MOUSEBUTTONDOWN:
bullets.add(Bullet((250,250)))
bullets.draw(window)
bullets.update()
pygame.display.flip()
clock.tick(60)
i followed what is said here
https://stackoverflow.com/a/59980344
am i missing something because whenever i click on the screen the bullets move in general direction of mouse but doesn't coincide with the mouse pointer itself or can i not use the same concept for sprites?
[![see the bullet is not precise most of the time it just goes from the side of the cursor][1]][1]
and I followed the solution from the question that is said to be the duplicate https://i.stack.imgur.com/AOiIt.gif