From 2152cc8213c260a31f4bbe4ed970bba4206a2ebe Mon Sep 17 00:00:00 2001 From: Ricky Barrette Date: Tue, 3 Apr 2012 13:24:45 -0400 Subject: [PATCH] Addded in / de crement methods to the Status Signed-off-by: Ricky Barrette --- .../RickBarrette/asteroids/AsteroidGame.java | 34 +++-- .../com/RickBarrette/asteroids/GameFrame.java | 36 +++--- .../com/RickBarrette/asteroids/Status.java | 116 +++++++++++++++--- 3 files changed, 141 insertions(+), 45 deletions(-) diff --git a/Asteroids/src/com/RickBarrette/asteroids/AsteroidGame.java b/Asteroids/src/com/RickBarrette/asteroids/AsteroidGame.java index f004b91..6deeea8 100644 --- a/Asteroids/src/com/RickBarrette/asteroids/AsteroidGame.java +++ b/Asteroids/src/com/RickBarrette/asteroids/AsteroidGame.java @@ -20,6 +20,7 @@ */ package com.RickBarrette.asteroids; +import java.util.ArrayList; import java.util.Vector; /** @@ -29,7 +30,7 @@ import java.util.Vector; public class AsteroidGame extends Thread { private static final int DELAY_IN_MSEC = 50; - private Vector mWorld; + private ArrayList mWorld; private GameFrame mGameFrame; public boolean isStarted = false; @@ -50,12 +51,12 @@ public class AsteroidGame extends Thread { */ public void addElement(Object o) { if(o instanceof Shot) - mGameFrame.getStatusBar().setShotCount(mGameFrame.getStatusBar().getShotCount()+1); + mGameFrame.getStatusBar().incrementShotCount(); if(o instanceof Asteroid) - mGameFrame.getStatusBar().setAsteroidCount(mGameFrame.getStatusBar().getAsteroidCount()+1); + mGameFrame.getStatusBar().incrementAsteroidCount(); - mWorld.addElement(o); + mWorld.add(o); } /** @@ -63,13 +64,21 @@ public class AsteroidGame extends Thread { * @author ricky barrette */ public void createGame() { - mWorld = new Vector(); + mWorld = new ArrayList(); 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(0); + mGameFrame.getStatusBar().setTime(0); + mGameFrame.getStatusBar().setShotCount(0); } - public Vector getWorld() { + /** + * @return the world + * @author ricky barrette + */ + public ArrayList getWorld() { return mWorld; } @@ -89,6 +98,7 @@ public class AsteroidGame extends Thread { mWorld.clear(); mGameFrame.setDisplayText(null); createGame(); + startGame(); } /** @@ -108,11 +118,13 @@ public class AsteroidGame extends Thread { */ public void removeElement(Object o) { if(o instanceof Shot) - mGameFrame.getStatusBar().setShotCount(mGameFrame.getStatusBar().getShotCount()-1); + mGameFrame.getStatusBar().decrementShotCount(); - if(o instanceof Asteroid) - mGameFrame.getStatusBar().setAsteroidCount(mGameFrame.getStatusBar().getAsteroidCount()-1); - mWorld.removeElement(o); + if(o instanceof Asteroid) { + mGameFrame.getStatusBar().decrementAsteroidCount(); + mGameFrame.getStatusBar().incrementScore(2); + } + mWorld.remove(o); } /** @@ -177,7 +189,7 @@ public class AsteroidGame extends Thread { mWorld.remove(i); } - mGameFrame.getStatusBar().setShipCount(mGameFrame.getStatusBar().getShipCount() -1); + mGameFrame.getStatusBar().decrementShipCount(); if(mGameFrame.getStatusBar().getShipCount() > 0){ pauseGame(); diff --git a/Asteroids/src/com/RickBarrette/asteroids/GameFrame.java b/Asteroids/src/com/RickBarrette/asteroids/GameFrame.java index b2acc8d..1642444 100644 --- a/Asteroids/src/com/RickBarrette/asteroids/GameFrame.java +++ b/Asteroids/src/com/RickBarrette/asteroids/GameFrame.java @@ -233,24 +233,24 @@ public class GameFrame extends JFrame implements KeyListener, ActionListener{ break; - /* - * [H] Hyoer jump - */ - case KeyEvent.VK_H: - if(isKeyPressed){ - Random myRNG = new Random(); - ship.setLocation(myRNG.nextInt(mDisplay.getHeight()), myRNG.nextInt(mDisplay.getWidth())); - } - break; - - /* - * [Space] Pew Pew Pew!!! - */ - case KeyEvent.VK_SPACE: - if(isKeyPressed) - mGame.addElement(new Shot(ship.getX(), ship.getY(), ship.getAngle(), ship.getXVelocity(), ship.getYVelocity(), mGame)); - break; - } + /* + * [H] Hyoer jump + */ + case KeyEvent.VK_H: + if(isKeyPressed){ + Random myRNG = new Random(); + ship.setLocation(myRNG.nextInt(mDisplay.getHeight()), myRNG.nextInt(mDisplay.getWidth())); + } + break; + + /* + * [Space] Pew Pew Pew!!! + */ + case KeyEvent.VK_SPACE: + if(isKeyPressed) + mGame.addElement(new Shot(ship.getX(), ship.getY(), ship.getAngle(), ship.getXVelocity(), ship.getYVelocity(), mGame)); + break; + } } @Override diff --git a/Asteroids/src/com/RickBarrette/asteroids/Status.java b/Asteroids/src/com/RickBarrette/asteroids/Status.java index 4c539b5..2e37c9c 100644 --- a/Asteroids/src/com/RickBarrette/asteroids/Status.java +++ b/Asteroids/src/com/RickBarrette/asteroids/Status.java @@ -57,72 +57,156 @@ public class Status extends JPanel { } /** - * @return the mAsteroidCount + * decreases total asteroid count by 1 + * @author ricky barrette */ - public int getAsteroidCount() { - return mAsteroidCount; + public synchronized void decrementAsteroidCount() { + this.mAsteroidCount -= 1; } + /** + * reduces total score by x amount + * @param score + * @author ricky barrette + */ + public synchronized void decrementScore(int score) { + this.mScore -= mScore; + } + + /** + * reduces total ships by 1 + * @author ricky barrette + */ + public synchronized void decrementShipCount() { + this.mShipCount -= 1; + } + + /** + * reduces total shots by 1 + * @author ricky barrette + */ + public synchronized void decrementShotCount() { + this.mShotCount -= 1; + } + + /** + * Reduces time be x amount + * @param mTime + * @author ricky barrette + */ + public synchronized void decrementTime(long mTime) { + this.mTime -= mTime; + } + + /** + * @return the mAsteroidCount + */ + public synchronized int getAsteroidCount() { + return mAsteroidCount; + } + /** * @return the mScore */ - public long getScore() { + public synchronized long getScore() { return mScore; } /** * @return the mShipCount */ - public int getShipCount() { + public synchronized int getShipCount() { return mShipCount; } /** * @return the mShotCount */ - public int getShotCount() { + public synchronized int getShotCount() { return mShotCount; } - + /** * @return the mTime */ - public long getTime() { + public synchronized long getTime() { return mTime; } + + /** + * increases total asteroid count by 1 + * @author ricky barrette + */ + public synchronized void incrementAsteroidCount() { + this.mAsteroidCount += 1; + } + /** + * increases total score by x amount + * @param score + * @author ricky barrette + */ + public synchronized void incrementScore(int score) { + this.mScore += score; + } + + /** + * increases total ships by one + * @author ricky barrette + */ + public synchronized void incrementShipCount() { + this.mShipCount += 1; + } + + /** + * increases total shots by one + * @author ricky barrette + */ + public synchronized void incrementShotCount() { + this.mShotCount += 1; + } + + /** + * increates time by x amount + * @param mTime + * @author ricky barrette + */ + public synchronized void incrementTime(long mTime) { + this.mTime += mTime; + } + /** * @param mAsteroidCount the mAsteroidCount to set */ - public void setAsteroidCount(int mAsteroidCount) { + public synchronized void setAsteroidCount(int mAsteroidCount) { this.mAsteroidCount = mAsteroidCount; } - + /** * @param mScore the mScore to set */ - public void setScore(long mScore) { + public synchronized void setScore(long mScore) { this.mScore = mScore; } /** * @param mShipCount the mShipCount to set */ - public void setShipCount(int mShipCount) { + public synchronized void setShipCount(int mShipCount) { this.mShipCount = mShipCount; } - + /** * @param mShotCount the mShotCount to set */ - public void setShotCount(int mShotCount) { + public synchronized void setShotCount(int mShotCount) { this.mShotCount = mShotCount; } - + /** * @param mTime the mTime to set */ - public void setTime(long mTime) { + public synchronized void setTime(long mTime) { this.mTime = mTime; }