Created AsteroidGame.hyperJump() for moving the ship to a random x,y

Signed-off-by: Ricky Barrette <rickbarrette@gmail.com>
This commit is contained in:
2012-04-05 12:16:39 -04:00
parent 2cef445289
commit 817e8b4d9f
2 changed files with 25 additions and 10 deletions

View File

@@ -58,7 +58,6 @@ public class AsteroidGame extends Thread {
* @author ricky barrette
*/
private void downShip() {
Random gen = new Random();
System.out.println("Ship collision dected");
/*
* move the players ship's
@@ -72,15 +71,15 @@ public class AsteroidGame extends Thread {
}
if(s != null){
s.setLocation(gen.nextInt(mGameFrame.getDisplayWidth()), gen.nextInt(mGameFrame.getDispalyHeight()));
s.allStop();
hyperJump(s);
}
mGameFrame.getStatusBar().decrementShipCount();
if(mGameFrame.getStatusBar().getShipCount() > 0){
pauseGame();
mGameFrame.setDisplayText("You died, You can hyper jump to a safe place now...\nPress start when ready.");
mGameFrame.setDisplayText("You died, You can hyper jump to a safe place now... Press start when ready.");
} else {
mGameFrame.setDisplayText("Game Over");
if(s != null)
@@ -97,6 +96,25 @@ public class AsteroidGame extends Thread {
return mWorld;
}
/**
* Hyperjumps the sip
* @param ship
* @author ricky barrette
*/
public void hyperJump(Ship ship) {
// boolean isSafe = true;
Random gen = new Random();
// do{
System.out.println("hyper jumping");
ship.setLocation(gen.nextInt(mGameFrame.getDisplayWidth()), gen.nextInt(mGameFrame.getDispalyHeight()));
// for(int i = 0; i < mWorld.size(); i++)
// if(mWorld.get(i) instanceof Collider)
// if(((Collider) mWorld.get(i)).checkForCollision(ship))
// isSafe = false;
// } while (!isSafe);
mGameFrame.repaintDispaly();
}
/**
* populates the world for a new game
* @author ricky barrette
@@ -109,7 +127,7 @@ public class AsteroidGame extends Thread {
for(int i = 0; i < mGameFrame.getStatusBar().getLevel(); i ++)
addElement(new Asteroid(gen.nextInt(mGameFrame.getDisplayWidth()), gen.nextInt(mGameFrame.getDispalyHeight()), 1, 10, 50, 3, 3, this));
}
/**
* @return true if the world is empty
* @author ricky barrette
@@ -257,7 +275,7 @@ public class AsteroidGame extends Thread {
if(item instanceof MovingSpaceObject)
((MovingSpaceObject) item).setActive(b);
}
/**
* @return the number of objects in the world
* @author ricky barrette
@@ -265,7 +283,7 @@ public class AsteroidGame extends Thread {
public int size() {
return mWorld.size();
}
/**
* Starts the game
* @author ricky barrette

View File

@@ -25,7 +25,6 @@ import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JMenu;
@@ -181,9 +180,7 @@ public class GameFrame extends JFrame implements KeyListener, ActionListener{
*/
case KeyEvent.VK_H:
if(isKeyPressed){
Random myRNG = new Random();
ship.setLocation(myRNG.nextInt(mDisplay.getWidth()), myRNG.nextInt(mDisplay.getHeight()));
repaint();
mGame.hyperJump(ship);
}
break;