Added Shot

Currently you can shoot balls of light, but they dont do anything yet

Signed-off-by: Ricky Barrette <rickbarrette@gmail.com>
This commit is contained in:
2012-04-02 02:19:16 -04:00
parent c54e39b01c
commit 1eaca0998a
7 changed files with 203 additions and 86 deletions

View File

@@ -22,7 +22,6 @@ package com.RickBarrette.asteroids;
import java.util.Vector; import java.util.Vector;
/** /**
* This class maintain's the game logic. It is the main driver * This class maintain's the game logic. It is the main driver
* @author ricky barrette * @author ricky barrette
@@ -48,8 +47,10 @@ public class AsteroidGame extends Thread {
* @param add * @param add
* @author ricky barrette * @author ricky barrette
*/ */
public void addElement(Object add) { public void addElement(Object o) {
mWorld.addElement(add); if(o instanceof Shot)
mGameFrame.getStatusBar().setShotCount(mGameFrame.getStatusBar().getShotCount()+1);
mWorld.addElement(o);
} }
/** /**
@@ -58,7 +59,7 @@ public class AsteroidGame extends Thread {
*/ */
public void createGame() { public void createGame() {
mWorld = new Vector<Object>(); mWorld = new Vector<Object>();
mWorld.add(new Ship(500,500,0,.35,.98,.4,1)); mWorld.add(new Ship(100,100,0,.35,.98,.4,1));
} }
public Vector<Object> getWorld() { public Vector<Object> getWorld() {
@@ -96,6 +97,8 @@ public class AsteroidGame extends Thread {
* @author ricky barrette * @author ricky barrette
*/ */
public void removeElement(Object o) { public void removeElement(Object o) {
if(o instanceof Shot)
mGameFrame.getStatusBar().setShotCount(mGameFrame.getStatusBar().getShotCount()-1);
mWorld.removeElement(o); mWorld.removeElement(o);
} }
@@ -107,22 +110,9 @@ public class AsteroidGame extends Thread {
@Override @Override
public void run() { public void run() {
while(isStarted) { while(isStarted) {
// if (!(mWorld.isEmpty())) {
//
// /*
// * handle the ship
// */
// for (Object item : mWorld) {
// if(item instanceof Ship){
// Ship ship = (Ship) item;
//
//
// }
//
//
// }
// }
mGameFrame.repaintDisplay(); mGameFrame.repaintDisplay();
mGameFrame.getStatusBar().updateStatus();
/* /*
* sleep till next time * sleep till next time
@@ -149,6 +139,9 @@ public class AsteroidGame extends Thread {
*/ */
@Override @Override
public synchronized void start(){ public synchronized void start(){
mGameFrame.setMovingSpaceObjectsEnabled(true);
isStarted = true; isStarted = true;
super.start(); super.start();
} }

View File

@@ -59,14 +59,17 @@ public class Display extends JPanel {
super.paintComponent(g); super.paintComponent(g);
/* /*
* Draw the world's objects * Move & Draw the world's objects
*/ */
for (Object item : mGame.getWorld()) { Object item;
if (item instanceof Ship) { for (int i = 0; i < mGame.getWorld().size(); i++) {
Ship s = (Ship) (item); item = mGame.getWorld().get(i);
s.move(getHeight(), getWidth());
s.draw(g); if (item instanceof MovingSpaceObject)
} ((MovingSpaceObject) item).move(getHeight(), getWidth());
if(item instanceof Drawable)
((Drawable) item).draw(g);
} }
} }
} }

View File

@@ -40,16 +40,16 @@ public class GameFrame extends JFrame implements KeyListener{
private static final long serialVersionUID = -2051298505681885632L; private static final long serialVersionUID = -2051298505681885632L;
private JMenuBar bar; private JMenuBar mMenuBar;
private JMenu menu; private JMenu mMenu;
private JMenuItem newGame; private JMenuItem mMenuNewGame;
private JMenuItem quit; private JMenuItem mMenuQuit;
private Status statusBar; private Status mStatusBar;
private Display mDisplay; private Display mDisplay;
private Container container; private Container mContainer;
private FlowLayout layout; private FlowLayout mLayout;
private menuListener xlistener; private menuListener xlistener;
private AsteroidGame game; private AsteroidGame mGame;
/** /**
* Creates a new GameFrame * Creates a new GameFrame
@@ -59,50 +59,45 @@ public class GameFrame extends JFrame implements KeyListener{
*/ */
public GameFrame(AsteroidGame g) { public GameFrame(AsteroidGame g) {
super("ASTEROIDS"); super("ASTEROIDS");
game = g; mGame = g;
bar = new JMenuBar(); mMenuBar = new JMenuBar();
setJMenuBar(bar); setJMenuBar(mMenuBar);
menu = new JMenu("File"); mMenu = new JMenu("File");
newGame = new JMenuItem("New Game"); mMenuNewGame = new JMenuItem("New Game");
quit = new JMenuItem("Quit"); mMenuQuit = new JMenuItem("Quit");
menu.add(newGame); mMenu.add(mMenuNewGame);
menu.addSeparator(); mMenu.addSeparator();
menu.add(quit); mMenu.add(mMenuQuit);
bar.add(menu); mMenuBar.add(mMenu);
layout = new FlowLayout(); mLayout = new FlowLayout();
layout.setAlignment(FlowLayout.LEFT); mLayout.setAlignment(FlowLayout.LEFT);
container = getContentPane(); mContainer = getContentPane();
statusBar = new Status(container, game); mStatusBar = new Status(mContainer, mGame);
mDisplay = new Display(container, game); mDisplay = new Display(mContainer, mGame);
xlistener = new menuListener(); xlistener = new menuListener();
newGame.addActionListener(xlistener); mMenuNewGame.addActionListener(xlistener);
quit.addActionListener(xlistener); mMenuQuit.addActionListener(xlistener);
addKeyListener(this); addKeyListener(this);
setDefaultCloseOperation(EXIT_ON_CLOSE); setDefaultCloseOperation(EXIT_ON_CLOSE);
// sets up window's location and sets size**** // sets up window's location and sets size****
// setLocation(0, 0); // the default location
setSize(1000, 800); setSize(1000, 800);
setVisible(true); setVisible(true);
} }
public void setStatus(String temp) {
statusBar.setStatus(temp);
}
public void repaintDisplay() { public void repaintDisplay() {
mDisplay.repaint(); mDisplay.repaint();
} }
@@ -111,7 +106,7 @@ public class GameFrame extends JFrame implements KeyListener{
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("New Game")) { if (e.getActionCommand().equals("New Game")) {
game.newGame(); mGame.newGame();
mDisplay.repaint(); mDisplay.repaint();
} }
@@ -128,8 +123,9 @@ public class GameFrame extends JFrame implements KeyListener{
*/ */
@Override @Override
public void keyPressed(KeyEvent e) { public void keyPressed(KeyEvent e) {
for (Object item : game.getWorld()) { Object item;
for (int i = 0; i < mGame.getWorld().size(); i++) {
item = mGame.getWorld().get(i);
if (item instanceof Ship) { if (item instanceof Ship) {
Ship ship = (Ship) item; Ship ship = (Ship) item;
switch (e.getKeyCode()) { switch (e.getKeyCode()) {
@@ -147,6 +143,7 @@ public class GameFrame extends JFrame implements KeyListener{
* [Space] Pew Pew Pew!!! * [Space] Pew Pew Pew!!!
*/ */
case KeyEvent.VK_SPACE: case KeyEvent.VK_SPACE:
mGame.addElement(new Shot(ship.getX(), ship.getY(), ship.getAngle(), ship.getXVelocity(), ship.getYVelocity(), mGame));
break; break;
/* /*
@@ -199,7 +196,7 @@ public class GameFrame extends JFrame implements KeyListener{
*/ */
@Override @Override
public void keyReleased(KeyEvent e) { public void keyReleased(KeyEvent e) {
for (Object item : game.getWorld()) { for (Object item : mGame.getWorld()) {
if (item instanceof Ship) { if (item instanceof Ship) {
Ship ship = (Ship) item; Ship ship = (Ship) item;
@@ -266,4 +263,23 @@ public class GameFrame extends JFrame implements KeyListener{
public int getDisplayWidth() { public int getDisplayWidth() {
return mDisplay.getWidth(); 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;
}
} }

View File

@@ -26,7 +26,7 @@ package com.RickBarrette.asteroids;
*/ */
public class Main { public class Main {
public static final boolean DEBUG = true; public static final boolean DEBUG = false;
/** /**
* Creates a new Main * Creates a new Main

View File

@@ -65,9 +65,10 @@ public class Ship extends MovingSpaceObject implements Drawable {
this.mAcceleration = acceleration; this.mAcceleration = acceleration;
this.mVelocityDecay = velocityDecay; this.mVelocityDecay = velocityDecay;
this.mRotationalSpeed = rotationalSpeed; this.mRotationalSpeed = rotationalSpeed;
this.mColor = Color.CYAN;
// start off paused // start off paused
this.isActive = true; this.isActive = false;
// # of frames between shots // # of frames between shots
this.shotDelay = shotDelay; this.shotDelay = shotDelay;
@@ -76,6 +77,18 @@ public class Ship extends MovingSpaceObject implements Drawable {
this.shotDelayLeft = 0; this.shotDelayLeft = 0;
} }
/**
* @return true if the ship can shoot
* @author ricky barrette
*/
public boolean canShoot() {
if (shotDelayLeft > 0) // checks to see if the ship is ready to
return false;
// shoot again yet or if it needs to wait longer
else
return true;
}
/** /**
* Called by the Display panel when it needs to draw the ship * Called by the Display panel when it needs to draw the ship
* (non-Javadoc) * (non-Javadoc)
@@ -106,7 +119,7 @@ public class Ship extends MovingSpaceObject implements Drawable {
} }
if (isActive) if (isActive)
g.setColor(Color.CYAN); g.setColor(mColor);
else else
// draw the ship dark gray if the game is paused // draw the ship dark gray if the game is paused
g.setColor(Color.darkGray); g.setColor(Color.darkGray);
@@ -114,6 +127,22 @@ public class Ship extends MovingSpaceObject implements Drawable {
g.fillPolygon(mXpoints, mYpoints, 4); // 4 is the number of points g.fillPolygon(mXpoints, mYpoints, 4); // 4 is the number of points
} }
/**
* @return the ship's current angle
* @author ricky barrette
*/
public double getAngle() {
return mAngle;
}
/**
* @return radius of circle that approximates the ship
* @author ricky barrette
*/
public double getRadius() {
return radius;
}
/** /**
* Called when ... * Called when ...
* (non-Javadoc) * (non-Javadoc)
@@ -131,23 +160,11 @@ public class Ship extends MovingSpaceObject implements Drawable {
super.move(scrnWidth, scrnHeight); super.move(scrnWidth, scrnHeight);
} }
/** public double getXVelocity() {
* @return radius of circle that approximates the ship return this.xVelocity;
* @author ricky barrette
*/
public double getRadius() {
return radius;
} }
/** public double getYVelocity() {
* @return true if the ship can shoot return this.yVelocity;
* @author ricky barrette
*/
public boolean canShoot() {
if (shotDelayLeft > 0) // checks to see if the ship is ready to
return false;
// shoot again yet or if it needs to wait longer
else
return true;
} }
} }

View File

@@ -29,7 +29,7 @@ import java.awt.Color;
*/ */
public class SpaceObject { public class SpaceObject {
protected Color color; protected Color mColor;
protected double mX; protected double mX;
protected double mY; protected double mY;
@@ -38,7 +38,7 @@ public class SpaceObject {
* @author ricky barrette * @author ricky barrette
*/ */
public Color getColor() { public Color getColor() {
return this.color; return this.mColor;
} }
/** /**
@@ -63,7 +63,7 @@ public class SpaceObject {
* @author ricky barrette * @author ricky barrette
*/ */
public void setColor(Color c) { public void setColor(Color c) {
this.color = c; this.mColor = c;
} }
/** /**

View File

@@ -35,6 +35,12 @@ public class Status extends JPanel {
private static final long serialVersionUID = -169321993637429941L; private static final long serialVersionUID = -169321993637429941L;
private JLabel status; private JLabel status;
private StringBuffer mBuffer;
private int mShotCount = 0;
private int mAsteroidCount = 0;
private int mShipCount = 0;
private long mScore = 0;
private long mTime = 0;
/** /**
* Creates a new Status * Creates a new Status
@@ -47,6 +53,77 @@ public class Status extends JPanel {
status = new JLabel("Missiles 0 Asteroids 0 Ships 0 Score 0 Time: 0"); status = new JLabel("Missiles 0 Asteroids 0 Ships 0 Score 0 Time: 0");
northSubPanel.add(status); northSubPanel.add(status);
container.add(northSubPanel, BorderLayout.NORTH); container.add(northSubPanel, BorderLayout.NORTH);
mBuffer = new StringBuffer();
}
/**
* @return the mAsteroidCount
*/
public int getAsteroidCount() {
return mAsteroidCount;
}
/**
* @return the mScore
*/
public long getScore() {
return mScore;
}
/**
* @return the mShipCount
*/
public int getShipCount() {
return mShipCount;
}
/**
* @return the mShotCount
*/
public int getShotCount() {
return mShotCount;
}
/**
* @return the mTime
*/
public long getTime() {
return mTime;
}
/**
* @param mAsteroidCount the mAsteroidCount to set
*/
public void setAsteroidCount(int mAsteroidCount) {
this.mAsteroidCount = mAsteroidCount;
}
/**
* @param mScore the mScore to set
*/
public void setScore(long mScore) {
this.mScore = mScore;
}
/**
* @param mShipCount the mShipCount to set
*/
public void setShipCount(int mShipCount) {
this.mShipCount = mShipCount;
}
/**
* @param mShotCount the mShotCount to set
*/
public void setShotCount(int mShotCount) {
this.mShotCount = mShotCount;
}
/**
* @param mTime the mTime to set
*/
public void setTime(long mTime) {
this.mTime = mTime;
} }
/** /**
@@ -54,7 +131,18 @@ public class Status extends JPanel {
* @param temp * @param temp
* @author ricky barrette * @author ricky barrette
*/ */
public void setStatus(String temp) { public void updateStatus() {
status.setText(temp); mBuffer.append("Missiles ");
mBuffer.append(getShotCount());
mBuffer.append(" Asteroids ");
mBuffer.append(getAsteroidCount());
mBuffer.append(" Ships ");
mBuffer.append(getShipCount());
mBuffer.append(" Score ");
mBuffer.append(getScore());
mBuffer.append(" Time: ");
mBuffer.append(getTime());
status.setText(mBuffer.toString());
mBuffer = new StringBuffer();
} }
} }