diff --git a/Asteroids/.classpath b/Asteroids/.classpath index fb565a5..18d70f0 100644 --- a/Asteroids/.classpath +++ b/Asteroids/.classpath @@ -1,6 +1,6 @@ - + diff --git a/Asteroids/.settings/org.eclipse.jdt.core.prefs b/Asteroids/.settings/org.eclipse.jdt.core.prefs index 7341ab1..8000cd6 100644 --- a/Asteroids/.settings/org.eclipse.jdt.core.prefs +++ b/Asteroids/.settings/org.eclipse.jdt.core.prefs @@ -1,11 +1,11 @@ eclipse.preferences.version=1 org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled -org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6 org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve -org.eclipse.jdt.core.compiler.compliance=1.7 +org.eclipse.jdt.core.compiler.compliance=1.6 org.eclipse.jdt.core.compiler.debug.lineNumber=generate org.eclipse.jdt.core.compiler.debug.localVariable=generate org.eclipse.jdt.core.compiler.debug.sourceFile=generate org.eclipse.jdt.core.compiler.problem.assertIdentifier=error org.eclipse.jdt.core.compiler.problem.enumIdentifier=error -org.eclipse.jdt.core.compiler.source=1.7 +org.eclipse.jdt.core.compiler.source=1.6 diff --git a/Asteroids/src/com/RickBarrette/asteroids/Asteroid.java b/Asteroids/src/com/RickBarrette/asteroids/Asteroid.java index c45c479..c78e426 100644 --- a/Asteroids/src/com/RickBarrette/asteroids/Asteroid.java +++ b/Asteroids/src/com/RickBarrette/asteroids/Asteroid.java @@ -33,7 +33,7 @@ public class Asteroid extends MovingSpaceObject implements Collider { private final int mRadius; private final double mMinVelocity; private final double mMaxVelocity; - private final AsteroidGame mGame; + private final AsteroidGameThread mGame; /** * Creates a new Asteroid @@ -45,7 +45,7 @@ public class Asteroid extends MovingSpaceObject implements Collider { * @param hitsLeft number of hits left * @author ricky barrette */ - public Asteroid(double x, double y, double minVelocity, double maxVelocity, int radius, int numberSplit, int hitsLeft, AsteroidGame game) { + public Asteroid(double x, double y, double minVelocity, double maxVelocity, int radius, int numberSplit, int hitsLeft, AsteroidGameThread game) { mGame = game; mColor = new Color(66,33,0); mX = x; diff --git a/Asteroids/src/com/RickBarrette/asteroids/AsteroidGame.java b/Asteroids/src/com/RickBarrette/asteroids/AsteroidGameThread.java similarity index 73% rename from Asteroids/src/com/RickBarrette/asteroids/AsteroidGame.java rename to Asteroids/src/com/RickBarrette/asteroids/AsteroidGameThread.java index 6e945c5..95bbd34 100644 --- a/Asteroids/src/com/RickBarrette/asteroids/AsteroidGame.java +++ b/Asteroids/src/com/RickBarrette/asteroids/AsteroidGameThread.java @@ -26,11 +26,11 @@ import java.util.Random; * This class maintain's the game logic. It is the main driver * @author ricky barrette */ -public class AsteroidGame extends Thread { +public class AsteroidGameThread extends Thread { private static final int DELAY_IN_MSEC = 50; private final ArrayList mWorld; - private final GameFrame mGameFrame; + private final GameApplet mGameApplet; public boolean isStarted = false; private long mLastTime; @@ -38,8 +38,8 @@ public class AsteroidGame extends Thread { * Creates an new Asteroids game * @author ricky barrette */ - public AsteroidGame() { - mGameFrame = new GameFrame(this); + public AsteroidGameThread(GameApplet gameFrame) { + mGameApplet = gameFrame; mWorld = new ArrayList(); //TODO simulate game play unitll game ist started this.start(); @@ -76,25 +76,25 @@ public class AsteroidGame extends Thread { s.hyperJump(); } - mGameFrame.getStatusBar().decrementShipCount(); + mGameApplet.getStatusBar().decrementShipCount(); - if(mGameFrame.getStatusBar().getShipCount() > 0){ + if(mGameApplet.getStatusBar().getShipCount() > 0){ pauseGame(); - mGameFrame.setDisplayText("You died, You can hyper jump to a safe place now... Press start when ready."); + mGameApplet.setDisplayText("You died, You can hyper jump to a safe place now... Press start when ready."); } else { - mGameFrame.setDisplayText("Game Over"); + mGameApplet.setDisplayText("Game Over"); if(s != null) mWorld.remove(s); } - mGameFrame.repaint(); + mGameApplet.repaint(); } /** * @return the game's frame * @author ricky barrette */ - public GameFrame getGameFrame() { - return this.mGameFrame; + public GameApplet getGameFrame() { + return this.mGameApplet; } /** @@ -114,10 +114,10 @@ public class AsteroidGame extends Thread { /* * added a asteroid per level */ - for(int i = 0; i < mGameFrame.getStatusBar().getLevel(); i ++) - addElement(new Asteroid(gen.nextInt(mGameFrame.getDisplayWidth()), gen.nextInt(mGameFrame.getDispalyHeight()), 1, 10, 50, 3, 3, this)); + for(int i = 0; i < mGameApplet.getStatusBar().getLevel(); i ++) + addElement(new Asteroid(gen.nextInt(mGameApplet.getDisplayWidth()), gen.nextInt(mGameApplet.getDispalyHeight()), 1, 10, 50, 3, 3, this)); - notification("Level "+ mGameFrame.getStatusBar().getLevel()); + notification("Level "+ mGameApplet.getStatusBar().getLevel()); } /** @@ -135,23 +135,23 @@ public class AsteroidGame extends Thread { public void newGame() { Random gen = new Random(); mWorld.clear(); - mGameFrame.setDisplayText(null); + mGameApplet.setDisplayText(null); - mGameFrame.getStatusBar().setShipCount(3); - mGameFrame.getStatusBar().setScore(0); - mGameFrame.getStatusBar().setAsteroidCount(1); - mGameFrame.getStatusBar().setTime(0); - mGameFrame.getStatusBar().setShotCount(0); - mGameFrame.getStatusBar().setLevel(1); + mGameApplet.getStatusBar().setShipCount(3); + mGameApplet.getStatusBar().setScore(0); + mGameApplet.getStatusBar().setAsteroidCount(1); + mGameApplet.getStatusBar().setTime(0); + mGameApplet.getStatusBar().setShotCount(0); + mGameApplet.getStatusBar().setLevel(1); - mWorld.add(new Ship(gen.nextInt(mGameFrame.getDisplayWidth()), gen.nextInt(mGameFrame.getDispalyHeight()), 0, .35, .98, .2, 1, this)); + mWorld.add(new Ship(gen.nextInt(mGameApplet.getDisplayWidth()), gen.nextInt(mGameApplet.getDispalyHeight()), 0, .35, .98, .2, 1, this)); initLevel(); startGame(); - notification("Level "+ mGameFrame.getStatusBar().getLevel()); - mGameFrame.repaintDispaly(); + notification("Level "+ mGameApplet.getStatusBar().getLevel()); + mGameApplet.repaintDispaly(); } /** @@ -160,7 +160,7 @@ public class AsteroidGame extends Thread { * @author ricky barrette */ private void notification(final String string) { - mGameFrame.setDisplayText(string); + mGameApplet.setDisplayText(string); new Thread(new Runnable(){ @Override @@ -171,7 +171,7 @@ public class AsteroidGame extends Thread { e.printStackTrace(); } if(isStarted) - mGameFrame.setDisplayText(null); + mGameApplet.setDisplayText(null); } }).start(); } @@ -182,7 +182,7 @@ public class AsteroidGame extends Thread { */ public synchronized void pauseGame(){ isStarted = false; - mGameFrame.setDisplayText("Paused"); + mGameApplet.setDisplayText("Paused"); setMovingSpaceObjectsEnabled(false); } @@ -193,7 +193,7 @@ public class AsteroidGame extends Thread { */ public synchronized void removeElement(final Object o) { if(o instanceof Asteroid) { - mGameFrame.getStatusBar().incrementScore(2); + mGameApplet.getStatusBar().incrementScore(2); } mWorld.remove(o); } @@ -216,14 +216,14 @@ public class AsteroidGame extends Thread { /* * increment time */ - mGameFrame.getStatusBar().incrementTime(System.currentTimeMillis() - mLastTime); + mGameApplet.getStatusBar().incrementTime(System.currentTimeMillis() - mLastTime); mLastTime = System.currentTimeMillis(); /* * update the display and stats */ - mGameFrame.repaintDispaly(); - mGameFrame.getStatusBar().updateStatus(); + mGameApplet.repaintDispaly(); + mGameApplet.getStatusBar().updateStatus(); /* * check for collsions @@ -250,16 +250,16 @@ public class AsteroidGame extends Thread { * if there are no more asteroids, then increment the level */ if(asteroidCount == 0){ - mGameFrame.getStatusBar().incrementLevel(); + mGameApplet.getStatusBar().incrementLevel(); initLevel(); } /* * 1up every 200 points */ - if(mGameFrame.getStatusBar().getScore() > 0 && mGameFrame.getStatusBar().getScore() % 200 == 0){ + if(mGameApplet.getStatusBar().getScore() > 0 && mGameApplet.getStatusBar().getScore() % 200 == 0){ if(!hasOneUped){ - mGameFrame.getStatusBar().incrementShipCount(); + mGameApplet.getStatusBar().incrementShipCount(); hasOneUped = true; notification("1up!"); @@ -270,8 +270,8 @@ public class AsteroidGame extends Thread { /* * update the status bar with the new counts */ - mGameFrame.getStatusBar().setShotCount(shotCount); - mGameFrame.getStatusBar().setAsteroidCount(asteroidCount); + mGameApplet.getStatusBar().setShotCount(shotCount); + mGameApplet.getStatusBar().setAsteroidCount(asteroidCount); /* * reset counters @@ -315,7 +315,7 @@ public class AsteroidGame extends Thread { */ public synchronized void startGame(){ mLastTime = System.currentTimeMillis(); - mGameFrame.setDisplayText(null); + mGameApplet.setDisplayText(null); setMovingSpaceObjectsEnabled(true); isStarted = true; } diff --git a/Asteroids/src/com/RickBarrette/asteroids/Display.java b/Asteroids/src/com/RickBarrette/asteroids/Display.java index 4af3c70..a5106b9 100644 --- a/Asteroids/src/com/RickBarrette/asteroids/Display.java +++ b/Asteroids/src/com/RickBarrette/asteroids/Display.java @@ -35,7 +35,7 @@ import javax.swing.JPanel; public class Display extends JPanel { private static final long serialVersionUID = -9105117186423881937L; - private final AsteroidGame mGame; + private final AsteroidGameThread mGame; private final Font mFont; private String mText; @@ -45,7 +45,7 @@ public class Display extends JPanel { * @param g * @author ricky barrette */ - public Display(final Container c, final AsteroidGame g) { + public Display(final Container c, final AsteroidGameThread g) { mGame = g; this.setBackground(new Color(0, 0, 0)); c.add(this, BorderLayout.CENTER); diff --git a/Asteroids/src/com/RickBarrette/asteroids/GameFrame.java b/Asteroids/src/com/RickBarrette/asteroids/GameApplet.java similarity index 86% rename from Asteroids/src/com/RickBarrette/asteroids/GameFrame.java rename to Asteroids/src/com/RickBarrette/asteroids/GameApplet.java index 55ec527..acb4165 100644 --- a/Asteroids/src/com/RickBarrette/asteroids/GameFrame.java +++ b/Asteroids/src/com/RickBarrette/asteroids/GameApplet.java @@ -26,7 +26,7 @@ import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; -import javax.swing.JFrame; +import javax.swing.JApplet; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; @@ -36,68 +36,14 @@ import javax.swing.JMenuItem; * 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, ActionListener{ +public class GameApplet extends JApplet implements ActionListener, KeyListener { private static final long serialVersionUID = -2051298505681885632L; - private final Status mStatusBar; - private final Display mDisplay; - private final AsteroidGame mGame; - - /** - * Creates a new GameFrame - * @param g - * @author ricky barrette - */ - public GameFrame(final AsteroidGame g) { - super("ASTEROIDS"); - mGame = g; - - /* - * 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"); - - 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); - - menuBar.add(fileMenu); - - FlowLayout layout = new FlowLayout(); - layout.setAlignment(FlowLayout.LEFT); - - Container container = getContentPane(); - mStatusBar = new Status(container, mGame); - mDisplay = new Display(container, mGame); - - addKeyListener(this); - - setDefaultCloseOperation(EXIT_ON_CLOSE); - - // sets up window's location and sets size**** - setSize(1000, 800); - setVisible(true); - - } - + private Status mStatusBar; + private Display mDisplay; + private AsteroidGameThread mGameThread; + /** * Called when a menu item is selected from the benu bar * (non-Javadoc) @@ -105,21 +51,21 @@ public class GameFrame extends JFrame implements KeyListener, ActionListener{ */ public void actionPerformed(final ActionEvent e) { if (e.getActionCommand().equals("New Game")) { - mGame.newGame(); + mGameThread.newGame(); } if (e.getActionCommand().equals("Start")) { - mGame.startGame(); + mGameThread.startGame(); } if (e.getActionCommand().equals("Pause")) { - mGame.pauseGame(); + mGameThread.pauseGame(); } if (e.getActionCommand().equals("Quit")) System.exit(0); } - + /** * Drives the user's ship * @param e @@ -129,7 +75,7 @@ public class GameFrame extends JFrame implements KeyListener, ActionListener{ private void driveShip(final KeyEvent e, final boolean isKeyPressed) { Ship ship = null; //get the user's ship - for (Object item : mGame.getWorld()) { + for (Object item : mGameThread.getWorld()) { if (item instanceof Ship) { ship = (Ship) item; } @@ -216,6 +162,62 @@ public class GameFrame extends JFrame implements KeyListener, ActionListener{ return mStatusBar; } + /** + * (non-Javadoc) + * @see java.applet.Applet#init() + */ + @Override + public void init() { + mGameThread = new AsteroidGameThread(this); + + /* + * 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"); + + 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); + + menuBar.add(fileMenu); + + FlowLayout layout = new FlowLayout(); + layout.setAlignment(FlowLayout.LEFT); + + Container container = getContentPane(); + mStatusBar = new Status(container, mGameThread); + mDisplay = new Display(container, mGameThread); + + this.addKeyListener(this); + this.setFocusable(true); + this.setFocusCycleRoot(true); + this.requestFocusInWindow(); + + setSize(1000, 800); + setVisible(true); + + repaint(); + + mGameThread.newGame(); + } + /** * Called when a key is pressed * (non-Javadoc) @@ -230,10 +232,10 @@ public class GameFrame extends JFrame implements KeyListener, ActionListener{ * Start of pause the game */ case KeyEvent.VK_ENTER: - if(mGame.isStarted) - mGame.pauseGame(); + if(mGameThread.isStarted) + mGameThread.pauseGame(); else - mGame.startGame(); + mGameThread.startGame(); break; } diff --git a/Asteroids/src/com/RickBarrette/asteroids/Main.java b/Asteroids/src/com/RickBarrette/asteroids/Main.java deleted file mode 100644 index cbc29b0..0000000 --- a/Asteroids/src/com/RickBarrette/asteroids/Main.java +++ /dev/null @@ -1,51 +0,0 @@ -/** - * Main.java - * @date Mar 31, 2012 - * @author ricky barrette - * - * Copyright 2012 Richard Barrette - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License - */ -package com.RickBarrette.asteroids; - -/** - * This is the main class of my asteroids game. - * This application is based from "A Guide to Programming Asteroids As a Java Applet" by Brandon Carroll. - * @author ricky barrette - */ -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() { - mGame.newGame(); - } - - /** - * @param args - * @author ricky barrette - */ - public static void main(String[] args) { - new Main(); - } -} \ No newline at end of file diff --git a/Asteroids/src/com/RickBarrette/asteroids/MovingSpaceObject.java b/Asteroids/src/com/RickBarrette/asteroids/MovingSpaceObject.java index ddf1527..ca0e722 100644 --- a/Asteroids/src/com/RickBarrette/asteroids/MovingSpaceObject.java +++ b/Asteroids/src/com/RickBarrette/asteroids/MovingSpaceObject.java @@ -130,9 +130,6 @@ public abstract class MovingSpaceObject extends SpaceObject implements Moveable{ // adds accel to velocity in direction pointed if (isAccelerating) { - if(Main.DEBUG) - System.out.println("accelerating by "+mAcceleration); - // calculates components of accel and adds them to velocity mXVelocity += mAcceleration * Math.cos(mAngle); mYVelocity += mAcceleration * Math.sin(mAngle); diff --git a/Asteroids/src/com/RickBarrette/asteroids/Ship.java b/Asteroids/src/com/RickBarrette/asteroids/Ship.java index 5e22da8..357b680 100644 --- a/Asteroids/src/com/RickBarrette/asteroids/Ship.java +++ b/Asteroids/src/com/RickBarrette/asteroids/Ship.java @@ -36,7 +36,7 @@ public class Ship extends MovingSpaceObject { private final int mShotDelay; private int[] mXpoints = new int[4], mYpoints = new int[4], mFlameXpoints = new int[3], mFlameYpoints = new int[3]; private int mShotDelayLeft; - private final AsteroidGame mGame; + private final AsteroidGameThread mGame; /** * radius of circle used to approximate the ship @@ -55,7 +55,7 @@ public class Ship extends MovingSpaceObject { * @author ricky barrette * @param game */ - public Ship(final double x, final double y, final double angle, final double acceleration, final double velocityDecay, final double rotationalSpeed, final int shotDelay, final AsteroidGame game) { + public Ship(final double x, final double y, final double angle, final double acceleration, final double velocityDecay, final double rotationalSpeed, final int shotDelay, final AsteroidGameThread game) { this.mX = x; this.mY = y; this.mAngle = angle; @@ -97,9 +97,6 @@ public class Ship extends MovingSpaceObject { @Override 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 diff --git a/Asteroids/src/com/RickBarrette/asteroids/Shot.java b/Asteroids/src/com/RickBarrette/asteroids/Shot.java index 0ba89d1..6d7bd11 100644 --- a/Asteroids/src/com/RickBarrette/asteroids/Shot.java +++ b/Asteroids/src/com/RickBarrette/asteroids/Shot.java @@ -30,7 +30,7 @@ public class Shot extends MovingSpaceObject { public static final int TOTAL_DRAWS = 50; public static final int SPEED = 10; - private final AsteroidGame mGame;; + private final AsteroidGameThread mGame;; private int mCount = 0; /** @@ -38,7 +38,7 @@ public class Shot extends MovingSpaceObject { * @author ricky barrette * @param mGame */ - public Shot(final double x, final double y, final double angle, final double shipXVel, final double shipYVel, final AsteroidGame game) { + public Shot(final double x, final double y, final double angle, final double shipXVel, final double shipYVel, final AsteroidGameThread game) { mX = x; mY = y; mAngle = angle; diff --git a/Asteroids/src/com/RickBarrette/asteroids/Status.java b/Asteroids/src/com/RickBarrette/asteroids/Status.java index aabe4de..6cd3ef9 100644 --- a/Asteroids/src/com/RickBarrette/asteroids/Status.java +++ b/Asteroids/src/com/RickBarrette/asteroids/Status.java @@ -48,7 +48,7 @@ public class Status extends JPanel { * @param g * @author ricky barrette */ - public Status(final Container container, final AsteroidGame g) { + public Status(final Container container, final AsteroidGameThread g) { JPanel northSubPanel = new JPanel(); status = new JLabel("Missiles 0 Asteroids 0 Ships 0 Score 0 Time: 0"); northSubPanel.add(status);