Original Post
Hello everyone !
So far I managed to make a simple vertical scrolling game but it is not working very good. I managed to make my space ship move and shoot , made the background move, and managed to handle collision detection but for ONE enemy only. If I add multiple enemies the collision doesn't work. I am not sure why, and I am not sure if I am doing it right.
(all the graphics I use are images)
I added all the enemies in ArrayList, and since then the collision stopped working. This is part of my code:
[source lang="java"]public class Game extends JPanel implements ActionListener {
private Player player;
private SpaceCraft enemy;
...........................
public ArrayList enemies = new ArrayList();
Random randGen = new Random();
public Game(){
player = new Player();
enemy = new SpaceCraft(200, -600);
// add enemies to the array
for (int i = 0; i < 1000 ; ++i){
int x_positon = 50 + randGen.nextInt(651);
int y_position = -randGen.nextInt(100000);
enemies.add(new SpaceCraft(x_positon, y_position));
}
...........................
}
...................................................................
public void detectCollisions(){
// FOR ONE ENEMY ONLY
Rectangle enemyBounds = enemy.getBounds();
ArrayList blasterShots = player.getBlasterShots();
for (int i = 0; i < blasterShots.size(); ++i){
Blaster temp = (Blaster)blasterShots.get(i);
Rectangle blasterBounds = temp.getBounds();
/* check if blaster collides with enemy */
if (enemyBounds.intersects(blasterBounds) && enemy.isAlive()){
enemy.setAlive(false);
temp.setVisible(false);
}
}
Rectangle playerBounds = player.getBounds();
if (playerBounds.intersects(enemyBounds)){
lostGame = true;
}
// HANDLE MANY ENEMIES - FAIL
ArrayList enemiesBounds = new ArrayList();
for (int i = 0; i < enemies.size(); ++i){
enemiesBounds.add(enemies.get(i).getBounds());
}
for (int i = 0; i > blasterShots.size(); ++i){
Blaster temp = (Blaster)blasterShots.get(i);
Rectangle blasterBounds = temp.getBounds();
for (int j = 0; j < enemies.size(); ++j){
if (enemiesBounds.get(j).intersects(blasterBounds) && enemies.get(j).isAlive()){
enemies.get(j).setAlive(false);
temp.setVisible(false);
}
}
}
}
// THIS IS THE CLASS FOR ENEMY BATTLE SHIPS
public class SpaceCraft implements Enemy {
private Image enemyImg;
private int x_pos;
private int y_pos;
private boolean alive;
public SpaceCraft(int x, int y){
this.x_pos = x;
this.y_pos = y;
this.alive = true;
ImageIcon img = new ImageIcon("images/enemy.png");
enemyImg = img.getImage();
}
public void moveRight(){
}
public void moveLeft(){
}
public void moveForward(int moveSpeed){
y_pos += moveSpeed;
}
public void setAlive(boolean is){
this.alive = is;
}
public boolean isAlive() {
return alive;
}
public Image getImage(){
return enemyImg;
}
public int getX(){
return this.x_pos;
}
public int getY(){
return this.y_pos;
}
// create rectangle around the enemy so we can check for collisions
public Rectangle getBounds(){
return new Rectangle(x_pos, y_pos, enemyImg.getWidth(null), enemyImg.getHeight(null));
}
}[/source]
If you need more code or more explanation tell me.
So far I managed to make a simple vertical scrolling game but it is not working very good. I managed to make my space ship move and shoot , made the background move, and managed to handle collision detection but for ONE enemy only. If I add multiple enemies the collision doesn't work. I am not sure why, and I am not sure if I am doing it right.
(all the graphics I use are images)
I added all the enemies in ArrayList, and since then the collision stopped working. This is part of my code:
[source lang="java"]public class Game extends JPanel implements ActionListener {
private Player player;
private SpaceCraft enemy;
...........................
public ArrayList
Random randGen = new Random();
public Game(){
player = new Player();
enemy = new SpaceCraft(200, -600);
// add enemies to the array
for (int i = 0; i < 1000 ; ++i){
int x_positon = 50 + randGen.nextInt(651);
int y_position = -randGen.nextInt(100000);
enemies.add(new SpaceCraft(x_positon, y_position));
}
...........................
}
...................................................................
public void detectCollisions(){
// FOR ONE ENEMY ONLY
Rectangle enemyBounds = enemy.getBounds();
ArrayList blasterShots = player.getBlasterShots();
for (int i = 0; i < blasterShots.size(); ++i){
Blaster temp = (Blaster)blasterShots.get(i);
Rectangle blasterBounds = temp.getBounds();
/* check if blaster collides with enemy */
if (enemyBounds.intersects(blasterBounds) && enemy.isAlive()){
enemy.setAlive(false);
temp.setVisible(false);
}
}
Rectangle playerBounds = player.getBounds();
if (playerBounds.intersects(enemyBounds)){
lostGame = true;
}
// HANDLE MANY ENEMIES - FAIL
ArrayList
for (int i = 0; i < enemies.size(); ++i){
enemiesBounds.add(enemies.get(i).getBounds());
}
for (int i = 0; i > blasterShots.size(); ++i){
Blaster temp = (Blaster)blasterShots.get(i);
Rectangle blasterBounds = temp.getBounds();
for (int j = 0; j < enemies.size(); ++j){
if (enemiesBounds.get(j).intersects(blasterBounds) && enemies.get(j).isAlive()){
enemies.get(j).setAlive(false);
temp.setVisible(false);
}
}
}
}
// THIS IS THE CLASS FOR ENEMY BATTLE SHIPS
public class SpaceCraft implements Enemy {
private Image enemyImg;
private int x_pos;
private int y_pos;
private boolean alive;
public SpaceCraft(int x, int y){
this.x_pos = x;
this.y_pos = y;
this.alive = true;
ImageIcon img = new ImageIcon("images/enemy.png");
enemyImg = img.getImage();
}
public void moveRight(){
}
public void moveLeft(){
}
public void moveForward(int moveSpeed){
y_pos += moveSpeed;
}
public void setAlive(boolean is){
this.alive = is;
}
public boolean isAlive() {
return alive;
}
public Image getImage(){
return enemyImg;
}
public int getX(){
return this.x_pos;
}
public int getY(){
return this.y_pos;
}
// create rectangle around the enemy so we can check for collisions
public Rectangle getBounds(){
return new Rectangle(x_pos, y_pos, enemyImg.getWidth(null), enemyImg.getHeight(null));
}
}[/source]
If you need more code or more explanation tell me.