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;
/**
* This class maintain's the game logic. It is the main driver
* @author ricky barrette
@@ -48,8 +47,10 @@ public class AsteroidGame extends Thread {
* @param add
* @author ricky barrette
*/
public void addElement(Object add) {
mWorld.addElement(add);
public void addElement(Object o) {
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() {
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() {
@@ -96,6 +97,8 @@ public class AsteroidGame extends Thread {
* @author ricky barrette
*/
public void removeElement(Object o) {
if(o instanceof Shot)
mGameFrame.getStatusBar().setShotCount(mGameFrame.getStatusBar().getShotCount()-1);
mWorld.removeElement(o);
}
@@ -107,23 +110,10 @@ public class AsteroidGame extends Thread {
@Override
public void run() {
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
*/
@@ -149,6 +139,9 @@ public class AsteroidGame extends Thread {
*/
@Override
public synchronized void start(){
mGameFrame.setMovingSpaceObjectsEnabled(true);
isStarted = true;
super.start();
}

View File

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

View File

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

View File

@@ -65,9 +65,10 @@ public class Ship extends MovingSpaceObject implements Drawable {
this.mAcceleration = acceleration;
this.mVelocityDecay = velocityDecay;
this.mRotationalSpeed = rotationalSpeed;
this.mColor = Color.CYAN;
// start off paused
this.isActive = true;
this.isActive = false;
// # of frames between shots
this.shotDelay = shotDelay;
@@ -76,6 +77,18 @@ public class Ship extends MovingSpaceObject implements Drawable {
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
* (non-Javadoc)
@@ -106,13 +119,29 @@ public class Ship extends MovingSpaceObject implements Drawable {
}
if (isActive)
g.setColor(Color.CYAN);
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
}
/**
* @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 ...
@@ -130,24 +159,12 @@ public class Ship extends MovingSpaceObject implements Drawable {
super.move(scrnWidth, scrnHeight);
}
/**
* @return radius of circle that approximates the ship
* @author ricky barrette
*/
public double getRadius() {
return radius;
public double getXVelocity() {
return this.xVelocity;
}
/**
* @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;
public double getYVelocity() {
return this.yVelocity;
}
}

View File

@@ -29,7 +29,7 @@ import java.awt.Color;
*/
public class SpaceObject {
protected Color color;
protected Color mColor;
protected double mX;
protected double mY;
@@ -38,7 +38,7 @@ public class SpaceObject {
* @author ricky barrette
*/
public Color getColor() {
return this.color;
return this.mColor;
}
/**
@@ -63,7 +63,7 @@ public class SpaceObject {
* @author ricky barrette
*/
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 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
@@ -47,6 +53,77 @@ public class Status extends JPanel {
status = new JLabel("Missiles 0 Asteroids 0 Ships 0 Score 0 Time: 0");
northSubPanel.add(status);
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
* @author ricky barrette
*/
public void setStatus(String temp) {
status.setText(temp);
public void updateStatus() {
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();
}
}