# Help me make bullets for a game I'm trying to make

**URL:** <https://discourse.processing.org/t/help-me-make-bullets-for-a-game-im-trying-to-make/24555>\
**Category:** Coding Questions\
**Created:** [October 12, 2020, 4:51pm UTC](https://discourse.processing.org/t/help-me-make-bullets-for-a-game-im-trying-to-make/24555 "2020-10-12T16:51:58Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cal\_codes](https://avatars.discourse-cdn.com/v4/letter/c/ecd19e/32.png) [@Cal\_codes](https://discourse.processing.org/u/Cal_codes)\
**Post date:** [October 12, 2020, 4:51pm UTC](https://discourse.processing.org/t/help-me-make-bullets-for-a-game-im-trying-to-make/24555/1 "2020-10-12T16:51:58Z")

</div>

> Hi! I’m trying to build a retro top down arena shooter game and I’m struggling to get shooting to work. What I want is to have an ArrayList of bullets that can be shot using a keypressed fucntion and that are shot in the direction of the player is moving or the direction that the player last moved. I sort of have it working now, but I can’t shoot the bullets diagonally and I would really like to be able to do that. If anyone can maybe help me change my code up a bit to solve this problem or if anyone has ant tips to make my code better in general please help 🙂

+//classes  
player myPlayer;  
Bullet myBullets;

void setup() {  
size(1600, 1000);  
myPlayer = new player(width/2, height/2, 4, 40);  
myBullets = new Bullet(0, 0, 0, 0, 10);  
}  
void draw() {  
background(0);  
myPlayer.update();  
myBullets.update(myBullets.x, myBullets.y, myBullets.speedX,  
myBullets.speedY, myBullets.diameter);

myPlayer.draw();  
myBullets.draw();  
}

void keyPressed() {  
myPlayer.keyp();  
myBullets.keyp();  
}

void keyReleased() {  
myPlayer.keyr();  
myBullets.keyr();  
}

+class player {

float x, y, speed, size;  
float xVelocity, yVelocity;  
boolean keys = new boolean[4];

//constructor  
player(int x1, int y1, int speed1, int size1) {  
x = x1;  
y = y1;  
speed = speed1;  
size = size1;  
}

void update() {  
xVelocity = 0;

// gives the bullet a speed and direction when keys are pressed  
if (keys[0] == true) {  
yVelocity = -speed;

```
}
if (keys[1] == true) {
  xVelocity = -speed;

}
if (keys[2] == true) {
  yVelocity = speed;
  
}
if (keys[3] == true) {
  xVelocity = speed;
  
}

x = x + xVelocity;
y = y + yVelocity;

collisionDetec();

```

}

void draw() {  
circle(x, y, size);  
}

void keyp() {  
//allows for character movement when keys are pressed  
if (key == ‘w’ || key == ‘W’) {  
keys[0] = true;  
}  
if (key == ‘a’ || key == ‘A’) {  
keys[1] = true;  
}  
if (key == ‘s’ || key == ‘S’) {  
keys[2] = true;  
}  
if (key == ‘d’ || key == ‘D’) {  
keys[3] = true;  
}  
}

void keyr() {  
//ensures character doesn’t move when keys are released  
if (key == ‘w’ || key == ‘W’) {  
keys[0] = false;  
}  
if (key == ‘a’ || key == ‘A’) {  
keys[1] = false;  
}  
if (key == ‘s’ || key == ‘S’) {  
keys[2] = false;  
}  
if (key == ‘d’ || key == ‘D’) {  
keys[3] = false;  
}  
}

void collisionDetec() {  
//speed and direction in which x moves  
//collision detection x axis  
if (x \> width - size/2) {  
x = width - size/2;  
}  
if ( x \< size/2 ) {  
x = size/2;  
}  
//speed and direction in which y moves  
//collision detection y axis  
if ( y \>= height - size/2) {  
y = height - size/2;  
}  
if ( y \<= size/2) {  
y = size/2;  
}  
}  
}

ArrayList bullets = new ArrayList();

class Bullet {  
float x, y, speedX, speedY, diameter, startX, startY;  
boolean keys = new boolean[4];

//constructor  
Bullet(float x1, float y1, float speedX1, float speedY1, float  
diameter1) {  
x = x1;  
y = y1;  
speedX = speedX1;  
speedY = speedY1;  
diameter = diameter1;  
}

void draw() {  
// – because bullets are removed when it leaves screen  
for (int i = bullets.size() - 1; i \>= 0; i–) {  
Bullet b = bullets.get(i);  
b.update(b.x, b.y, b.speedX, b.speedY, diameter);  
noStroke();  
circle(b.x, b.y, diameter);  
//when bullets are beyond screen they are removed  
if ( ( b.x \>= width || b.x \< 0 ) || ( b.y \>= height || b.y \< 0 ) ) {  
bullets.remove(i);  
}  
}  
}

void update( float x2, float y2, float speedX2, float speedY2, float  
diameter2 ) {  
x = x2 + speedX2;  
y = y2 + speedY2;  
diameter = diameter2;  
}

void keyp() {  
if (key == ’ ') {  
x = myPlayer.x;  
y = myPlayer.y;  
if ( startY == 0 ) {  
speedX = startX;  
speedY = 0;  
} else {  
speedY = startY;  
speedX = 0;  
}  
//adds new bullet to ArrayList when spacebar is pressed  
bullets.add(new Bullet(x, y, speedX, speedY, diameter));  
}

```
if (key == 'w' || key == 'W') {
  keys[0] = true;
  startY = myPlayer.speed * - 1.5;
  startX = 0;
}
//gives bullets direction based on last direction of player
if (key == 'a' || key == 'A') {
  keys[1] = true;
  startX = myPlayer.speed * - 1.5;
  startY = 0;
}
if (key == 's' || key == 'S') {
  keys[2] = true;
  startY = myPlayer.speed * 1.5;
  startX = 0;
}
if (key == 'd' || key == 'D') {
  keys[3] = true;
  startX = myPlayer.speed * 1.5;
  startY = 0;
}

```

}

void keyr() {  
//gives bullet its own speed and direction after it is fired  
if (key == ‘w’ || key == ‘W’) {  
keys[0] = false;  
startY = myPlayer.speed \* - 1.5;  
}  
if (key == ‘a’ || key == ‘A’) {  
keys[1] = false;  
startX = myPlayer.speed \* - 1.5;  
}  
if (key == ‘s’ || key == ‘S’) {  
keys[2] = false;  
startY = myPlayer.speed \* 1.5;  
}  
if (key == ‘d’ || key == ‘D’) {  
keys[3] = false;  
startX = myPlayer.speed \* 1.5;  
}  
}  
}

---

<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:** [October 12, 2020, 6:35pm UTC](https://discourse.processing.org/t/help-me-make-bullets-for-a-game-im-trying-to-make/24555/2 "2020-10-12T18:35:12Z")

</div>

from a technical perspective, your code is not good.

Especially, the class Bullet should know only ONE bullet - hence the name. Everything like adding to the bullets ArrayList or for-loop over the ArrayList should take outside the class.

For example in a class BulletsManagement.

for diagonal shooting make sure speedX and speedY have a value that is for example both -1 OR 1,1 OR -1,1 and so on

---

<div class="post-metadata">

**Author:** ![Cal\_codes](https://avatars.discourse-cdn.com/v4/letter/c/ecd19e/32.png) [@Cal\_codes](https://discourse.processing.org/u/Cal_codes)\
**Post date:** [October 12, 2020, 6:53pm UTC](https://discourse.processing.org/t/help-me-make-bullets-for-a-game-im-trying-to-make/24555/3 "2020-10-12T18:53:13Z")

</div>

do you think you could show me how to do that? I’m really new to using processing and programming in general so I wouldn’t even know where to begin

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [October 12, 2020, 7:12pm UTC](https://discourse.processing.org/t/help-me-make-bullets-for-a-game-im-trying-to-make/24555/4 "2020-10-12T19:12:46Z")

</div>

> [@Cal\_codes](#):
>
> if anyone has ant tips to make my code better in general please help

Hello,

Please format your code as a courtesy to this community:  
[https://discourse.processing.org/faq#format-your-code](https://discourse.processing.org/faq#format-your-code)

`:)`  
This is what happens if you do not format properly:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/2/2475e07c40403e6d9fe0ca3933651d595426be46.png)

---

<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:** [October 13, 2020, 6:46am UTC](https://discourse.processing.org/t/help-me-make-bullets-for-a-game-im-trying-to-make/24555/5 "2020-10-13T06:46:28Z")

</div>

> [@Cal\_codes](#):
>
> you could show me how to do that?

Not really, maybe i can make a mock-up

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [October 13, 2020, 8:23am UTC](https://discourse.processing.org/t/help-me-make-bullets-for-a-game-im-trying-to-make/24555/6 "2020-10-13T08:23:44Z")

</div>

- [Studio.ProcessingTogether.com/sp/pad/export/ro.91kLmk61vAOZp](http://Studio.ProcessingTogether.com/sp/pad/export/ro.91kLmk61vAOZp)

---

<div class="post-metadata">

**Author:** ![Cal\_codes](https://avatars.discourse-cdn.com/v4/letter/c/ecd19e/32.png) [@Cal\_codes](https://discourse.processing.org/u/Cal_codes)\
**Post date:** [October 13, 2020, 8:38am UTC](https://discourse.processing.org/t/help-me-make-bullets-for-a-game-im-trying-to-make/24555/7 "2020-10-13T08:38:35Z")

</div>

if you don’t mind doing that

---

<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:** [October 13, 2020, 11:46am UTC](https://discourse.processing.org/t/help-me-make-bullets-for-a-game-im-trying-to-make/24555/8 "2020-10-13T11:46:19Z")

</div>

I advice you to read the objects tutorial : [https://www.processing.org/tutorials/objects/](https://www.processing.org/tutorials/objects/)

here you can see a clean Bullet class - everything done to the ArrayList happens OUTSIDE of the class (mostly in the player class)

```auto

// ********************************************************************************
// joined pde-file of folder ShootTest3
// ********************************************************************************

/* OpenProcessing Tweak of *@*http://www.openprocessing.org/sketch/77863*@* */
/* !do not delete the line above, required for linking your tweak if you upload again */

// overall structure: The tab ShootTest1 is the main program.
// A player (only a white rectangle) 
// shoots bullets (in class Bullet).
// The player can only shoot upwards, follows mouse, click mouse. 
//
// idea from 
// http://forum.processing.org/topic/need-help-adding-shooting
// with help from 
// http://forum.processing.org/topic/have-eyes-follow-mouse
//
// Player
Player thePlayer = new Player();

// ------------------------------------------------------------------

void setup() {
  size(600, 480);
  cursor(CROSS);
}

void draw() {
  background(111);

  thePlayer.draw();
}

void mousePressed() {
  thePlayer.fire();
}

void keyPressed() {
  thePlayer.fire();
}

// ================================================================
// Bullet - Simple class for bullets
// http://www.processing.org/learning/topics/arraylistclass.html

class Bullet {

  float x;
  float y;
  float speedX;  
  float speedY;
  float widthBullet;
  float life = 255;

  Bullet(float tempX, float tempY, 
    float tempSpeedX, float tempSpeedY, 
    float tempWidthBullet) {
    x = tempX;
    y = tempY;
    widthBullet = tempWidthBullet;
    speedX = tempSpeedX; 
    speedY = tempSpeedY;
  }

  void move() {
    // Add speed to location
    x = x + speedX; 
    y = y + speedY;
    //
    // kill bullet when outside screen
    if (x<4) 
      life=-1;
    if (y<4) 
      life=-1;
    if (x>width-4) 
      life=-1;
    if (y>width-4) 
      life=-1;
    //
  } // method 

  boolean finished() {
    // bullet dead?
    if (life < 0) {
      return true;
    } else {
      return false;
    }
  }

  void display() {
    // Display the circle
    fill(244, 2, 2);
    noStroke();
    ellipse(x, y, 
      widthBullet, widthBullet);
  }
} // class 

// =====================================================================
// the Player class

class Player {

  ArrayList<Bullet> bullets;   
  PVector position, velocity; // contains x and y...

  // bullets
  int lastFired=millis(); // timer to determine when next bullet starts 
  final float bulletFlySpeed = 4.2; // how fast bullet flies
  final int fireSpeed=200; // how often you fire / distance between bullets

  Player() {
    bullets = new ArrayList();   
    position = new PVector(333, 333);
    velocity = new PVector();
    velocity.x = 0;
    velocity.y = 0;
  }

  void fire() { // new2
    if (fireSpeed<=millis()-lastFired) {

      // ammoCount--;
      lastFired=millis();

      float xSpeed ;
      float ySpeed ; 
      float angle = update(mouseX, mouseY);
      xSpeed = cos(angle);
      ySpeed = sin(angle);
      xSpeed*= bulletFlySpeed;
      ySpeed*= bulletFlySpeed;
      if (ySpeed>0)
        ySpeed=0;
      bullets.add ( new Bullet( 
        position.x+12, position.y-14, 
        xSpeed, ySpeed, 
        5 ));
    } // if
  } // method 

  float update(int mx, int my) {
    //determines angle to mouse
    float angle = atan2(float(my)-(position.y-14), float(mx)-(position.x+12));
    return angle;
  }

  void draw() {
    // draw player & bullets
    //
    // draw player 
    pushMatrix();
    translate(position.x, position.y);
    noStroke();
    fill(255);
    rect (0, 0, 20, 40);
    popMatrix(); // undoes all

    // draw bullets
    for (Bullet currentBullet : bullets ) {    
      currentBullet.move();
      currentBullet.display();
    }

    // With an array, we say length, with an ArrayList, we say size()
    // The length of an ArrayList is dynamic
    // Notice how we are looping through the ArrayList backwards
    // This is because we are deleting elements from the list  
    for (int i = bullets.size()-1; i >= 0; i--) { 
      // An ArrayList 
      Bullet currentBullet = bullets.get(i);
      if (currentBullet.finished()) {
        // Items can be deleted with remove()
        bullets.remove(i);
      }//if
    }//for
  }//method
}//class
// End of joined file. ****************************************************************************

```
