I'm trying to make the bullets in my game shoot toward the direction of my cursor until it hits the window borders. My bullets currently go toward my cursor but stops once it reaches where the cursor was.
I noticed that the bullet could reach the border if my cursor were offscreen and so I tried to modify the cursor position used to calculate the movement of my bullet to always be outside of the window via multiplying and adding but I couldn't quite get it working the way I want it to.
win = pygame.display.set_mode((1000, 800))
pos = pygame.mouse.get_pos()
keys = pygame.key.get_pressed()
def shoot(bullets):
for bullet in bullets:
if bullet.x > 0 and bullet.x <1000 and bullet.y > 0 and bullet.y < 800:
pygame.draw.circle(win, (255, 255, 255), (round(bullet.x) ,round(bullet.y)),5)
diffX = bullet.targetX - bullet.x
diffY = bullet.targetY - bullet.y
ang = math.atan2(diffY,diffX)
bullet.x += math.cos(ang)*bullet.vel
bullet.y += math.sin(ang)*bullet.vel
if keys[pygame.K_SPACE] and RegBullet.canShoot:
RegBullet.canShoot = False
regBullets.append(RegBullet(win,x=p1.getX(),y=p1.getY(),targetX=pos[0],targetY=pos[1]))
pause = threading.Thread(target=cool_down,args=(1,))
pause.start()