Updated conventions, cleaned up comments and code

Signed-off-by: Ricky Barrette <rickbarrette@gmail.com>
This commit is contained in:
2012-04-06 14:09:38 -04:00
parent 253bb9a302
commit db4cb9e5f1
13 changed files with 107 additions and 91 deletions

View File

@@ -26,14 +26,14 @@ 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 {
public class Asteroid extends MovingSpaceObject implements Collider {
private final int mNumberSplit;
private final int mHitsLeft;
private final int mRadius;
private final double mMinVelocity;
private final double mMaxVelocity;
private AsteroidGame mGame;
private final AsteroidGame mGame;
/**
* Creates a new Asteroid

View File

@@ -50,7 +50,7 @@ public class AsteroidGame extends Thread {
* @param add
* @author ricky barrette
*/
public synchronized void addElement(Object o) {
public synchronized void addElement(final Object o) {
mWorld.add(o);
}
@@ -102,7 +102,7 @@ public class AsteroidGame extends Thread {
* @param ship
* @author ricky barrette
*/
public void hyperJump(Ship ship) {
public void hyperJump(final Ship ship) {
// boolean isSafe = true;
Random gen = new Random();
// do{
@@ -162,6 +162,7 @@ public class AsteroidGame extends Thread {
startGame();
notification("Level "+ mGameFrame.getStatusBar().getLevel());
mGameFrame.repaintDispaly();
}
/**
@@ -201,7 +202,7 @@ public class AsteroidGame extends Thread {
* @param o object to be removed
* @author ricky barrette
*/
public synchronized void removeElement(Object o) {
public synchronized void removeElement(final Object o) {
if(o instanceof Asteroid) {
mGameFrame.getStatusBar().incrementScore(2);
}
@@ -305,7 +306,7 @@ public class AsteroidGame extends Thread {
* @param b
* @author ricky barrette
*/
public void setMovingSpaceObjectsEnabled(boolean b) {
public void setMovingSpaceObjectsEnabled(final boolean b) {
for(Object item : mWorld)
if(item instanceof MovingSpaceObject)
((MovingSpaceObject) item).setActive(b);

View File

@@ -26,6 +26,6 @@ package com.RickBarrette.asteroids;
*/
public interface Collider {
public boolean checkForCollision(Object o);
public boolean checkForCollision(final Object o);
}

View File

@@ -1,5 +1,5 @@
/**
java * Display.java
* Display.java
* @date Mar 31, 2012
* @author ricky barrette
*
@@ -35,10 +35,9 @@ import javax.swing.JPanel;
public class Display extends JPanel {
private static final long serialVersionUID = -9105117186423881937L;
private AsteroidGame mGame;
private Container mContainer;
private final AsteroidGame mGame;
private final Font mFont;
private String mText;
private Font mFont;
/**
* Creates a new Dispay
@@ -46,11 +45,10 @@ public class Display extends JPanel {
* @param g
* @author ricky barrette
*/
public Display(Container c, AsteroidGame g) {
public Display(final Container c, final AsteroidGame g) {
mGame = g;
mContainer = c;
this.setBackground(new Color(0, 0, 0));
mContainer.add(this, BorderLayout.CENTER);
c.add(this, BorderLayout.CENTER);
int screenRes = Toolkit.getDefaultToolkit().getScreenResolution();
int fontSize = (int)Math.round(14.0 * screenRes / 72.0);
mFont = new Font("Arial", Font.PLAIN, fontSize);
@@ -61,10 +59,9 @@ public class Display extends JPanel {
* @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
*/
@Override
public void paintComponent(Graphics g) {
public void paintComponent(final Graphics g) {
super.paintComponent(g);
/*
* Move & Draw the world's objects
*/
@@ -93,7 +90,12 @@ public class Display extends JPanel {
}
}
public void setDisplayText(String string) {
/**
* Sets the text to be displayed in the center of the game's display
* @param string
* @author ricky barrette
*/
public void setDisplayText(final String string) {
mText = string;
}
}

View File

@@ -27,6 +27,6 @@ import java.awt.Graphics;
*/
public interface Drawable {
public void draw(Graphics g);
public void draw(final Graphics g);
}

View File

@@ -40,16 +40,16 @@ public class GameFrame extends JFrame implements KeyListener, ActionListener{
private static final long serialVersionUID = -2051298505681885632L;
private Status mStatusBar;
private Display mDisplay;
private AsteroidGame mGame;
private final Status mStatusBar;
private final Display mDisplay;
private final AsteroidGame mGame;
/**
* Creates a new GameFrame
* @param g
* @author ricky barrette
*/
public GameFrame(AsteroidGame g) {
public GameFrame(final AsteroidGame g) {
super("ASTEROIDS");
mGame = g;
@@ -103,10 +103,9 @@ public class GameFrame extends JFrame implements KeyListener, ActionListener{
* (non-Javadoc)
* @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
*/
public void actionPerformed(ActionEvent e) {
public void actionPerformed(final ActionEvent e) {
if (e.getActionCommand().equals("New Game")) {
mGame.newGame();
mDisplay.repaint();
}
if (e.getActionCommand().equals("Start")) {
@@ -127,7 +126,7 @@ public class GameFrame extends JFrame implements KeyListener, ActionListener{
* @param isKeyPressed
* @author ricky barrette
*/
private void driveShip(KeyEvent e, boolean isKeyPressed) {
private void driveShip(final KeyEvent e, final boolean isKeyPressed) {
Ship ship = null;
//get the user's ship
for (Object item : mGame.getWorld()) {
@@ -223,7 +222,7 @@ public class GameFrame extends JFrame implements KeyListener, ActionListener{
* @see java.awt.event.KeyListener#keyPressed(java.awt.event.KeyEvent)
*/
@Override
public void keyPressed(KeyEvent e) {
public void keyPressed(final KeyEvent e) {
switch(e.getKeyCode()){
/*
@@ -247,12 +246,12 @@ public class GameFrame extends JFrame implements KeyListener, ActionListener{
* @see java.awt.event.KeyListener#keyReleased(java.awt.event.KeyEvent)
*/
@Override
public void keyReleased(KeyEvent e) {
public void keyReleased(final KeyEvent e) {
driveShip(e, false);
}
@Override
public void keyTyped(KeyEvent e) {
public void keyTyped(final KeyEvent e) {
}
/**
@@ -278,7 +277,7 @@ public class GameFrame extends JFrame implements KeyListener, ActionListener{
* @param string to be displayed
* @author ricky barrette
*/
public void setDisplayText(String string) {
public void setDisplayText(final String string) {
mDisplay.setDisplayText(string);
this.repaint();
}

View File

@@ -27,14 +27,18 @@ package com.RickBarrette.asteroids;
public class Main {
public static final boolean DEBUG = false;
private static final AsteroidGame mGame;
static {
mGame = new AsteroidGame();
}
/**
* Creates a new Main
* @author ricky barrette
*/
public Main() {
AsteroidGame game = new AsteroidGame();
game.newGame();
mGame.newGame();
}
/**

View File

@@ -25,11 +25,11 @@ package com.RickBarrette.asteroids;
*/
public interface Moveable {
public void move(int scrnWidth, int scrnHeight);
public void move(final int scrnWidth, final int scrnHeight);
public void setAccelerating(boolean accelerating);
public void setAccelerating(final boolean accelerating);
public void setTurningLeft(boolean turningLeft);
public void setTurningLeft(final boolean turningLeft);
public void setTurningRight(boolean turningRight);
public void setTurningRight(final boolean turningRight);
}

View File

@@ -23,7 +23,7 @@ package com.RickBarrette.asteroids;
* This class will track the information required for moving objects.
* @author ricky barrette
*/
public class MovingSpaceObject extends SpaceObject implements Moveable{
public abstract class MovingSpaceObject extends SpaceObject implements Moveable{
protected double mAngle;
protected double mVelocityDecay = 1;
@@ -83,7 +83,7 @@ public class MovingSpaceObject extends SpaceObject implements Moveable{
* @see com.RickBarrette.asteroids.Moveable#move(int, int)
*/
@Override
public void move(int scrnWidth, int scrnHeight) {
public void move(final int scrnWidth, final int scrnHeight) {
if(isActive){
/*
* this is backwards from typical polar coordinates
@@ -135,7 +135,7 @@ public class MovingSpaceObject extends SpaceObject implements Moveable{
* @see com.RickBarrette.asteroids.Moveable#setAccelerating(boolean)
*/
@Override
public void setAccelerating(boolean accelerating) {
public void setAccelerating(final boolean accelerating) {
this.isAccelerating = accelerating; // start or stop accelerating the ship
}
@@ -144,7 +144,7 @@ public class MovingSpaceObject extends SpaceObject implements Moveable{
* @param active
* @author ricky barrette
*/
public void setActive(boolean active) {
public void setActive(final boolean active) {
this.isActive = active; // used when the game is paused or unpaused
}
@@ -154,7 +154,7 @@ public class MovingSpaceObject extends SpaceObject implements Moveable{
* @see com.RickBarrette.asteroids.Moveable#setTurningLeft(boolean)
*/
@Override
public void setTurningLeft(boolean turningLeft) {
public void setTurningLeft(final boolean turningLeft) {
this.isTurningLeft = turningLeft; // start or stop turning the ship
}
@@ -164,7 +164,7 @@ public class MovingSpaceObject extends SpaceObject implements Moveable{
* @see com.RickBarrette.asteroids.Moveable#setTurningRight(boolean)
*/
@Override
public void setTurningRight(boolean turningRight) {
public void setTurningRight(final boolean turningRight) {
this.isTurningRight = turningRight;
}
@@ -173,7 +173,7 @@ public class MovingSpaceObject extends SpaceObject implements Moveable{
* when it goes out of the screen's bounds
* @author ricky barrette
*/
public void wrapSpace(int scrnHeight, int scrnWidth) {
public void wrapSpace(final int scrnHeight, final int scrnWidth) {
if (mX < 0)
mX += scrnHeight;
else if (mX > scrnHeight)

View File

@@ -26,21 +26,17 @@ import java.awt.Graphics;
* This class will be the user's ship. I will be used to destroy the asteroids, with it's laser! Pew Pew Pew!!!
* @author ricky barrette
*/
public class Ship extends MovingSpaceObject implements Drawable {
public class Ship extends MovingSpaceObject {
private final int[] mOrigXpoints = { 14, -10, -6, -10 };
private final int[] mOrigYpoints = { 0, -8, 0, 8 };
private final int[] mOrigFlameXpoints = { -6, -23, -6 };
private final int[] mOrigFlameYpoints = { -3, 0, 3 };
private final int shotDelay;
private int[] mXpoints = new int[4], mYpoints = new int[4], mFlameXpoints = new int[3], mFlameYpoints = new int[3];
private int shotDelayLeft;
/*
* store the current locations of the points used to draw the ship and its
* flame
*/
int[] mXpoints = new int[4], mYpoints = new int[4], mFlameXpoints = new int[3], mFlameYpoints = new int[3];
int shotDelay, shotDelayLeft; // used to determine the rate of firing
/*
/**
* radius of circle used to approximate the ship
*/
private final int mRadius = 6;
@@ -56,8 +52,7 @@ public class Ship extends MovingSpaceObject implements Drawable {
* @param shotDelay of the ship
* @author ricky barrette
*/
public Ship(double x, double y, double angle, double acceleration, double velocityDecay, double rotationalSpeed, int shotDelay) {
// this.x refers to the Ship's x, x refers to the x parameter
public Ship(final double x, final double y, final double angle, final double acceleration, final double velocityDecay, final double rotationalSpeed, final int shotDelay) {
this.mX = x;
this.mY = y;
this.mAngle = angle;
@@ -81,9 +76,11 @@ public class Ship extends MovingSpaceObject implements Drawable {
* @author ricky barrette
*/
public boolean canShoot() {
if (shotDelayLeft > 0) // checks to see if the ship is ready to
/*
* check if the shot delay has been satifiyed
*/
if (shotDelayLeft > 0)
return false;
// shoot again yet or if it needs to wait longer
else
return true;
}
@@ -94,36 +91,50 @@ public class Ship extends MovingSpaceObject implements Drawable {
* @see com.RickBarrette.asteroids.Drawable#draw(java.awt.Graphics)
*/
@Override
public void draw(Graphics g) {
public void draw(final Graphics g) {
if(Main.DEBUG)
System.out.println("draw()"+ mX + ", "+ mY);
// rotate the points, translate them to the ship's location (by
// adding x and y), then round them by adding .5 and casting them
// as integers (which truncates any decimal place)
if (isAccelerating && isActive) { // draw flame if accelerating
/*
* rotate the points, translate them to the ship's location (by
* adding x and y), then round them by adding .5 and casting them
* as integers (which truncates any decimal place)
*/
/*
* draw the ship's flame, if accelerating
*/
if (isAccelerating && isActive) {
for (int i = 0; i < 3; i++) {
mFlameXpoints[i] = (int) (mOrigFlameXpoints[i] * Math.cos(mAngle) - mOrigFlameYpoints[i] * Math.sin(mAngle) + mX + .5);
mFlameYpoints[i] = (int) (mOrigFlameXpoints[i] * Math.sin(mAngle) + mOrigFlameYpoints[i] * Math.cos(mAngle) + mY + .5);
}
g.setColor(Color.red); // set color of flame
/*
* draw the flame
*/
g.setColor(Color.red);
g.fillPolygon(mFlameXpoints, mFlameYpoints, 3);
}
// calculate the polygon for the ship, then draw it
/*
* calculate the polygon for the ship, then draw it
*/
for (int i = 0; i < 4; i++) {
this.mXpoints[i] = (int) (this.mOrigXpoints[i] * Math.cos(mAngle) - this.mOrigYpoints[i] * Math.sin(mAngle) + mX + .5);
this.mYpoints[i] = (int) (this.mOrigXpoints[i] * Math.sin(mAngle) + this.mOrigYpoints[i] * Math.cos(mAngle) + mY + .5);
}
/*
* draw the ship dark gray if the game is paused
*/
if (isActive)
g.setColor(mColor);
else
// draw the ship dark gray if the game is paused
g.setColor(Color.darkGray);
g.fillPolygon(mXpoints, mYpoints, 4); // 4 is the number of points
g.fillPolygon(mXpoints, mYpoints, 4);
}
/**
@@ -156,7 +167,7 @@ public class Ship extends MovingSpaceObject implements Drawable {
* @see com.RickBarrette.asteroids.MovingSpaceObject#move(int, int)
*/
@Override
public void move(int scrnWidth, int scrnHeight) {
public void move(final int scrnWidth, final int scrnHeight) {
/*
* move() is called every frame that the game
* is run, so this ticks down the shot delay

View File

@@ -26,19 +26,19 @@ import java.awt.Graphics;
* This class will bt the shots fired bt the ship. Their job is to destroy the asteroids
* @author ricky barrette
*/
public class Shot extends MovingSpaceObject implements Drawable {
public class Shot extends MovingSpaceObject {
public static final int TOTAL_DRAWS = 50;
public static final int SPEED = 10;
private final AsteroidGame mGame;;
private int mCount = 0;
private AsteroidGame mGame;;
/**
* Creates a new shot
* @author ricky barrette
* @param mGame
*/
public Shot(double x, double y, double angle, double shipXVel, double shipYVel, AsteroidGame game) {
public Shot(final double x, final double y, final double angle, final double shipXVel, final double shipYVel, final AsteroidGame game) {
mX = x;
mY = y;
mAngle = angle;

View File

@@ -26,7 +26,7 @@ import java.awt.Color;
* More accuractly the object's location and color.
* @author ricky barrette
*/
public class SpaceObject {
public abstract class SpaceObject implements Drawable{
protected Color mColor;
protected double mX;
@@ -61,7 +61,7 @@ public class SpaceObject {
* @param c
* @author ricky barrette
*/
public void setColor(Color c) {
public void setColor(final Color c) {
this.mColor = c;
}
@@ -71,7 +71,7 @@ public class SpaceObject {
* @param y1
* @author ricky barrette
*/
public void setLocation(int x, int y) {
public void setLocation(final int x, final int y) {
mX = x;
mY = y;
}

View File

@@ -33,7 +33,7 @@ import javax.swing.JPanel;
public class Status extends JPanel {
private static final long serialVersionUID = -169321993637429941L;
private JLabel status;
private final JLabel status;
private StringBuffer mBuffer;
private int mShotCount = 0;
private int mAsteroidCount = 0;
@@ -48,7 +48,7 @@ public class Status extends JPanel {
* @param g
* @author ricky barrette
*/
public Status(Container container, AsteroidGame g) {
public Status(final Container container, final AsteroidGame g) {
JPanel northSubPanel = new JPanel();
status = new JLabel("Missiles 0 Asteroids 0 Ships 0 Score 0 Time: 0");
northSubPanel.add(status);
@@ -79,7 +79,7 @@ public class Status extends JPanel {
* @param score
* @author ricky barrette
*/
public synchronized void decrementScore(int score) {
public synchronized void decrementScore(final int score) {
if(this.mScore - mScore > 0)
this.mScore -= mScore;
}
@@ -107,7 +107,7 @@ public class Status extends JPanel {
* @param mTime
* @author ricky barrette
*/
public synchronized void decrementTime(long mTime) {
public synchronized void decrementTime(final long mTime) {
if(this.mTime - mTime > 0)
this.mTime -= mTime;
}
@@ -175,7 +175,7 @@ public class Status extends JPanel {
* @param score
* @author ricky barrette
*/
public synchronized void incrementScore(int score) {
public synchronized void incrementScore(final int score) {
this.mScore += score;
}
@@ -200,7 +200,7 @@ public class Status extends JPanel {
* @param mTime
* @author ricky barrette
*/
public synchronized void incrementTime(long mTime) {
public synchronized void incrementTime(final long mTime) {
this.mTime += mTime;
}
@@ -210,7 +210,7 @@ public class Status extends JPanel {
* @return formated string
* @author ricky barrette
*/
private String padTime(int time){
private String padTime(final int time){
if (time <= 9)
return "0"+ time;
return ""+ time;
@@ -219,42 +219,42 @@ public class Status extends JPanel {
/**
* @param mAsteroidCount the mAsteroidCount to set
*/
public synchronized void setAsteroidCount(int mAsteroidCount) {
public synchronized void setAsteroidCount(final int mAsteroidCount) {
this.mAsteroidCount = mAsteroidCount;
}
/**
* @param level the level to set
*/
public void setLevel(int level) {
public void setLevel(final int level) {
this.mLevel = level;
}
/**
* @param mScore the mScore to set
*/
public synchronized void setScore(long mScore) {
public synchronized void setScore(final long mScore) {
this.mScore = mScore;
}
/**
* @param mShipCount the mShipCount to set
*/
public synchronized void setShipCount(int mShipCount) {
public synchronized void setShipCount(final int mShipCount) {
this.mShipCount = mShipCount;
}
/**
* @param mShotCount the mShotCount to set
*/
public synchronized void setShotCount(int mShotCount) {
public synchronized void setShotCount(final int mShotCount) {
this.mShotCount = mShotCount;
}
/**
* @param mTime the mTime to set
*/
public synchronized void setTime(long mTime) {
public synchronized void setTime(final long mTime) {
this.mTime = mTime;
}
@@ -264,12 +264,11 @@ public class Status extends JPanel {
* @return human readable hour : minutes format
* @author ricky barrette
*/
private String stringTime(long mills){
int hours = (int) (mills / 3600000);
mills = mills - (hours * 3600000);
int minutes = (int) ( mills / 60000);
int seconds = (int) (mills % 60000);
seconds = seconds / 1000;
private String stringTime(final long mills){
final int hours = (int) (mills / 3600000);
final long millsMinusHours = mills - (hours * 3600000);
final int minutes = (int) ( millsMinusHours / 60000);
final int seconds = ((int) (millsMinusHours % 60000)) / 1000;
return hours +" : "+ padTime(minutes) +" : "+ padTime(seconds);
}