IMplemtned levels

in each level, there are the equivilent number of asteroids in respect
to the game's level. i.e. level 1 = 1 asteroids, level 6 = 6 asteroids

Signed-off-by: Ricky Barrette <rickbarrette@gmail.com>
This commit is contained in:
2012-04-03 23:24:53 -04:00
parent 2d008808c8
commit 01b1fa016c
5 changed files with 263 additions and 212 deletions

View File

@@ -30,8 +30,8 @@ import java.util.Vector;
public class AsteroidGame extends Thread {
private static final int DELAY_IN_MSEC = 50;
private ArrayList<Object> mWorld;
private GameFrame mGameFrame;
private final ArrayList<Object> mWorld;
private final GameFrame mGameFrame;
public boolean isStarted = false;
/**
@@ -40,6 +40,7 @@ public class AsteroidGame extends Thread {
*/
public AsteroidGame() {
mGameFrame = new GameFrame(this);
mWorld = new ArrayList<Object>();
//TODO simulate game play unitll game ist started
this.start();
}
@@ -49,7 +50,7 @@ public class AsteroidGame extends Thread {
* @param add
* @author ricky barrette
*/
public void addElement(Object o) {
public synchronized void addElement(Object o) {
if(o instanceof Shot)
mGameFrame.getStatusBar().incrementShotCount();
@@ -59,120 +60,6 @@ public class AsteroidGame extends Thread {
mWorld.add(o);
}
/**
* popoluates the world for a new game
* @author ricky barrette
*/
public void createGame() {
mWorld = new ArrayList<Object>();
mWorld.add(new Ship(100,100,0,.35,.98,.4,1));
addElement(new Asteroid(500, 500, 1, 10, 50, 3, 3, this));
mGameFrame.getStatusBar().setShipCount(3);
mGameFrame.getStatusBar().setScore(0);
mGameFrame.getStatusBar().setAsteroidCount(1);
mGameFrame.getStatusBar().setTime(0);
mGameFrame.getStatusBar().setShotCount(0);
}
/**
* @return the world
* @author ricky barrette
*/
public ArrayList<Object> getWorld() {
return mWorld;
}
/**
* @return true if the world is empty
* @author ricky barrette
*/
public boolean isEmpty() {
return mWorld.isEmpty();
}
/**
* Clears the world, and Creates a new game
* @author ricky barrette
*/
public void newGame() {
mWorld.clear();
mGameFrame.setDisplayText(null);
createGame();
startGame();
}
/**
* Pauses the game
* @author ricky barrette
*/
public synchronized void pauseGame(){
isStarted = false;
mGameFrame.setDisplayText("Paused");
setMovingSpaceObjectsEnabled(false);
}
/**
* Removes an object from this world.
* @param o object to be removed
* @author ricky barrette
*/
public void removeElement(Object o) {
if(o instanceof Shot)
mGameFrame.getStatusBar().decrementShotCount();
if(o instanceof Asteroid) {
mGameFrame.getStatusBar().decrementAsteroidCount();
mGameFrame.getStatusBar().incrementScore(2);
}
mWorld.remove(o);
}
/**
* Main game driver method.
*
* @author ricky barrette
*/
@Override
public void run() {
while (true){
if(isStarted) {
/*
* update the display and stats
*/
mGameFrame.repaintDispaly();
mGameFrame.getStatusBar().updateStatus();
/*
* check for collsions
*/
Object o;
Collider c;
Vector<Object> wolrd = new Vector<Object>(mWorld);
for (int i = 0; i < wolrd.size(); i++){
o = wolrd.get(i);
if(o instanceof Collider){
c = (Collider) o;
for(int index = 0; index < wolrd.size(); index++)
if(c.checkForCollision(wolrd.get(index)))
//check to see if the ship blew up
if(wolrd.get(index) instanceof Ship)
downShip();
}
}
}
/*
* sleep till next time
*/
try {
sleep(DELAY_IN_MSEC);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
/**
* When this methods is called, the player's ship is down.
* @author ricky barrette
@@ -202,23 +89,136 @@ public class AsteroidGame extends Thread {
}
/**
* @return the number of objects in the world
* @return the world
* @author ricky barrette
*/
public int size() {
return mWorld.size();
public ArrayList<Object> getWorld() {
return mWorld;
}
/**
* populates the world for a new game
* @author ricky barrette
*/
public void initLevel() {
/*
* added a asteroid per level
*/
for(int i = 0; i < mGameFrame.getStatusBar().getLevel(); i ++)
addElement(new Asteroid(500, 500, 1, 10, 50, 3, 3, this));
}
/**
* @return true if the world is empty
* @author ricky barrette
*/
public boolean isEmpty() {
return mWorld.isEmpty();
}
/**
* Starts the game
* Clears the world, and Creates a new game
* @author ricky barrette
*/
public synchronized void startGame(){
public void newGame() {
mWorld.clear();
mGameFrame.setDisplayText(null);
setMovingSpaceObjectsEnabled(true);
isStarted = true;
mGameFrame.getStatusBar().setShipCount(3);
mGameFrame.getStatusBar().setScore(0);
mGameFrame.getStatusBar().setAsteroidCount(1);
mGameFrame.getStatusBar().setTime(0);
mGameFrame.getStatusBar().setShotCount(0);
mGameFrame.getStatusBar().setLevel(1);
mWorld.add(new Ship(100,100,0,.35,.98,.4,1));
initLevel();
startGame();
}
/**
* Pauses the game
* @author ricky barrette
*/
public synchronized void pauseGame(){
isStarted = false;
mGameFrame.setDisplayText("Paused");
setMovingSpaceObjectsEnabled(false);
}
/**
* Removes an object from this world.
* @param o object to be removed
* @author ricky barrette
*/
public synchronized void removeElement(Object o) {
if(o instanceof Shot)
mGameFrame.getStatusBar().decrementShotCount();
if(o instanceof Asteroid) {
mGameFrame.getStatusBar().decrementAsteroidCount();
mGameFrame.getStatusBar().incrementScore(2);
}
mWorld.remove(o);
}
/**
* Main game driver method.
*
* @author ricky barrette
*/
@Override
public void run() {
while (true){
if(isStarted) {
boolean isThereAsteroids = false;
/*
* update the display and stats
*/
mGameFrame.repaintDispaly();
mGameFrame.getStatusBar().updateStatus();
/*
* check for collsions
*/
Object o;
Collider c;
Vector<Object> wolrd = new Vector<Object>(mWorld);
for (int i = 0; i < wolrd.size(); i++){
o = wolrd.get(i);
if(o instanceof Collider){
isThereAsteroids = true;
c = (Collider) o;
for(int index = 0; index < wolrd.size(); index++)
if(c.checkForCollision(wolrd.get(index)))
//check to see if the ship blew up
if(wolrd.get(index) instanceof Ship)
downShip();
}
}
/*
* if there are no more asteroids, then increment the level
*/
if(!isThereAsteroids){
mGameFrame.getStatusBar().incrementLevel();
initLevel();
}
}
/*
* sleep till next time
*/
try {
sleep(DELAY_IN_MSEC);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
/**
* Sets the enabled state of Moving Space Objects
* @param b
@@ -229,4 +229,22 @@ public class AsteroidGame extends Thread {
if(item instanceof MovingSpaceObject)
((MovingSpaceObject) item).setActive(b);
}
/**
* @return the number of objects in the world
* @author ricky barrette
*/
public int size() {
return mWorld.size();
}
/**
* Starts the game
* @author ricky barrette
*/
public synchronized void startGame(){
mGameFrame.setDisplayText(null);
setMovingSpaceObjectsEnabled(true);
isStarted = true;
}
}

View File

@@ -122,63 +122,6 @@ public class GameFrame extends JFrame implements KeyListener, ActionListener{
System.exit(0);
}
/**
* @return the height of the game display panel
* @author ricky barrette
*/
public int getDispalyHeight() {
return mDisplay.getHeight();
}
/**
* @return the width of the game dispaly panel
* @author ricky barrette
*/
public int getDisplayWidth() {
return mDisplay.getWidth();
}
/**
* @return the mStatusBar
*/
public Status getStatusBar() {
return mStatusBar;
}
/**
* Called when a key is pressed
* (non-Javadoc)
* @see java.awt.event.KeyListener#keyPressed(java.awt.event.KeyEvent)
*/
@Override
public void keyPressed(KeyEvent e) {
switch(e.getKeyCode()){
/*
* [Enter]
* Start of pause the game
*/
case KeyEvent.VK_ENTER:
if(mGame.isStarted)
mGame.pauseGame();
else
mGame.startGame();
break;
}
driveShip(e, true);
}
/**
* Called when a key is released
* (non-Javadoc)
* @see java.awt.event.KeyListener#keyReleased(java.awt.event.KeyEvent)
*/
@Override
public void keyReleased(KeyEvent e) {
driveShip(e, false);
}
/**
* Drives the user's ship
* @param e
@@ -253,6 +196,63 @@ public class GameFrame extends JFrame implements KeyListener, ActionListener{
}
}
/**
* @return the height of the game display panel
* @author ricky barrette
*/
public int getDispalyHeight() {
return mDisplay.getHeight();
}
/**
* @return the width of the game dispaly panel
* @author ricky barrette
*/
public int getDisplayWidth() {
return mDisplay.getWidth();
}
/**
* @return the mStatusBar
*/
public Status getStatusBar() {
return mStatusBar;
}
/**
* Called when a key is pressed
* (non-Javadoc)
* @see java.awt.event.KeyListener#keyPressed(java.awt.event.KeyEvent)
*/
@Override
public void keyPressed(KeyEvent e) {
switch(e.getKeyCode()){
/*
* [Enter]
* Start of pause the game
*/
case KeyEvent.VK_ENTER:
if(mGame.isStarted)
mGame.pauseGame();
else
mGame.startGame();
break;
}
driveShip(e, true);
}
/**
* Called when a key is released
* (non-Javadoc)
* @see java.awt.event.KeyListener#keyReleased(java.awt.event.KeyEvent)
*/
@Override
public void keyReleased(KeyEvent e) {
driveShip(e, false);
}
@Override
public void keyTyped(KeyEvent e) {
}

View File

@@ -28,16 +28,6 @@ public class Main {
public static final boolean DEBUG = false;
/**
* Creates a new Main
* @author ricky barrette
*/
public Main() {
AsteroidGame game = new AsteroidGame();
game.createGame();
game.startGame();
}
/**
* @param args
* @author ricky barrette
@@ -46,4 +36,14 @@ public class Main {
new Main();
}
/**
* Creates a new Main
* @author ricky barrette
*/
public Main() {
AsteroidGame game = new AsteroidGame();
game.newGame();
game.startGame();
}
}

View File

@@ -143,6 +143,14 @@ public class Ship extends MovingSpaceObject implements Drawable {
return mRadius;
}
public double getXVelocity() {
return this.mXVelocity;
}
public double getYVelocity() {
return this.mYVelocity;
}
/**
* Called when ...
* (non-Javadoc)
@@ -159,12 +167,4 @@ public class Ship extends MovingSpaceObject implements Drawable {
super.move(scrnWidth, scrnHeight);
}
public double getXVelocity() {
return this.mXVelocity;
}
public double getYVelocity() {
return this.mYVelocity;
}
}

View File

@@ -41,6 +41,7 @@ public class Status extends JPanel {
private int mShipCount = 0;
private long mScore = 0;
private long mTime = 0;
private int mLevel = 0;
/**
* Creates a new Status
@@ -64,6 +65,14 @@ public class Status extends JPanel {
this.mAsteroidCount -= 1;
}
/**
* reduces the level by one
* @author ricky barrette
*/
public void decrementLevel() {
this.mLevel -= 1;
}
/**
* reduces total score by x amount
* @param score
@@ -97,42 +106,49 @@ public class Status extends JPanel {
public synchronized void decrementTime(long mTime) {
this.mTime -= mTime;
}
/**
* @return the mAsteroidCount
*/
public synchronized int getAsteroidCount() {
return mAsteroidCount;
}
/**
* @return the level
*/
public int getLevel() {
return mLevel;
}
/**
* @return the mScore
*/
public synchronized long getScore() {
return mScore;
}
/**
* @return the mShipCount
*/
public synchronized int getShipCount() {
return mShipCount;
}
/**
* @return the mShotCount
*/
public synchronized int getShotCount() {
return mShotCount;
}
/**
* @return the mTime
*/
public synchronized long getTime() {
return mTime;
}
/**
* increases total asteroid count by 1
* @author ricky barrette
@@ -140,6 +156,14 @@ public class Status extends JPanel {
public synchronized void incrementAsteroidCount() {
this.mAsteroidCount += 1;
}
/**
* increases the level by one
* @author ricky barrette
*/
public void incrementLevel() {
this.mLevel += 1;
}
/**
* increases total score by x amount
@@ -149,7 +173,7 @@ public class Status extends JPanel {
public synchronized void incrementScore(int score) {
this.mScore += score;
}
/**
* increases total ships by one
* @author ricky barrette
@@ -182,6 +206,13 @@ public class Status extends JPanel {
this.mAsteroidCount = mAsteroidCount;
}
/**
* @param level the level to set
*/
public void setLevel(int level) {
this.mLevel = level;
}
/**
* @param mScore the mScore to set
*/
@@ -195,7 +226,7 @@ public class Status extends JPanel {
public synchronized void setShipCount(int mShipCount) {
this.mShipCount = mShipCount;
}
/**
* @param mShotCount the mShotCount to set
*/
@@ -209,20 +240,22 @@ public class Status extends JPanel {
public synchronized void setTime(long mTime) {
this.mTime = mTime;
}
/**
* Sets the status
* @param temp
* @author ricky barrette
*/
public void updateStatus() {
mBuffer.append("Missiles ");
mBuffer.append("Level: ");
mBuffer.append(mLevel);
mBuffer.append(" Missiles: ");
mBuffer.append(getShotCount());
mBuffer.append(" Asteroids ");
mBuffer.append(" Asteroids: ");
mBuffer.append(getAsteroidCount());
mBuffer.append(" Ships ");
mBuffer.append(" Ships: ");
mBuffer.append(getShipCount());
mBuffer.append(" Score ");
mBuffer.append(" Score: ");
mBuffer.append(getScore());
mBuffer.append(" Time: ");
mBuffer.append(getTime());