Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Completed basic game

  • Loading branch information...
commit 6ca3e649ce34947d9932bf8c4ca0ce39e9e2d581 1 parent ad9d2c9
@wylieconlon authored
View
146 GameRunner.java
@@ -20,6 +20,7 @@
private Level[] levels;
private int level = 0;
+ private boolean done = false;
// timer to trigger movement while user is facing one direction
boolean movingRight = false;
@@ -37,11 +38,11 @@ public GameRunner() {
createLevels();
}
-
private void createLevels() {
setBackground(Color.BLACK);
// get all files from "levels" directory with .lvl extension
+ // create and parse a new level for each
File dir = new File("levels");
File[] files = dir.listFiles(new FilenameFilter() {
@Override
@@ -53,7 +54,7 @@ public boolean accept(File dir, String name) {
levels = new Level[files.length];
for(int i=0; i<files.length; i++) {
- levels[i] = new Level(files[i], this.width, this.height, this);
+ levels[i] = new Level(files[i], this.width, this.height);
}
}
@@ -61,7 +62,26 @@ public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
- levels[level].draw(g2d);
+ g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
+ RenderingHints.VALUE_ANTIALIAS_ON);
+
+ if(!done) {
+ levels[level].draw(g2d);
+ } else {
+ Font font = new Font("SansSerif", Font.BOLD, 32);
+ FontMetrics fm = g2d.getFontMetrics(font);
+
+ g2d.setFont(font);
+ g2d.setPaint(Color.WHITE);
+
+ String title = "Congratulations!";
+ String subtitle = "You've beaten BlockDude.";
+ int titleW = fm.stringWidth(title);
+ int subtitleW = fm.stringWidth(subtitle);
+ g2d.drawString(title, this.width/2 - titleW/2, this.height/2 - 50);
+ g2d.drawString(subtitle, this.width/2 - subtitleW/2, this.height/2);
+
+ }
}
public Dimension getPreferredSize() {
return new Dimension(this.width, this.height);
@@ -69,67 +89,93 @@ public Dimension getPreferredSize() {
public void nextLevel() {
- if(level < levels.length-1) {
- level++;
- timer.stop();
- repaint();
+ if(!done) {
+ if(level < levels.length-1) {
+ level++;
+ timer.stop();
+ repaint();
+ } else {
+ done = true;
+ timer.stop();
+ movingLeft = false;
+ movingRight = false;
+ repaint();
+ }
}
}
-
// called whenever a gesture is detected
public void pose(int userID, GestureName gest, boolean isActivated) {
- if (isActivated) {
- if(gest.equals(GestureName.TURN_LEFT)) { // MOVE LEFT
- System.out.println("Moving left");
- movingLeft = true;
- levels[level].moveLeft();
- repaint();
- timer.restart();
- } else if(gest.equals(GestureName.TURN_RIGHT)) { // MOVE RIGHT
- System.out.println("Moving right");
- movingRight = true;
- levels[level].moveRight();
- repaint();
- timer.restart();
- } else if(gest.equals(GestureName.RH_LIFT)) { // LIFT RIGHT
- levels[level].liftBlockRight();
- repaint();
- } else if(gest.equals(GestureName.LH_LIFT)) { // LIFT LEFT
- levels[level].liftBlockLeft();
- repaint();
- } else if(gest.equals(GestureName.RH_EXTEND)) { // DROP RIGHT
- levels[level].placeBlockRight();
- repaint();
- } else if(gest.equals(GestureName.LH_EXTEND)) { // DROP LEFT
- levels[level].placeBlockLeft();
- repaint();
+ if (!done && levels[level] != null && levels[level].checkWin()) {
+ nextLevel();
+ return;
+ } else if(!done) {
+ if (isActivated) {
+ if(gest.equals(GestureName.TURN_LEFT)) { // MOVE LEFT
+ System.out.println("Moving left");
+ levels[level].moveLeft();
+ if(levels[level].checkWin()) {
+ nextLevel();
+ } else {
+ movingLeft = true;
+ repaint();
+ timer.restart();
+ }
+ } else if(gest.equals(GestureName.TURN_RIGHT)) { // MOVE RIGHT
+ System.out.println("Moving right");
+ levels[level].moveRight();
+ if(levels[level].checkWin()) {
+ nextLevel();
+ } else {
+ movingRight = true;
+ repaint();
+ timer.restart();
+ }
+ } else if(gest.equals(GestureName.RH_LIFT)) { // LIFT RIGHT
+ levels[level].liftBlockRight();
+ repaint();
+ } else if(gest.equals(GestureName.LH_LIFT)) { // LIFT LEFT
+ levels[level].liftBlockLeft();
+ repaint();
+ } else if(gest.equals(GestureName.RH_EXTEND)) { // DROP RIGHT
+ levels[level].placeBlockRight();
+ repaint();
+ } else if(gest.equals(GestureName.LH_EXTEND)) { // DROP LEFT
+ levels[level].placeBlockLeft();
+ repaint();
+ } else {
+ //System.out.println(gest + " " + userID + " on");
+ }
} else {
- //System.out.println(gest + " " + userID + " on");
+ if(gest.equals(GestureName.TURN_LEFT)) { // MOVE LEFT
+ System.out.println("Stopped moving left");
+ movingLeft = false;
+ timer.stop();
+ } else if(gest.equals(GestureName.TURN_RIGHT)) { // MOVE RIGHT
+ System.out.println("Stopped moving right");
+ movingRight = false;
+ timer.stop();
+ }
}
- } else {
- if(gest.equals(GestureName.TURN_LEFT)) { // MOVE LEFT
- System.out.println("Stopped moving left");
- movingLeft = false;
- timer.stop();
- } else if(gest.equals(GestureName.TURN_RIGHT)) { // MOVE RIGHT
- System.out.println("Stopped moving right");
- movingRight = false;
- timer.stop();
- }
- //System.out.println(" " + gest + " " + userID + " off");
}
}
// timer callback, used to continually move while user is turned to side
public void actionPerformed(ActionEvent e) {
- if( movingRight ) {
- levels[level].moveRight();
- } else if( movingLeft ) {
- levels[level].moveLeft();
+ if(!done && levels[level].checkWin()) {
+ nextLevel();
+ return;
}
- repaint();
+ if( !done ) {
+ if( movingRight ) {
+ levels[level].moveRight();
+ } else if( movingLeft ) {
+ levels[level].moveLeft();
+ }
+
+ repaint();
+ }
}
}
View
44 Level.java
@@ -8,8 +8,6 @@
import java.awt.*;
public class Level {
- GameRunner owner;
-
int width;
int height;
@@ -30,9 +28,7 @@
int pHeight = 0;
boolean carrying = false;
- public Level(File file, int width, int height, GameRunner owner) {
- this.owner = owner;
-
+ public Level(File file, int width, int height) {
this.width = width;
this.height = height;
@@ -124,9 +120,7 @@ public void draw(Graphics2D g2d) {
break;
case 3: // MOVABLE BLOCK
- g2d.setPaint(Color.GRAY);
- g2d.fillRect(offsetX, offsetY, tileSize, tileSize);
-
+ drawBlock(g2d, offsetX, offsetY);
break;
case 7: // INITIAL PLAYER
break;
@@ -135,6 +129,7 @@ public void draw(Graphics2D g2d) {
g2d.fillRect(offsetX, offsetY, tileSize, tileSize);
g2d.setPaint(Color.BLACK);
+ g2d.setStroke(new BasicStroke(2));
g2d.drawRect(offsetX, offsetY, tileSize, tileSize);
break;
@@ -158,11 +153,24 @@ private void drawPlayer(Graphics2D g2d) {
g2d.fillRect(offsetX, offsetY, tileSize, tileSize*2);
if(carrying) {
- g2d.setPaint(Color.GRAY);
- g2d.fillRect(offsetX, offsetY - tileSize, tileSize, tileSize);
+ drawBlock(g2d, offsetX, offsetY - tileSize);
}
}
+ // draw movable block
+ private void drawBlock(Graphics2D g2d, int offsetX, int offsetY) {
+ g2d.setPaint(Color.GRAY);
+ g2d.fillRoundRect(offsetX, offsetY, tileSize, tileSize, 20, 20);
+
+ g2d.setPaint(Color.BLACK);
+
+ g2d.setStroke(new BasicStroke(3));
+ g2d.drawRoundRect(offsetX, offsetY, tileSize, tileSize, 20, 20);
+
+ g2d.setStroke(new BasicStroke(2));
+ g2d.drawRoundRect(offsetX+10, offsetY+10, tileSize-20, tileSize-20, 10, 10);
+ }
+
// movement methods called by GameRunner when it gets skeleton position
public void moveRight() {
if(player < columns-1) {
@@ -182,9 +190,9 @@ public void moveRight() {
}
setScroll(player);
-
- checkWin();
}
+
+ checkWin();
}
public void moveLeft() {
if(player > 1) {
@@ -204,9 +212,9 @@ public void moveLeft() {
}
setScroll(player);
-
- checkWin();
}
+
+ checkWin();
}
// game actions called by GameRunner when lifting/placing blocks
@@ -272,10 +280,8 @@ public void placeBlockLeft() {
}
- private void checkWin() {
- if(tiles[pHeight-1][player] == 1 || tiles[pHeight-2][player] == 1) {
- // on a door
- owner.nextLevel();
- }
+ // is the player on a door?
+ public boolean checkWin() {
+ return (tiles[pHeight-1][player] == 1 || tiles[pHeight-2][player] == 1);
}
}
View
2  Skeletons.java
@@ -373,7 +373,7 @@ private void drawUserStatus(Graphics2D g2d, int userID) throws StatusException
userGen.getUserCoM(userID));
String label = null;
if (skelCap.isSkeletonTracking(userID)) // tracking
- label = new String("Tracking user " + userID);
+ label = ""; // WYLIE: label is ugly
else if (skelCap.isSkeletonCalibrating(userID)) // calibrating
label = new String("Calibrating user " + userID);
else // pose detection
View
9 TrackerPanel.java
@@ -292,16 +292,15 @@ private void writeStats(Graphics2D g2d)
/* write statistics in bottom-left corner, or
"Loading" at start time */
{
- g2d.setColor(Color.BLUE);
+ g2d.setColor(Color.WHITE);
int panelHeight = getHeight();
if (imageCount > 0) {
double avgGrabTime = (double) totalTime / imageCount;
- g2d.drawString("Pic " + imageCount + " " +
- df.format(avgGrabTime) + " ms",
- 5, panelHeight-10); // bottom left
+ g2d.drawString(df.format(avgGrabTime) + " ms",
+ 10, panelHeight-10); // bottom left
}
else // no image yet
- g2d.drawString("Loading...", 5, panelHeight-10);
+ g2d.drawString("Loading...", 10, panelHeight-10);
} // end of writeStats()
View
24 levels/1.lvl
@@ -1,12 +1,12 @@
-0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
-0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
-0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
-0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
-0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
-0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
-0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
-0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
-8 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