From faeb1b790cd865f769e5915fd8e932da1f554044 Mon Sep 17 00:00:00 2001 From: Ricky Barrette Date: Mon, 2 Apr 2012 11:46:34 -0400 Subject: [PATCH] Fixed space wrap for the asteroids Signed-off-by: Ricky Barrette --- .../com/RickBarrette/asteroids/Asteroid.java | 44 ++++++++++++++----- .../asteroids/MovingSpaceObject.java | 30 +++++++------ 2 files changed, 50 insertions(+), 24 deletions(-) diff --git a/Asteroids/src/com/RickBarrette/asteroids/Asteroid.java b/Asteroids/src/com/RickBarrette/asteroids/Asteroid.java index c3f848c..d596abe 100644 --- a/Asteroids/src/com/RickBarrette/asteroids/Asteroid.java +++ b/Asteroids/src/com/RickBarrette/asteroids/Asteroid.java @@ -23,9 +23,9 @@ package com.RickBarrette.asteroids; import java.awt.Color; 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 Drawable { @@ -37,23 +37,27 @@ public class Asteroid extends MovingSpaceObject implements Drawable { /** * Creates a new Asteroid - * @param x + * + * @param x * @param y * @param xVelocity * @param yVelocity - * @param numberSplit number of smaller asteroids to create after being blown up - * @param hitsLeft number of hits left + * @param numberSplit + * number of smaller asteroids to create after being blown up + * @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, AsteroidGame game) { mGame = game; mColor = Color.GRAY; mX = x; mY = y; - double vel=minVelocity + Math.random()*(maxVelocity-minVelocity); - mAngle = 2*Math.PI*Math.random(); // random direction - mXVelocity=vel*Math.cos(mAngle); - mYVelocity=vel*Math.sin(mAngle); + double vel = minVelocity + Math.random() * (maxVelocity - minVelocity); + mAngle = 2 * Math.PI * Math.random(); // random direction + mXVelocity = vel * Math.cos(mAngle); + mYVelocity = vel * Math.sin(mAngle); mNumberSplit = numberSplit; mHitsLeft = hitsLeft; @@ -64,8 +68,26 @@ public class Asteroid extends MovingSpaceObject implements Drawable { @Override public void draw(Graphics g) { - g.setColor(mColor); - g.fillOval((int)(mX-mRadius+.5),(int)(mY-mRadius+.5), (int)(2*mRadius),(int)(2*mRadius)); + g.setColor(mColor); + g.fillOval((int) (mX - mRadius + .5), (int) (mY - mRadius + .5), + (int) (2 * mRadius), (int) (2 * mRadius)); } + + /** + * (non-Javadoc) + * @see com.RickBarrette.asteroids.MovingSpaceObject#wrapSpace(int, int) + */ + @Override + public void wrapSpace(int scrnHeight, int scrnWidth) { + if (mX < 0 - mRadius) + mX += scrnHeight + 2 * mRadius; + else if (mX > scrnHeight + mRadius) + mX -= scrnHeight + 2 * mRadius; + + if (mY < 0 - mRadius) + mY += scrnWidth + 2 * mRadius; + else if (mY > scrnWidth + mRadius) + mY -= scrnWidth + 2 * mRadius; + } } \ 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 bab4786..67105a0 100644 --- a/Asteroids/src/com/RickBarrette/asteroids/MovingSpaceObject.java +++ b/Asteroids/src/com/RickBarrette/asteroids/MovingSpaceObject.java @@ -123,20 +123,9 @@ public class MovingSpaceObject extends SpaceObject implements Moveable{ mXVelocity *= mVelocityDecay; mYVelocity *= 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; + wrapSpace(scrnHeight, scrnWidth); } - + /** * Sets wether or not this space object is accelerating * (non-Javadoc) @@ -176,4 +165,19 @@ public class MovingSpaceObject extends SpaceObject implements Moveable{ this.isTurningRight = turningRight; } + /** + * wraps the space object around to the opposite side of the screen + * when it goes out of the screen's bounds + * @author ricky barrette + */ + public void wrapSpace(int scrnHeight, int scrnWidth) { + if (mX < 0) + mX += scrnHeight; + else if (mX > scrnHeight) + mX -= scrnHeight; + if (mY < 0) + mY += scrnWidth; + else if (mY > scrnWidth) + mY -= scrnWidth; + } } \ No newline at end of file