# Shooting in a game

**URL:** <https://discourse.processing.org/t/shooting-in-a-game/5328>\
**Category:** Coding Questions\
**Created:** [November 7, 2018, 6:44pm UTC](https://discourse.processing.org/t/shooting-in-a-game/5328 "2018-11-07T18:44:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![kinglucas](https://avatars.discourse-cdn.com/v4/letter/k/53a042/32.png) [@kinglucas](https://discourse.processing.org/u/kinglucas)\
**Post date:** [November 7, 2018, 6:44pm UTC](https://discourse.processing.org/t/shooting-in-a-game/5328/1 "2018-11-07T18:44:30Z")

</div>

Hello, I am a beginner to programming and I really want this shooting code in my game but I don’t know how to put this code so that my character in my game shoots these bullets please help:

```auto
//Objects
Player player = new Player();
//Bullet arraylist
ArrayList<Bullet> bullets;
//Vars to regulate shooting speed
boolean canShoot = true;
float canShootCounter;

int i;

void setup() {
  size(800, 600);
  //standard array setup
  bullets = new ArrayList<Bullet>();
  player = new Player();
}

void draw() {
  player.update();
  background(100);
  //since we are adding and removing objects so often, we need to do the forloop backwards preventing any nullPointer errors
  for (i = bullets.size()-1; i >= 0; i--) {
    //you need a seperate var to get the object from the bullets arraylist then use that variable to call the functions
    Bullet bullet = bullets.get(i);
    bullet.update();
  }
}

class Player {
  PVector location;
  Player() {
    location = new PVector(width/2, height/2);
  } 
  void update() {
    if (mousePressed == true) {
      // this regulates the shooting speed
      if (canShoot == true) {
        bullets.add( new Bullet());
        canShoot = false;
        canShootCounter = 0;
      }
    }
    // this checks if the right amount of time has passed before canShoot can = true again
    if (canShoot == false) {
      canShootCounter ++;
      //if the right amount of time has passed. make canShoot true
      if (canShootCounter == 5)/*change this number to change the duration*/{
        canShoot = true;
      }
    }
  }
}

class Bullet {
  //standard PVector used for the location of the bullet
  PVector location;
  //vars used to check the angle between location and the mouse
  float oldPosX, oldPosY, rotation, speed;
  Bullet() {
    //places the bullet in the middle of the room
    location= new PVector(width/2, height/2);
    //this checks the angle
    oldPosX = mouseX;
    oldPosY = mouseY;
    rotation = atan2(oldPosY - location.y, oldPosX - location.x) / PI * 180;
    //bullet speed
    speed = 10;//change this number to change the speed
  } 
  void update() {
    //move the bullet
    location.x = location.x + cos(rotation/180*PI)*speed;
    location.y = location.y + sin(rotation/180*PI)*speed;
    ellipse(location.x, location.y, 10, 10);

    //removes the bullet from the arrayList if it is off the room
    if (location.x > 0 && location.x < width && location.y > 0 && location.x < height) {
    }
    else {
      bullets.remove(i);
    }
  }
}

```

This is my game:

```auto
ArrayList <Spider> spiders = new ArrayList<Spider>();
PImage bg;
int numSpiders = 10;
boolean [] keys;
Player player;

void setup() {
  size(600, 600);
  frameRate(60);  
  bg = loadImage("grass.jpg");
  player = new Player();
  for (int i = 0; i < numSpiders; i++) {
    spiders.add(new Spider());
    keys = new boolean[128];
  }
}

void draw() { 
  image(bg, 0, 0);  
  move();
  player.drawPlayer();
  for (int i = 0; i < numSpiders; i++) {
    spiders.get(i).drawSpider();
  }
}

  void move() {
    int xDelta = 0;
    int yDelta = 0;

    if (keys['a'])
      xDelta = xDelta - 2;

    if (keys['d'])
      xDelta = xDelta + 2;

    if (keys['s'])
      yDelta = yDelta + 2;

    if (keys['w'])
      yDelta = yDelta - 2;

    player.updatePlayer(xDelta, yDelta);
  }
  void keyPressed() {
  keys[key] = true;
}

void keyReleased() {
  keys[key] = false;
}

```

```auto

class Player {
  float x, y;
  PImage spriteSheet;
  PImage [][] movement;
  boolean inMotion;
  int currentDirection;
  float currentFrame;
  final int UP = 0, LEFT = 1, DOWN = 2, RIGHT = 3;

  Player() {
    inMotion = false;
    currentDirection = 1;
    currentFrame = 0;
    x = width/2;
    y = height/2;

    setupSprites();
  }

  void setupSprites() {
    movement = new PImage[4][9];
    spriteSheet = loadImage("Spritesheet.png");
    for (int i = 0; i < 9; i++) {
      movement[0][i] = spriteSheet.get(16 + 64 * i, 8, 32, 56);
      movement[1][i] = spriteSheet.get(16 + 64 * i, 72, 32, 56);
      movement[2][i] = spriteSheet.get(16 + 64 * i, 136, 32, 56);
      movement[3][i] = spriteSheet.get(16 + 64 * i, 200, 32, 56);
    }
  }

  void drawPlayer() {
    if (inMotion)
      image(movement[currentDirection][1 + int (currentFrame)], x, y);
    else
      image(movement[currentDirection][0], x, y);
  }

  void updatePlayer(int xDelta, int yDelta) {
    currentFrame = (currentFrame + 0.5) % 8;
    inMotion = true;

    if (xDelta == 0 && yDelta == 0)
      inMotion = false;
    else if (xDelta == -2)
      currentDirection = LEFT;
    else if (xDelta == 2)
      currentDirection = RIGHT;
    else if (yDelta == -2)
      currentDirection = UP;
    else if (yDelta == 2)
      currentDirection = DOWN;

    x = x + xDelta;
    y = y + yDelta;

    if(isPlayerOffScreen(x, y)) {
      x = x - xDelta;
      y = y - yDelta;
    }
  }

  boolean isPlayerOffScreen(float x, float y) {
    if (x < 0 || x > width - 30 ||
      y < 0 || y > height - 56)
      return true;
    return false;
  }
}

```

````auto

class Spider {
  float x, y;
  PVector speed;
  PImage img;
  

  Spider() {
    x = random(50, 450);
    y = random(50, 450);
    speed = new PVector();
    speed.x = random(-1, 1);
    speed.y = random(-1, 1);
    img = loadImage("spider.png");
    
  }

  void drawSpider() {
    moveSpider();
    pushMatrix();
    translate(x + img.width/2, y + img.height/2);
    img.resize(0, 40);
    rotate(speed.heading() + PI/2);
    translate(-img.width/2, -img.height/2); 
    image(img, 0, 0);
    popMatrix();
  }

  void moveSpider() {
    float testMove = random(0, 1);
    if (testMove < 0.10) {
      speed.rotate(random(-0.4, 0.4));
    }

    if (testMove > 0.97) {
      speed.rotate(random(-1, 1));
    }
    x = x + speed.x;
    y = y + speed.y;

    if (isSpiderOffScreen() == true) {
      x = x - speed.x;
      y = y - speed.y;
      speed.rotate(random(-2, 2));
    }
  }

  boolean isSpiderOffScreen() {
    if (x < 30 || x > width - 30 || 
      y < 30 || y > height - 30)
      return true;
    return false;
  }
}```
````

---

<div class="post-metadata">

**Author:** ![kinglucas](https://avatars.discourse-cdn.com/v4/letter/k/53a042/32.png) [@kinglucas](https://discourse.processing.org/u/kinglucas)\
**Post date:** [November 8, 2018, 1:47pm UTC](https://discourse.processing.org/t/shooting-in-a-game/5328/2 "2018-11-08T13:47:11Z")

</div>

can someone please help me with this issue?

---

<div class="post-metadata">

**Author:** ![Kevin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kevin/32/2297_2.png) [@Kevin](https://discourse.processing.org/u/Kevin)\
**Post date:** [November 8, 2018, 8:22pm UTC](https://discourse.processing.org/t/shooting-in-a-game/5328/3 "2018-11-08T20:22:49Z")

</div>

It’s really hard to answer general “how do I do this” type questions. You’ll have much better luck if you [break your problem down into smaller steps](https://happycoding.io/tutorials/how-to/program) and take those steps on one at a time. For example, can you create a sketch that shows a single hard-coded rectangle that “shoots” circles when you click?

If you get stuck on that, then you can post a [MCVE](https://stackoverflow.com/help/mcve) along with a more specific question. Good luck!

You might also read this for some tips on asking questions:

> [@Guidelines—Asking Questions](https://discourse.processing.org/t/guidelines-tips-on-asking-questions/2147):
>
> Summary (TL;DR) Ask complete questions to get better answers! Be specific. For example: I want to load an image. I tried using createImg() , and I expected the image to be on canvas, but what happened instead was the image showing up below the canvas. Isolate your problem and work in small steps. Share only the code directly related to your problem if it is part of a bigger project, and if something isn’t working, try the smallest code that could do the thing you want. Can we run your code to …

---

<div class="post-metadata">

**Author:** ![codingking03](https://avatars.discourse-cdn.com/v4/letter/c/3ec8ea/32.png) [@codingking03](https://discourse.processing.org/u/codingking03)\
**Post date:** [November 8, 2018, 9:35pm UTC](https://discourse.processing.org/t/shooting-in-a-game/5328/5 "2018-11-08T21:35:33Z")

</div>

hmm maybe maybe not…😔

---

<div class="post-metadata">

**Author:** ![Sarah](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/sarah/32/1675_2.png) [@Sarah](https://discourse.processing.org/u/Sarah)\
**Post date:** [November 8, 2018, 10:27pm UTC](https://discourse.processing.org/t/shooting-in-a-game/5328/6 "2018-11-08T22:27:05Z")

</div>

Give your `Player` an `ArrayList<Bullet> bullets;`. Then you can make a function `void shoot() {}`, and `void update() {}`. Eg.

```auto
void shoot() {
  bullets.add(new Bullet(position,atan2(mouseY-y,mouseX-x))); // Add a position and rotation parameter to your Bullet class
}
void update() {
  for(Bullet b : bullets) {
    b.update();
  }
}

```
