Compare commits

...

10 Commits

Author SHA1 Message Date
e7c4f9937c Merge branch 'topic/applet' 2012-04-22 15:03:05 -04:00
801be01be1 Removed quit from menu
closes #11

Signed-off-by: Ricky Barrette <rickbarrette@gmail.com>
2012-04-22 15:00:14 -04:00
e835d3c795 Added called to requestFocus() in GameApplet.init() and GameThread.run()
refs #11

The brute force method of requesting focus seems to be the only
fix/workaround that I could find. However it doesn't seem to cause any
preformance issues

Signed-off-by: Ricky Barrette <rickbarrette@gmail.com>
2012-04-22 14:59:07 -04:00
d53c84839d Added git ignore file
added Asteroids/bin to .gitignore
2012-04-16 10:38:18 -04:00
390e962462 Added apache license and fixed formatting of web files 2012-04-16 10:27:19 -04:00
cc3800bdfe Created asteroids.html
asteroids.html is a model for inserting the Asteroids game applet into a web page
2012-04-15 23:02:46 -04:00
365d465271 Created Asteroids.jnlp to start the Asteroids game 2012-04-15 19:28:32 -04:00
fee5a5bbf9 Initial commit of jframe to applet conversion
Signed-off-by: Ricky Barrette <rickbarrette@gmail.com>
2012-04-15 17:26:41 -04:00
a8c959700b Implmented the shot delay
Also moved some methods around to clean up the classes

Signed-off-by: Ricky Barrette <rickbarrette@gmail.com>
2012-04-06 14:28:18 -04:00
db4cb9e5f1 Updated conventions, cleaned up comments and code
Signed-off-by: Ricky Barrette <rickbarrette@gmail.com>
2012-04-06 14:09:38 -04:00
18 changed files with 370 additions and 289 deletions

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
Asteroids/bin/

33
Asteroids.jnlp Executable file
View File

@@ -0,0 +1,33 @@
<!--
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
-->
<?xml version="1.0" encoding="UTF-8"?>
<jnlp spec="1.0+" codebase="" href="">
<information>
<title>Asteroids</title>
<vendor>Richard Barrette</vendor>
</information>
<resources>
<j2se version="1.6+" ref="http://java.sun.com/products/autodl/j2se" />
<jar href="Asteroids.jar" main="true" />
</resources>
<applet-desc
name="Asteroids"
main-class="com.RickBarrette.asteroids.GameApplet"
width="1000"
height="800">
</applet-desc>
<update check="background"/>
</jnlp>

View File

@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
<classpathentry kind="output" path="bin"/>
</classpath>

View File

