diff --git a/Asteroids/src/com/RickBarrette/asteroids/Asteroid.java b/Asteroids/src/com/RickBarrette/asteroids/Asteroid.java index 397a3a0..608575f 100644 --- a/Asteroids/src/com/RickBarrette/asteroids/Asteroid.java +++ b/Asteroids/src/com/RickBarrette/asteroids/Asteroid.java @@ -25,7 +25,6 @@ import java.awt.Graphics; /** * This class will be used to make astroids, to destroy the ship! - * * @author ricky barrette */ public class Asteroid extends MovingSpaceObject implements Collider, Drawable { @@ -33,13 +32,12 @@ public class Asteroid extends MovingSpaceObject implements Collider, Drawable { private final int mNumberSplit; private final int mHitsLeft; private final int mRadius; + private final double mMinVelocity; + private final double mMaxVelocity; private AsteroidGame mGame; - private double mMinVelocity; - private double mMaxVelocity; /** * Creates a new Asteroid - * * @param x * @param y * @param xVelocity @@ -63,8 +61,30 @@ public class Asteroid extends MovingSpaceObject implements Collider, Drawable { mNumberSplit = numberSplit; mHitsLeft = hitsLeft; mRadius = radius; - mVelocityDecay = 1; - isActive = true; + } + + /** + * Called when a collision check needs to be made. + * Only checks for ship and shots + * (non-Javadoc) + * @see com.RickBarrette.asteroids.Collider#checkForCollision(java.lang.Object) + */ + @Override + public boolean checkForCollision(Object o) { + + if(o instanceof Asteroid){ + //TODO inverse directions of both asteroids + } + + if(o instanceof Ship) { + return shipCollision((Ship) o); + } + + if(o instanceof Shot) { + return shotCollision((Shot) o); + } + + return false; } /** @@ -75,15 +95,15 @@ public class Asteroid extends MovingSpaceObject implements Collider, Drawable { * @author ricky barrette */ public Asteroid createSplitAsteroid(double minVelocity, double maxVelocity){ - //when this asteroid gets hit by a shot, this method is called - //numSplit times by AsteroidsGame to create numSplit smaller - //asteroids. Dividing the radius by sqrt(numSplit) makes the - //sum of the areas taken up by the smaller asteroids equal to - //the area of this asteroid. Each smaller asteroid has one - //less hit left before being completely destroyed. - return new Asteroid(mX,mY, minVelocity, maxVelocity, (int) (mRadius/Math.sqrt(mNumberSplit)), mNumberSplit, mHitsLeft-1, mGame); + /* + * Dividing the radius by sqrt(numSplit) makes the + * sum of the areas taken up by the smaller asteroids equal to + * the area of this asteroid. Each smaller asteroid has one + * less hit left before being completely destroyed. + */ + return new Asteroid(mX,mY, minVelocity, maxVelocity, (int) (mRadius/Math.sqrt(mNumberSplit)), mNumberSplit, mHitsLeft-1, mGame); } - + /** * Called when the Asteroid needs to be drawn * (non-Javadoc) @@ -165,24 +185,4 @@ public class Asteroid extends MovingSpaceObject implements Collider, Drawable { else if (mY > scrnWidth + mRadius) mY -= scrnWidth + 2 * mRadius; } - - /** - * Called when a collision check needs to be made. - * Only checks for ship and shots - * (non-Javadoc) - * @see com.RickBarrette.asteroids.Collider#checkForCollision(java.lang.Object) - */ - @Override - public boolean checkForCollision(Object o) { - - if(o instanceof Ship) { - return shipCollision((Ship) o); - } - - if(o instanceof Shot) { - return shotCollision((Shot) o); - } - - return false; - } } \ No newline at end of file diff --git a/Asteroids/src/com/RickBarrette/asteroids/AsteroidGame.java b/Asteroids/src/com/RickBarrette/asteroids/AsteroidGame.java index 7c7e6f7..f45b7a0 100644 --- a/Asteroids/src/com/RickBarrette/asteroids/AsteroidGame.java +++ b/Asteroids/src/com/RickBarrette/asteroids/AsteroidGame.java @@ -95,9 +95,10 @@ public class AsteroidGame extends Thread { * Pauses the game * @author ricky barrette */ - public synchronized void pause(){ + public synchronized void pauseGame(){ isStarted = false; mGameFrame.setDisplayText("Paused"); + setMovingSpaceObjectsEnabled(false); } /** @@ -124,7 +125,10 @@ public class AsteroidGame extends Thread { while (true){ if(isStarted) { - mGameFrame.repaintDisplay(); + /* + * update the display and stats + */ + mGameFrame.repaintDispaly(); mGameFrame.getStatusBar().updateStatus(); /* @@ -176,9 +180,9 @@ public class AsteroidGame extends Thread { mGameFrame.getStatusBar().setShipCount(mGameFrame.getStatusBar().getShipCount() -1); if(mGameFrame.getStatusBar().getShipCount() > 0){ - pause(); + pauseGame(); mWorld.add(new Ship(100,100,0,.35,.98,.4,1)); - mGameFrame.setDisplayText("You died, press start when ready."); + mGameFrame.setDisplayText("You died, You can hyper jump to a safe place now...\nPress start when ready."); } else { mGameFrame.setDisplayText("Game Over"); } @@ -199,8 +203,18 @@ public class AsteroidGame extends Thread { */ public synchronized void startGame(){ mGameFrame.setDisplayText(null); - mGameFrame.setMovingSpaceObjectsEnabled(true); - isStarted = true; - + setMovingSpaceObjectsEnabled(true); + isStarted = true; + } + + /** + * Sets the enabled state of Moving Space Objects + * @param b + * @author ricky barrette + */ + public void setMovingSpaceObjectsEnabled(boolean b) { + for(Object item : mWorld) + if(item instanceof MovingSpaceObject) + ((MovingSpaceObject) item).setActive(b); } } \ No newline at end of file diff --git a/Asteroids/src/com/RickBarrette/asteroids/Display.java b/Asteroids/src/com/RickBarrette/asteroids/Display.java index b90cd86..47e6ddf 100644 --- a/Asteroids/src/com/RickBarrette/asteroids/Display.java +++ b/Asteroids/src/com/RickBarrette/asteroids/Display.java @@ -61,18 +61,22 @@ public class Display extends JPanel { if(mText != null){ g.setColor(Color.ORANGE); - g.drawString(mText, this.getHeight() /2 , this.getWidth() / 2); + g.drawString(mText, this.getWidth() / 2, this.getHeight() /2 ); } /* * Move & Draw the world's objects */ Object item; + MovingSpaceObject mso; for (int i = 0; i < mGame.getWorld().size(); i++) { item = mGame.getWorld().get(i); - if (item instanceof MovingSpaceObject) - ((MovingSpaceObject) item).move(getHeight(), getWidth()); + if (item instanceof MovingSpaceObject){ + mso = (MovingSpaceObject) item; + if(mso.isActive) + mso.move(getHeight(), getWidth()); + } if(item instanceof Drawable) ((Drawable) item).draw(g); diff --git a/Asteroids/src/com/RickBarrette/asteroids/GameFrame.java b/Asteroids/src/com/RickBarrette/asteroids/GameFrame.java index b64b8d7..6676997 100644 --- a/Asteroids/src/com/RickBarrette/asteroids/GameFrame.java +++ b/Asteroids/src/com/RickBarrette/asteroids/GameFrame.java @@ -33,31 +33,20 @@ import javax.swing.JMenuBar; import javax.swing.JMenuItem; /** - * This class will maintian the game's frame, and handle key events from the user + * This class will maintian the game's frame. + * It will be used to display all the game's information to the user and handle key events from the user * @author ricky barrette */ -public class GameFrame extends JFrame implements KeyListener{ +public class GameFrame extends JFrame implements KeyListener, ActionListener{ private static final long serialVersionUID = -2051298505681885632L; - private JMenuBar mMenuBar; - private JMenu mMenu; - private JMenuItem mMenuNewGame; - private JMenuItem mMenuQuit; private Status mStatusBar; private Display mDisplay; - private Container mContainer; - private FlowLayout mLayout; - private menuListener xlistener; private AsteroidGame mGame; - private JMenuItem mMenuStartGame; - - private JMenuItem mMenuPauseGame; - /** * Creates a new GameFrame - * * @param g * @author ricky barrette */ @@ -65,39 +54,40 @@ public class GameFrame extends JFrame implements KeyListener{ super("ASTEROIDS"); mGame = g; - mMenuBar = new JMenuBar(); - setJMenuBar(mMenuBar); + /* + * set up the game's menus + */ + JMenuBar menuBar = new JMenuBar(); + setJMenuBar(menuBar); + + /* + * file menu + */ + JMenu fileMenu = new JMenu("File"); + JMenuItem menuNewGame = new JMenuItem("New Game"); + JMenuItem menuStartGame = new JMenuItem("Start"); + JMenuItem menuPauseGame = new JMenuItem("Pause"); + JMenuItem menuQuit = new JMenuItem("Quit"); - mMenu = new JMenu("File"); + fileMenu.add(menuNewGame); + fileMenu.addSeparator(); + fileMenu.add(menuStartGame); + fileMenu.add(menuPauseGame); + fileMenu.addSeparator(); + fileMenu.add(menuQuit); + menuNewGame.addActionListener(this); + menuQuit.addActionListener(this); + menuStartGame.addActionListener(this); + menuPauseGame.addActionListener(this); - mMenuNewGame = new JMenuItem("New Game"); - mMenuStartGame = new JMenuItem("Start"); - mMenuPauseGame = new JMenuItem("Pause"); - mMenuQuit = new JMenuItem("Quit"); + menuBar.add(fileMenu); + + FlowLayout layout = new FlowLayout(); + layout.setAlignment(FlowLayout.LEFT); - mMenu.add(mMenuNewGame); - mMenu.addSeparator(); - mMenu.add(mMenuStartGame); - mMenu.add(mMenuPauseGame); - mMenu.addSeparator(); - mMenu.add(mMenuQuit); - - mMenuBar.add(mMenu); - - mLayout = new FlowLayout(); - mLayout.setAlignment(FlowLayout.LEFT); - - - mContainer = getContentPane(); - mStatusBar = new Status(mContainer, mGame); - mDisplay = new Display(mContainer, mGame); - - - xlistener = new menuListener(); - mMenuNewGame.addActionListener(xlistener); - mMenuQuit.addActionListener(xlistener); - mMenuStartGame.addActionListener(xlistener); - mMenuPauseGame.addActionListener(xlistener); + Container container = getContentPane(); + mStatusBar = new Status(container, mGame); + mDisplay = new Display(container, mGame); addKeyListener(this); @@ -109,38 +99,50 @@ public class GameFrame extends JFrame implements KeyListener{ } - public void repaintDisplay() { - mDisplay.repaint(); + /** + * Called when a menu item is selected from the benu bar + * (non-Javadoc) + * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent) + */ + public void actionPerformed(ActionEvent e) { + if (e.getActionCommand().equals("New Game")) { + mGame.newGame(); + mDisplay.repaint(); + } + + if (e.getActionCommand().equals("Start")) { + mGame.startGame(); + } + + if (e.getActionCommand().equals("Pause")) { + mGame.pauseGame(); + } + + if (e.getActionCommand().equals("Quit")) + System.exit(0); } /** - * (non-Javadoc) - * @see java.awt.Component#repaint() + * @return the height of the game display panel + * @author ricky barrette */ - @Override - public void repaint() { - mDisplay.repaint(); - super.repaint(); + public int getDispalyHeight() { + return mDisplay.getHeight(); } - private class menuListener implements ActionListener { - - public void actionPerformed(ActionEvent e) { - if (e.getActionCommand().equals("New Game")) { - mGame.newGame(); - mDisplay.repaint(); - - } - if (e.getActionCommand().equals("Start")) { - mGame.startGame(); - } - if (e.getActionCommand().equals("Pause")) { - mGame.pause(); - } - if (e.getActionCommand().equals("Quit")) - System.exit(0); - } + /** + * @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; } /** @@ -272,43 +274,31 @@ public class GameFrame extends JFrame implements KeyListener{ @Override public void keyTyped(KeyEvent e) { // TODO Auto-generated method stub - } /** - * @return the height of the game display panel + * (non-Javadoc) + * @see java.awt.Component#repaint() + */ + @Override + public void repaint() { + mDisplay.repaint(); + super.repaint(); + } + + /** + * updates and repaints all world objects * @author ricky barrette */ - public int getDispalyHeight() { - return mDisplay.getHeight(); + public void repaintDispaly() { + mDisplay.repaint(); } /** - * @return the width of the game dispaly panel + * Sets test to be displayed in the center of the display + * @param string to be displayed * @author ricky barrette */ - public int getDisplayWidth() { - return mDisplay.getWidth(); - } - - /** - * Sets the enabled state of Moving Space Objects - * @param b - * @author ricky barrette - */ - public void setMovingSpaceObjectsEnabled(boolean b) { - for(Object item : mGame.getWorld()) - if(item instanceof MovingSpaceObject) - ((MovingSpaceObject) item).setActive(b); - } - - /** - * @return the mStatusBar - */ - public Status getStatusBar() { - return mStatusBar; - } - public void setDisplayText(String string) { mDisplay.setDisplayText(string); this.repaint(); diff --git a/Asteroids/src/com/RickBarrette/asteroids/MovingSpaceObject.java b/Asteroids/src/com/RickBarrette/asteroids/MovingSpaceObject.java index 0767591..f5c77f2 100644 --- a/Asteroids/src/com/RickBarrette/asteroids/MovingSpaceObject.java +++ b/Asteroids/src/com/RickBarrette/asteroids/MovingSpaceObject.java @@ -27,12 +27,12 @@ package com.RickBarrette.asteroids; public class MovingSpaceObject extends SpaceObject implements Moveable{ protected double mAngle; - protected double mVelocityDecay; + protected double mVelocityDecay = 1; protected double mRotationalSpeed; protected double mXVelocity = 0; protected double mYVelocity = 0; protected double mAcceleration; - protected boolean isTurningLeft = false, isTurningRight = false, isAccelerating = false, isActive; + protected boolean isTurningLeft = false, isTurningRight = false, isAccelerating = false, isActive = true; /** * @return true if the space object is accelerating diff --git a/Asteroids/src/com/RickBarrette/asteroids/Shot.java b/Asteroids/src/com/RickBarrette/asteroids/Shot.java index 0b4fd2f..933007e 100644 --- a/Asteroids/src/com/RickBarrette/asteroids/Shot.java +++ b/Asteroids/src/com/RickBarrette/asteroids/Shot.java @@ -46,11 +46,8 @@ public class Shot extends MovingSpaceObject implements Drawable { mAcceleration = SPEED; mColor = Color.WHITE; mGame = game; - isActive = true; mXVelocity = SPEED*Math.cos(angle)+shipXVel; mYVelocity = SPEED*Math.sin(angle)+shipYVel; - mVelocityDecay = 1; - } @Override