3

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()
X and Y
  • 91
  • 1
  • 7
  • Only the code related to the bullet is provided. If you need to see more code please let me know. – X and Y Jul 31 '19 at 05:20

1 Answers1

4

See Shooting a bullet in pygame in the direction of mouse and calculating direction of the player to shoot pygame. The issue is that you calculate the the direction of the bullet by vector from the current position of the bullet to the target point (bullet.targetX, bullet.targetY).
Once the bullet reaches the target, then this direction is (0, 0) and the bullet doesn't move any more.
Don't store the target position in bullet. Store the initial direction vector instead. e.g.:

bullet.diffX = targetX - bullet.x
bullet.diffY = targetY - bullet.y
ang = math.atan2(bullet.diffY, bullet.diffX)
bullet.x += math.cos(ang)*bullet.vel
bullet.y += math.sin(ang)*bullet.vel

Use pygame.Rect and .collidepoint() to verify if the bullet is inside the window:

for bullet in bullets:
   if pygame.Rect(0, 0, 1000, 800).collidepoint(bullet.x, bullet.y):
       # [...]

Or even .colliderect:

for bullet in bullets:
   radius = 5
   bullet_rect = pygame.Rect(-radius, -radius, radius, radius);
   bullet_rect.center = (bullet.x, bullet.y)
   if pygame.Rect(0, 0, 1000, 800).colliderect(bullet_rect):
       # [...]

I recommend to use pygame.math.Vector2 for calculation of the movement of the bullet e.g.:

bullet.pos = pg.math.Vector2(bullet.x, bullet.y)
bullet.dir = pg.math.Vector2(targetX, targetY) - bullet.pos
bullet.dir = bullet.dir.normalize()
for bullet in bullets:

    if #[...]

        bullet.pos += bullet.dir * bullet.vel
        bullet.x, bullet.y = (round(bullet.pos.x), round(bullet.pos.y))
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • Thank you for pointing that out. I moved the diffX and diffY to my bullet class and called it from there instead of constantly refreshing them. – X and Y Jul 31 '19 at 05:53