@@ -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

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 AsteroidGameThread mGame;
/**
* Creates a new Asteroid
@@ -45,7 +45,7 @@ public class Asteroid extends MovingSpaceObject implements Collider, Drawable {
* @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;

View File

@@ -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<Object> 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<Object>();
//TODO simulate game play unitll game ist started
this.start();
@@ -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);
}
@@ -73,20 +73,28 @@ public class AsteroidGame extends Thread {
if(s != null){
s.allStop();
hyperJump(s);
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 GameApplet getGameFrame() {
return this.mGameApplet;
}
/**
@@ -97,25 +105,6 @@ public class AsteroidGame extends Thread {
return mWorld;
}
/**
* Hyperjumps the sip
* @param ship
* @author ricky barrette
*/
public void hyperJump(Ship ship) {
// boolean isSafe = true;
Random gen = new Random();
// do{
System.out.println("hyper jumping");
ship.setLocation(gen.nextInt(mGameFrame.getDisplayWidth()), gen.nextInt(mGameFrame.getDispalyHeight()));
// for(int i = 0; i < mWorld.size(); i++)
// if(mWorld.get(i) instanceof Collider)
// if(((Collider) mWorld.get(i)).checkForCollision(ship))
// isSafe = false;
// } while (!isSafe);
mGameFrame.repaintDispaly();
}
/**
* populates the world for a new game
* @author ricky barrette
@@ -125,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());
}
/**
@@ -146,22 +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));
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());
notification("Level "+ mGameApplet.getStatusBar().getLevel());
mGameApplet.repaintDispaly();
}
/**
@@ -170,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
@@ -181,7 +171,7 @@ public class AsteroidGame extends Thread {
e.printStackTrace();
}
if(isStarted)
mGameFrame.setDisplayText(null);
mGameApplet.setDisplayText(null);
}
}).start();
}
@@ -192,7 +182,7 @@ public class AsteroidGame extends Thread {
*/
public synchronized void pauseGame(){
isStarted = false;
mGameFrame.setDisplayText("Paused");
mGameApplet.setDisplayText("Paused");
setMovingSpaceObjectsEnabled(false);
}
@@ -201,9 +191,9 @@ 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);
mGameApplet.getStatusBar().incrementScore(2);
}
mWorld.remove(o);
}
@@ -223,17 +213,23 @@ public class AsteroidGame extends Thread {
while (true){
if(isStarted) {
/*
* brute force focus,
* this seems to be the only fix I can find, for now
*/
mGameApplet.requestFocus();
/*
* 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
@@ -260,16 +256,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!");
@@ -280,8 +276,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
@@ -305,7 +301,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);
@@ -325,7 +321,7 @@ public class AsteroidGame extends Thread {
*/
public synchronized void startGame(){
mLastTime = System.currentTimeMillis();
mGameFrame.setDisplayText(null);
mGameApplet.setDisplayText(null);
setMovingSpaceObjectsEnabled(true);
isStarted = true;
}

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 AsteroidGameThread 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 AsteroidGameThread 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

@@ -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,89 +36,31 @@ 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 Status mStatusBar;
private Display mDisplay;
private AsteroidGame mGame;
/**
* Creates a new GameFrame
* @param g
* @author ricky barrette
*/
public GameFrame(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 AsteroidGameThread mGameThread;
/**
* 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) {
public void actionPerformed(final ActionEvent e) {
if (e.getActionCommand().equals("New Game")) {
mGame.newGame();
mDisplay.repaint();
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);
}
/**
@@ -127,10 +69,10 @@ 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()) {
for (Object item : mGameThread.getWorld()) {
if (item instanceof Ship) {
ship = (Ship) item;
}
@@ -180,7 +122,7 @@ public class GameFrame extends JFrame implements KeyListener, ActionListener{
*/
case KeyEvent.VK_H:
if(isKeyPressed){
mGame.hyperJump(ship);
ship.hyperJump();
}
break;
@@ -189,7 +131,7 @@ public class GameFrame extends JFrame implements KeyListener, ActionListener{
*/
case KeyEvent.VK_SPACE:
if(isKeyPressed)
mGame.addElement(new Shot(ship.getX(), ship.getY(), ship.getAngle(), ship.getXVelocity(), ship.getYVelocity(), mGame));
ship.shoot();
break;
}
}
@@ -217,13 +159,69 @@ 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");
fileMenu.add(menuNewGame);
fileMenu.addSeparator();
fileMenu.add(menuStartGame);
fileMenu.add(menuPauseGame);
fileMenu.addSeparator();
menuNewGame.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();
this.setFocusable(true);
this.requestFocus();
}
/**
* Called when a key is pressed
* (non-Javadoc)
* @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()){
/*
@@ -231,10 +229,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;
}
@@ -247,12 +245,26 @@ 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) {
switch(e.getKeyCode()){
/*
* [Enter]
* Start of pause the game
*/
case KeyEvent.VK_ENTER:
if(mGameThread.isStarted)
mGameThread.pauseGame();
else
mGameThread.startGame();
break;
}
driveShip(e, true);
}
/**
@@ -278,7 +290,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

@@ -1,47 +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;
/**
* Creates a new Main
* @author ricky barrette
*/
public Main() {
AsteroidGame game = new AsteroidGame();
game.newGame();
}
/**
* @param args
* @author ricky barrette
*/
public static void main(String[] args) {
new Main();
}
}

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;
@@ -45,6 +45,30 @@ public class MovingSpaceObject extends SpaceObject implements Moveable{
isAccelerating = false;
}
/**
* @return the moving space object's angle
* @author ricky barrette
*/
public double getAngle() {
return mAngle;
}
/**
* @return the moving space object's x volocity
* @author ricky barrette
*/
public double getXVelocity() {
return this.mXVelocity;
}
/**
* @return the moving space object's y velocity
* @author ricky barrette
*/
public double getYVelocity() {
return this.mYVelocity;
}
/**
* @return true if the space object is accelerating
* @author ricky barrette
@@ -83,7 +107,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
@@ -106,9 +130,6 @@ public 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);
@@ -135,7 +156,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 +165,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 +175,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 +185,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 +194,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

@@ -21,26 +21,24 @@ package com.RickBarrette.asteroids;
import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;
/**
* 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 mShotDelay;
private int[] mXpoints = new int[4], mYpoints = new int[4], mFlameXpoints = new int[3], mFlameYpoints = new int[3];
private int mShotDelayLeft;
private final AsteroidGameThread mGame;
/*
* 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;
@@ -55,9 +53,9 @@ public class Ship extends MovingSpaceObject implements Drawable {
* @param rotationalSpeed of the ship
* @param shotDelay of the ship
* @author ricky barrette
* @param game
*/
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, final AsteroidGameThread game) {
this.mX = x;
this.mY = y;
this.mAngle = angle;
@@ -65,15 +63,16 @@ public class Ship extends MovingSpaceObject implements Drawable {
this.mVelocityDecay = velocityDecay;
this.mRotationalSpeed = rotationalSpeed;
this.mColor = Color.CYAN;
mGame = game;
// start off paused
this.isActive = false;
// # of frames between shots
this.shotDelay = shotDelay;
this.mShotDelay = shotDelay;
// ready to shoot
this.shotDelayLeft = 0;
this.mShotDelayLeft = 0;
}
/**
@@ -81,9 +80,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 (mShotDelayLeft > 0)
return false;
// shoot again yet or if it needs to wait longer
else
return true;
}
@@ -94,44 +95,47 @@ 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)
*/
// 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
/*
* 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
}
/**
* @return the ship's current angle
* @author ricky barrette
*/
public double getAngle() {
return mAngle;
g.fillPolygon(mXpoints, mYpoints, 4);
}
/**
@@ -142,12 +146,23 @@ public class Ship extends MovingSpaceObject implements Drawable {
return mRadius;
}
public double getXVelocity() {
return this.mXVelocity;
}
public double getYVelocity() {
return this.mYVelocity;
/**
* Hyperjumps the sip
* @param ship
* @author ricky barrette
*/
public void hyperJump() {
// boolean isSafe = true;
Random gen = new Random();
// do{
System.out.println("hyper jumping");
setLocation(gen.nextInt(mGame.getGameFrame().getDisplayWidth()), gen.nextInt(mGame.getGameFrame().getDispalyHeight()));
// for(int i = 0; i < mWorld.size(); i++)
// if(mWorld.get(i) instanceof Collider)
// if(((Collider) mWorld.get(i)).checkForCollision(ship))
// isSafe = false;
// } while (!isSafe);
mGame.getGameFrame().repaintDispaly();
}
/**
@@ -156,14 +171,25 @@ 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
*/
if (shotDelayLeft > 0)
shotDelayLeft--;
if (mShotDelayLeft > 0)
mShotDelayLeft--;
super.move(scrnWidth, scrnHeight);
}
/**
* Fires a shot from the ship
* @author ricky barrette
*/
public void shoot() {
if(canShoot()) {
mGame.addElement(new Shot(mX, mY, mAngle, mXVelocity, mYVelocity, mGame));
mShotDelayLeft += mShotDelay;
}
}
}

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 AsteroidGameThread 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 AsteroidGameThread 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 AsteroidGameThread 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);
}

38
asteroids.html Normal file
View File

@@ -0,0 +1,38 @@
<!--
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
-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang="en-US">
<HEAD>
<TITLE> Asteroids </TITLE>
<h1> Asteroids </h1>
An Asteroids Clone by Rick Barrette
<br/>
</HEAD>
<BODY>
<applet width="100%" height="70%" code="com.RickBarrette.asteroids.GameApplet">
<param name="jnlp_href" value="Asteroids.jnlp">
</applet>
<h3>Controls</h3>
<strong>Steering: </strong> [W] [A] [S] [D] or [Arrow] Keys
<br/>
<strong>Fire:</strong> [Space]
<br/>
<strong>Start/Pausee:</strong> [Enter]
<br/>
<strong>Hyper Jump:</strong> [H]
</BODY>
</html>