# Randomizing shot

**URL:** https://discourse.processing.org/t/randomizing-shot/13679
**Category:** Coding Questions
**Created:** [August 29, 2019, 3:49am UTC](https://discourse.processing.org/t/randomizing-shot/13679 "2019-08-29T03:49:19Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Not\_River](https://avatars.discourse-cdn.com/v4/letter/n/e79b87/32.png) [@Not\_River](https://discourse.processing.org/u/Not_River)
#### Post date: [August 29, 2019, 3:49am UTC](https://discourse.processing.org/t/randomizing-shot/13679/1 "2019-08-29T03:49:19Z")

</div>

I’m trying to design an enemy that shoots multiple shots in random directions, I have an Idea of what I want to do, but I don’t know where to put it. What I want to do is make the xSpeed of the shot randomize between 0 and 12, and then take that amount and subtract it from 12 to get the ySpeed. All of the code related to it is here:

```auto
abstract class Projectile extends GameObject
{
  Projectile(float x, float y, float w, float h, float xSpeed, float ySpeed)
  {
    super(x, y, w, h);
    this.xSpeed = xSpeed;
    this.ySpeed = ySpeed;
  }
}

abstract class EnemyProjectile extends Projectile
{
  EnemyProjectile(float x, float y, float w, float h, float xSpeed, float ySpeed)
  {
    super(x, y, w, h, xSpeed, ySpeed);
  }

  void reactions()
  {
    for (int x = 0; x < collisions.size (); x++)
    {
      GameObject o = collisions.get(x);
      if (o instanceof Player)
      {
        die();
      }
      if (x<0 || x > width || y < 0 || y > height)
      {
        die();
      }
    }
  }
  void act()
  {
    super.act();
    if (x<0 || x > width || y < 0 || y > height)
    {
      die();
    }
  }
} 

class RandomShot extends EnemyProjectile
{
  RandomShot(float x, float y)
  {
    super(x, y, redShot.width, redShot.height, 0, RED_SHOT_SPEED);
    image = redShot;
    damage = RED_SHOT_DAMAGE;
  }
}

if (timer > EVIL_SQUARE_COOLDOWN)
    {
      objects.add(new RandomShot(x + (image.width/2), y + image.height));  
      timer = 0;
    }

```

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [August 29, 2019, 8:49am UTC](https://discourse.processing.org/t/randomizing-shot/13679/2 "2019-08-29T08:49:25Z")

</div>

When the shooting happens by adding x2 and y2 to the bullet position :

```auto
x2=random(-6,6);
y2=random(-6,6);

```
