Started cleaing up the code
Game world objects no longer move (when paused) while the window is being redrawn for resize events Signed-off-by: Ricky Barrette <rickbarrette@gmail.com>
This commit is contained in:
@@ -25,7 +25,6 @@ import java.awt.Graphics;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* This class will be used to make astroids, to destroy the ship!
|
* This class will be used to make astroids, to destroy the ship!
|
||||||
*
|
|
||||||
* @author ricky barrette
|
* @author ricky barrette
|
||||||
*/
|
*/
|
||||||
public class Asteroid extends MovingSpaceObject implements Collider, Drawable {
|
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 mNumberSplit;
|
||||||
private final int mHitsLeft;
|
private final int mHitsLeft;
|
||||||
private final int mRadius;
|
private final int mRadius;
|
||||||
|
private final double mMinVelocity;
|
||||||
|
private final double mMaxVelocity;
|
||||||
private AsteroidGame mGame;
|
private AsteroidGame mGame;
|
||||||
private double mMinVelocity;
|
|
||||||
private double mMaxVelocity;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new Asteroid
|
* Creates a new Asteroid
|
||||||
*
|
|
||||||
* @param x
|
* @param x
|
||||||
* @param y
|
* @param y
|
||||||
* @param xVelocity
|
* @param xVelocity
|
||||||
@@ -63,8 +61,30 @@ public class Asteroid extends MovingSpaceObject implements Collider, Drawable {
|
|||||||
mNumberSplit = numberSplit;
|
mNumberSplit = numberSplit;
|
||||||
mHitsLeft = hitsLeft;
|
mHitsLeft = hitsLeft;
|
||||||
mRadius = radius;
|
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,13 +95,13 @@ public class Asteroid extends MovingSpaceObject implements Collider, Drawable {
|
|||||||
* @author ricky barrette
|
* @author ricky barrette
|
||||||
*/
|
*/
|
||||||
public Asteroid createSplitAsteroid(double minVelocity, double maxVelocity){
|
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
|
* Dividing the radius by sqrt(numSplit) makes the
|
||||||
//asteroids. Dividing the radius by sqrt(numSplit) makes the
|
* sum of the areas taken up by the smaller asteroids equal to
|
||||||
//sum of the areas taken up by the smaller asteroids equal to
|
* the area of this asteroid. Each smaller asteroid has one
|
||||||
//the area of this asteroid. Each smaller asteroid has one
|
* less hit left before being completely destroyed.
|
||||||
//less hit left before being completely destroyed.
|
*/
|
||||||
return new Asteroid(mX,mY, minVelocity, maxVelocity, (int) (mRadius/Math.sqrt(mNumberSplit)), mNumberSplit, mHitsLeft-1, mGame);
|
return new Asteroid(mX,mY, minVelocity, maxVelocity, (int) (mRadius/Math.sqrt(mNumberSplit)), mNumberSplit, mHitsLeft-1, mGame);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -165,24 +185,4 @@ public class Asteroid extends MovingSpaceObject implements Collider, Drawable {
|
|||||||
else if (mY > scrnWidth + mRadius)
|
else if (mY > scrnWidth + mRadius)
|
||||||
mY -= scrnWidth + 2 * 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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -95,9 +95,10 @@ public class AsteroidGame extends Thread {
|
|||||||
* Pauses the game
|
* Pauses the game
|
||||||
* @author ricky barrette
|
* @author ricky barrette
|
||||||
*/
|
*/
|
||||||
public synchronized void pause(){
|
public synchronized void pauseGame(){
|
||||||
isStarted = false;
|
isStarted = false;
|
||||||
mGameFrame.setDisplayText("Paused");
|
mGameFrame.setDisplayText("Paused");
|
||||||
|
setMovingSpaceObjectsEnabled(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -124,7 +125,10 @@ public class AsteroidGame extends Thread {
|
|||||||
while (true){
|
while (true){
|
||||||
if(isStarted) {
|
if(isStarted) {
|
||||||
|
|
||||||
mGameFrame.repaintDisplay();
|
/*
|
||||||
|
* update the display and stats
|
||||||
|
*/
|
||||||
|
mGameFrame.repaintDispaly();
|
||||||
mGameFrame.getStatusBar().updateStatus();
|
mGameFrame.getStatusBar().updateStatus();
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -176,9 +180,9 @@ public class AsteroidGame extends Thread {
|
|||||||
mGameFrame.getStatusBar().setShipCount(mGameFrame.getStatusBar().getShipCount() -1);
|
mGameFrame.getStatusBar().setShipCount(mGameFrame.getStatusBar().getShipCount() -1);
|
||||||
|
|
||||||
if(mGameFrame.getStatusBar().getShipCount() > 0){
|
if(mGameFrame.getStatusBar().getShipCount() > 0){
|
||||||
pause();
|
pauseGame();
|
||||||
mWorld.add(new Ship(100,100,0,.35,.98,.4,1));
|
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 {
|
} else {
|
||||||
mGameFrame.setDisplayText("Game Over");
|
mGameFrame.setDisplayText("Game Over");
|
||||||
}
|
}
|
||||||
@@ -199,8 +203,18 @@ public class AsteroidGame extends Thread {
|
|||||||
*/
|
*/
|
||||||
public synchronized void startGame(){
|
public synchronized void startGame(){
|
||||||
mGameFrame.setDisplayText(null);
|
mGameFrame.setDisplayText(null);
|
||||||
mGameFrame.setMovingSpaceObjectsEnabled(true);
|
setMovingSpaceObjectsEnabled(true);
|
||||||
isStarted = 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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -61,18 +61,22 @@ public class Display extends JPanel {
|
|||||||
|
|
||||||
if(mText != null){
|
if(mText != null){
|
||||||
g.setColor(Color.ORANGE);
|
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
|
* Move & Draw the world's objects
|
||||||
*/
|
*/
|
||||||
Object item;
|
Object item;
|
||||||
|
MovingSpaceObject mso;
|
||||||
for (int i = 0; i < mGame.getWorld().size(); i++) {
|
for (int i = 0; i < mGame.getWorld().size(); i++) {
|
||||||
item = mGame.getWorld().get(i);
|
item = mGame.getWorld().get(i);
|
||||||
|
|
||||||
if (item instanceof MovingSpaceObject)
|
if (item instanceof MovingSpaceObject){
|
||||||
((MovingSpaceObject) item).move(getHeight(), getWidth());
|
mso = (MovingSpaceObject) item;
|
||||||
|
if(mso.isActive)
|
||||||
|
mso.move(getHeight(), getWidth());
|
||||||
|
}
|
||||||
|
|
||||||
if(item instanceof Drawable)
|
if(item instanceof Drawable)
|
||||||
((Drawable) item).draw(g);
|
((Drawable) item).draw(g);
|
||||||
|
|||||||
@@ -33,31 +33,20 @@ import javax.swing.JMenuBar;
|
|||||||
import javax.swing.JMenuItem;
|
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
|
* @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 static final long serialVersionUID = -2051298505681885632L;
|
||||||
|
|
||||||
private JMenuBar mMenuBar;
|
|
||||||
private JMenu mMenu;
|
|
||||||
private JMenuItem mMenuNewGame;
|
|
||||||
private JMenuItem mMenuQuit;
|
|
||||||
private Status mStatusBar;
|
private Status mStatusBar;
|
||||||
private Display mDisplay;
|
private Display mDisplay;
|
||||||
private Container mContainer;
|
|
||||||
private FlowLayout mLayout;
|
|
||||||
private menuListener xlistener;
|
|
||||||
private AsteroidGame mGame;
|
private AsteroidGame mGame;
|
||||||
|
|
||||||
private JMenuItem mMenuStartGame;
|
|
||||||
|
|
||||||
private JMenuItem mMenuPauseGame;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new GameFrame
|
* Creates a new GameFrame
|
||||||
*
|
|
||||||
* @param g
|
* @param g
|
||||||
* @author ricky barrette
|
* @author ricky barrette
|
||||||
*/
|
*/
|
||||||
@@ -65,39 +54,40 @@ public class GameFrame extends JFrame implements KeyListener{
|
|||||||
super("ASTEROIDS");
|
super("ASTEROIDS");
|
||||||
mGame = g;
|
mGame = g;
|
||||||
|
|
||||||
mMenuBar = new JMenuBar();
|
/*
|
||||||
setJMenuBar(mMenuBar);
|
* set up the game's menus
|
||||||
|
*/
|
||||||
|
JMenuBar menuBar = new JMenuBar();
|
||||||
|
setJMenuBar(menuBar);
|
||||||
|
|
||||||
mMenu = new JMenu("File");
|
/*
|
||||||
|
* 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");
|
||||||
|
|
||||||
mMenuNewGame = new JMenuItem("New Game");
|
fileMenu.add(menuNewGame);
|
||||||
mMenuStartGame = new JMenuItem("Start");
|
fileMenu.addSeparator();
|
||||||
mMenuPauseGame = new JMenuItem("Pause");
|
fileMenu.add(menuStartGame);
|
||||||
mMenuQuit = new JMenuItem("Quit");
|
fileMenu.add(menuPauseGame);
|
||||||
|
fileMenu.addSeparator();
|
||||||
|
fileMenu.add(menuQuit);
|
||||||
|
menuNewGame.addActionListener(this);
|
||||||
|
menuQuit.addActionListener(this);
|
||||||
|
menuStartGame.addActionListener(this);
|
||||||
|
menuPauseGame.addActionListener(this);
|
||||||
|
|
||||||
mMenu.add(mMenuNewGame);
|
menuBar.add(fileMenu);
|
||||||
mMenu.addSeparator();
|
|
||||||
mMenu.add(mMenuStartGame);
|
|
||||||
mMenu.add(mMenuPauseGame);
|
|
||||||
mMenu.addSeparator();
|
|
||||||
mMenu.add(mMenuQuit);
|
|
||||||
|
|
||||||
mMenuBar.add(mMenu);
|
FlowLayout layout = new FlowLayout();
|
||||||
|
layout.setAlignment(FlowLayout.LEFT);
|
||||||
|
|
||||||
mLayout = new FlowLayout();
|
Container container = getContentPane();
|
||||||
mLayout.setAlignment(FlowLayout.LEFT);
|
mStatusBar = new Status(container, mGame);
|
||||||
|
mDisplay = new Display(container, mGame);
|
||||||
|
|
||||||
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);
|
|
||||||
|
|
||||||
addKeyListener(this);
|
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)
|
* @return the height of the game display panel
|
||||||
* @see java.awt.Component#repaint()
|
* @author ricky barrette
|
||||||
*/
|
*/
|
||||||
@Override
|
public int getDispalyHeight() {
|
||||||
public void repaint() {
|
return mDisplay.getHeight();
|
||||||
mDisplay.repaint();
|
|
||||||
super.repaint();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private class menuListener implements ActionListener {
|
/**
|
||||||
|
* @return the width of the game dispaly panel
|
||||||
public void actionPerformed(ActionEvent e) {
|
* @author ricky barrette
|
||||||
if (e.getActionCommand().equals("New Game")) {
|
*/
|
||||||
mGame.newGame();
|
public int getDisplayWidth() {
|
||||||
mDisplay.repaint();
|
return mDisplay.getWidth();
|
||||||
|
}
|
||||||
}
|
|
||||||
if (e.getActionCommand().equals("Start")) {
|
|
||||||
mGame.startGame();
|
|
||||||
}
|
|
||||||
if (e.getActionCommand().equals("Pause")) {
|
|
||||||
mGame.pause();
|
|
||||||
}
|
|
||||||
if (e.getActionCommand().equals("Quit"))
|
|
||||||
System.exit(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the mStatusBar
|
||||||
|
*/
|
||||||
|
public Status getStatusBar() {
|
||||||
|
return mStatusBar;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -272,43 +274,31 @@ public class GameFrame extends JFrame implements KeyListener{
|
|||||||
@Override
|
@Override
|
||||||
public void keyTyped(KeyEvent e) {
|
public void keyTyped(KeyEvent e) {
|
||||||
// TODO Auto-generated method stub
|
// 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
|
* @author ricky barrette
|
||||||
*/
|
*/
|
||||||
public int getDispalyHeight() {
|
public void repaintDispaly() {
|
||||||
return mDisplay.getHeight();
|
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
|
* @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) {
|
public void setDisplayText(String string) {
|
||||||
mDisplay.setDisplayText(string);
|
mDisplay.setDisplayText(string);
|
||||||
this.repaint();
|
this.repaint();
|
||||||
|
|||||||
@@ -27,12 +27,12 @@ package com.RickBarrette.asteroids;
|
|||||||
public class MovingSpaceObject extends SpaceObject implements Moveable{
|
public class MovingSpaceObject extends SpaceObject implements Moveable{
|
||||||
|
|
||||||
protected double mAngle;
|
protected double mAngle;
|
||||||
protected double mVelocityDecay;
|
protected double mVelocityDecay = 1;
|
||||||
protected double mRotationalSpeed;
|
protected double mRotationalSpeed;
|
||||||
protected double mXVelocity = 0;
|
protected double mXVelocity = 0;
|
||||||
protected double mYVelocity = 0;
|
protected double mYVelocity = 0;
|
||||||
protected double mAcceleration;
|
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
|
* @return true if the space object is accelerating
|
||||||
|
|||||||
@@ -46,11 +46,8 @@ public class Shot extends MovingSpaceObject implements Drawable {
|
|||||||
mAcceleration = SPEED;
|
mAcceleration = SPEED;
|
||||||
mColor = Color.WHITE;
|
mColor = Color.WHITE;
|
||||||
mGame = game;
|
mGame = game;
|
||||||
isActive = true;
|
|
||||||
mXVelocity = SPEED*Math.cos(angle)+shipXVel;
|
mXVelocity = SPEED*Math.cos(angle)+shipXVel;
|
||||||
mYVelocity = SPEED*Math.sin(angle)+shipYVel;
|
mYVelocity = SPEED*Math.sin(angle)+shipYVel;
|
||||||
mVelocityDecay = 1;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
Reference in New Issue
Block a user