Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added SpaceEvaders/bin/.DS_Store
Binary file not shown.
Binary file modified SpaceEvaders/bin/game/Polygon.class
Binary file not shown.
Binary file modified SpaceEvaders/bin/game/SpaceEvaders.class
Binary file not shown.
19 changes: 19 additions & 0 deletions SpaceEvaders/src/game/Asteroid.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
package game;

public class Asteroid extends Polygon implements Projectile {
private int XVelocity;
private int YVelocity;
Polygon hitbox;

public Asteroid() {
super(instantiateShape(), new Point(300, 0), 180);
this.scalePolygon(37);
XVelocity = 0;
YVelocity = 20;
hitbox = new Polygon(instantiateShape(), this.position, this.rotation);
}
private static Point[] instantiateShape(){
Point[] shape = {new Point(1,0), new Point(3,0), new Point(4,1), new Point(4,3), new Point(2,7), new Point(0,3), new Point(0,1), new Point(1,0)};
return shape;
}
@Override
public boolean checkCollision(Polygon other) {
// TODO Auto-generated method stub
return false;
}
}
5 changes: 0 additions & 5 deletions SpaceEvaders/src/game/BackgroundGenerator.java

This file was deleted.

53 changes: 49 additions & 4 deletions SpaceEvaders/src/game/BlackHole.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,55 @@
package game;

