Commit 21713988 by Adam Gerber

Merge branch 'lab08g'

Conflicts:
	src/lec08/glab/blackjack/Hand.java
parents b59df3cd c5b73399
Showing with 3745 additions and 534 deletions
...@@ -20,170 +20,4 @@ public class BlackJack { ...@@ -20,170 +20,4 @@ public class BlackJack {
private Hand hanPlayer; private Hand hanPlayer;
private Shoe sho; private Shoe sho;
public BlackJack() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
ply = new Player(1000.00);
sho = new Shoe();
initHands();
}
public void initHands() {
hanPlayer = new Hand(false);
hanDealer = new Hand(true);
dlr = new Dealer(sho, hanDealer, hanPlayer);
dlr.hitDealer();
dlr.hitDealer();
dlr.hitPlayer();
dlr.hitPlayer();
}
public String status() {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("Dealer has:" + hanDealer);
stringBuilder.append("\n");
stringBuilder.append("You have :" + hanPlayer);
return stringBuilder.toString();
}
public Dealer getDealer() {
return dlr;
}
public String showStatusAfterHit() {
StringBuilder stringBuilder = new StringBuilder();
boolean bAces = hanPlayer.isThereAces();
int nBetter = hanPlayer.getBetterScore(hanPlayer.getSoftValue(), hanPlayer.getSemiSoftValue());
if (bAces) {
if (nBetter == 21) {
stringBuilder.append("BLACKJACK: " + nBetter);
ply.setMoney(ply.getMoney() + BET * 1.5);
initHands();
return showMoney(stringBuilder);
} else if (nBetter > 21) {
stringBuilder.append("BUSTED: " + nBetter);
ply.setMoney(ply.getMoney() - BET);
initHands();
return showMoney(stringBuilder);
}
}
//no aces
else {
if (hanPlayer.getHardValue() == 21) {
stringBuilder.append("BLACKJACK: " + hanPlayer.getHardValue());
ply.setMoney(ply.getMoney() + BET * 1.5);
initHands();
return showMoney(stringBuilder);
} else if (hanPlayer.getHardValue() > 21) {
stringBuilder.append("BUSTED: " + hanPlayer.getHardValue());
ply.setMoney(ply.getMoney() - BET);
initHands();
return showMoney(stringBuilder);
}
}
return stringBuilder.toString();
}
public String showStatusAfterDealerAutoHit(boolean bDoubleDown) {
double dBet = BET;
if (bDoubleDown)
dBet *= 2;
boolean bAces = hanPlayer.isThereAces();
int nBetter = hanPlayer.getBetterScore(hanPlayer.getSoftValue(), hanPlayer.getSemiSoftValue());
StringBuilder stringBuilder = new StringBuilder();
if (hanDealer.getHardValue() > 21) {
stringBuilder.append("DEALER BUSTED :" + hanDealer.getHardValue() + " YOU WIN");
ply.setMoney(ply.getMoney() + dBet);
return showMoney(stringBuilder);
}
if (bAces) {
if (nBetter > hanDealer.getHardValue()) {
stringBuilder.append("You:" +
" " + nBetter + " versus Dealer: " + hanDealer.getHardValue() + " YOU WIN");
ply.setMoney(ply.getMoney() + dBet);
} else if (nBetter < hanDealer.getHardValue()) {
stringBuilder.append("You: " + nBetter + " versus Dealer: " + hanDealer.getHardValue() + " YOU LOSE");
ply.setMoney(ply.getMoney() - dBet);
} else {
stringBuilder.append("You: " + nBetter + " versus Dealer: " + hanDealer.getHardValue() + " PUSH");
}
} else {
if (hanPlayer.getHardValue() > hanDealer.getHardValue()) {
stringBuilder.append("You: " + nBetter + " versus Dealer: " + hanDealer.getHardValue() + " YOU WIN");
ply.setMoney(ply.getMoney() + dBet);
} else if (hanPlayer.getHardValue() < hanDealer.getHardValue()) {
stringBuilder.append("You: " + nBetter + " versus Dealer: " + hanDealer.getHardValue() + " YOU LOSE");
ply.setMoney(ply.getMoney() - dBet);
} else {
stringBuilder.append("You: " + hanPlayer.getHardValue() + " versus Dealer: " + hanDealer.getHardValue() + " PUSH");
}
}
return showMoney(stringBuilder);
}
private String showMoney(StringBuilder stringBuilder) {
stringBuilder.append("\n########HAND OVER##########\n");
stringBuilder.append(hanDealer.showEndState());
stringBuilder.append("\n");
stringBuilder.append(hanPlayer.showEndState());
stringBuilder.append("\n");
stringBuilder.append(ply.getStringMoney());
stringBuilder.append("\n########HAND OVER##########\n");
stringBuilder.append("\n");
return stringBuilder.toString();
}
} }
...@@ -28,111 +28,12 @@ public class Card { ...@@ -28,111 +28,12 @@ public class Card {
} }
//public static enum
public static enum Face {
ACE('A'),
TWO('2'),
THREE('3'),
FOUR('4'),
FIVE('5'),
SIX('6'),
SEVEN('7'),
EIGHT('8'),
NINE('9'),
TEN('T'),
JACK('J'),
QUEEN('Q'),
KING('K');
private char value;
//you MUST make this constructor private!
private Face(char value) {
this.value = value;
}
public char getLetterVal(){
return value;
}
}
//instance members
private Face mFace;
private Suit mSuit;
//constructor
public Card(Face face, Suit suit) {
mFace = face;
mSuit = suit;
}
@Override
public String toString() {
return String.valueOf(mFace.getLetterVal()) + mSuit.getLetterVal();
}
private int getRank(Face face){
switch (face.getLetterVal()){
case '2':
return 2;
case '3':
return 3;
case '4':
return 4;
case '5':
return 5;
case '6':
return 6;
case '7':
return 7;
case '8':
return 8;
case '9':
return 9;
case 'T':
return 10;
case 'J':
return 10;
case 'K':
return 10;
case 'Q':
return 10;
case 'A':
return 11;
default:
return 0;
}
}
//getters and setters
public Face getFace() {
return mFace;
}
public void setFace(Face face) {
mFace = face;
}
public Suit getSuit() {
return mSuit;
}
public void setSuit(Suit suit) {
mSuit = suit;
}
public int getValue() {
return getRank(getFace());
}
} }
...@@ -11,38 +11,7 @@ import java.util.Scanner; ...@@ -11,38 +11,7 @@ import java.util.Scanner;
*/ */
public class ConsoleDriver { public class ConsoleDriver {
public static void main(String[] args) { public static void main(String[] args) {
BlackJack blackJack = new BlackJack();
Scanner scan = new Scanner(System.in);
outer:
while (true){
View.reportln(blackJack.status());
View.reportln("hit (h) stick (s) double down (d) or (x) to exit:");
String strResponse = scan.nextLine();
switch (strResponse){
case "h":
blackJack.getDealer().hitPlayer();
View.reportln(blackJack.showStatusAfterHit());
break;
case "s":
blackJack.getDealer().autoHit();
View.reportln(blackJack.showStatusAfterDealerAutoHit(false));
blackJack.initHands();
break;
case "d":
//one hit only and double the bet
blackJack.getDealer().hitPlayer();
blackJack.getDealer().autoHit();
View.reportln(blackJack.showStatusAfterDealerAutoHit(true));
blackJack.initHands();
break;
case "x":
break outer;
}
}
View.reportln("Thank you for playing.");
} }
......
...@@ -16,21 +16,6 @@ public class Dealer { ...@@ -16,21 +16,6 @@ public class Dealer {
this.hanPlayer = hanPlayer; this.hanPlayer = hanPlayer;
} }
public void autoHit(){
while(hanDealer.getBetterScore(hanDealer.getSoftValue(), hanDealer.getSemiSoftValue()) <=17){
hanDealer.hit(sho.deal());
}
}
public void hitPlayer(){
hanPlayer.hit(sho.deal());
}
public void hitDealer(){
hanDealer.hit(sho.deal());
}
public Hand getHanDealer() { public Hand getHanDealer() {
......
...@@ -8,164 +8,6 @@ public class Hand { ...@@ -8,164 +8,6 @@ public class Hand {
private boolean bDealer; private boolean bDealer;
public Hand(boolean bDealer) {
this.bDealer = bDealer;
carHandCards = new ArrayList<Card>();
}
public void hit(Card carHit) {
carHandCards.add(carHit);
}
public ArrayList<Card> getCards() {
return carHandCards;
}
public int getHardValue() {
//put up the values of hte cards on face value; aces are 11
int nRet = 0;
for (Card car : carHandCards) {
nRet += car.getValue();
}
return nRet;
}
public int getSoftValue() {
//put up ; count aces as one
int nRet = 0;
for (Card car : carHandCards) {
if (car.getValue() == 11) {
nRet += 1;
} else {
nRet += car.getValue();
}
}
return nRet;
}
public int getSemiSoftValue() {
int nRet = 0;
//are there any aces?
boolean bAcesPresent = isThereAces();
if (bAcesPresent && getSoftValue() >= 10) {
sortDescending();
//the first aces is 11
nRet += 11;
//start at index 1
for (int nC = 1; nC < carHandCards.size(); nC++) {
if (carHandCards.get(nC).getValue() == 11) {
nRet += 1;
} else {
nRet += carHandCards.get(nC).getValue();
}
}
} else {
nRet = getHardValue();
}
return nRet;
}
public boolean isThereAces() {
boolean bAcesPresent = false;
for (Card car : carHandCards) {
if (car.getValue() == 11) {
bAcesPresent = true;
break;
}
}
return bAcesPresent;
}
@Override
public String toString() {
StringBuilder stringBuilder = new StringBuilder();
int nC = 0;
for (Card carHandCard : carHandCards) {
if (nC++ == 0 && bDealer)
stringBuilder.append("##" + " ");
else
stringBuilder.append(carHandCard.toString() + " ");
}
if (!bDealer)
stringBuilder.append(getBetterScore(getSoftValue(), getSemiSoftValue()));
return stringBuilder.toString();
}
public String showEndState() {
StringBuilder stringBuilder = new StringBuilder();
for (Card carHandCard : carHandCards) {
stringBuilder.append(carHandCard.toString() + " ");
}
stringBuilder.append(getBetterScore(getSoftValue(), getSemiSoftValue()));
return stringBuilder.toString();
}
//gets the better score of player's hand for soft Aces
//soft is one ace
public int getBetterScore(int nSoft, int nSemi) {
if (nSoft <= 21 && nSemi <= 21) {
return nSemi;
} else if (nSoft <= 21 && nSemi > 21) {
return nSoft;
}
//busted
else {
return nSoft;
}
}
private void sortDescending() {
//sort in descending order
for (int nB = 0; nB < carHandCards.size(); nB++) {
for (int nC = nB + 1; nC < carHandCards.size(); nC++) {
if (carHandCards.get(nB).getValue() < carHandCards.get(nC).getValue()) {
//swap
Card carTemp = carHandCards.get(nB);
carHandCards.set(nB, carHandCards.get(nC));
carHandCards.set(nC, carTemp);
}
}
}
}
} }
...@@ -11,62 +11,8 @@ public class Shoe { ...@@ -11,62 +11,8 @@ public class Shoe {
private int nCount; private int nCount;
public Shoe() {
loadShoe();
//toggle to see shuffle effect
shuffle();
nCount = 0;
}
public void shuffle(){
//simple swapping operations
//for each card in the chute
//get a random swap index and swap
for (int nC = 0; nC < carCards.length; nC++) {
int nSwapIndex = (int) ( Math.random() * carCards.length);
Card carTemp = carCards[nC];
carCards[nC] = carCards[nSwapIndex];
carCards[nSwapIndex] = carTemp;
}
}
public void loadShoe(){
int nC = 0;
carCards = new Card[Card.Face.values().length * Card.Suit.values().length* DECKS];
for (int nDeck = 0; nDeck < DECKS; nDeck++) {
for (Card.Face face : Card.Face.values()) {
for (Card.Suit suit : Card.Suit.values()){
carCards[nC++] = new Card(face, suit);
}
}
}
}//end meth
//to avoid card-counting, a shoe is reshuffled by the dealer
public Card deal(){
if (nCount > (int)(carCards.length * SLUG)){
shuffle();
nCount = 0;
}
return carCards[nCount++];
}
//this is just for show //this is just for show
// public CardOld[] getCards(){ // public CardOld[] getCards(){
// return carCards; // return carCards;
......
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>
...@@ -12,17 +12,7 @@ import java.awt.*; ...@@ -12,17 +12,7 @@ import java.awt.*;
public class ReflectDriver { public class ReflectDriver {
public static void main(String[] args) { public static void main(String[] args) {
// Rectangle recMe = new Rectangle(5,6,45,89);
// Reflector.printClass(recMe.getClass());
//
// String strMe = "Adam";
// Reflector.printClass(strMe.getClass());
try {
Reflector.printClass("java.lang.Double");
} catch (ClassNotFoundException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
} }
} }
,2Ah5si3BMr .3HA
r:: ,H@G: .;&M; s@#@@. .3As
.:rS22r. ;@@@B .#@, ... @i s#. @@: .3@#@@ :A&r
:sXB#@@#Hh22XX#@: h# :@@ X@ ..... .S@@2 S5XX2A: iB #@r .2@#: @9 i@MM@: ,:
i@A;:, 2@r:#s .@@: @2 ..,,,.,iB@@S. @@SS52@2 SH G@2s@@; .@r ,A@i @M h@@A 2i:.
@S ri ,sX@XA A@i #2 ..,,,.r@@2 s@..,.A@i5G s@@r ;@, r@M, 2@. .#h3@S ;@##@@#
X# ....@@. ;Xr@@: . r@5.M ......:ii29Xi; .@@@@#Ai92 .. . .. i@ .9@5 @G r@, M@: A# .;2@
;@r .,. ,:s5:M@A ..@@. rs#A5S .... s#;@; :Bs ... .. G,X@ ,##: ,s. h@ &A @@;#; ;@:
#A ... 2hS23h@9M . sS 3;9@AS: ;5S@@. i@, .... @.HB;@X . &@2 ..@S ;#r .@@M .. @@
5@. .. 9X;@2::Xi .. .:,#:;@Xr22srrriM##i #3irr;:,.:XSsG @#9, .. . 3;3@ SM . ,@r .. ;@S
,@i . #.@ iA .,:sAG3hS@@5 .:sX9S; X@@@@@@@@@@#hX@@: .. ,..A.@93i ... ;. ., M@
M# :;;;& #9 M@SH@@@@@@BXir;, .,;riS&M@@@@@AXsXXr& 2@H ... rH,@i
r@i9HHBAH@@ 2hi;, .:s3B@@@#HXh@A ,. @:9@
sMH2r:. :r5G@@@@AS;2Xsi& @S
:SH@@@@#H:9@
,;s5hr
___________________________________________________________________________________________________________________
LINK TO DEMO VIDEO- https://www.youtube.com/watch?v=WXvOjMfTUmk
CONTROLS- Use the arrow keys to control pac man, he will automatically move in whatever direction he is
facing, provided there is not a wall in front of him. Do not hold them down, as the arrow keys do not respond as well to
being held down versus letter keys.
Press 'S' to start the game
KEY FEATURES- The game plays very true to the original Pac-Man, most notably in the ghost logic. The board consists of a
28 x 36 grid, which contains the playable space as well as life and score display. All 4 ghosts use the same logic to determine
their directions as they do in the original game, all code for ghost logic is original, based on several publications and websites
discussing the way they decide to move.
Ghosts have 3 modes: Scatter, Chase, and Frightened. They start the game in scatter mode for 7 seconds, then chase mode for 20, then
back to scatter for 7. This pattern continues for 3 cycles until they stay in perpetual chase mode.
When a PacMan eats an Energizer, one of the big dots, the ghosts enter Frightened mode and turn blue. As the levels progress,
the time that the ghosts stay frightened. When the ghosts are in frightened mode, they turn randomly.
When the ghosts are in Scatter mode, they each try to move as close to a pre-determined square as possible. When they are in chase mode
they each try to get as close to a certain target square as possible, more details on each ghosts logic are available on each ghost's
class file.
Ghosts make their decision one turn in advance. When they reach a square, they immediately look to the next square and decide
which way they will turn, if it all. Ghosts ONLY reverse direction when a mode change occurs, provided they have not just completed
a turn and are still in the same square.
BONUS LIFE- As in the original, the player is awarded a bonus life when their score reaches 10,000
FRUIT- After 70 dots, a cherry pops up and awards pac-man 100 bonus points if eaten.
In addition to these features, there are a couple of extra features:
Press 'O' for Ouija mode, this will summon an extra ghost for each time it's pressed. Avoid holding it down as ghosts spawned
at exactly the same time will essentially act as one ghost, more on that later.
Press 'G' for Ghostbuster mode, this will give pac-man a 3 second proton-pack which will allow him to suck up ghosts and
get him out of a jam. You only get 3 of these per game.
package lec08.pmnehls.finalproject.game.model;
import java.awt.*;
import java.util.ArrayList;
/**
* Created by pmnehls on 12/1/14.
*/
public class Cherry extends Sprite
{
public Cherry()
{
super();
ArrayList<Point> pntCs = new ArrayList<Point>();
pntCs.add(new Point(6, 6));
pntCs.add(new Point(6, 4));
pntCs.add(new Point(4, 4));
pntCs.add(new Point(4, 2));
pntCs.add(new Point(3, 2));
pntCs.add(new Point(3, 1));
pntCs.add(new Point(2, 1));
pntCs.add(new Point(2, 0));
pntCs.add(new Point(4, 0));
pntCs.add(new Point(4, -1));
pntCs.add(new Point(5, -1));
pntCs.add(new Point(5, -5));
pntCs.add(new Point(4, -5));
pntCs.add(new Point(4, -6));
pntCs.add(new Point(0, -6));
pntCs.add(new Point(0, -5));
pntCs.add(new Point(-1, -5));
pntCs.add(new Point(-1, -1));
pntCs.add(new Point(0, -1));
pntCs.add(new Point(0, 0));
pntCs.add(new Point(1, 0));
pntCs.add(new Point(1, 1));
pntCs.add(new Point(2, 1));
pntCs.add(new Point(2, 2));
pntCs.add(new Point(3, 2));
pntCs.add(new Point(3, 4));
pntCs.add(new Point(2, 4));
pntCs.add(new Point(2, 3));
pntCs.add(new Point(0, 3));
pntCs.add(new Point(0, 2));
pntCs.add(new Point(-1, 2));
pntCs.add(new Point(-1, 1));
pntCs.add(new Point(0, 1));
pntCs.add(new Point(0, 0));
pntCs.add(new Point(-1, 0));
pntCs.add(new Point(-1, -1));
pntCs.add(new Point(-2, -1));
pntCs.add(new Point(-2, -4));
pntCs.add(new Point(-5, -4));
pntCs.add(new Point(-5, -3));
pntCs.add(new Point(-6, -3));
pntCs.add(new Point(-6, 1));
pntCs.add(new Point(-5, 1));
pntCs.add(new Point(-5, 2));
pntCs.add(new Point(-1, 2));
pntCs.add(new Point(-1, 3));
pntCs.add(new Point(0, 3));
pntCs.add(new Point(0, 4));
pntCs.add(new Point(2, 4));
pntCs.add(new Point(2, 5));
pntCs.add(new Point(3, 5));
pntCs.add(new Point(3, 6));
assignPolarPoints(pntCs);
setColor(Color.RED);
//put cherry in correct location
setCenter(new Point(TargetSpace.TS_WIDTH*(14),
TargetSpace.TS_HEIGHT*(20) + TargetSpace.TS_HEIGHT/2));
setOrientation(270);
setRadius(14);
}
public void draw(Graphics g)
{
super.draw(g);
g.fillPolygon(getXcoords(), getYcoords(), dDegrees.length);
}
}
package lec08.pmnehls.finalproject.game.model;
import java.util.concurrent.CopyOnWriteArrayList;
import lec08.pmnehls.finalproject.controller.Game;
import lec08.pmnehls.finalproject.sounds.Sound;
import javax.sound.sampled.Clip;
public class CommandCenter {
private static int nNumFalcon;
private static int nLevel = 1;
private static long lScore;
private static Pacman pacman;
private static boolean bPlaying;
private static boolean bIntroDone = false;
private static boolean bPaused;
private static int nProtonTick;
private static boolean bProton;
// These ArrayLists are thread-safe
public static CopyOnWriteArrayList<Movable> movDebris = new CopyOnWriteArrayList<Movable>();
public static CopyOnWriteArrayList<Movable> movFriends = new CopyOnWriteArrayList<Movable>();
public static CopyOnWriteArrayList<Movable> movPacman = new CopyOnWriteArrayList<Movable>();
public static CopyOnWriteArrayList<Movable> movFoes = new CopyOnWriteArrayList<Movable>();
public static CopyOnWriteArrayList<Movable> movFloaters = new CopyOnWriteArrayList<Movable>();
public static CopyOnWriteArrayList<Movable> movEnergizers = new CopyOnWriteArrayList<Movable>();
public static CopyOnWriteArrayList<Movable> movDots = new CopyOnWriteArrayList<Movable>();
public static CopyOnWriteArrayList<Movable> movLives = new CopyOnWriteArrayList<Movable>();
public static CopyOnWriteArrayList<Movable> movProton = new CopyOnWriteArrayList<Movable>();
//create board as a grid of target spaces
public static TargetSpace[][] grid = new TargetSpace[28][36];
// Constructor made private - static Utility class only
private CommandCenter() {}
public static void initGame(){
setMaze();
setLevel(nLevel);
setTimers();
setScore(0);
initPacmanLives();
initProtonPacks();
}
public static void setMaze()
{
//loop over TargetSpaces and initialize contents
for (int nD = 0; nD < 36; nD++)
{
for (int nC = 0; nC < 28; nC++)
{
grid[nC][nD] = new TargetSpace(nC+1, nD+1);
if(grid[nC][nD].getIsEnergizer())
{
movEnergizers.add(new Energizer(nC+1, nD+1));
}
else if (grid[nC][nD].getIsDot())
{
movDots.add(new Dot(nC+1, nD+1));
}
else if (grid[nC][nD].getIsGhostBox())
{
movDebris.add(new GhostBox(nC+1, nD+1));
}
else if (grid[nC][nD].getIsWall())
{
movDebris.add(new Wall(nC+1, nD+1));
}
}
}
}
public static void setTimers()
{
if(nLevel == 1)
{
Game.setScaredSeconds(6);
}
else if (nLevel == 2)
{
Game.setScaredSeconds(5);
}
else if (nLevel == 3)
{
Game.setScaredSeconds(4);
}
else if (nLevel == 4)
{
Game.setScaredSeconds(3);
}
else if (nLevel == 5)
{
Game.setScaredSeconds(2);
}
else if (nLevel == 6)
{
Game.setScaredSeconds(5);
}
else if (nLevel >= 7 && nLevel < 10)
{
Game.setScaredSeconds(2);
}
else
{
Game.setScaredSeconds(1);
}
}
public static void playIntro()
{
Game.setnTick(0);
Sound.playSound("pacman_beginning.wav");
bIntroDone = true;
}
public static void initPacmanLives()
{
for (int nC = 0; nC < Game.getLives() - 1; nC++)
{
movLives.add(new Life(TargetSpace.TS_WIDTH * (1 + (2 * nC)), TargetSpace.TS_HEIGHT*35));
}
}
public static void initProtonPacks()
{
for (int nC = 0; nC < Game.getProtonsLeft(); nC++)
{
movLives.add(new ProtonPackDisplay(TargetSpace.TS_WIDTH * (14 + (2 * nC)), TargetSpace.TS_HEIGHT*35));
}
}
public static void spawnPacman(boolean bFirst)
{
if (bFirst)
{
pacman = new Pacman();
movPacman.add(pacman);
}
else
{
pacman = new Pacman();
movPacman.add(pacman);
}
}
public static void spawnCherry()
{
Cherry cherry = new Cherry();
movDots.add(cherry);
}
public static void spawnBlinky(boolean bFirstBlinky)
{
if (bFirstBlinky)
{
Blinky blinky = new Blinky(true);
//Eyes eyes = new Eyes(blinky);
movFoes.add(blinky);
//movDebris.add(eyes);
}
else
{
Blinky blinky = new Blinky(false);
blinky.setRespawn(true);
movFoes.add(blinky);
}
}
public static void spawnPinky(boolean bFirstPinky)
{
if (bFirstPinky)
{
Pinky pinky = new Pinky();
movFoes.add(pinky);
}
else
{
Pinky pinky = new Pinky();
pinky.setRespawn(true);
movFoes.add(pinky);
}
}
public static void spawnInky(boolean bFirstInky)
{
if (bFirstInky)
{
Inky inky = new Inky();
movFoes.add(inky);
}
else
{
Inky inky = new Inky();
inky.setRespawn(true);
movFoes.add(inky);
}
}
public static void spawnClyde(boolean bFirstClyde)
{
if (bFirstClyde)
{
Clyde clyde = new Clyde();
movFoes.add(clyde);
}
else
{
Clyde clyde = new Clyde();
clyde.setRespawn(true);
movFoes.add(clyde);
}
}
public static void clearAll(){
movDebris.clear();
movFriends.clear();
movFoes.clear();
movDots.clear();
movEnergizers.clear();
movFloaters.clear();
movPacman.clear();
movProton.clear();
}
public static void startNextLevel(int nLevel)
{
initPacmanLives();
initProtonPacks();
setMaze();
setLevel(nLevel);
Game.setnTick(0);
Game.setDotCounter(0);
Game.setEnergizerCounter(0);
Game.setIsInvincible(false);
Game.setFruitBool(false);
}
public static void spawnAllAfterPause()
{
Game.setnTick(0);
setTimers();
spawnPacman(true);
spawnBlinky(true);
spawnPinky(true);
spawnInky(true);
spawnClyde(true);
Game.setIsInvincible(false);
Game.getSiren().loop(Clip.LOOP_CONTINUOUSLY);
}
public static void initialSpawn()
{
setTimers();
spawnPacman(true);
spawnBlinky(true);
spawnPinky(true);
spawnInky(true);
spawnClyde(true);
}
public static void resetLevel()
{
movLives.clear();
movProton.clear();
Game.setnTick(0);
initPacmanLives();
initProtonPacks();
setTimers();
}
public static void ouija(int nTick)
{
spawnBlinky(false);
}
public static void protonPack()
{
Sound.playSound("export.wav");
if (movPacman.size() != 0)
{
pacman = (Pacman)movPacman.get(0);
Proton proton = new Proton(pacman);
proton.setOrientation(pacman.getOrientation());
movProton.add(proton);
CommandCenter.nProtonTick = Game.getnTick();
bProton = true;
movLives.clear();
initPacmanLives();
initProtonPacks();
}
}
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 pacman's lives = 0, then game over
if (Game.getLives() == 0) {
Game.setStarted(false);
Game.setLives(4);
Game.setProtonsLeft(3);
Game.setRespawnAfterDeath(false);
Game.setInitial(true);
Game.setFruitBool(false);
Game.setDotCounter(0);
return true;
}
return false;
}
public static int getLevel() {
return nLevel;
}
public static long getScore() {
return lScore;
}
public static void setScore(long lParam) {
lScore = lParam;
}
public static void setLevel(int n) {
nLevel = n;
}
//public static int getNumFalcons() {
// return nNumFalcon;
//}
//public static Falcon getFalcon(){
// return falShip;
//}
public static Pacman getPacman() { return pacman;}
public static boolean getProton()
{
return CommandCenter.bProton;
}
public static void setProton(boolean bProton)
{
CommandCenter.bProton = bProton;
}
public static int getProtonTick()
{
return CommandCenter.nProtonTick;
}
}
package lec08.pmnehls.finalproject.game.model;
import java.awt.*;
import java.util.ArrayList;
/**
* Created by pmnehls on 11/19/14.
*/
public class Dot extends Sprite
{
private int nDotX;
private int nDotY;
public Dot(int x, int y)
{
super();
nDotX = x;
nDotY = y;
ArrayList<Point> pntCs = new ArrayList<Point>();
pntCs.add(new Point(2, 4));
pntCs.add(new Point(4, 2));
pntCs.add(new Point(4, -2));
pntCs.add(new Point(2, -4));
pntCs.add(new Point(-2, -4));
pntCs.add(new Point(-4, -2));
pntCs.add(new Point(-4, 2));
pntCs.add(new Point(-2, 4));
assignPolarPoints(pntCs);
int nHeight = TargetSpace.TS_HEIGHT;
int nWidth = TargetSpace.TS_WIDTH;
setCenter(new Point(nHeight * (x - 1) + nHeight/2, nWidth * (y - 1) + nWidth/2));
setRadius(2);
}
@Override
public void draw(Graphics g) {
super.draw(g);
g.fillPolygon(getXcoords(), getYcoords(), dDegrees.length);
}
}
package lec08.pmnehls.finalproject.game.model;
import lec08.pmnehls.finalproject.controller.Game;
import java.awt.*;
import java.util.ArrayList;
/**
* Created by pmnehls on 11/19/14.
*/
public class Energizer extends Sprite
{
private int nEnergizerX;
private int nEnergizerY;
public Energizer(int x, int y)
{
super();
nEnergizerX = x;
nEnergizerY = y;
ArrayList<Point> pntCs = new ArrayList<Point>();
pntCs.add(new Point(2, 4));
pntCs.add(new Point(4, 2));
pntCs.add(new Point(4, -2));
pntCs.add(new Point(2, -4));
pntCs.add(new Point(-2, -4));
pntCs.add(new Point(-4, -2));
pntCs.add(new Point(-4, 2));
pntCs.add(new Point(-2, 4));
assignPolarPoints(pntCs);
setColor(new Color(200,200,200));
int nHeight = TargetSpace.TS_HEIGHT;
int nWidth = TargetSpace.TS_WIDTH;
setCenter(new Point(nHeight * (x - 1) + nHeight/2, nWidth * (y - 1) + nWidth/2));
setRadius(8);
}
public void draw(Graphics g) {
super.draw(g);
g.fillPolygon(getXcoords(), getYcoords(), dDegrees.length);
}
}
package lec08.pmnehls.finalproject.game.model;
import lec08.pmnehls.finalproject.sounds.Sound;
import java.awt.*;
import java.util.ArrayList;
/**
* Created by pmnehls on 12/3/14.
*/
public class Eyes extends Sprite
{
private Movable ghost;
public Eyes(Movable ghost) {
super();
ArrayList<Point> pntCs = new ArrayList<Point>();
pntCs.add(new Point(-5, -1));
pntCs.add(new Point(-5, 2));
pntCs.add(new Point(-4, 2));
pntCs.add(new Point(-4, 3));
pntCs.add(new Point(-2, 3));
pntCs.add(new Point(-2, 2));
pntCs.add(new Point(-1, 2));
pntCs.add(new Point(-1, 0));
pntCs.add(new Point(1, 0));
pntCs.add(new Point(1, 2));
pntCs.add(new Point(2, 2));
pntCs.add(new Point(2, 3));
pntCs.add(new Point(4, 3));
pntCs.add(new Point(4, 2));
pntCs.add(new Point(5, 2));
pntCs.add(new Point(5, -1));
pntCs.add(new Point(4, -1));
pntCs.add(new Point(4, -2));
pntCs.add(new Point(2, -2));
pntCs.add(new Point(2, -1));
pntCs.add(new Point(1, -1));
pntCs.add(new Point(1, 0));
pntCs.add(new Point(-1, 0));
pntCs.add(new Point(-1, -1));
pntCs.add(new Point(-2, -1));
pntCs.add(new Point(-2, -2));
pntCs.add(new Point(-4, -2));
pntCs.add(new Point(-4, -1));
assignPolarPoints(pntCs);
setColor(Color.WHITE);
this.ghost = ghost;
setCenter(ghost.getCenter());
//facing left
setOrientation(270);
//this is the size of Eyes
setRadius(6);
}
public void draw(Graphics g)
{
super.draw(g);
g.fillPolygon(getXcoords(), getYcoords(), dDegrees.length);
}
public void move()
{
super.move();
setCenter(ghost.getCenter());
}
public Movable getGhost()
{
return ghost;
}
}
package lec08.pmnehls.finalproject.game.model;
import java.awt.*;
import java.util.ArrayList;
/**
* Created by pmnehls on 11/20/14.
*/
public class GhostBox extends Sprite
{
private boolean isLid;
public GhostBox(int x, int y)
{
super();
if (y == 16 && (x == 14 || x == 15))
{
isLid = true;
}
ArrayList<Point> pntCs = new ArrayList<Point>();
if (isLid)
{
pntCs.add(new Point(1, 5));
pntCs.add(new Point(1, -5));
pntCs.add(new Point(-1, -5));
pntCs.add(new Point(-1, 5));
assignPolarPoints(pntCs);
setColor(Color.WHITE);
setRadius(8);
}
else
{
pntCs.add(new Point(1, 1));
pntCs.add(new Point(1, -1));
pntCs.add(new Point(-1, -1));
pntCs.add(new Point(-1, 1));
assignPolarPoints(pntCs);
setColor(new Color(0,0,180));
setRadius(6);
}
int nHeight = TargetSpace.TS_HEIGHT;
int nWidth = TargetSpace.TS_WIDTH;
setCenter(new Point(nWidth * (x - 1) + nWidth/2, nHeight * (y - 1) + nHeight/2));
}
@Override
public void draw(Graphics g) {
super.draw(g);
g.fillPolygon(getXcoords(), getYcoords(), dDegrees.length);
}
}
package lec08.pmnehls.finalproject.game.model;
import java.awt.*;
import java.util.ArrayList;
/**
* Created by pmnehls on 11/23/14.
*/
public class Life extends Sprite
{
public Life(int x, int y)
{
super();
ArrayList<Point> pntCs = new ArrayList<Point>();
pntCs.add(new Point(3, 7));
pntCs.add(new Point(3, 6));
pntCs.add(new Point(5, 6));
pntCs.add(new Point(5, 5));
pntCs.add(new Point(6, 5));
pntCs.add(new Point(6, 3));
pntCs.add(new Point(4, 3));
pntCs.add(new Point(4, 2));
pntCs.add(new Point(1, 2));
pntCs.add(new Point(1, 1));
pntCs.add(new Point(-2, 1));
pntCs.add(new Point(-2, 0));
pntCs.add(new Point(1, 0));
pntCs.add(new Point(1, -1));
pntCs.add(new Point(4, -1));
pntCs.add(new Point(4, -2));
pntCs.add(new Point(6, -2));
pntCs.add(new Point(6, -4));
pntCs.add(new Point(5, -4));
pntCs.add(new Point(5, -5));
pntCs.add(new Point(3, -5));
pntCs.add(new Point(3, -6));
pntCs.add(new Point(-2, -6));
pntCs.add(new Point(-2, -5));
pntCs.add(new Point(-4, -5));
pntCs.add(new Point(-4, -4));
pntCs.add(new Point(-5, -4));
pntCs.add(new Point(-5, -2));
pntCs.add(new Point(-6, -2));
pntCs.add(new Point(-6, 3));
pntCs.add(new Point(-5, 3));
pntCs.add(new Point(-5, 5));
pntCs.add(new Point(-4, 5));
pntCs.add(new Point(-4, 6));
pntCs.add(new Point(-2, 6));
pntCs.add(new Point(-2, 7));
assignPolarPoints(pntCs);
setColor(Color.yellow);
//put life in correct location
setCenter(new Point(x, y - TargetSpace.TS_HEIGHT/4));
setOrientation(270);
setRadius(12);
}
public void draw(Graphics g)
{
super.draw(g);
g.fillPolygon(getXcoords(), getYcoords(), dDegrees.length);
}
}
package lec08.pmnehls.finalproject.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();
public int getOrientation();
public void setRespawn(boolean bRespawn);
public boolean getRespawn();
} //end Movable
package lec08.pmnehls.finalproject.game.model;
import java.awt.*;
import java.util.ArrayList;
import java.util.Random;
/**
* Created by pmnehls on 12/3/14.
*/
public class Proton extends Sprite
{
private Pacman pacman;
public Proton(Pacman pacman)
{
super();
ArrayList<Point> pntCs = new ArrayList<Point>();
pntCs.add(new Point(0, 0));
pntCs.add(new Point(0, 1));
pntCs.add(new Point(6, 1));
pntCs.add(new Point(6, 2));
pntCs.add(new Point(13, 2));
pntCs.add(new Point(13, 3));
pntCs.add(new Point(17, 3));
pntCs.add(new Point(17, -3));
pntCs.add(new Point(13, -3));
pntCs.add(new Point(13, -2));
pntCs.add(new Point(6, -2));
pntCs.add(new Point(6, -1));
pntCs.add(new Point(0, -1));
assignPolarPoints(pntCs);
setColor(Color.WHITE);
setCenter(pacman.getCenter());
//facing left
setOrientation(270);
//this is the size of Eyes
setRadius(30);
this.pacman = pacman;
}
public void draw(Graphics g)
{
super.draw(g);
g.fillPolygon(getXcoords(), getYcoords(), dDegrees.length);
}
public void move()
{
super.move();
if (pacman.getOrientation() == 0)
{
setCenter(new Point(pacman.getCenter().x, pacman.getCenter().y - 12));
}
else if (pacman.getOrientation() == 90)
{
setCenter(new Point(pacman.getCenter().x + 12, pacman.getCenter().y));
}
else if (pacman.getOrientation() == 180)
{
setCenter(new Point(pacman.getCenter().x, pacman.getCenter().y + 12));
}
else if (pacman.getOrientation() == 270)
{
setCenter(new Point(pacman.getCenter().x - 12, pacman.getCenter().y));
}
Random ran = new Random();
setColor(new Color(ran.nextInt(255), ran.nextInt(255), ran.nextInt(255)));
setOrientation(pacman.getOrientation());
}
}
package lec08.pmnehls.finalproject.game.model;
import java.awt.*;
import java.util.ArrayList;
/**
* Created by pmnehls on 12/4/14.
*/
public class ProtonPackDisplay extends Sprite
{
public ProtonPackDisplay(int x, int y)
{
super();
ArrayList<Point> pntCs = new ArrayList<Point>();
pntCs.add(new Point(-1, -6));
pntCs.add(new Point(0, -6));
pntCs.add(new Point(2, -4));
pntCs.add(new Point(2, -2));
pntCs.add(new Point(1, -1));
pntCs.add(new Point(1, 3));
pntCs.add(new Point(2, 3));
pntCs.add(new Point(2, 1));
pntCs.add(new Point(3, 0));
pntCs.add(new Point(3, 7));
pntCs.add(new Point(4, 7));
pntCs.add(new Point(4, -4));
pntCs.add(new Point(0, -7));
pntCs.add(new Point(-1, -7));
pntCs.add(new Point(-1, -6));
pntCs.add(new Point(-2, -6));
pntCs.add(new Point(-2, -8));
pntCs.add(new Point(0, -8));
pntCs.add(new Point(5, -4));
pntCs.add(new Point(5, 7));
pntCs.add(new Point(4, 8));
pntCs.add(new Point(3, 8));
pntCs.add(new Point(2, 7));
pntCs.add(new Point(2, 4));
pntCs.add(new Point(-5, 4));
pntCs.add(new Point(-5, 5));
pntCs.add(new Point(-6, 5));
pntCs.add(new Point(-6, 1));
pntCs.add(new Point(-5, 1));
pntCs.add(new Point(-5, -4));
pntCs.add(new Point(-3, -6));
assignPolarPoints(pntCs);
setColor(Color.GRAY);
//put pack display in correct location
setCenter(new Point(x, y - TargetSpace.TS_HEIGHT/4));
setOrientation(270);
setRadius(12);
}
public void draw(Graphics g)
{
super.draw(g);
g.fillPolygon(getXcoords(), getYcoords(), dDegrees.length);
}
}
package lec08.pmnehls.finalproject.game.model;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.util.ArrayList;
import java.util.Random;
import lec08.pmnehls.finalproject.controller.Game;
public abstract class Sprite implements Movable {
//the center-point of this sprite
private Point pntCenter;
//this causes movement; change in x and change in y
private double dDeltaX, dDeltaY;
//every sprite needs to know about the size of the gaming environ
private Dimension dim; //dim of the gaming environment
//the radius of circumscibing circle
private int nRadius;
//is this DEBRIS, FRIEND, FOE, OR FLOATER
//private byte yFriend;
//degrees (where the sprite is pointing out of 360)
private int nOrientation;
private int nExpiry; //natural mortality (short-living objects)
//the color of this sprite
private Color col;
//radial coordinates
//this game uses radial coordinates to render sprites
public double[] dLengths;
public double[] dDegrees;
//for drawing alternative
//public double[] dLengthAlts;
//public double[] dDegreeAlts;
//fade value for fading in and out
private int nFade;
//these are used to draw the polygon. You don't usually need to interface with these
private Point[] pntCoords; //an array of points used to draw polygon
private int[] nXCoords;
private int[] nYCoords;
public void move() {
Point pnt = getCenter();
double dX = pnt.x + getDeltaX();
double dY = pnt.y + getDeltaY();
//this just keeps the sprite inside the bounds of the frame
if (pnt.x > getDim().width) {
setCenter(new Point(1, pnt.y));
} else if (pnt.x < 0) {
setCenter(new Point(getDim().width - 1, pnt.y));
} else if (pnt.y > getDim().height) {
setCenter(new Point(pnt.x, 1));
} else if (pnt.y < 0) {
setCenter(new Point(pnt.x, getDim().height - 1));
} else {
setCenter(new Point((int) dX, (int) dY));
}
}
public Sprite() {
//you can override this and many more in the subclasses
setDim(Game.DIM);
setColor(Color.WHITE);
setCenter(new Point(Game.R.nextInt(Game.DIM.width),
Game.R.nextInt(Game.DIM.height)));
}
public void setExpire(int n) {
nExpiry = n;
}
public double[] getLengths() {
return this.dLengths;
}
public void setLengths(double[] dLengths) {
this.dLengths = dLengths;
}
public double[] getDegrees() {
return this.dDegrees;
}
public void setDegrees(double[] dDegrees) {
this.dDegrees = dDegrees;
}
public Color getColor() {
return col;
}
public void setColor(Color col) {
this.col = col;
}
public int points() {
//default is zero
return 0;
}
public int getExpire() {
return nExpiry;
}
public boolean isExploding() {
return false;
}
public void fadeInOut() {
};
@Override
public int getOrientation() {
return nOrientation;
}
public void setOrientation(int n) {
nOrientation = n;
}
public void setDeltaX(double dSet) {
dDeltaX = dSet;
}
public void setDeltaY(double dSet) {
dDeltaY = dSet;
}
public double getDeltaY() {
return dDeltaY;
}
public double getDeltaX() {
return dDeltaX;
}
public int getRadius() {
return nRadius;
}
public void setRadius(int n) {
nRadius = n;
}
public Dimension getDim() {
return dim;
}
public void setDim(Dimension dim) {
this.dim = dim;
}
public Point getCenter() {
return pntCenter;
}
public void setCenter(Point pntParam) {
pntCenter = pntParam;
}
public void setYcoord(int nValue, int nIndex) {
nYCoords[nIndex] = nValue;
}
public void setXcoord(int nValue, int nIndex) {
nXCoords[nIndex] = nValue;
}
public int getYcoord( int nIndex) {
return nYCoords[nIndex];// = nValue;
}
public int getXcoord( int nIndex) {
return nXCoords[nIndex];// = nValue;
}
public int[] getXcoords() {
return nXCoords;
}
public int[] getYcoords() {
return nYCoords;
}
public void setXcoords( int[] nCoords) {
nXCoords = nCoords;
}
public void setYcoords(int[] nCoords) {
nYCoords =nCoords;
}
protected double hypot(double dX, double dY) {
return Math.sqrt(Math.pow(dX, 2) + Math.pow(dY, 2));
}
//utility function to convert from cartesian to polar
//since it's much easier to describe a sprite as a list of cartesean points
//sprites (except Asteroid) should use the cartesean technique to describe the coordinates
//see Falcon or Bullet constructor for examples
protected double[] convertToPolarDegs(ArrayList<Point> pntPoints) {
//ArrayList<Tuple<Double,Double>> dblCoords = new ArrayList<Tuple<Double,Double>>();
double[] dDegs = new double[pntPoints.size()];
int nC = 0;
for (Point pnt : pntPoints) {
dDegs[nC++]=(Math.toDegrees(Math.atan2(pnt.y, pnt.x))) * Math.PI / 180 ;
}
return dDegs;
}
//utility function to convert to polar
protected double[] convertToPolarLens(ArrayList<Point> pntPoints) {
double[] dLens = new double[pntPoints.size()];
//determine the largest hypotenuse
double dL = 0;
for (Point pnt : pntPoints)
if (hypot(pnt.x, pnt.y) > dL)
dL = hypot(pnt.x, pnt.y);
int nC = 0;
for (Point pnt : pntPoints) {
if (pnt.x == 0 && pnt.y > 0) {
dLens[nC] = hypot(pnt.x, pnt.y) / dL;
} else if (pnt.x < 0 && pnt.y > 0) {
dLens[nC] = hypot(pnt.x, pnt.y) / dL;
} else {
dLens[nC] = hypot(pnt.x, pnt.y) / dL;
}
nC++;
}
// holds <thetas, lens>
return dLens;
}
protected void assignPolarPoints(ArrayList<Point> pntCs) {
setDegrees(convertToPolarDegs(pntCs));
setLengths(convertToPolarLens(pntCs));
}
public void draw(Graphics g) {
nXCoords = new int[dDegrees.length];
nYCoords = new int[dDegrees.length];
//need this as well
pntCoords = new Point[dDegrees.length];
for (int nC = 0; nC < dDegrees.length; nC++) {
nXCoords[nC] = (int) (getCenter().x + getRadius()
* dLengths[nC]
* Math.sin(Math.toRadians(getOrientation()) + dDegrees[nC]));
nYCoords[nC] = (int) (getCenter().y - getRadius()
* dLengths[nC]
* Math.cos(Math.toRadians(getOrientation()) + dDegrees[nC]));
//need this line of code to create the points which we will need for debris
pntCoords[nC] = new Point(nXCoords[nC], nYCoords[nC]);
}
g.setColor(getColor());
g.drawPolygon(getXcoords(), getYcoords(), dDegrees.length);
}
public Point[] getObjectPoints() {
return pntCoords;
}
public void setObjectPoints(Point[] pntPs) {
pntCoords = pntPs;
}
public void setObjectPoint(Point pnt, int nIndex) {
pntCoords[nIndex] = pnt;
}
public void expire() {
}
public int getFadeValue() {
return nFade;
}
public void setFadeValue(int n) {
nFade = n;
}
@Override
public void setRespawn(boolean bRespawn)
{
}
@Override
public boolean getRespawn()
{
return false;
}
}
package lec08.pmnehls.finalproject.game.model;
import java.awt.*;
/**
* Created by pmnehls on 11/19/14.
*
* Target Spaces play a key role in this game. The grid is composed of 28x36 target spaces. The long equations below determine
* which square is a wall, which is a dot, and which is empty. passing in an x,y value can tell a ghost or pacman what is in any
* given square, telling them if they need to stop, turn, or if they are eating a dot
*/
public class TargetSpace extends Sprite
{
private boolean isWall;
private boolean isDot;
private boolean isEmpty;
private boolean isEnergizer;
private boolean isGhostBox;
private boolean isReachable;
public static final int TS_HEIGHT= 16;
public static final int TS_WIDTH = 16;
private Sprite inside;
private Point pCenter;
private int nX;
private int nY;
public TargetSpace(int x, int y)
{
nX = x;
nY = y;
if((x == 2 && y == 7) || (x == 2 && y == 27) || (x == 27 && y == 7) || (x == 27 && y == 27))
{
isEnergizer = true;
isReachable = true;
this.inside = new Energizer(x, y);
}
else if (((y == 5 || y == 24) && (x != 1 && x != 14 && x!= 15 && x != 28)) || ((y == 9 || y == 33) && (x != 1 && x != 28))
|| ((y == 12 || y == 30) && ((x != 1 && x != 8 && x != 9 && x != 14 && x != 15 && x != 20 && x != 21 && x != 28)))
|| (y == 27 && ((x != 1 && x != 5 && x != 6 && x != 14 && x != 15 && x != 23 && x != 24 && x != 28)))
|| ((y == 6 || y == 8 || y == 25 || y == 26) && (x == 2 || x == 7 || x == 13 || x == 16 || x == 22 || x == 27 ))
|| (y == 7 && (x == 7 || x == 13 || x == 16 || x == 22))
|| ((y == 10 || y == 11) && (x == 2 || x == 7 || x == 10 || x == 19 || x == 22 || x == 27 ))
|| (y > 12 && y < 24) && (x == 7 || x == 22)
|| (y == 28 || y == 29) && (x == 4 || x == 7 || x == 10 || x == 19 || x == 22 || x == 25)
|| (y == 31 || y == 32) && (x == 2 || x == 13 || x == 16 || x == 27))
{
this.isDot = true;
this.isReachable = true;
this.inside = new Dot(x, y);
}
else if (((y == 13 || y == 14) && (x == 13 || x == 16)) || ((y == 15 || y == 21) && ( x > 9 && x < 20))
|| ((y == 16 || y == 17 || y == 18 || y == 19 || y == 20 || y == 21 || y == 23 || y == 24) && (x == 10 || x == 19))
|| (y == 18 && (x != 7 && x != 22 && (x < 11 || x > 18))) || (y == 27 && (x == 14 || x == 15))
|| (y == 22 && (x == 10 || x == 19)))
{
isEmpty = true;
isReachable = true;
}
else if(y == 1 || y == 2 || y == 3 || y == 35 || y == 36 || (y == 7 && ( x == 4 || x == 5 || x == 9 || x == 10 || x == 11
|| x == 18 || x == 19 || x == 20 || x == 24 || x == 25 )) || ((y == 14 || y == 15 || y == 16 || y == 20
|| y == 21 || y == 22) && ( x < 6 || x > 23)))
{
isEmpty = true;
}
else if ((y > 15 && y < 21 ) && (x > 10 && x < 19 ))
{
if (y == 16 || y == 20)
{
isGhostBox = true;
isWall = true;
}
else if (x == 11 || x == 18)
{
isGhostBox = true;
isWall = true;
}
}
else
{
isWall = true;
this.inside = new Wall(x ,y );
}
pCenter = new Point(TS_HEIGHT * (x-1) + TS_HEIGHT/2, TS_WIDTH * (y-1) + TS_WIDTH/2);
setCenter(new Point(pCenter));
}
public boolean getIsWall()
{
return isWall;
}
public boolean getIsDot()
{
return isDot;
}
public void setIsDot(boolean isDot)
{
this.isDot = isDot;
}
public boolean getIsEmpty()
{
return isEmpty;
}
public boolean getIsEnergizer()
{
return isEnergizer;
}
public void setIsEnergizer(boolean isEnergizer)
{
this.isEnergizer = isEnergizer;
}
public boolean getIsGhostBox()
{
return isGhostBox;
}
public boolean getIsReachable()
{
return isReachable;
}
public Movable getInside()
{
return inside;
}
public Point getTargetCenter()
{
return pCenter;
}
public int getSpaceX()
{
return nX;
}
public void setSpaceX(int x)
{
this.nX = x;
}
public int getSpaceY()
{
return nY;
}
public void setSpaceY(int y)
{
this.nY = y;
}
public Point getSpacePoint()
{
return new Point(nX, nY);
}
public Point getSpaceCenterCoord()
{
return pCenter;
}
@Override
public void setRespawn(boolean bRespawn)
{
}
}
package lec08.pmnehls.finalproject.game.model;
import java.awt.*;
import java.util.ArrayList;
/**
* Created by pmnehls on 11/19/14.
*/
public class Wall extends Sprite
{
public Wall(int x, int y)
{
super();
ArrayList<Point> pntCs = new ArrayList<Point>();
//simple square wall
pntCs.add(new Point(1, 1));
pntCs.add(new Point(1, -1));
pntCs.add(new Point(-1, -1));
pntCs.add(new Point(-1, 1));
assignPolarPoints(pntCs);
setColor(new Color(0,0,180));
setCenter(new Point(TargetSpace.TS_WIDTH * x - TargetSpace.TS_WIDTH/2,
TargetSpace.TS_HEIGHT* y - TargetSpace.TS_HEIGHT/2));
setRadius(6);
}
@Override
public void draw(Graphics g) {
super.draw(g);
g.fillPolygon(getXcoords(), getYcoords(), dDegrees.length);
}
}
package lec08.pmnehls.finalproject.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.pmnehls.finalproject.game.view;
import java.awt.*;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.CopyOnWriteArrayList;
import javax.swing.JFrame;
import lec08.pmnehls.finalproject.controller.Game;
import lec08.pmnehls.finalproject.game.model.CommandCenter;
import lec08.pmnehls.finalproject.game.model.Movable;
import lec08.pmnehls.finalproject.game.model.TargetSpace;
import lec08.pmnehls.finalproject.sounds.Sound;
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("Joystix", Font.PLAIN, 18);
private Font fntBig = new Font("Joystix", Font.BOLD, 64);
private Font fntlogo = new Font("SansSerif", Font.PLAIN, 4);
private FontMetrics fmt;
private int nFontWidth;
private int nFontHeight;
private String strDisplay = "";
private static final String strFile = System.getProperty("user.dir") + "/src/lec08/pmnehls/finalproject/game/model/Joystix.TTF";
public Font joystix = initJoystixFont();
// ==============================================================
// CONSTRUCTOR
// ==============================================================
public GamePanel(Dimension dim){
initJoystixFont();
gmf = new GameFrame();
gmf.getContentPane().add(this);
gmf.pack();
initView();
gmf.setSize(dim);
gmf.setTitle("PAC-MAN");
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("" + CommandCenter.getScore(), nFontWidth, nFontHeight);
} else {
g.drawString("00", nFontWidth, nFontHeight);
}
}
private void drawLevel(Graphics g)
{
g.setColor(Color.WHITE);
g.setFont(fnt);
if(CommandCenter.getLevel() != 0)
{
g.drawString("Level " + CommandCenter.getLevel(), TargetSpace.TS_WIDTH * 22, TargetSpace.TS_HEIGHT * 35);
}
}
public void drawLogo(Graphics g)
{
g.setColor(Color.YELLOW);
g.setFont(fntBig);
if(!CommandCenter.isPlaying())
{
g.drawString("PAC-MAN", 62, 180);
}
}
@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);
drawLogo(grpOff);
drawScore(grpOff);
drawLevel(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.movLives,
CommandCenter.movDots,
CommandCenter.movEnergizers,
CommandCenter.movFoes,
CommandCenter.movDebris,
CommandCenter.movProton,
CommandCenter.movPacman,
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();
}
}
}
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 = "use the arrow keys to turn Pac-Man";
grpOff.drawString(strDisplay,
(Game.DIM.width - fmt.stringWidth(strDisplay)) / 2, Game.DIM.height / 4
+ nFontHeight + 100);
strDisplay = "'S' to Start";
grpOff.drawString(strDisplay,
(Game.DIM.width - fmt.stringWidth(strDisplay)) / 2, Game.DIM.height / 4
+ nFontHeight + 160);
strDisplay = "'P' to Pause";
grpOff.drawString(strDisplay,
(Game.DIM.width - fmt.stringWidth(strDisplay)) / 2, Game.DIM.height / 4
+ nFontHeight + 200);
strDisplay = "'Q' to Quit";
grpOff.drawString(strDisplay,
(Game.DIM.width - fmt.stringWidth(strDisplay)) / 2, Game.DIM.height / 4
+ nFontHeight + 240);
strDisplay = "------BONUS FEATURES------";
grpOff.drawString(strDisplay,
(Game.DIM.width - fmt.stringWidth(strDisplay)) / 2, Game.DIM.height / 4
+ nFontHeight + 280);
strDisplay = "Add more ghosts: 'O' for Ouija";
grpOff.drawString(strDisplay,
(Game.DIM.width - fmt.stringWidth(strDisplay)) / 2, Game.DIM.height / 4
+ nFontHeight + 320);
strDisplay = "GhostBuster Mode: press 'G'";
grpOff.drawString(strDisplay,
(Game.DIM.width - fmt.stringWidth(strDisplay)) / 2, Game.DIM.height / 4
+ nFontHeight + 360);
}
public GameFrame getFrm() {return this.gmf;}
public void setFrm(GameFrame frm) {this.gmf = frm;}
public Font initJoystixFont()
{
try
{
Font fntTemp = Font.createFont(Font.TRUETYPE_FONT,new File(strFile));
return fntTemp.deriveFont(18f);
} catch (FontFormatException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
return null;
}
}
\ No newline at end of file
package lec08.pmnehls.finalproject.sounds;
import java.io.IOException;
import java.net.URL;
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) {
new Thread(new Runnable() {
public void run() {
try {
Clip clp = AudioSystem.getClip();
AudioInputStream aisStream =
AudioSystem.getAudioInputStream(Sound.class.getResourceAsStream(strPath));
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;
// this line caused the original exceptions
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();
//the next three lines were added to catch all exceptions generated
}catch(Exception exp){
System.out.println("error");
}
return clp;
}
}
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