Skip to content

Commit

Permalink
Added graphics for each tile color, pause, fixed some applet bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
nickpenaranda committed Apr 12, 2012
1 parent dcd6904 commit 5a33162
Show file tree
Hide file tree
Showing 11 changed files with 109 additions and 58 deletions.
15 changes: 0 additions & 15 deletions .classpath

This file was deleted.

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
bin/*
.settings
.classpath
*.dll
Binary file added res/P1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/P2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/P3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/P4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/P5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/P6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/P7.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
129 changes: 89 additions & 40 deletions src/com/nickpenaranda/kokgee/KokgeeGame.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.BasicGame;
import org.newdawn.slick.Color;
import org.newdawn.slick.Font;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.Sound;
Expand All @@ -33,6 +35,7 @@ public class KokgeeGame extends BasicGame {
private static final int START_LEVEL = 0;
private static final int BOOST_INTERVAL_INDEX = 9;
private static final int HALT_PAUSE = 250;
private static final String PAUSE_TEXT = "PRESS 'P' TO UNPAUSE";

/*
* PHYSICS_INTERVALS Array of times (ms) between drop impulses for each
Expand Down Expand Up @@ -92,6 +95,8 @@ private enum Movement {
* bEnableBoost Can boost be activated?
*/

private GameContainer mGameContainer;