public class BlackHole extends Polygon {
import java.awt.Color;
import java.awt.Graphics;

public BlackHole(Point[] inShape, Point inPosition, double inRotation) {
super(inShape, inPosition, inRotation);
// TODO Auto-generated constructor stub
public class BlackHole extends Polygon {
private Polygon hitbox;
int counter = 0;

public BlackHole() {
super(instantiateShape(), new Point(400,300), 0);
this.scalePolygon(10);
Point[] shape = {new Point(1,0), new Point(3,0), new Point(4,1), new Point(4,3), new Point(3,4), new Point(1,4), new Point(0,3), new Point(0,1), new Point(1,0)};
hitbox = new Polygon(shape, this.findCenter(), this.rotation);
hitbox.scalePolygon(10);

}

private static Point[] instantiateShape() {
Point[] shape = {new Point(2,2), new Point(5,3), new Point(6,0), new Point(7,3), new Point(10,2), new Point(9,5), new Point(12,6), new Point(9,7), new Point(10,10), new Point(7,9), new Point(6,12), new Point(5,9), new Point(2,10), new Point(3,7), new Point(0,6), new Point(3,5)};
return shape;
}

public void paint(Graphics brush) {
counter++; //erase later
this.rotate(counter);//use get counter from space evaders class
int[] x2 = new int[this.getPoints().length];
int[] y2 = new int[this.getPoints().length];
for(int i = 0; i < x2.length; i++) {
x2[i] = (int) this.getPoints()[i].x;
y2[i] = (int) this.getPoints()[i].y;
}

brush.setColor(Color.DARK_GRAY);
brush.fillPolygon(x2, y2, x2.length);

hitbox.rotation = this.rotation;
hitbox.position = this.position;
int[] x3 = new int[hitbox.getPoints().length];
int[] y3 = new int[hitbox.getPoints().length];
for(int i = 0; i < x3.length; i++) {
x3[i] = (int) hitbox.getPoints()[i].x;
y3[i] = (int) hitbox.getPoints()[i].y;
}

brush.setColor(Color.BLACK);
brush.drawPolygon(x3, y3, x3.length);

counter++;
}

public getHitbox() {
//implement...
}
}
5 changes: 5 additions & 0 deletions SpaceEvaders/src/game/BorderGenerator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package game;
import java.awt.Graphics;
public interface BorderGenerator {
void generateBorder(Graphics brush);
}
9 changes: 9 additions & 0 deletions SpaceEvaders/src/game/Polygon.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@ public Point[] getPoints() {
return points;
}

public void scalePolygon(int scalar) {//makes stuff off center... figure out how to fix that?
for(int i = 0; i < shape.length; i++) {
shape[i] = new Point(shape[i].getX() * scalar, shape[i].getY() * scalar);
}
//shape = getPoints(); <- figure out how to re-center the dilated polygon...

}


// "contains" implements some magical math (i.e. the ray-casting algorithm).
public boolean contains(Point point) {
Point[] points = getPoints();
Expand Down
6 changes: 3 additions & 3 deletions SpaceEvaders/src/game/Projectile.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package game;

public interface Projectile {
Point getVelocity();
boolean checkBlackHoleCollision(BlackHole blackHole);
boolean checkShipCollision(Spaceship shipHitbox);
double getVelocity();
boolean checkCollision(Polygon other);

}
46 changes: 42 additions & 4 deletions SpaceEvaders/src/game/SpaceEvaders.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,37 @@
*/
import java.awt.*;
import java.awt.event.*;
import java.util.Iterator;

class SpaceEvaders extends Game {
static int counter = 0;
public static final int WIDTH = 800;
public static final int LENGTH = 600;

private BorderGenerator borderGen;
private Polygon borderHitbox;
static Spaceship thing1 = new Spaceship(new Point(300, 300), 0);
static Spaceship thing2 = new Spaceship(new Point(600, 300), 0);

public SpaceEvaders() {
super("Space Evaders", WIDTH, LENGTH);
this.setFocusable(true);
this.requestFocus();
borderGen = (Graphics brush) -> {
int[] XCoords = {50, 750, 750, 50};
int[] YCoords = {50, 50, 550, 550};

brush.setColor(Color.BLUE);
brush.drawPolygon(XCoords, YCoords, 4);
};
Point[] shape = {new Point(50,50), new Point(750,50), new Point(750, 550), new Point(50,550)};
borderHitbox = new Polygon(shape, new Point(400,300), 0);
this.addKeyListener(thing1);
this.addKeyListener(thing2);
}

public Polygon getBorderHitbox() {
return borderHitbox;
}
public void paint(Graphics brush) {

brush.setColor(Color.black);
Expand All @@ -33,20 +48,41 @@ public void paint(Graphics brush) {
// sample code for printing message for debugging
// counter is incremented and this message printed
// each time the canvas is repainted

borderGen.generateBorder(brush);

brush.setColor(Color.white);

thing1.paint(brush);
thing2.paint(brush);

Asteroid ast = new Asteroid();//please turn this process into a method in polygon!!!!!!
int[] x = new int[ast.getPoints().length];
int[] y = new int[ast.getPoints().length];
for(int i = 0; i < x.length; i++) {
x[i] = (int) ast.getPoints()[i].x;
y[i] = (int) ast.getPoints()[i].y + counter;
}

brush.drawPolygon(x, y, x.length);

BlackHole bh = new BlackHole();//please turn this process into a method in polygon!!!!!!
bh.paint(brush);


//for(int i = 0; ) {}
//int[] yMissileCords =
//int[] xMissileCords =
//brush.setColor(Color.ORANGE);
//brush.fillPolygon(xMissileCords, yMissileCords, 8);



/*
* the comment below is just me messing with the code before we meet
* officially on friday
* feel free to delete it or something it doesn't do anything important
*/
brush.drawString("Time: " + Integer.toString(counter / 60) + ":" + Integer.toString(counter % 60 * 5 / 3), 10, 10);
//brush.drawString("Time: " + Integer.toString(counter / 60) + ":" + Integer.toString(counter % 60 * 5 / 3), 10, 10);

counter++;
}

Expand All @@ -58,4 +94,6 @@ public static void main (String[] args) {
SpaceEvaders a = new SpaceEvaders();
a.repaint();
}


}
101 changes: 72 additions & 29 deletions SpaceEvaders/src/game/Spaceship.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Iterator;

public class Spaceship extends Polygon implements KeyListener{
public class Spaceship extends Polygon implements KeyListener, Iterable<Projectile>{

private static int id = 1;

private static final int SIZE = 30; // Will be deprecated once Hans is done
private static final double ACCELRATE = 0.1;
private static final int ROTATERATE = 3;
private static final int MAXMISSILES = 400;
private static final int MAXPROJECTILES = 400;

private int player;
private double xVel;
Expand All @@ -22,21 +23,20 @@ public class Spaceship extends Polygon implements KeyListener{
private boolean isTurningLeft;
private boolean isTurningRight;
private boolean missileFired;
private Missile[] missiles;
private Projectile[] firedProjectiles;

public Spaceship(Point position, double rotation) {
super(instantiateShape(), position, rotation);
xVel = 0.0;
yVel = 0.0;
xAccel = 0.0;
yAccel = 0.0;
missiles = new Missile[MAXMISSILES];
firedProjectiles = new Projectile[MAXPROJECTILES];
player = id++;

if (player == 2) {
this.rotate(180);
}

}

/*
Expand All @@ -45,13 +45,8 @@ public Spaceship(Point position, double rotation) {
* here.
*/
private static Point[] instantiateShape() {
Point[] square = new Point[4];
square[0] = new Point(0, 0);
square[1] = new Point(SIZE, 0);
square[2] = new Point(SIZE, SIZE);
square[3] = new Point(0, SIZE);

return square;
Point[] shape = {new Point(0,0), new Point(2,0), new Point(1,1), new Point(3,1), new Point(4,2), new Point(3,3), new Point(1,3), new Point(2,4), new Point(0,4), new Point(0,0)};
return shape;
}


Expand Down Expand Up @@ -112,23 +107,23 @@ private void handleMovement() {
private void fireMissile() {
int openMissile = canFireMissile();
if (openMissile != -1) {
missiles[openMissile] = new Spaceship.Missile(this, this.rotation);
firedProjectiles[openMissile] = new Spaceship.Missile(this, this.rotation);
}
}

private int canFireMissile() {
for (int i = 0; i < MAXMISSILES; i++) {
if (missiles[i] == null) {
for (int i = 0; i < MAXPROJECTILES; i++) {
if (firedProjectiles[i] == null) {
return i;
}
}
return -1;
}

private void drawMissiles(Graphics brush) {
for (int i = 0; i < MAXMISSILES; i++) {
if (missiles[i] != null) {
missiles[i].paint(brush);
for (int i = 0; i < MAXPROJECTILES; i++) {
if (firedProjectiles[i] != null) {
((Missile)firedProjectiles[i]).paint(brush);
}
}
}
Expand Down Expand Up @@ -244,14 +239,8 @@ public Missile(Spaceship spaceship, double rotation) {
* missile polygon construction code should go here.
*/
private static Point[] instantiateShape() {
Point[] square = new Point[4];

square[0] = new Point(0, 0);
square[1] = new Point(0, WIDTH);
square[2] = new Point(LENGTH, WIDTH);
square[3] = new Point(LENGTH, 0);

return square;
Point[] shape = {new Point(0,0), new Point(4,0), new Point(3,1), new Point(3,4), new Point(2,5), new Point(1,4), new Point(1,1), new Point(0,0)};
return shape;
}


Expand Down Expand Up @@ -331,7 +320,61 @@ public boolean checkShipCollision(Spaceship spaceship) {
}


// private class Mine extends Polygon implements Projectile {
//
// }
//comments for this iterator must explicitly state that it mutates as it traverses...
@Override
public Iterator<Projectile> iterator() {
Iterator<Projectile> it = new Iterator<Projectile>() {
int pos = 0; //so basically u call the next method manually and never use a for in enhaced for loop and it iterates to the next null after rmemoving any out of bounds missiles and then u just use the pos instance var to set that null value to a new missile; essentially the next method only serves to iterate the pos instance to the next null elemnt rather than returning the next null elemnt...
public boolean hasNext() {
eraseOutOfBoundsProjectiles();
if(pos < MAXPROJECTILES && hasNullsAfter(pos)) {
return true;
}
return false;
}
public Projectile next() {
eraseOutOfBoundsProjectiles();
pos++;
while(firedProjectiles[pos] != null) {
pos++;
}
return firedProjectiles[pos];
}

private boolean hasNullsAfter(int pos) {
for(int i = pos; i < firedProjectiles.length; i++) {
if(firedProjectiles[i] == null) {
return true;
}
}
return false;
}

public void eraseOutOfBoundsProjectiles() {
for(int i = 0; i < firedProjectiles.length; i++) {
if(!firedProjectiles[i].checkCollision(getBorderHitbox())) { //how can I access this method from the space evaders class?
firedProjectiles[i] = null;
}
}
}
};
return it;
}


private class Mine extends Polygon implements Projectile {
public Mine() {
super(instantiateShape(), Spaceship.position, Spaceship.roatation);
this.scalePolygon(7);
}

private static Point[] instantiateShape() {
Point[] shape = {new Point(1,0), new Point(3,0), new Point(4,1), new Point(4,3), new Point(3,4), new Point(1,4), new Point(0,3), new Point(0,1), new Point(1,0)};
return shape;
}


}


}