Skip to main content
Improved answer to address the bullet direction issue as well.
Source Link
Romen
  • 406
  • 2
  • 7

You canwill need to fix your update method and add some variables to your Bullet class and set them at the time of creating a Bullet like this:

# Bullet Class

class Bullet(pygame.sprite.Sprite):
   def __init__(self, txvx, tyvy):
       super().__init__()

       self.image = pygame.Surface([4,10])
       self.image.fill(red)
       self.rect = self.image.get_rect()
       self.target_xvelocity_x = txvx
       self.target_yvelocity_y = tyvy
   
   def update(self):
       if self.rect.x >+= self.target_x:velocity_x
           self.rect.xy -=+= bullet_speedself.velocity_y

# Spawning the Bullet

# 1. You need elifto self.rect.xdetermine <the self.target_x:
direction from the player to the crosshair

# X distance from player self.rect.xto +=crosshair
dx bullet_speed= (chx - hgrect.x)
# Y distance from player to crosshair
dy if= self.rect.y(chy >- selfhgrect.target_y:y)
# Direction from player to crosshair in radians
dir = math.atan2(dy, dx)

# self.rect2.y -=Calculate bullet_speed
a velocity vector with a given speed elifand self.rect.ythe <direction self.target_y:above
speed = 20 # units: pixels/update
vx = speed * math.cos(dir)
vy = self.rect.yspeed +=* bullet_speedmath.sin(dir)

# Then3. Create athe bullet likewith this
the calculated X and Y velocity
bullet = new Bullet(chxvx, chyvy)
```

You can add variables to your Bullet class and set them at the time of creating a Bullet like this:

# Bullet Class

class Bullet(pygame.sprite.Sprite):
   def __init__(self, tx, ty):
       super().__init__()

       self.image = pygame.Surface([4,10])
       self.image.fill(red)
       self.rect = self.image.get_rect()
       self.target_x = tx
       self.target_y = ty
   
   def update(self):
       if self.rect.x > self.target_x:
           self.rect.x -= bullet_speed
       elif self.rect.x < self.target_x:
           self.rect.x += bullet_speed
       if self.rect.y > self.target_y:
           self.rect.y -= bullet_speed
       elif self.rect.y < self.target_y:
           self.rect.y += bullet_speed

# Then Create a bullet like this

bullet = new Bullet(chx, chy)

You will need to fix your update method and add some variables to your Bullet class:

# Bullet Class

class Bullet(pygame.sprite.Sprite):
   def __init__(self, vx, vy):
       super().__init__()

       self.image = pygame.Surface([4,10])
       self.image.fill(red)
       self.rect = self.image.get_rect()
       self.velocity_x = vx
       self.velocity_y = vy
   
   def update(self):
       self.rect.x += self.velocity_x
       self.rect.y += self.velocity_y

# Spawning the Bullet

# 1. You need to determine the direction from the player to the crosshair

# X distance from player to crosshair
dx = (chx - hgrect.x)
# Y distance from player to crosshair
dy = (chy - hgrect.y)
# Direction from player to crosshair in radians
dir = math.atan2(dy, dx)

# 2. Calculate a velocity vector with a given speed and the direction above
speed = 20 # units: pixels/update
vx = speed * math.cos(dir)
vy = speed * math.sin(dir)

# 3. Create the bullet with the calculated X and Y velocity
bullet = new Bullet(vx, vy)
```
Source Link
Romen
  • 406
  • 2
  • 7

You can add variables to your Bullet class and set them at the time of creating a Bullet like this:

# Bullet Class

class Bullet(pygame.sprite.Sprite):
   def __init__(self, tx, ty):
       super().__init__()

       self.image = pygame.Surface([4,10])
       self.image.fill(red)
       self.rect = self.image.get_rect()
       self.target_x = tx
       self.target_y = ty
   
   def update(self):
       if self.rect.x > self.target_x:
           self.rect.x -= bullet_speed
       elif self.rect.x < self.target_x:
           self.rect.x += bullet_speed
       if self.rect.y > self.target_y:
           self.rect.y -= bullet_speed
       elif self.rect.y < self.target_y:
           self.rect.y += bullet_speed

# Then Create a bullet like this

bullet = new Bullet(chx, chy)