Commit d82d24ca by Joe Student

added dan's game

parent cec50d4b
Showing with 1789 additions and 0 deletions
package lec08.glab.danshiff.game.model;
import lec08.glab.danshiff.controller.Game;
import java.awt.*;
import java.util.ArrayList;
/**
* Created with IntelliJ IDEA.
* User: danshiff
* Date: 11/21/13
* Time: 12:40 PM
* To change this template use File | Settings | File Templates.
*/
public class Ant extends Foe{
public static final int LOWEST_SPEED = 10;
public static final int TOP = 530; //Where bug won't go above after going below.
private static double sSpeed = LOWEST_SPEED;
private boolean bGoingUp; //Is the ant going up.
private int nCode = STICKS; //Initially, the ant's y movement is sticks. This changes to strong sticks if it reaches
//the bottom.
private boolean bPoisoned;
public Ant(){
super();
setRadius(Game.STANDARD_RADIUS);
setFull(true);
ArrayList<Point> pntSs = new ArrayList<>();
pntSs.add(new Point(2, 9));
pntSs.add(new Point(4, 10));
pntSs.add(new Point(3, 8));
pntSs.add(new Point(4, 6));
pntSs.add(new Point(7, 7));
pntSs.add(new Point(5, 5));
pntSs.add(new Point(5, 2));
pntSs.add(new Point(8, 3));
pntSs.add(new Point(5, 1));
pntSs.add(new Point(5, -1));
pntSs.add(new Point(8, 1));
pntSs.add(new Point(5, -2));
pntSs.add(new Point(5, -3));
pntSs.add(new Point(4, -6));
pntSs.add(new Point(3, -8));
pntSs.add(new Point(2, -9));
assignPolarPoints(symmetricSprite(10, pntSs, -10));
setValue((int)(Ant.getSpeed()));
setColor(Color.ORANGE);
setDeltaX(sSpeed);
setValue((int)sSpeed);
}
/**
* Method for getting ant to turn.
*/
public void turn(){
setDeltaX(-1 * getDeltaX());
//If the bug reaches the bottom:
if(getCenter().y == Game.BOTTOM){
bGoingUp = true; //It starts going up
bPoisoned = false; //Isn't poisoned
setDeltaX(getDeltaX() + (.25 * (getDeltaX() > 0 ? 1 : -1))); //Goes a bit faster
nCode = STRONGSTICK; //Won't go arbitrarily high
}
if(getCenter().y == TOP){
bGoingUp = false; //So that it doesn't have weird behavior at top of player-zone
}
//Go up or down a row on the turn.
if(bGoingUp){
setCenter(new Point(getCenter().x, getCenter().y - (2 * Game.STANDARD_RADIUS)));
}
else{
setCenter(new Point(getCenter().x, getCenter().y + (2 * Game.STANDARD_RADIUS)));
}
setOrientation((getOrientation() + 180) % 360);
}
public static void setSpeed(double speed){
sSpeed = speed;
}
public static double getSpeed(){
return sSpeed;
}
/**
* Moves normally if not poisoned. Charges if poisoned.
*/
public void move(){
if(!bPoisoned){
move(STICKS, nCode);
}
else{
super.move(STICKS, nCode);
turn();
}
}
/**
* Gets poisoned by poison mushrooms. Turns at all other mushrooms and loses poison at them too.
* @param mus
*/
public void interactWithMushroom(Mushroom mus){
if(mus.isPoisonous()){
setbPoisoned(true);
setbGoingUp(false);
return;
}
setbPoisoned(false);
if(this.getCenter().x < mus.getCenter().x){
this.setCenter(
new Point(mus.getCenter().x - 2 - (2 * Game.STANDARD_RADIUS), mus.getCenter().y));
}
else{
this.setCenter(
new Point(mus.getCenter().x + 2 + (2 * Game.STANDARD_RADIUS), mus.getCenter().y));
}
this.turn();
}
/**
* When killed, they turn into mushrooms if there's space.
* @return
*/
public boolean kill(){
Point pntDied = this.getCenter();
Point pntShroom = new Point(1, 1);
pntShroom.setLocation((pntDied.x/(2*Game.STANDARD_RADIUS))*(2*Game.STANDARD_RADIUS) + Game.STANDARD_RADIUS, pntDied.y);
CommandCenter.addMushroom(new Mushroom(pntShroom));
return true;
}
public boolean isbPoisoned(){
return bPoisoned;
}
public void setbPoisoned(boolean bPoisoned){
this.bPoisoned = bPoisoned;
}
public boolean isbGoingUp(){
return bGoingUp;
}
public void setbGoingUp(boolean bGoingUp){
this.bGoingUp = bGoingUp;
}
}
package lec08.glab.danshiff.game.model;
import java.awt.*;
import java.util.ArrayList;
import lec08.glab.danshiff.controller.Game;
/**
* This is almost unchanged from the initial falcon class. Just drawn differently and has some power-up based private
* vars and getter and setter methods for them.
*/
public class BugHunter extends Sprite {
// ==============================================================
// FIELDS
// ==============================================================
public static final int PLAY_SPACE_ROWS = 7; //How many rows up the hunter can go.
public static final int SPEED = 7;
private int tripleShot;
private boolean laser;
// ==============================================================
// CONSTRUCTOR
// ==============================================================
public BugHunter() {
super();
setRadius(Game.STANDARD_RADIUS);
setFull(true);
// left
ArrayList<Point> pntSides = new ArrayList<>();
pntSides.add(new Point(7,-7));
setOrientation(UP);
assignPolarPoints(symmetricSprite(10, pntSides, -4));
setCenter(new Point((int)(Game.DIM.getWidth()/2), Game.BOTTOM));
}
// ==============================================================
// METHODS
// ==============================================================
public void move() {
super.move(LOOPS, STRONGSTICK);
} //end move
/**
* Gets hunter to start moving.
* @param nDir
*/
public void move(int nDir){
switch(nDir){
case UP:
setDeltaY(-SPEED);
break;
case DOWN:
setDeltaY(SPEED);
break;
case LEFT:
setDeltaX(-SPEED);
break;
case RIGHT:
setDeltaX(SPEED);
break;
default:
break;
}
}
/**
* Gets hunter to stop moving.
* @param nDir
*/
public void stopMove(int nDir){
switch(nDir){
case UP:
setDeltaY(0);
break;
case DOWN:
setDeltaY(0);
break;
case LEFT:
setDeltaX(0);
break;
case RIGHT:
setDeltaX(0);
break;
default:
break;
}
}
public void draw(Graphics g) {
drawHunterWithColor(g, Color.WHITE);
} //end draw()
public void drawHunterWithColor(Graphics g, Color col) {
super.draw(g);
g.setColor(col);
g.drawPolygon(getXcoords(), getYcoords(), dDegrees.length);
}
public int getTripleShot(){
return tripleShot;
}
public void setTripleShot(int tripleShot){
this.tripleShot = tripleShot;
//Color is set to purplish elsewhere when it picks up the floater.
if(this.tripleShot == 0){
setColor(Color.WHITE);
}
}
public boolean isLaser(){
return laser;
}
public void setLaser(boolean laser){
this.laser = laser;
if(this.laser){
setColor(Color.RED);
}
else{
setColor(Color.WHITE);
}
}
} //end class
package lec08.glab.danshiff.game.model;
import lec08.glab.danshiff.controller.Game;
import java.awt.Point;
import java.util.ArrayList;
public class Bullet extends Sprite {
private final double FIRE_POWER = Game.STANDARD_RADIUS * 2;
public Bullet(BugHunter fal){
super();
//defined the points on a cartesean grid
ArrayList<Point> pntCs = new ArrayList<Point>();
pntCs.add(new Point(0,3)); //top point
pntCs.add(new Point(1,-1));
pntCs.add(new Point(0,-2));
pntCs.add(new Point(-1,-1));
assignPolarPoints(pntCs);
//a bullet expires after 20 frames
setExpire( Game.ROW );
setRadius(6);
//everything is relative to the falcon ship that fired the bullet
setDeltaX( fal.getDeltaX() +
Math.cos( Math.toRadians( fal.getOrientation() ) ) * FIRE_POWER );
setDeltaY( fal.getDeltaY() +
Math.sin( Math.toRadians( fal.getOrientation() ) ) * FIRE_POWER );
setCenter( fal.getCenter() );
//set the bullet orientation to the falcon (ship) orientation
setOrientation(fal.getOrientation());
}
//override the expire method - once an object expires, then remove it from the arrayList.
public void expire(){
expire(CommandCenter.movFriends);
}
}
package lec08.glab.danshiff.game.model;
import lec08.glab.danshiff.controller.Game;
import lec08.glab.danshiff.sounds.Sound;
import java.awt.*;
/**
* Created with IntelliJ IDEA.
* User: danshiff
* Date: 11/27/13
* Time: 5:39 PM
* To change this template use File | Settings | File Templates.
*/
/**
* Slows the bugs for a bit.
*/
public class ChillPill extends Floater{
public static final Color CHILL = new Color(83, 180, 211);
public ChillPill(int x, int y){
super(CHILL, x, y);
}
public void performEffect(Game game){
CommandCenter.setChilled(CommandCenter.getChilled() + 450);
Sound.playSound("chill.wav");
}
}
package lec08.glab.danshiff.game.model;
import java.util.concurrent.CopyOnWriteArrayList;
import lec08.glab.danshiff.controller.Game;
import lec08.glab.danshiff.sounds.Sound;
// I only want one Command Center and therefore this is a perfect candidate for static
// Able to get access to methods and my movMovables ArrayList from the static context.
public class CommandCenter {
private static int nLives;
private static int nLevel;
private static long lScore;
private static BugHunter bhAvatar;
private static boolean bPlaying;
private static boolean bPaused;
private static int nChilled;
// These ArrayLists are thread-safe
public static CopyOnWriteArrayList<Movable> movDebris = new CopyOnWriteArrayList<>();
public static CopyOnWriteArrayList<Movable> movFriends = new CopyOnWriteArrayList<>();
public static CopyOnWriteArrayList<Movable> movFoes = new CopyOnWriteArrayList<>();
public static CopyOnWriteArrayList<Movable> movFungus = new CopyOnWriteArrayList<>(); //I added this.
public static CopyOnWriteArrayList<Movable> movFloaters = new CopyOnWriteArrayList<>();
//Want to make sure mushrooms do not overlap. This grid that tracks whether there's a shroom in each "cell" of the
//board does that. It gives parallel information, but the simplest alternatives do horrible things to running time
//(have each mushroom being added make sure there isn't a mushroom in the location it wants to go).
public static boolean[][] bMushrooms = new boolean[Game.ROW][Game.COL];
// Constructor made private - static Utility class only
private CommandCenter() {}
public static void initGame(){
setLevel(0);
setScore(0);
setLives(3);
setChilled(0);
spawnHunter(true);
}
// The parameter is true if this is for the beginning of the game, otherwise false
// When you spawn a new falcon, you need to decrement its number
public static void spawnHunter(boolean bFirst) {
if (getLives() != 0) {
bhAvatar = new BugHunter();
movFriends.add(bhAvatar);
if (!bFirst)
setLives(getLives() - 1);
}
//Bugs don't stay chilled upon dying.
setChilled(0);
Sound.playSound("shipspawn.wav");
}
/**
* Special method for adding mushrooms. Checks grid array to make sure there isn't overlapping. Then lets the grid
* know a shroom is there.
* @param musAdd
*/
public static void addMushroom(Mushroom musAdd){
int row = musAdd.getCenter().y/(Game.STANDARD_RADIUS * 2);
int col = musAdd.getCenter().x/(Game.STANDARD_RADIUS * 2);
//Make sure edge cases don't screw me up.
if(col >= Game.COL){
col = Game.COL - 1;
}
if(col < 0){
col = 0;
}
System.out.println("" + row + " " + col);
if(!bMushrooms[row][col]){
movFungus.add(musAdd);
}
bMushrooms[row][col] = true;
}
/**
* Special method for removing a shroom. Lets the grid know the shroom is gone.
* @param musRem
*/
public static void removeMushroom(Mushroom musRem){
int row = musRem.getCenter().y/(Game.STANDARD_RADIUS * 2);
int col = musRem.getCenter().x/(Game.STANDARD_RADIUS * 2);
if(col > Game.COL - 1 || col < 0){
movFungus.remove(musRem);
return;
}
movFungus.remove(musRem);
System.out.println("" + row + " " + col);
bMushrooms[row][col] = false;
}
public static void clearAll(){
movDebris.clear();
movFriends.clear();
movFoes.clear();
movFungus.clear();
movFloaters.clear();
bMushrooms = new boolean[Game.ROW][Game.COL];
}
public static boolean isPlaying() {
return bPlaying;
}
public static void setPlaying(boolean bPlaying) {
CommandCenter.bPlaying = bPlaying;
}
public static boolean isPaused() {
return bPaused;
}
public static void setPaused(boolean bPaused) {
CommandCenter.bPaused = bPaused;
}
public static boolean isGameOver() { //if the number of hunters is zero, then game over
if (getLives() == 0) {
return true;
}
return false;
}
public static int getLevel() {
return nLevel;
}
public static long getScore() {
return lScore;
}
public static void setScore(long lParam) {
setLives(getLives() + (int)((lParam / 1000)-(lScore / 1000)));
lScore = lParam;
}
public static void setLevel(int n) {
nLevel = n;
}
public static int getLives() {
return nLives;
}
public static void setLives(int nParam) {
nLives = nParam;
}
public static BugHunter getHunter(){
return bhAvatar;
}
public static void setHunter(BugHunter falParam){
bhAvatar = falParam;
}
public static int getChilled(){
return nChilled;
}
public static void setChilled(int nParam){
nChilled = nParam;
}
public static CopyOnWriteArrayList<Movable> getMovDebris() {
return movDebris;
}
public static CopyOnWriteArrayList<Movable> getMovFriends() {
return movFriends;
}
public static CopyOnWriteArrayList<Movable> getMovFoes() {
return movFoes;
}
public static CopyOnWriteArrayList<Movable> getMovFloaters() {
return movFungus;
}
}
package lec08.glab.danshiff.game.model;
import lec08.glab.danshiff.controller.Game;
import java.awt.*;
import java.util.ArrayList;
/**
* Created with IntelliJ IDEA.
* User: danshiff
* Date: 12/2/13
* Time: 12:46 PM
* To change this template use File | Settings | File Templates.
*/
/**
* Abstract class for various power-ups. They also move in a drunk walk.
*/
public abstract class Floater extends Sprite{
public static final int EXPIRE_TIME = 20 * 22; //I want about 20 sec 1000/45 ~22
public static final int INITIAL_SPEED_RANGE = 7, SPEED_CHANGE = 2, MAX_SPEED = 20;
public static final int TOP = 500, BOTTOM = 650;
private int mX, mY;
/**
* Constructor requires a color and a starting location.
* @param color
* @param x
* @param y
*/
public Floater(Color color, int x, int y){
setRadius(Game.STANDARD_RADIUS);
setColor(color);
setFull(true);
setRadius(Game.STANDARD_RADIUS);
setOrientation(UP);
setCenter(new Point(x, y));
ArrayList<Point> pntSs = new ArrayList<>();
pntSs.add(new Point(10, 0));
assignPolarPoints(symmetricSprite(10, pntSs, -10));
setDeltaX(Game.R.nextInt((INITIAL_SPEED_RANGE * 2) -1) - INITIAL_SPEED_RANGE);
setDeltaY(Game.R.nextInt((INITIAL_SPEED_RANGE * 2) -1) - INITIAL_SPEED_RANGE);
setExpire(EXPIRE_TIME);
}
/**
* Moves in a drunk walk. But won't go too high or low. Won't go too fast either
*/
public void move(){
super.move(LOOPS, STICKS);
//Reverse direction if too high or low.
if(getCenter().y < TOP){
setDeltaY(Game.R.nextInt(INITIAL_SPEED_RANGE + 1) );
}
if(getCenter().y >= BOTTOM){
setDeltaY(-1 * (Game.R.nextInt(INITIAL_SPEED_RANGE + 1)));
}
//Change speed a bit.
setDeltaX(getDeltaX() + Game.R.nextInt((SPEED_CHANGE * 2) + 1) - SPEED_CHANGE);
setDeltaY(getDeltaY() + Game.R.nextInt((SPEED_CHANGE * 2) + 1) - SPEED_CHANGE);
//Don't go too quickly in X-direction.
if(Math.abs(getDeltaX()) > MAX_SPEED){
setDeltaX(getDeltaX() * -.5);
}
}
public abstract void performEffect(Game game); //All floaters do something to the game.
public void expire(){
expire(CommandCenter.movFloaters);
}
}
package lec08.glab.danshiff.game.model;
import lec08.glab.danshiff.controller.Game;
import lec08.glab.danshiff.sounds.Sound;
import java.awt.*;
import java.util.ArrayList;
/**
* Created with IntelliJ IDEA.
* User: danshiff
* Date: 11/21/13
* Time: 4:56 PM
* To change this template use File | Settings | File Templates.
*/
/**
* Flies drop from the top and take two hits to kill. They also leave mushrooms in their wake.
*/
public class Fly extends Foe{
public static final int FLY_HITS = 2;
public static final int FLY_SCORE = 75;
public static final int SPEED_RANGE = 10, MIN_SPEED = 15;
private int mHits;
public Fly(){
setRadius(Game.STANDARD_RADIUS);
setValue(FLY_SCORE);
setOrientation(UP);
setDeltaY(Game.R.nextInt(1 + SPEED_RANGE) + MIN_SPEED);
setColor(Color.LIGHT_GRAY);
setCenter(new Point(Game.R.nextInt((int)Game.DIM.getWidth()), 3 * Game.STANDARD_RADIUS));
ArrayList<Point> pntSs = new ArrayList<>();
pntSs.add(new Point(1, 9));
pntSs.add(new Point(2, 8));
pntSs.add(new Point(4, 6));
pntSs.add(new Point(7, 7));
pntSs.add(new Point(8, 7));
pntSs.add(new Point(10, 6));
pntSs.add(new Point(10, 5));
pntSs.add(new Point(9, 4));
pntSs.add(new Point(7, 3));
pntSs.add(new Point(5, 3));
pntSs.add(new Point(5, 3));
pntSs.add(new Point(5, 1));
pntSs.add(new Point(4, -1));
pntSs.add(new Point(3, -2));
pntSs.add(new Point(2, -3));
assignPolarPoints(symmetricSprite(9, pntSs, -3));
Sound.playSound("fly.wav");
}
public int getHits(){
return mHits;
}
public void setHits(int hits){
mHits = hits;
}
public void move(){
super.move(LOOPS, LEAVES);
//The higher your level, the more shrooms you should have. This makes the fly's drop chance depend on
//the level and how many there already are.
if(Game.R.nextInt(CommandCenter.getLevel() * Game.ROW) > (2 * CommandCenter.movFungus.size())){
dropMushroom();
}
}
public void dropMushroom(){
//Makes sure the mushroom snaps to the underlying grid regardless of the fly's position.
int nX = ((getCenter().x/(2*Game.STANDARD_RADIUS)) * 2 * Game.STANDARD_RADIUS) + Game.STANDARD_RADIUS;
int nY = ((getCenter().y/(2*Game.STANDARD_RADIUS)) * 2 * Game.STANDARD_RADIUS) + Game.STANDARD_RADIUS;
if(nY != Game.BOTTOM){
CommandCenter.addMushroom(new Mushroom(nX, nY));
}
}
public void interactWithMushroom(Mushroom mus){
//Do nothing
}
/**
* Increases the hit count. Returns true if hit count is at limit.
* @return
*/
public boolean kill(){
this.setHits(this.getHits() + 1);
return this.getHits() == FLY_HITS;
}
}
package lec08.glab.danshiff.game.model;
/**
* Created with IntelliJ IDEA.
* User: danshiff
* Date: 12/2/13
* Time: 12:28 PM
* To change this template use File | Settings | File Templates.
*/
/**
* A Foe is any sort of bug.
*/
public abstract class Foe extends Sprite{
private long mValue; //How many points killing this is worth.
public abstract void interactWithMushroom(Mushroom mus); //Most bugs interact with mushrooms in some way.
public abstract boolean kill(); //Some bugs do weird things when they die. Boolean because not all bugs die with one
//hit.
public void setValue(long value){
mValue = value;
}
public long getValue(){
return mValue;
}
/**
* This override of the move method ensures that bugs will move at half their normal rate when slowed.
* @param nXCode Can Loop (like asteroids), Stick (can't leave frame), or Leave the world
* @param nYCode As above (except loop) but can also StrongStick (can't go above a certain location)
*/
public void move(int nXCode, int nYCode){
if(CommandCenter.getChilled() % 2 !=0){
return;
}
super.move(nXCode, nYCode);
}
}
package lec08.glab.danshiff.game.model;
import lec08.glab.danshiff.controller.Game;
import lec08.glab.danshiff.sounds.Sound;
import java.awt.*;
/**
* Created with IntelliJ IDEA.
* User: danshiff
* Date: 11/27/13
* Time: 5:52 PM
* To change this template use File | Settings | File Templates.
*/
/**
* Removes a third of the mushrooms from the game board.
*/
public class Fungicide extends Floater{
public static final Color CIDE = new Color(253, 255, 29);
public Fungicide(int x, int y){
super(CIDE, x, y);
}
public void performEffect(Game game){
game.mushpocolypse();
Sound.playSound("boom.wav");
}
}
package lec08.glab.danshiff.game.model;
import java.awt.*;
/**
* Created with IntelliJ IDEA.
* User: danshiff
* Date: 11/27/13
* Time: 6:20 PM
* To change this template use File | Settings | File Templates.
*/
/**
* Use this to create a laser (column of red bullets). They don't move, but they expire quickly.
*/
public class LaserBullet extends Bullet{
public LaserBullet(BugHunter fal){
super(fal);
setDeltaY(0);
setDeltaX(0);
setColor(Color.RED);
setExpire(4);
}
}
package lec08.glab.danshiff.game.model;
import lec08.glab.danshiff.controller.Game;
import java.awt.*;
/**
* Created with IntelliJ IDEA.
* User: danshiff
* Date: 11/27/13
* Time: 6:22 PM
* To change this template use File | Settings | File Templates.
*/
/**
* Makes the next shot a laser. Also takes away triple shot.
*/
public class LaserFloat extends Floater{
public static final Color LASER = Color.RED;
public LaserFloat(int x, int y){
super(LASER, x, y);
}
public void performEffect(Game game){
CommandCenter.getHunter().setTripleShot(0);
CommandCenter.getHunter().setLaser(true);
}
}
package lec08.glab.danshiff.game.model;
import java.awt.*;
public interface Movable {
//for the game to draw
public void move();
public void draw(Graphics g);
//for collisions
//if two objects are both friends or both foes, then nothing happens, otherwise exlode both
//the friend type may be DEBRIS, in which case it is inert
//public byte getFriend();
//when a foe explodes, your points increase
public int points();
public Point getCenter();
public int getRadius();
//for short-lasting objects only like powerUps, bullets, special weapons and UFOs
//controls nExpiry that clicks down to expire, then the object should remove itself
//see Bullet class for how this works.
public void expire();
//for fading objects
public void fadeInOut();
} //end Movable
package lec08.glab.danshiff.game.model;
import lec08.glab.danshiff.controller.Game;
import java.awt.*;
import java.util.ArrayList;
/**
* Created with IntelliJ IDEA.
* User: danshiff
* Date: 11/20/13
* Time: 2:40 PM
* To change this template use File | Settings | File Templates.
*/
public class Mushroom extends Sprite{
//Colors for shrooms in various states of destruction, poison.
public static final Color FULL = new Color(95, 187, 179),
QUARTER = new Color(50, 163, 255),
HALF = new Color(4, 9, 255),
THREEQ = new Color(31, 18, 155),
POISON_Q = new Color(0, 241, 21),
POISON_F = new Color(213, 255, 26),
POISON_H = new Color(43, 169, 6),
POISON_T = new Color(34, 105, 6);
private boolean mPoisonous;
private int mHits; //Mushroom takes four hits to destroy
public Mushroom(Point pntLocation){
super();
setRadius(Game.STANDARD_RADIUS);
setCenter(pntLocation);
mHits = 0;
mPoisonous = false;
setFull(true);
setColor(FULL);
setOrientation(UP);
drawMushroom();
}
/**
* Overload to make mushroom w/ location coordinates instead of the point.
* @param nX
* @param nY
*/
public Mushroom(int nX, int nY){
this(new Point(nX, nY));
}
/**
* Overload to make a shroom w/ one more hit spawn from a shroom.
* @param musHit
*/
public Mushroom(Mushroom musHit){
this(musHit.getCenter());
mHits = musHit.getHits() + 1;
switch(mHits){
case(1):
setColor(QUARTER);
break;
case(2):
setColor(HALF);
break;
case(3):
setColor(THREEQ);
break;
default:
break;
}
setPoisonous(musHit.isPoisonous());
}
public void drawMushroom(){
ArrayList<Point> pntSides = new ArrayList<>();
int bottom = -10;
pntSides.add(new Point(3,9));
pntSides.add(new Point(6,8));
pntSides.add(new Point(8,6));
pntSides.add(new Point(9,3));
pntSides.add(new Point(8,2));
pntSides.add(new Point(7,2));
pntSides.add(new Point(5,4));
pntSides.add(new Point(3,5));
pntSides.add(new Point(3,bottom+2 ));
pntSides.add(new Point(2,bottom+1 ));
assignPolarPoints(symmetricSprite(9, pntSides, bottom));
}
public int getHits(){
return mHits;
}
public void setHits(int hits){
mHits = hits;
}
public boolean isPoisonous(){
return mPoisonous;
}
public void setPoisonous(boolean poisonous){
mPoisonous = poisonous;
if(mPoisonous){
switch(mHits){
case 0:
setColor(POISON_F);
break;
case 1:
setColor(POISON_Q);
break;
case 2:
setColor(POISON_H);
break;
case 3:
setColor(POISON_T);
break;
}
}
}
}
package lec08.glab.danshiff.game.model;
import lec08.glab.danshiff.controller.Game;
import lec08.glab.danshiff.sounds.Sound;
import java.awt.*;
import java.util.ArrayList;
/**
* Created with IntelliJ IDEA.
* User: danshiff
* Date: 11/21/13
* Time: 8:30 PM
* To change this template use File | Settings | File Templates.
*/
/**
* Scorpions walk slowly and in a straight line. They are unlikely to touch you. But they do poison every mushroom they
* tocuh.
*/
public class Scorpion extends Foe{
public static final int SCORP_SCORE = 75;
public static final int SCORP_SPEED = 4;
public Scorpion(boolean left, int row){ //First param is whether it enters on the left side.
//Orientation, initial position (obviously), and sign of speed (but not
//absolute value) affected by this.
setRadius(Game.STANDARD_RADIUS);
setColor(Color.RED);
setFull(true);
setValue(SCORP_SCORE);
setOrientation(RIGHT + (left ? 0 : 180));
ArrayList<Point> pntCs = new ArrayList<>();
pntCs.add(new Point(0,7));
pntCs.add(new Point(1,7));
pntCs.add(new Point(2,6));
pntCs.add(new Point(3,4));
pntCs.add(new Point(6,6));
pntCs.add(new Point(9,4));
pntCs.add(new Point(6,7));
pntCs.add(new Point(8,8));
pntCs.add(new Point(7,6));
pntCs.add(new Point(5,4));
pntCs.add(new Point(4,2));
pntCs.add(new Point(4,1));
pntCs.add(new Point(6,1));
pntCs.add(new Point(4,0));
pntCs.add(new Point(4,-2));
pntCs.add(new Point(6,-3));
pntCs.add(new Point(4,-3));
pntCs.add(new Point(3,-5));
pntCs.add(new Point(6,-6));
pntCs.add(new Point(3,-6));
pntCs.add(new Point(2,-7));
pntCs.add(new Point(3,-7));
pntCs.add(new Point(6,-8));
pntCs.add(new Point(7,-6));
pntCs.add(new Point(6,-5));
pntCs.add(new Point(7,-3));
pntCs.add(new Point(5,-1));
pntCs.add(new Point(8,-2));
pntCs.add(new Point(9,-5));
pntCs.add(new Point(8,-8));
pntCs.add(new Point(5,-9));
pntCs.add(new Point(0,-8));
pntCs.add(new Point(-2,-6));
pntCs.add(new Point(-3,-5));
pntCs.add(new Point(-7,-4));
pntCs.add(new Point(-4,-3));
pntCs.add(new Point(-4,-2));
pntCs.add(new Point(-7,-1));
pntCs.add(new Point(-4,-1));
pntCs.add(new Point(-4,1));
pntCs.add(new Point(-7,2));
pntCs.add(new Point(-4,2));
pntCs.add(new Point(-3,3));
pntCs.add(new Point(-5,4));
pntCs.add(new Point(-6,4));
pntCs.add(new Point(-8,9));
pntCs.add(new Point(-6,6));
pntCs.add(new Point(-4,8));
pntCs.add(new Point(-5,5));
pntCs.add(new Point(-3,4));
pntCs.add(new Point(-1,7));
assignPolarPoints(pntCs);
setCenter(new Point(left ? 1 : (int)Game.DIM.getWidth() , row * (Game.STANDARD_RADIUS * 2) + Game.STANDARD_RADIUS));
setDeltaX(SCORP_SPEED * (left ? 1 : -1));
Sound.playSound("scorpion.wav");
}
/**
* Make the shroom poisonous.
* @param mus
*/
public void interactWithMushroom(Mushroom mus){
mus.setPoisonous(true);
}
/**
* Die without fanfare
* @return
*/
public boolean kill(){
return true;
}
}
package lec08.glab.danshiff.game.model;
import lec08.glab.danshiff.controller.Game;
import lec08.glab.danshiff.sounds.Sound;
import java.awt.*;
import java.util.ArrayList;
/**
* Created with IntelliJ IDEA.
* User: danshiff
* Date: 11/21/13
* Time: 6:17 PM
* To change this template use File | Settings | File Templates.
*/
/**
* Spiders scuttle around in and near your playing area. They eat mushrooms.
*/
public class Spider extends Foe{
public static final int SPIDER_SCORE = 50;
public static final int RADIUS = 15;
public static final int EATS_SHROOM_CHANCE = 4;
public static final int TOP = 400, BOTTOM = 665;
public Spider(boolean leftSide, int row){ //First param is whether it enters on left (or right). This affects
//starting center, and initial x-speed's sign.
setRadius(RADIUS);
setColor(new Color(154, 59, 15));
setFull(true);
setValue(SPIDER_SCORE);
setOrientation(UP);
ArrayList<Point> pntSs = new ArrayList<>();
pntSs.add(new Point(1,12));
pntSs.add(new Point(2,11));
pntSs.add(new Point(2,9));
pntSs.add(new Point(1,8));
pntSs.add(new Point(3,7));
pntSs.add(new Point(4,7));
pntSs.add(new Point(6,8));
pntSs.add(new Point(7, 9));
pntSs.add(new Point(8,11));
pntSs.add(new Point(10,14));
pntSs.add(new Point(9,11));
pntSs.add(new Point(5,6));
pntSs.add(new Point(6,4));
pntSs.add(new Point(11,6));
pntSs.add(new Point(13,10));
pntSs.add(new Point(13,4));
pntSs.add(new Point(6,1));
pntSs.add(new Point(7,-2));
pntSs.add(new Point(11,-3));
pntSs.add(new Point(13,-4));
pntSs.add(new Point(15,-7));
pntSs.add(new Point(15,-12));
pntSs.add(new Point(13,-6));
pntSs.add(new Point(7,-3));
pntSs.add(new Point(5,-7));
pntSs.add(new Point(8,-8));
pntSs.add(new Point(11,-10));
pntSs.add(new Point(12,-12));
pntSs.add(new Point(12,-14));
pntSs.add(new Point(8,-10));
pntSs.add(new Point(4,-8));
pntSs.add(new Point(1,-10));
assignPolarPoints(symmetricSprite(12, pntSs, -10));
//Horizontal speed is between 4 and 8
int xSpeed = Game.R.nextInt(5)+4;
if(leftSide){
setDeltaX(xSpeed);
}
else{
setDeltaX(-1 * xSpeed);
}
//Vertical speed is between 0 and 3.
setDeltaY(Game.R.nextInt(7)-3);
setCenter(new Point(leftSide ? 1 : (int)Game.DIM.getWidth()-1, row));
Sound.playSound("spider.wav");
}
public void interactWithMushroom(Mushroom musEat){
if(Game.R.nextInt(EATS_SHROOM_CHANCE)==0){
CommandCenter.removeMushroom(musEat);
}
}
public boolean kill(){
return true;
}
public void move(){
super.move(LEAVES, STICKS); //When they leave, they're gone.
//After moving, their speed randomly adjusts by a bit so they drunk walk.
setDeltaX(getDeltaX() -2 + Game.R.nextInt(5));
setDeltaY(getDeltaY() -1 + Game.R.nextInt(3));
//If they get too high or low, correct them w/ a hard change of the vertical speed.
if(getCenter().y < TOP){
setDeltaY(3);
}
if(getCenter().y > BOTTOM){
setDeltaY(-5);
}
}
}
package lec08.glab.danshiff.game.model;
import lec08.glab.danshiff.controller.Game;
import java.awt.*;
/**
* Created with IntelliJ IDEA.
* User: danshiff
* Date: 11/25/13
* Time: 5:51 PM
* To change this template use File | Settings | File Templates.
*/
/**
* Makes the next 20 shots triple shots (this can accumulate). Takes away laser.
*/
public class TripleShot extends Floater{
public static final Color TRIPLE = new Color(145, 89, 148);
public TripleShot(int x, int y){
super(TRIPLE, x, y);
}
public void performEffect(Game game){
CommandCenter.getHunter().setLaser(false);
CommandCenter.getHunter().setTripleShot(CommandCenter.getHunter().getTripleShot() + 20);
CommandCenter.getHunter().setColor(new Color(135, 12, 148));
}
}
package lec08.glab.danshiff.game.view;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GameFrame extends JFrame {
private JPanel contentPane;
private BorderLayout borderLayout1 = new BorderLayout();
public GameFrame() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
initialize();
} catch (Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void initialize() throws Exception {
contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(borderLayout1);
}
@Override
//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
}
package lec08.glab.danshiff.game.view;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Panel;
import java.awt.Point;
import java.util.concurrent.CopyOnWriteArrayList;
import lec08.glab.danshiff.controller.Game;
import lec08.glab.danshiff.game.model.*;
public class GamePanel extends Panel {
// ==============================================================
// FIELDS
// ==============================================================
// The following "off" vars are used for the off-screen double-bufferred image.
private Dimension dimOff;
private Image imgOff;
private Graphics grpOff;
private GameFrame gmf;
private Font fnt = new Font("SansSerif", Font.BOLD, 12);
private Font fntBig = new Font("SansSerif", Font.BOLD + Font.ITALIC, 36);
private FontMetrics fmt;
private int nFontWidth;
private int nFontHeight;
private String strDisplay = "";
// ==============================================================
// CONSTRUCTOR
// ==============================================================
public GamePanel(Dimension dim){
gmf = new GameFrame();
gmf.getContentPane().add(this);
gmf.pack();
initView();
gmf.setSize(dim);
gmf.setTitle("Game Base");
gmf.setResizable(false);
gmf.setVisible(true);
this.setFocusable(true);
}
// ==============================================================
// METHODS
// ==============================================================
private void drawScore(Graphics g) {
g.setColor(Color.white);
g.setFont(fnt);
if (CommandCenter.getScore() != 0) {
g.drawString("SCORE : " + CommandCenter.getScore() +
" LEVEL : " + CommandCenter.getLevel(), nFontWidth, nFontHeight);
} else {
g.drawString("NO SCORE", nFontWidth, nFontHeight);
}
}
@SuppressWarnings("unchecked")
public void update(Graphics g) {
if (grpOff == null || Game.DIM.width != dimOff.width
|| Game.DIM.height != dimOff.height) {
dimOff = Game.DIM;
imgOff = createImage(Game.DIM.width, Game.DIM.height);
grpOff = imgOff.getGraphics();
}
// Fill in background with black.
grpOff.setColor(Color.black);
grpOff.fillRect(0, 0, Game.DIM.width, Game.DIM.height);
drawScore(grpOff);
if(CommandCenter.getHunter() != null){
drawNumberShipsLeft(grpOff);
}
if (!CommandCenter.isPlaying()) {
displayTextOnScreen();
} else if (CommandCenter.isPaused()) {
strDisplay = "Game Paused";
grpOff.drawString(strDisplay,
(Game.DIM.width - fmt.stringWidth(strDisplay)) / 2, Game.DIM.height / 4);
}
//playing and not paused!
else {
//draw them in decreasing level of importance
//friends will be on top layer and debris on the bottom
iterateMovables(grpOff,
CommandCenter.movDebris,
CommandCenter.movFloaters,
CommandCenter.movFungus,
CommandCenter.movFoes,
CommandCenter.movFriends);
//drawNumberShipsLeft(grpOff);
if (CommandCenter.isGameOver()) {
CommandCenter.setPlaying(false);
//bPlaying = false;
}
}
//draw the double-Buffered Image to the graphics context of the panel
g.drawImage(imgOff, 0, 0, this);
}
//for each movable array, process it.
private void iterateMovables(Graphics g, CopyOnWriteArrayList<Movable>...movMovz){
for (CopyOnWriteArrayList<Movable> movMovs : movMovz) {
for (Movable mov : movMovs) {
mov.move();
mov.draw(g);
mov.fadeInOut();
mov.expire();
}
}
}
// Draw the number of falcons left on the bottom-right of the screen.
private void drawNumberShipsLeft(Graphics g) {
BugHunter fal = CommandCenter.getHunter();
double[] dLens = fal.getLengths();
int nLen = fal.getDegrees().length;
Point[] pntMs = new Point[nLen];
int[] nXs = new int[nLen];
int[] nYs = new int[nLen];
//convert to cartesean points
for (int nC = 0; nC < nLen; nC++) {
pntMs[nC] = new Point((int) (10 * dLens[nC] * Math.sin(Math
.toRadians(90) + fal.getDegrees()[nC])),
(int) (10 * dLens[nC] * Math.cos(Math.toRadians(90)
+ fal.getDegrees()[nC])));
}
//set the color to white
g.setColor(Color.white);
//for each falcon left (not including the one that is playing)
for (int nD = 1; nD < CommandCenter.getLives(); nD++) {
//create x and y values for the objects to the top right using cartesean points again
for (int nC = 0; nC < fal.getDegrees().length; nC++) {
nXs[nC] = pntMs[nC].x + Game.DIM.width - (20 * nD);
nYs[nC] = pntMs[nC].y + 10;
}
g.drawPolygon(nXs, nYs, nLen);
}
}
private void initView() {
Graphics g = getGraphics(); // get the graphics context for the panel
g.setFont(fnt); // take care of some simple font stuff
fmt = g.getFontMetrics();
nFontWidth = fmt.getMaxAdvance();
nFontHeight = fmt.getHeight();
g.setFont(fntBig); // set font info
}
// This method draws some text to the middle of the screen before/after a game
private void displayTextOnScreen() {
strDisplay = "GAME OVER";
grpOff.drawString(strDisplay,
(Game.DIM.width - fmt.stringWidth(strDisplay)) / 2, Game.DIM.height / 4 - 40);
strDisplay = "Watch Out for Bugs!";
grpOff.drawString(strDisplay,
(Game.DIM.width - fmt.stringWidth(strDisplay)) / 2, Game.DIM.height / 4);
strDisplay = "use the arrow keys to move";
grpOff.drawString(strDisplay,
(Game.DIM.width - fmt.stringWidth(strDisplay)) / 2, Game.DIM.height / 4
+ nFontHeight + 40);
strDisplay = "use the space bar to fire";
grpOff.drawString(strDisplay,
(Game.DIM.width - fmt.stringWidth(strDisplay)) / 2, Game.DIM.height / 4
+ nFontHeight + 80);
strDisplay = "'S' to Start";
grpOff.drawString(strDisplay,
(Game.DIM.width - fmt.stringWidth(strDisplay)) / 2, Game.DIM.height / 4
+ nFontHeight + 120);
strDisplay = "'P' to Pause";
grpOff.drawString(strDisplay,
(Game.DIM.width - fmt.stringWidth(strDisplay)) / 2, Game.DIM.height / 4
+ nFontHeight + 160);
strDisplay = "'Q' to Quit";
grpOff.drawString(strDisplay,
(Game.DIM.width - fmt.stringWidth(strDisplay)) / 2, Game.DIM.height / 4
+ nFontHeight + 200);
strDisplay = "Floating diamonds give you powers!";
grpOff.drawString(strDisplay,
(Game.DIM.width - fmt.stringWidth(strDisplay)) / 2, Game.DIM.height / 4
+ nFontHeight + 240);
strDisplay = "Laser";
grpOff.setColor(LaserFloat.LASER);
grpOff.drawString(strDisplay,
(Game.DIM.width - fmt.stringWidth(strDisplay)) / 2, Game.DIM.height / 4
+ nFontHeight + 260);
strDisplay = "Fungicide";
grpOff.setColor(Fungicide.CIDE);
grpOff.drawString(strDisplay,
(Game.DIM.width - fmt.stringWidth(strDisplay)) / 2, Game.DIM.height / 4
+ nFontHeight + 280);
strDisplay = "Chill Pill";
grpOff.setColor(ChillPill.CHILL);
grpOff.drawString(strDisplay,
(Game.DIM.width - fmt.stringWidth(strDisplay)) / 2, Game.DIM.height / 4
+ nFontHeight + 300);
strDisplay = "Triple Shot (x20)";
grpOff.setColor(TripleShot.TRIPLE);
grpOff.drawString(strDisplay,
(Game.DIM.width - fmt.stringWidth(strDisplay)) / 2, Game.DIM.height / 4
+ nFontHeight + 320);
/*
strDisplay = "left pinkie on 'A' for Shield";
grpOff.drawString(strDisplay,
(Game.DIM.width - fmt.stringWidth(strDisplay)) / 2, Game.DIM.height / 4
+ nFontHeight + 240);
strDisplay = "left index finger on 'F' for Guided Missile";
grpOff.drawString(strDisplay,
(Game.DIM.width - fmt.stringWidth(strDisplay)) / 2, Game.DIM.height / 4
+ nFontHeight + 280);
strDisplay = "'Numeric-Enter' for Hyperspace";
grpOff.drawString(strDisplay,
(Game.DIM.width - fmt.stringWidth(strDisplay)) / 2, Game.DIM.height / 4
+ nFontHeight + 320);
*/
}
public GameFrame getFrm() {return this.gmf;}
public void setFrm(GameFrame frm) {this.gmf = frm;}
}
\ No newline at end of file
package lec08.glab.danshiff.sounds;
import java.io.IOException;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
public class Sound {
// For individual wav sounds (not looped)
// http://stackoverflow.com/questions/26305/how-can-i-play-sound-in-java
public static synchronized void playSound(final String strPath) {
//this method commented-out becuase it was throwing exceptions
// new Thread(new Runnable() {
//
// public void run() {
// try {
// Clip clp = AudioSystem.getClip();
//
// InputStream audioSrc = Sound.class.getResourceAsStream(strPath);
// InputStream bufferedIn = new BufferedInputStream(audioSrc);
// AudioInputStream aisStream = AudioSystem.getAudioInputStream(bufferedIn);
//
// clp.open(aisStream);
// clp.start();
//
// } catch (Exception e) {
// System.err.println(e.getMessage());
// }
// }
//
// }).start();
}
// For looping wav clips
// http://stackoverflow.com/questions/4875080/music-loop-in-java
public static Clip clipForLoopFactory(String strPath){
Clip clp = null;
try {
AudioInputStream aisStream = AudioSystem.getAudioInputStream(Sound.class.getResourceAsStream(strPath));
clp = AudioSystem.getClip();
clp.open( aisStream );
} catch (UnsupportedAudioFileException exp) {
exp.printStackTrace();
} catch (IOException exp) {
exp.printStackTrace();
} catch (LineUnavailableException exp) {
exp.printStackTrace();
}catch(Exception exp){
System.out.println("error");
}
return clp;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>format</key>
<dict>
<key>bytePerSample</key>
<integer>4</integer>
<key>channelCount</key>
<integer>1</integer>
<key>sampleRate</key>
<real>32000</real>
</dict>
<key>info</key>
<dict>
<key>version</key>
<real>2</real>
</dict>
<key>playbackFormat</key>
<dict>
<key>bytePerSample</key>
<integer>4</integer>
<key>channelCount</key>
<integer>1</integer>
<key>sampleRate</key>
<real>32000</real>
</dict>
<key>revisions</key>
<array>
<dict>
<key>UID</key>
<integer>0</integer>
<key>channels</key>
<dict>
<key>channel_0</key>
<dict>
<key>blocks</key>
<array/>
<key>lengthInSamples</key>
<integer>0</integer>
</dict>
</dict>
<key>description</key>
<string>A newly created taf document.</string>
<key>flagged</key>
<false/>
<key>format</key>
<dict>
<key>bytePerSample</key>
<integer>4</integer>
<key>channelCount</key>
<integer>1</integer>
<key>sampleRate</key>
<real>32000</real>
</dict>
<key>label</key>
<string>Initial Revision</string>
<key>lengthInFrames</key>
<integer>0</integer>
<key>lengthInSeconds</key>
<real>0.0</real>
<key>markers</key>
<array/>
<key>metadata</key>
<dict>
<key>album</key>
<string></string>
<key>artist</key>
<string></string>
<key>comments</key>
<string></string>
<key>composer</key>
<string></string>
<key>copyright</key>
<string></string>
<key>genre</key>
<string></string>
<key>partOfCompilation</key>
<false/>
<key>tempo</key>
<string></string>
<key>title</key>
<string></string>
<key>trackNumber</key>
<string></string>
<key>trackNumberMax</key>
<string></string>
<key>year</key>
<string></string>
</dict>
<key>nextRevisionUID</key>
<integer>-1</integer>
<key>previousRevisionUID</key>
<integer>-1</integer>
</dict>
</array>
<key>size</key>
<dict>
<key>lengthInFrames</key>
<integer>0</integer>
<key>lengthInSeconds</key>
<real>0.0</real>
<key>sizeInByte</key>
<integer>0</integer>
</dict>
<key>state</key>
<dict>
<key>activeRevisionUID</key>
<integer>0</integer>
<key>nextRevisionUID</key>
<integer>0</integer>
</dict>
</dict>
</plist>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment