We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi there, I'm making a game where something comes across the screen, from one side to the other. What I'd like to do is that the invader can shoot the bullet itself at a random moment by using 1D array not the arraylist, and I'm really stuck with it... Here is what i got so far:
//Main class
int AlienRow = 4;
int AlienCol = 8;
int AfirstNullBullet = 0;
Alien[][] alien = new Alien[AlienCol][AlienRow];
BulletAlien[] bulletAlien = new BulletAlien[5];
void setup()
{
size(700, 600);
gameStart();
setAlien();
}
void draw()
{
background(255);
}
void setAlien()
{
/* Set up the position of the image and how many rows and cols */
for (int i = 0; i < AlienCol; i++)
{
for (int j = 0; j < AlienRow; j++)
{
alien[i][j] = new Alien(30 * (2*(i+1)), 30 * (1*(j+1)));
}
}//end for loop
}
void displayBullets()
{
for (int a = 0; a < bulletAlien.length; a++)
{
if (bulletAlien[a] != null)
{
bulletAlien[a].Adisplay();
bulletAlien[a].Amove();
if (bulletAlien[a].ay < height || bulletAlien[a].ay > 0)
{
bulletAlien[a] = null;
} else
{
AfirstNullBullet = a;
}
}
}
//Alien class
class Alien
{
float x, y;
float ximg = 0;
float speed = 2;
Boolean moveLeft = false;
Alien(float x, float y)
{
this.x = x;
this.y = y;
}
void display()
{
eclipse(x, y, 40, 40);
}
void move()
{
if (!moveLeft)
{
x += speed;
}
if (moveLeft)
{
x -= speed;
}
if (x <= 0)
{
moveLeft = false;
y += 90;
}
if (x >= width - 30)
{
moveLeft = true;
y += 90;
}
if (y >= 550)//When the alien go out of the button the game wiil end
{
gameEnd = true;
}
}
void update()
{
display();
move();
}
void Alienshoot()
{
if (int (random(100)) > 0)
{
bulletAlien[AfirstNullBullet] = new BulletAlien(x, y, 20);
}
}
}
//Bullet class
class BulletAlien
{
float ax, ay;
float aspeed;
BulletAlien(float ax, float ay, float aspeed)
{
this.ax = ax;
this.ay = ay;
this.aspeed = aspeed;
}
void Adisplay()
{
fill(255, 255, 255);
rect(ax, ay, 4, 15);
}
void Amove()
{
ay -= aspeed;
}
}
Answers
Copy and paste the code. Immediately see there are Too many opening curley braces - you're missing one on line 50, it looks like. Ctrl + t to automatically format the code. Add in missing bracket and Ctrl + t again.
Try to run it. The function gameStart() can not be found. Whoops! Did you try to run the code you posted? Remove the call to gameStart() by commenting it out.
Try to run the code again. The function eclipse() does not exist. Now I know you haven't tried running your own code. Change that to ellipse().
Try to run the code again. Now gameEnd is undefined. Remove reference to it.
Try to run the code again. Finally it starts. Nothing is drawn. Just a white screen. Oh, that's because all you're drawing in draw() is a white background.
Maybe you should get the aliens to be drawn before you try to get them to shoot?
As for your actual question, to have up to five bullets at once, I would do the following:
1) When a new bullet is to be created (and this happens at random for each Alien), look for a position in the bullets array that isn't already a bullet, and add a new bullet at that position. If there are no free positions, forget about adding the new bullet.
2) To update and display the bullets, look at every bullet in the array and move it down, and draw it. If it is below the bottom of the screen, set the bullet in that position back to null, freeing up that spot.
Tidied up code:
I'm sorry about the mess of the code, cause is only a part of my code. The background and the alien were imaged so that I change it for an easy post. Here is my full working code (image at the end):
You can provide a smaller version of your code centered around your current problem + any additional detail related to it. It is important to post a minimum runnable sketch demonstrating your current approach and provide details of what it does now and what it suppose to be doing instead, in case you are looking for feedback.Going smaller is important in your case. You don't want to try to debug all the process at once. Simple code provides clarity and it is always good to go back to revise concept or implement bigger changes.
This next tutorial provides an idea of how to generate a moving object: https://processing.org/examples/bounce.html
You should consider using an ArrayList as well so to manage multiple bullets: https://processing.org/examples/arrayobjects.html
This next post could also be helpful: https://forum.processing.org/two/discussion/comment/95303/#Comment_95303
*****EDIT: @LeegeeRicky Ok, after I follow your code, I figure that this is a working version of your program and that you solved your issue. Never mind my previous comment. Thanks for sharing your solution.
Kf
No is not working because the Alien still won't shoot itself. Thanks for the comment.
You mean, like when you press space?
Kf
No, when I press the space the shooter will shoot. Now what I want to do is make the alien to shoot automatically.
Ok, so what should it do? Aliens will fire all at once? Or randomly every so often?
I have an observation. Check this next code:
Both classes are almost almost identical. The only thing that is different is this:
y += speed;
vs.ay -= aspeed;
My proposal. Reduce your two classes two one. Remove BulletAlien class. When you create a bullet, you have two options:
Then your program can iterate over the array of bullets and based on the way you wrote your code, alien and shooter bullets are managed by the same array (Yes, you need to eliminate one of the arrays as well)
However, you should reconsider rewriting the section of the code where you remove bullets in your program that are out of the sketch. Your are setting the array entry to null. This might work but it is not a good idea. You are better off using an ArrayList and removing the actual item in the list when it leaves the sketch. Notice that to remove an item, you have to iterate the list backwards. Check the reference for ArrayList as they show an example of how to do that.
Kf