Help with bullets
in
Programming Questions
•
1 year ago
Hey guys, I'm doing a game where you shoot some bullets when you press space, that is already done. But instead of only firing one shot once, it shoots alot of bullets..how can i make it so that when you press space it only fire one bullet once?
Thanks!
Thanks!
- //variables
ball myBall;
ArrayList enemies;
ArrayList bullets;
void setup()
{
//size and shit
size(400, 400);
frameRate(60);
smooth();
//declare new instances of the classes
myBall = new ball(200, 300);
enemies = new ArrayList();
bullets = new ArrayList();
}
void draw()
{
background(0);
//run the classes
/////////////////////////////BALL///////////////////////////
myBall.run();
/////////////////////////BULLETS/////////////////////////////
//DISPLAY, FUNCTIONS for BULLETS
for (int i = bullets.size()-1; i>=0; i--)
{
Bullet bullet =(Bullet) bullets.get(i);
bullet.movement();
bullet.display();
}
////////////////////////////ENEMIES/////////////////////////////
enemies.add(new enemies1());
//add enemies
for (int i = enemies.size()-1; i>= 0; i--)
{
enemies1 e = (enemies1) enemies.get(i);
e.displayEnemies();
}
//remove enemies
if (enemies.size() > height)
{
enemies.remove(0);
}
////////////////////////// KEY ///////////////////////////////
//KEY RIGHT
if (keyPressed)
{
if (key == CODED)
{
if (keyCode == RIGHT)
{
myBall.rightArrow();
}
}
}
//KEY RIGHT
if (keyPressed)
{
if (key == CODED)
{
if (keyCode == LEFT)
{
myBall.leftArrow();
}
}
}
// SHOTBUTTON
if (keyPressed)
{
if (key == ' ')
{
bullets.add(new Bullet(myBall.xPos, myBall.yPos-25, 5, 5));
}
}
} - class Bullet {
//variables
float xPos;
float yPos;
float hPos;
float wPos;
float speed = 5;
boolean keys = false;
//constructor
Bullet(float _xPos, float _yPos, float _h, float _w)
{
xPos = _xPos;
yPos = _yPos;
hPos = _h;
wPos = _w;
}
void movement() {
// Add speed to y location
yPos = yPos - speed;
}
void display() {
fill(255);
strokeWeight(2);
rect(xPos,yPos,wPos,hPos);
}
}
1