Jump to content

  • Log In with Google      Sign In   
  • Create Account

Java Scroller Game


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 Getov   Members   -  Reputation: 471

Like
0Likes
Like

Posted 11 December 2012 - 12:44 PM

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<SpaceCraft> enemies = new ArrayList<SpaceCraft>(); 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<Rectangle> enemiesBounds = new ArrayList<Rectangle>(); 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 SHIPSpublic 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.

Sponsor:

#2 deathwearer   Members   -  Reputation: 367

Like
1Likes
Like

Posted 11 December 2012 - 12:57 PM

// HANDLE MANY ENEMIES - FAIL
ArrayList<Rectangle> enemiesBounds = new ArrayList<Rectangle>();
for (int i = 0; i < enemies.size(); ++i){
enemiesBounds.add(enemies.get(i).getBounds());
}
for (int i = 0; i > blasterShots.size(); ++i){

You wrote > when I think it should be <

Not sure if this cause your problem, but that would be a good bug to fix first Posted Image

#3 Getov   Members   -  Reputation: 471

Like
0Likes
Like

Posted 11 December 2012 - 01:00 PM

OMG , jesus , I can't believe...... so stupid error.... THANKS man, I didn't even notice it.
Shit happens :D .

Thanks again, in the next few days I might have questions about making the game smoother , using threads and pressing more than 1 key at once... but for now I will try to do it myself :).

#4 deathwearer   Members   -  Reputation: 367

Like
0Likes
Like

Posted 11 December 2012 - 01:26 PM

Before going into threading (Which is a very delicate process) you better see how you can reduce processing time and memory allocation.

If you do the calculation of the same object more than once you might want to cache it somewhere.
Review container, if you don't need random access use forward iterator
What is called each loop. Do you call the only necessary stuff? Are you calling everything but should only call few things based on some conditions?
There are other optimization you can apply depending on game type etc. If there are no enemies in the screen, don't even check the bullets collision, stuff like that.

Edited by deathwearer, 11 December 2012 - 01:27 PM.


#5 Getov   Members   -  Reputation: 471

Like
0Likes
Like

Posted 11 December 2012 - 06:03 PM

Actually I may not use threads in this simple game, I managed to smooth my movement. Now I will try to optimize my code as much as possible.
The only problem I have is that when I press 2 keys at the same time (fire & movement) it registers only one key. ( there is no anti-ghosting :D ).
Any tips on that how to achieve it ? Thanks in advance !

that's my code to register the keyboard input:

[source lang="java"] public void keyPressed(KeyEvent e){ switch (e.getKeyCode()){ case KeyEvent.VK_LEFT: keyLeft = true; break; case KeyEvent.VK_RIGHT: keyRight = true; break; case KeyEvent.VK_SPACE: space = true; break; } } public void keyReleased(KeyEvent e){ switch (e.getKeyCode()){ case KeyEvent.VK_LEFT: keyLeft = false; break; case KeyEvent.VK_RIGHT: keyRight = false; break; case KeyEvent.VK_SPACE: space = false; break; } }}[/source]

This is in my main class:

[source lang="java"] private class MyActionListener extends KeyAdapter { public void keyPressed(KeyEvent e){ player.keyPressed(e); } public void keyReleased(KeyEvent e){ player.keyReleased(e); } }...................................................................................... public void actionPerformed(ActionEvent e){ /* TODO: anti-ghosting */ if (player.keyLeft){ player.moveLeft(); } else if (player.keyRight){ player.moveRight(); } else if (player.space){ player.generateBlaster(); } ............................................}[/source]

Edited by Getov, 11 December 2012 - 06:08 PM.


#6 ~Helgon   Members   -  Reputation: 357

Like
3Likes
Like

Posted 11 December 2012 - 06:39 PM

Do not use switch then.

public void keyPressed(KeyEvent e){

   if(e.getKeyCode() == KeyEvent.VK_UP){
	 up = true;
   }

   if(e.getKeyCode() == KeyEvent.VK_DOWN){
	 down = true;
   }
}

and then in the game loop just check

if(up){
   player.moveUp();
}

if(down){
   player.moveDown();
}

Should work.. that's the way i done it in some sidescrollers in java

Regards

Edited by ~Helgon, 11 December 2012 - 06:40 PM.

from time to time i find time





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS