title: Creating a Auto targeting bullet
body: <p>I have been creating a typical type of space invader game using Python and pygame sprites. I have done most of the things and it's working fine. But actually in my game, the bullet always shoots straight, but I want it to target the enemy and always shoot where the enemy is.</p>
  
  <p>In my player update I am just doing that whenever space is hit, it fires the bullet.</p>
  
  <pre><code>if (keys[pygame.K_SPACE]):
      self.fire()
  </code></pre>
  
  <p>This below is my <code>fire</code> method which is just calling the Handgun (which is my bullet class):</p>
  
  <pre><code>def fire(self):
      now = pygame.time.get_ticks()
      self.shoot_delay = 600
      self.shot_position = handguns.rect.x - enemy.rect.x
      print (self.shot_position)
      if (now - self.last_shot &gt; self.shoot_delay):
          self.last_shot = now
          shot = HandGun(self.rect.centerx, self.rect.top)
          Every_Sprite.add(shot)
          handgun.add(shot)
  </code></pre>
  
  <p>This below is my enemy class where the position is just randomised:</p>
  
  <pre><code>class Enemy_Agent(pygame.sprite.Sprite):
      def __init__(self):
          pygame.sprite.Sprite.__init__(self)
          self.image = pygame.image.load(os.path.join(img_folder, "ship2.png")).convert()
          self.image.set_colorkey(WHITE)
          self.rect =self.image.get_rect()
          self.rect.x = random.randrange(width - self.rect.width)
          self.rect.y = random.randrange(-100, -40)
  
          self.speed = random.randrange(1,8)
          self.speedx = random.randrange(-3,3)
      def update(self):
          if (self.rect.right &gt; width):
              self.rect.right = width
          if (self.rect.left &lt; 0):
              self.rect.left = 0
  
          self.rect.y += self.speed
          #print("pos: ", self.rect.y)
          self.rect.x += self.speedx
          if(self.rect.top &gt; Height - 10 or self.rect.left &lt; -30 or     self.rect.right &gt; width + 20):
              self.rect.x = random.randrange(width - self.rect.width)
              self.rect.y = random.randrange(-100, -40)
              self.speed = random.randrange(1, 8)
  </code></pre>
  
  <p>And, last this my <code>HandGun</code> class. If anyone can help me and advise me to make the bullet target the enemy it would be a great help.</p>
  
  <pre><code>class HandGun(pygame.sprite.Sprite):
      def __init__(self, cx, cy):
          pygame.sprite.Sprite.__init__(self)
          self.image = pygame.image.load(os.path.join(img_folder, "bullet.png")).convert()
          self.image.set_colorkey(WHITE)
          self.rect = self.image.get_rect()
          self.rect.bottom = cy
          self.rect.centerx =cx
          self.speedy = -1
          self.speedx = None
      def update(self):
          self.rect.y += self.speedy + enemy.rect.centerx
          if(self.rect.bottom &lt; 0):
              self.kill()
          pass
  </code></pre>