commit c54e39b01ce19b2e535c93baeeb0e3d1e7d5503e Author: Ricky Barrette Date: Mon Apr 2 00:45:43 2012 -0400 Initial Commit of my Asteroids game clone. I have started this project, to betterunderstand basic game development. This commit contains Most of the frame work for the game. Currently the ship can be flown around the screen. Signed-off-by: Ricky Barrette diff --git a/Asteroids/.classpath b/Asteroids/.classpath new file mode 100644 index 0000000..fb565a5 --- /dev/null +++ b/Asteroids/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/Asteroids/.project b/Asteroids/.project new file mode 100644 index 0000000..27dc389 --- /dev/null +++ b/Asteroids/.project @@ -0,0 +1,17 @@ + + + Asteroids + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/Asteroids/.settings/org.eclipse.core.resources.prefs b/Asteroids/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000..ab66f03 --- /dev/null +++ b/Asteroids/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +encoding//src/com/RickBarrette/asteroids/Ship.java=UTF-8 diff --git a/Asteroids/.settings/org.eclipse.jdt.core.prefs b/Asteroids/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..7341ab1 --- /dev/null +++ b/Asteroids/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +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.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.7 +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 diff --git a/Asteroids/src/com/RickBarrette/asteroids/Asteroid.java b/Asteroids/src/com/RickBarrette/asteroids/Asteroid.java new file mode 100644 index 0000000..3df698d --- /dev/null +++ b/Asteroids/src/com/RickBarrette/asteroids/Asteroid.java @@ -0,0 +1,38 @@ +/** + * Asteroid.java + * @date Apr 1, 2012 + * @author ricky barrette + * @author Twenty Codes, LLC + * + * 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 class will be used to make astroids, to destroy the ship! + * @author ricky barrette + */ +public class Asteroid extends MovingSpaceObject { + + /** + * Creates a new Asteroid + * @author ricky barrette + */ + public Asteroid() { + // TODO Asteroid stuff + } + +} \ No newline at end of file diff --git a/Asteroids/src/com/RickBarrette/asteroids/AsteroidGame.java b/Asteroids/src/com/RickBarrette/asteroids/AsteroidGame.java new file mode 100644 index 0000000..483d749 --- /dev/null +++ b/Asteroids/src/com/RickBarrette/asteroids/AsteroidGame.java @@ -0,0 +1,155 @@ +/** + * AsteroidGame.java + * @date Mar 31, 2012 + * @author ricky barrette + * @author Twenty Codes, LLC + * + * 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; + +import java.util.Vector; + + +/** + * This class maintain's the game logic. It is the main driver + * @author ricky barrette + */ +public class AsteroidGame extends Thread { + + private static final int DELAY_IN_MSEC = 50; + private Vector mWorld; + private GameFrame mGameFrame; + public static boolean isStarted = false; + + /** + * Creates an new Asteroids game + * @author ricky barrette + */ + public AsteroidGame() { + mGameFrame = new GameFrame(this); + //TODO simulate game play unitll game ist started + } + + /** + * Adds an object to the world + * @param add + * @author ricky barrette + */ + public void addElement(Object add) { + mWorld.addElement(add); + } + + /** + * popoluates the world for a new game + * @author ricky barrette + */ + public void createGame() { + mWorld = new Vector(); + mWorld.add(new Ship(500,500,0,.35,.98,.4,1)); + } + + public Vector getWorld() { + return mWorld; + } + + /** + * @return true if the world is empty + * @author ricky barrette + */ + public boolean isEmpty() { + return mWorld.isEmpty(); + } + + /** + * Clears the world, and Creates a new game + * @author ricky barrette + */ + public void newGame() { + mWorld.clear(); + createGame(); + } + + /** + * Pauses the game + * @author ricky barrette + */ + public synchronized void pause(){ + isStarted = false; + } + + /** + * Removes an object from this world. + * @param o object to be removed + * @author ricky barrette + */ + public void removeElement(Object o) { + mWorld.removeElement(o); + } + + /** + * Main game driver method. + * + * @author ricky barrette + */ + @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(); + + /* + * sleep till next time + */ + try { + sleep(DELAY_IN_MSEC); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + } + + /** + * @return the number of objects in the world + * @author ricky barrette + */ + public int size() { + return mWorld.size(); + } + + /** + * Starts the game + * @author ricky barrette + */ + @Override + public synchronized void start(){ + isStarted = true; + super.start(); + } +} \ No newline at end of file diff --git a/Asteroids/src/com/RickBarrette/asteroids/Collider.java b/Asteroids/src/com/RickBarrette/asteroids/Collider.java new file mode 100644 index 0000000..1f6a1b1 --- /dev/null +++ b/Asteroids/src/com/RickBarrette/asteroids/Collider.java @@ -0,0 +1,30 @@ +/** + * Collider.java + * @date Mar 31, 2012 + * @author ricky barrette + * @author Twenty Codes, LLC + * + * 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 interface will be used for an object that should detect collisions + * @author ricky barrette + */ +public interface Collider { + +} diff --git a/Asteroids/src/com/RickBarrette/asteroids/Display.java b/Asteroids/src/com/RickBarrette/asteroids/Display.java new file mode 100644 index 0000000..845f15a --- /dev/null +++ b/Asteroids/src/com/RickBarrette/asteroids/Display.java @@ -0,0 +1,72 @@ +/** + * Display.java + * @date Mar 31, 2012 + * @author ricky barrette + * @author Twenty Codes, LLC + * + * 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; + +import java.awt.BorderLayout; +import java.awt.Color; +import java.awt.Container; +import java.awt.Graphics; + +import javax.swing.JPanel; + +/** + * This display panel will be used to display the game's object's and their world + * @author ricky barrette + */ +public class Display extends JPanel { + + private static final long serialVersionUID = -9105117186423881937L; + private AsteroidGame mGame; + private Container mContainer; + + /** + * Creates a new Dispay + * @param c + * @param g + * @author ricky barrette + */ + public Display(Container c, AsteroidGame g) { + mGame = g; + mContainer = c; + this.setBackground(new Color(0, 0, 0)); + mContainer.add(this, BorderLayout.CENTER); + } + + /** + * (non-Javadoc) + * @see javax.swing.JComponent#paintComponent(java.awt.Graphics) + */ + @Override + public void paintComponent(Graphics g) { + super.paintComponent(g); + + /* + * 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); + } + } + } +} \ No newline at end of file diff --git a/Asteroids/src/com/RickBarrette/asteroids/Drawable.java b/Asteroids/src/com/RickBarrette/asteroids/Drawable.java new file mode 100644 index 0000000..154e047 --- /dev/null +++ b/Asteroids/src/com/RickBarrette/asteroids/Drawable.java @@ -0,0 +1,33 @@ +/** + * Drawable.java + * @date Mar 31, 2012 + * @author ricky barrette + * @author Twenty Codes, LLC + * + * 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; + +import java.awt.Graphics; + +/** + * This interface will be used for objects that need to be drawn on the screen + * @author ricky barrette + */ +public interface Drawable { + + public void draw(Graphics g); + +} diff --git a/Asteroids/src/com/RickBarrette/asteroids/GameFrame.java b/Asteroids/src/com/RickBarrette/asteroids/GameFrame.java new file mode 100644 index 0000000..416d8f9 --- /dev/null +++ b/Asteroids/src/com/RickBarrette/asteroids/GameFrame.java @@ -0,0 +1,269 @@ +/** + * GameFrame.java + * @date Mar 31, 2012 + * @author ricky barrette + * @author Twenty Codes, LLC + * 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; + +import java.awt.Container; +import java.awt.FlowLayout; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.KeyEvent; +import java.awt.event.KeyListener; +import java.util.Random; + +import javax.swing.JFrame; +import javax.swing.JMenu; +import javax.swing.JMenuBar; +import javax.swing.JMenuItem; + +/** + * This class will maintian the game's frame, and handle key events from the user + * @author ricky barrette + */ +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 Display mDisplay; + private Container container; + private FlowLayout layout; + private menuListener xlistener; + private AsteroidGame game; + + /** + * Creates a new GameFrame + * + * @param g + * @author ricky barrette + */ + public GameFrame(AsteroidGame g) { + super("ASTEROIDS"); + game = g; + + bar = new JMenuBar(); + setJMenuBar(bar); + + menu = new JMenu("File"); + + newGame = new JMenuItem("New Game"); + quit = new JMenuItem("Quit"); + + menu.add(newGame); + menu.addSeparator(); + menu.add(quit); + + bar.add(menu); + + layout = new FlowLayout(); + layout.setAlignment(FlowLayout.LEFT); + + + container = getContentPane(); + statusBar = new Status(container, game); + mDisplay = new Display(container, game); + + + xlistener = new menuListener(); + newGame.addActionListener(xlistener); + quit.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(); + } + + private class menuListener implements ActionListener { + + public void actionPerformed(ActionEvent e) { + if (e.getActionCommand().equals("New Game")) { + game.newGame(); + mDisplay.repaint(); + + } + if (e.getActionCommand().equals("Quit")) + System.exit(0); + } + + } + + /** + * Called when a key is pressed + * (non-Javadoc) + * @see java.awt.event.KeyListener#keyPressed(java.awt.event.KeyEvent) + */ + @Override + public void keyPressed(KeyEvent e) { + for (Object item : game.getWorld()) { + + if (item instanceof Ship) { + Ship ship = (Ship) item; + switch (e.getKeyCode()) { + + /* + * [Left arrow] or [A] + * Rotate the ship left + */ + case KeyEvent.VK_A: + case KeyEvent.VK_LEFT: + ship.setTurningLeft(true); + break; + + /* + * [Space] Pew Pew Pew!!! + */ + case KeyEvent.VK_SPACE: + break; + + /* + * [Right arrow] or [D] + * Rotate the ship right + */ + case KeyEvent.VK_D: + case KeyEvent.VK_RIGHT: + ship.setTurningRight(true); + break; + + /* + * [Up arrow] or [W] + * Increase the ship's speed + */ + case KeyEvent.VK_W: + case KeyEvent.VK_UP: + ship.setAccelerating(true); + break; + + /* + * [Down arrow] or [S] + * Slow the ship + */ + case KeyEvent.VK_S: + case KeyEvent.VK_DOWN: + ship.setAccelerating(false); + break; + + /* + * [H] Hyoer jump + */ + case KeyEvent.VK_H: + Random myRNG = new Random(); + ship.setLocation(myRNG.nextInt(mDisplay.getHeight()), myRNG.nextInt(mDisplay.getWidth())); + break; + } + } + + mDisplay.repaint(); + + } + + } + + /** + * Called when a key is released + * (non-Javadoc) + * @see java.awt.event.KeyListener#keyReleased(java.awt.event.KeyEvent) + */ + @Override + public void keyReleased(KeyEvent e) { + for (Object item : game.getWorld()) { + if (item instanceof Ship) { + Ship ship = (Ship) item; + + switch (e.getKeyCode()) { + + /* + * [Left arrow] or [A] + * Rotate the ship left + */ + case KeyEvent.VK_A: + case KeyEvent.VK_LEFT: + ship.setTurningLeft(false); + break; + + /* + * [Right arrow] or [d] Rotate the ship right + */ + case KeyEvent.VK_D: + case KeyEvent.VK_RIGHT: + ship.setTurningRight(false); + break; + + /* + * [Up arrow] or [W] + * Increase the ship's speed + */ + case KeyEvent.VK_W: + case KeyEvent.VK_UP: + ship.setAccelerating(false); + break; + + /* + * [Down arrow] or [S] + * Slow the ship + */ + case KeyEvent.VK_S: + case KeyEvent.VK_DOWN: + ship.setAccelerating(false); + break; + + } + } + } + } + + @Override + public void keyTyped(KeyEvent e) { + // TODO Auto-generated method stub + + } + + /** + * @return the height of the game display panel + * @author ricky barrette + */ + public int getDispalyHeight() { + return mDisplay.getHeight(); + } + + /** + * @return the width of the game dispaly panel + * @author ricky barrette + */ + public int getDisplayWidth() { + return mDisplay.getWidth(); + } +} \ No newline at end of file diff --git a/Asteroids/src/com/RickBarrette/asteroids/Main.java b/Asteroids/src/com/RickBarrette/asteroids/Main.java new file mode 100644 index 0000000..0213a74 --- /dev/null +++ b/Asteroids/src/com/RickBarrette/asteroids/Main.java @@ -0,0 +1,49 @@ +/** + * Main.java + * @date Mar 31, 2012 + * @author ricky barrette + * @author Twenty Codes, LLC + * + * 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. + * @author ricky barrette + */ +public class Main { + + public static final boolean DEBUG = true; + + /** + * Creates a new Main + * @author ricky barrette + */ + public Main() { + AsteroidGame game = new AsteroidGame(); + game.createGame(); + game.start(); + } + + /** + * @param args + * @author ricky barrette + */ + public static void main(String[] args) { + new Main(); + } + +} diff --git a/Asteroids/src/com/RickBarrette/asteroids/Moveable.java b/Asteroids/src/com/RickBarrette/asteroids/Moveable.java new file mode 100644 index 0000000..2b87f78 --- /dev/null +++ b/Asteroids/src/com/RickBarrette/asteroids/Moveable.java @@ -0,0 +1,36 @@ +/** + * Moveable.java + * @date Mar 31, 2012 + * @author ricky barrette + * @author Twenty Codes, LLC + * + * 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 interface will be used to move objects with in the game + * @author ricky barrette + */ +public interface Moveable { + + public void move(int scrnWidth, int scrnHeight); + + public void setAccelerating(boolean accelerating); + + public void setTurningLeft(boolean turningLeft); + + public void setTurningRight(boolean turningRight); +} diff --git a/Asteroids/src/com/RickBarrette/asteroids/MovingSpaceObject.java b/Asteroids/src/com/RickBarrette/asteroids/MovingSpaceObject.java new file mode 100644 index 0000000..f25cf27 --- /dev/null +++ b/Asteroids/src/com/RickBarrette/asteroids/MovingSpaceObject.java @@ -0,0 +1,179 @@ +/** + * MovingSpaceObject.java + * @date Mar 31, 2012 + * @author ricky barrette + * @author Twenty Codes, LLC + * + * 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 class will track the information required for moving objects. + * @author ricky barrette + */ +public class MovingSpaceObject extends SpaceObject implements Moveable{ + + protected double mAngle; + protected double mVelocityDecay; + protected double mRotationalSpeed; + protected double xVelocity = 0; + protected double yVelocity = 0; + protected double mAcceleration; + protected boolean isTurningLeft = false, isTurningRight = false, isAccelerating = false, isActive; + + /** + * @return true if the space object is accelerating + * @author ricky barrette + */ + public boolean isAccelerating(){ + return isAccelerating; + } + + /** + * @return true if the space object is active + * @author ricky barrette + */ + public boolean isActive() { + return isActive; + } + + /** + * @return true if the space object is turning left + * @author ricky barrette + */ + public boolean isTurningLeft(){ + return isTurningLeft; + } + + /** + * @return true if the space object is turning right + * @author ricky barrette + */ + public boolean isTurningRight(){ + return isTurningRight; + } + + /** + * Called when the object's location is to be updated + * (non-Javadoc) + * @see com.RickBarrette.asteroids.Moveable#move(int, int) + */ + @Override + public void move(int scrnWidth, int scrnHeight) { + + if(Main.DEBUG){ + System.out.println("Move "+ scrnWidth +" x "+ scrnHeight); + System.out.println("angle "+mAngle); + System.out.println("xVelocity = "+xVelocity); + System.out.println("yVelocity = "+yVelocity); + System.out.println("yX = "+mX); + System.out.println("yY = "+mY); + } + + /* + * this is backwards from typical polar coordinates + * because positive y is downward. + */ + if (isTurningLeft) + mAngle -= mRotationalSpeed; + /* + * Because of that, adding to the angle is + * rotating clockwise (to the right) + */ + if (isTurningRight) + mAngle += mRotationalSpeed; + + // Keep angle within bounds of 0 to 2*PI + if (mAngle > (2 * Math.PI)) + mAngle -= (2 * Math.PI); + else if (mAngle < 0) + mAngle += (2 * Math.PI); + + // 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 + xVelocity += mAcceleration * Math.cos(mAngle); + yVelocity += mAcceleration * Math.sin(mAngle); + } + + // move the space object by adding velocity to position + mX += xVelocity; + mY += yVelocity; + + /* + * slows ship down by percentages (velDecay + * should be a decimal between 0 and 1 + */ + xVelocity *= mVelocityDecay; + yVelocity *= mVelocityDecay; + + /* + * wrap the ship around to the opposite side of the screen + * when it goes out of the screen's bounds + */ + if (mX < 0) + mX += scrnHeight; + else if (mX > scrnHeight) + mX -= scrnHeight; + if (mY < 0) + mY += scrnWidth; + else if (mY > scrnWidth) + mY -= scrnWidth; + } + + /** + * Sets wether or not this space object is accelerating + * (non-Javadoc) + * @see com.RickBarrette.asteroids.Moveable#setAccelerating(boolean) + */ + @Override + public void setAccelerating(boolean accelerating) { + this.isAccelerating = accelerating; // start or stop accelerating the ship + } + + /** + * Sets wether or not this space object is active + * @param active + * @author ricky barrette + */ + public void setActive(boolean active) { + this.isActive = active; // used when the game is paused or unpaused + } + + /** + * Sets wether or not this space object is turning left + * (non-Javadoc) + * @see com.RickBarrette.asteroids.Moveable#setTurningLeft(boolean) + */ + @Override + public void setTurningLeft(boolean turningLeft) { + this.isTurningLeft = turningLeft; // start or stop turning the ship + } + + /** + * Sets wether or not this space object is turning right + * (non-Javadoc) + * @see com.RickBarrette.asteroids.Moveable#setTurningRight(boolean) + */ + @Override + public void setTurningRight(boolean turningRight) { + this.isTurningRight = turningRight; + } + +} \ No newline at end of file diff --git a/Asteroids/src/com/RickBarrette/asteroids/Ship.java b/Asteroids/src/com/RickBarrette/asteroids/Ship.java new file mode 100644 index 0000000..27ec3c0 --- /dev/null +++ b/Asteroids/src/com/RickBarrette/asteroids/Ship.java @@ -0,0 +1,153 @@ +/** + * Ship.java + * @date Mar 31, 2012 + * @author ricky barrette + * @author Twenty Codes, LLC + * + * 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; + +import java.awt.Color; +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 { + + 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 }; + + /* + * 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 radius = 6; + + /** + * Creates a new ship + * @param x location of the ship + * @param y location of the ship + * @param angle of the ship + * @param acceleration of the ship + * @param velocityDecay of the ship + * @param rotationalSpeed of the ship + * @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 + this.mX = x; + this.mY = y; + this.mAngle = angle; + this.mAcceleration = acceleration; + this.mVelocityDecay = velocityDecay; + this.mRotationalSpeed = rotationalSpeed; + + // start off paused + this.isActive = true; + + // # of frames between shots + this.shotDelay = shotDelay; + + // ready to shoot + this.shotDelayLeft = 0; + } + + /** + * Called by the Display panel when it needs to draw the ship + * (non-Javadoc) + * @see com.RickBarrette.asteroids.Drawable#draw(java.awt.Graphics) + */ + @Override + public void draw(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 + 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 + g.fillPolygon(mFlameXpoints, mFlameYpoints, 3); + } + // 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); + } + + if (isActive) + g.setColor(Color.CYAN); + 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 + } + + /** + * Called when ... + * (non-Javadoc) + * @see com.RickBarrette.asteroids.MovingSpaceObject#move(int, int) + */ + @Override + public void move(int scrnWidth, int scrnHeight) { + /* + * move() is called every frame that the game + * is run, so this ticks down the shot delay + */ + if (shotDelayLeft > 0) + shotDelayLeft--; + + super.move(scrnWidth, scrnHeight); + } + + /** + * @return radius of circle that approximates the ship + * @author ricky barrette + */ + public double getRadius() { + return radius; + } + + /** + * @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; + } +} \ No newline at end of file diff --git a/Asteroids/src/com/RickBarrette/asteroids/SpaceObject.java b/Asteroids/src/com/RickBarrette/asteroids/SpaceObject.java new file mode 100644 index 0000000..bef7994 --- /dev/null +++ b/Asteroids/src/com/RickBarrette/asteroids/SpaceObject.java @@ -0,0 +1,79 @@ +/** + * SpaceObject.java + * @date Mar 31, 2012 + * @author ricky barrette + * @author Twenty Codes, LLC + * + * 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; + +import java.awt.Color; + +/** + * This class will be used for keeping tack of the space objects within the game. + * More accuractly the object's location and color. + * @author ricky barrette + */ +public class SpaceObject { + + protected Color color; + protected double mX; + protected double mY; + + /** + * @return the color of this space object + * @author ricky barrette + */ + public Color getColor() { + return this.color; + } + + /** + * @return the space object's x location + * @author ricky barrette + */ + public double getX(){ + return mX; + } + + /** + * @return the space object's y location + * @author ricky barrette + */ + public double getY(){ + return mY; + } + + /** + * Sets the color of this space object + * @param c + * @author ricky barrette + */ + public void setColor(Color c) { + this.color = c; + } + + /** + * Sets the location of this space object + * @param x1 + * @param y1 + * @author ricky barrette + */ + public void setLocation(int x, int y) { + mX = x; + mY = y; + } +} \ No newline at end of file diff --git a/Asteroids/src/com/RickBarrette/asteroids/Status.java b/Asteroids/src/com/RickBarrette/asteroids/Status.java new file mode 100644 index 0000000..d98aa1e --- /dev/null +++ b/Asteroids/src/com/RickBarrette/asteroids/Status.java @@ -0,0 +1,60 @@ +/** + * Status.java + * @date Mar 31, 2012 + * @author ricky barrette + * @author Twenty Codes, LLC + * + * 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; + +import java.awt.BorderLayout; +import java.awt.Container; + +import javax.swing.JLabel; +import javax.swing.JPanel; + +/** + * This class will be used for keeping track of and displaying the game status + * information. + * @author ricky barrette + */ +public class Status extends JPanel { + + private static final long serialVersionUID = -169321993637429941L; + private JLabel status; + + /** + * Creates a new Status + * @param container + * @param g + * @author ricky barrette + */ + public Status(Container container, AsteroidGame g) { + JPanel northSubPanel = new JPanel(); + status = new JLabel("Missiles 0 Asteroids 0 Ships 0 Score 0 Time: 0"); + northSubPanel.add(status); + container.add(northSubPanel, BorderLayout.NORTH); + } + + /** + * Sets the status + * @param temp + * @author ricky barrette + */ + public void setStatus(String temp) { + status.setText(temp); + } +} \ No newline at end of file