private int mLevel;
private int mPoints;
private int mGameTicks;
Expand All @@ -102,9 +107,12 @@ private enum Movement {
private int mLinesToNextLevel;

private boolean bBoost;
private boolean bPause;
private boolean bEnableBoost;
private boolean bSounds;
private boolean bSoundsBroken;
private boolean bForcePhysics; // @HACK
private boolean bForceMove; // @HACK

/*
* Sounds
Expand All @@ -122,7 +130,7 @@ private enum Movement {
/*
* Rendering colors, see Shape.initColors()
*/
private Color[] mColors;
private Image[] mIcons;

/*
* Barebones constructor simply calls super constructor to set process name
Expand All @@ -132,21 +140,47 @@ public KokgeeGame() {
}

@Override
public void init(GameContainer arg0) throws SlickException {
public void init(GameContainer c) throws SlickException {
mGameContainer = c;

mGameContainer.setShowFPS( false );
mGameContainer.setTargetFrameRate( 100 );

bSoundsBroken = false;
bSounds = true;
try {
mSoundGameover = new Sound("res/you_lose.wav");
mSoundSetPiece = new Sound("res/block_set.wav");
mSoundClear1 = new Sound("res/clear_x1.wav");
mSoundClear2 = new Sound("res/clear_x2.wav");
mSoundClear3 = new Sound("res/clear_x3.wav");
mSoundClear4 = new Sound("res/clear_x4.wav");
} catch(SlickException e) {
e.printStackTrace();
bSoundsBroken = true;
bSounds = false;
// @TODO Do the right thing!!!
}

mRowsToKill = new Stack<Integer>();
mShapes = Shape.initShapes();
mIcons = Shape.initIcons();

doReset();
}

private void doReset() {
mBoard = new int[BOARD_WIDTH][BOARD_HEIGHT];
for(int x=0;x<BOARD_WIDTH;++x)
for(int y=0;y<BOARD_HEIGHT;++y)
mBoard[x][y] = 0;

mShapes = Shape.initShapes();
mColors = Shape.initColors();
mRowsToKill.clear();

mGameTicks = 0;
mNextPhysicsTick = 0;
mNextRowKillTick = 0;

mRowsToKill = new Stack<Integer>();

mLevel = START_LEVEL;
mPoints = 0;
mLinesCompleted = 0;
Expand All @@ -155,28 +189,24 @@ public void init(GameContainer arg0) throws SlickException {
bEnableBoost = true;
bBoost = false;
bForcePhysics = false;
bForceMove = false;
movement = Movement.NONE;

bSounds = true;
try {
mSoundGameover = new Sound("res/you_lose.wav");
mSoundSetPiece = new Sound("res/block_set.wav");
mSoundClear1 = new Sound("res/clear_x1.wav");
mSoundClear2 = new Sound("res/clear_x2.wav");
mSoundClear3 = new Sound("res/clear_x3.wav");
mSoundClear4 = new Sound("res/clear_x4.wav");
} catch(SlickException e) {
e.printStackTrace();
bSounds = false;
// @TODO Do the right thing!!!
}
bPause = false;

genRandom();
spawnNext();
}

@Override
public void render(GameContainer c, Graphics g) throws SlickException {
Font f = g.getFont();
int tWidth = f.getWidth( PAUSE_TEXT );
if(bPause) {
g.drawString(PAUSE_TEXT, c.getWidth() / 2 - tWidth / 2, c.getHeight() / 2 );
return;
}

/*
* Calculate bottom left corner of board
*/
Expand All @@ -186,7 +216,7 @@ public void render(GameContainer c, Graphics g) throws SlickException {
int prex = (c.getWidth() / 2) + (PIECE_SIZE * BOARD_WIDTH / 2) + PIECE_SIZE;
int prey = BOARD_PADDING_V + (PIECE_SIZE * 4) + PIECE_SIZE;
/*
* Render text information
* Render text information (DEV) @TODO
*/
g.setColor(Color.cyan);
g.drawString("Level " + mLevel, 16, 8);
Expand All @@ -207,10 +237,11 @@ public void render(GameContainer c, Graphics g) throws SlickException {
for(int x=0;x<BOARD_WIDTH;++x)
for(int y=0;y<BOARD_HEIGHT;++y)
if(mBoard[x][y] != 0) {
g.setColor(mColors[mBoard[x][y]]);
g.fillRect(bx + (x * PIECE_SIZE), by - (y * PIECE_SIZE), PIECE_SIZE, PIECE_SIZE);
g.setColor(Color.white);
g.drawRect(bx + (x * PIECE_SIZE), by - (y * PIECE_SIZE), PIECE_SIZE, PIECE_SIZE);
// g.setColor(mColors[mBoard[x][y]]);
// g.fillRect(bx + (x * PIECE_SIZE), by - (y * PIECE_SIZE), PIECE_SIZE, PIECE_SIZE);
// g.setColor(Color.white);
// g.drawRect(bx + (x * PIECE_SIZE), by - (y * PIECE_SIZE), PIECE_SIZE, PIECE_SIZE);
g.drawImage( mIcons[mBoard[x][y]], bx + (x * PIECE_SIZE), by - (y * PIECE_SIZE));
}

/*
Expand All @@ -221,10 +252,11 @@ public void render(GameContainer c, Graphics g) throws SlickException {
for(int k : mNextShape.getParts(0)) {
int px = (k - 1) % 4;
int py = (k - 1) / 4;
g.setColor(mColors[mNextShape.getColor()]);
g.fillRect(prex + (px * PIECE_SIZE), prey - (py * PIECE_SIZE), PIECE_SIZE, PIECE_SIZE);
g.setColor(Color.white);
g.drawRect(prex + (px * PIECE_SIZE), prey - (py * PIECE_SIZE), PIECE_SIZE, PIECE_SIZE);
g.drawImage( mIcons[mNextShape.getColor()], prex + (px * PIECE_SIZE), prey - (py * PIECE_SIZE));
// g.setColor(mIcons[mNextShape.getColor()]);
// g.fillRect(prex + (px * PIECE_SIZE), prey - (py * PIECE_SIZE), PIECE_SIZE, PIECE_SIZE);
// g.setColor(Color.white);
// g.drawRect(prex + (px * PIECE_SIZE), prey - (py * PIECE_SIZE), PIECE_SIZE, PIECE_SIZE);
}

/*
Expand All @@ -241,11 +273,12 @@ public void render(GameContainer c, Graphics g) throws SlickException {
int py = (k - 1) / 4;
if(opx + px < 0 || opx + px >= BOARD_WIDTH || opy + py < 0 || opy + py >= BOARD_HEIGHT)
continue;
g.setColor(mColors[mPiece.getColor()]);
g.fillRect(ox + (px * PIECE_SIZE), oy - (py * PIECE_SIZE), PIECE_SIZE, PIECE_SIZE);

g.setColor(Color.white);
g.drawRect(ox + (px * PIECE_SIZE), oy - (py * PIECE_SIZE), PIECE_SIZE, PIECE_SIZE);
g.drawImage( mIcons[mPiece.getColor()], ox + (px * PIECE_SIZE), oy - (py * PIECE_SIZE));
// g.setColor(mIcons[mPiece.getColor()]);
// g.fillRect(ox + (px * PIECE_SIZE), oy - (py * PIECE_SIZE), PIECE_SIZE, PIECE_SIZE);
//
// g.setColor(Color.white);
// g.drawRect(ox + (px * PIECE_SIZE), oy - (py * PIECE_SIZE), PIECE_SIZE, PIECE_SIZE);
}
}
}
Expand All @@ -260,6 +293,9 @@ public void render(GameContainer c, Graphics g) throws SlickException {
* @see org.newdawn.slick.BasicGame#update(org.newdawn.slick.GameContainer, int)
*/
public void update(GameContainer c, int delta) throws SlickException {
if(bPause)
return;

mGameTicks += delta;

/*
Expand All @@ -283,7 +319,8 @@ public void update(GameContainer c, int delta) throws SlickException {
mNextRowKillTick = mGameTicks + ROW_KILL_INTERVAL;
}
else { // IFF no rows to be killed, process movement ticks first then physics ticks
if(mGameTicks > mNextMovementTick) {
if(mGameTicks > mNextMovementTick || bForceMove) {
bForceMove = false;
switch(movement) {
case LEFT:
doMoveLeft();
Expand Down Expand Up @@ -313,9 +350,11 @@ public void keyPressed(int key, char c) {
switch(key) {
case Input.KEY_LEFT:
movement = Movement.LEFT;
bForceMove = true;
break;
case Input.KEY_RIGHT:
movement = Movement.RIGHT;
bForceMove = true;
break;
case Input.KEY_DOWN:
if(bEnableBoost)
Expand All @@ -326,19 +365,32 @@ public void keyPressed(int key, char c) {
doDrop();
break;
case Input.KEY_F:
doRotateCW();
if(!bPause)
doRotateCW();
break;
case Input.KEY_D:
doRotateCCW();
if(!bPause)
doRotateCCW();
break;
case Input.KEY_F1:
if(mLevel > 0) mLevel--;
break;
case Input.KEY_F2:
if(mLevel < PHYSICS_INTERVALS.length - 1) mLevel++;
break;
case Input.KEY_F4:
doReset();
break;
case Input.KEY_ESCAPE:
System.exit(-1);
if(mGameContainer instanceof AppGameContainer)
System.exit(0);
break;
case Input.KEY_S:
if(!bSoundsBroken)
bSounds = !bSounds;
break;
case Input.KEY_P:
bPause = !bPause;
break;
}
}
Expand Down Expand Up @@ -366,9 +418,6 @@ public static void main(String[] args) {
try {
AppGameContainer appGameContainer = new AppGameContainer(new KokgeeGame());
appGameContainer.setDisplayMode(640, 480, false);
appGameContainer.setMinimumLogicUpdateInterval(10);
appGameContainer.setTargetFrameRate(100);
appGameContainer.setShowFPS(false);
appGameContainer.start();
} catch (SlickException e) {
// TODO Auto-generated catch block
Expand Down
22 changes: 19 additions & 3 deletions src/com/nickpenaranda/kokgee/Shape.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import java.util.ArrayList;

import org.newdawn.slick.Color;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;

public class Shape {
private final ArrayList<Shape.ShapeConfig> mConfigs;
Expand All @@ -21,8 +22,23 @@ public Shape(String label, int color, Shape.ShapeConfig[] configs) {
mNumConfigs = mConfigs.size();
}

public static Color[] initColors() {
return(new Color[] {Color.black,Color.magenta,Color.blue,Color.green,Color.red,Color.cyan,Color.yellow,Color.pink});
public static Image[] initIcons() {
try {
return(new Image[] {
null,
new Image("res/P1.png"),
new Image("res/P2.png"),
new Image("res/P3.png"),
new Image("res/P4.png"),
new Image("res/P5.png"),
new Image("res/P6.png"),
new Image("res/P7.png")
});
} catch (SlickException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return(null);
}

public static ArrayList<Shape> initShapes() {
Expand Down

0 comments on commit 5a33162

Please sign in to comment.