Skip to content

Commit

Permalink
adding score, removing multiple balls
Browse files Browse the repository at this point in the history
  • Loading branch information
pyricau committed Jun 6, 2011
1 parent 5fc7332 commit 2b3497f
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ public abstract class Constants {

public static final int WALL_HEIGHT = 16;

public static final int LINE_SCORE_BASE = 400;

public static final int LINE_POWER = 2;

public static final int MAX_BALLS = 10;

public static final int KEY_A = 65;
public static final int KEY_Q = 81;
public static final int KEY_D = 68;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ public class TooHardForYouEngine extends EntityEngine implements Pointer.Listene

private Piece piece;

private int score;

public TooHardForYouEngine(TooHardForYouGame game) {
super(buildWorldLayer());

Expand Down Expand Up @@ -80,15 +82,6 @@ public TooHardForYouEngine(TooHardForYouGame game) {

pieceFactory = new PieceFactory(this, wall);

new Timer() {
@Override
public void run() {
if (balls.size() < 10) {
createBallOnPaddle();
}
}
}.scheduleRepeating(500);

// hook up our pointer listener
pointer().setListener(this);
keyboard().setListener(this);
Expand All @@ -99,6 +92,10 @@ public void run() {
private void newGame() {
paddle.resetPosition();

score = 0;

uiTexts.resetAll();

if (piece != null) {
piece.destroy();
}
Expand All @@ -111,17 +108,27 @@ private void newGame() {
uiTexts.updateNumberOfBalls(balls.size());

wall.fillRandomly(5);

createBallOnPaddle();
}

private void createBallsOnPaddle(int numberOfBalls) {
for (int i = 0; i < numberOfBalls; i++) {
createBallOnPaddle();
}
}

private void createBallOnPaddle() {
Ball ball = new Ball(this, paddle.getPosX(), paddle.getPosY() - paddle.getHeight());
Vec2 velocity = new Vec2(random() - 0.5f, random() - 1);
velocity.normalize();
velocity.mulLocal(5);
ball.getBody().setLinearVelocity(velocity);
add(ball);
balls.add(ball);
uiTexts.updateNumberOfBalls(balls.size());
if (balls.size() < Constants.MAX_BALLS) {
Ball ball = new Ball(this, paddle.getPosX(), paddle.getPosY() - paddle.getHeight());
Vec2 velocity = new Vec2(random() - 0.5f, random() - 1);
velocity.normalize();
velocity.mulLocal(5);
ball.getBody().setLinearVelocity(velocity);
add(ball);
balls.add(ball);
uiTexts.updateNumberOfBalls(balls.size());
}
}

// create our world layer (scaled to "world space")
Expand Down Expand Up @@ -224,6 +231,11 @@ public void onKeyUp(int keyCode) {
}
}

private void incrementScore(int increment) {
score += increment;
uiTexts.updateScore(score);
}

@Override
public void paint(float delta) {
super.paint(delta);
Expand All @@ -247,6 +259,13 @@ public void pieceFrozen() {
newGame();
} else {
int fullLines = wall.checkFullLines();

if (fullLines > 0) {
createBallsOnPaddle(fullLines - 1);

incrementScore((int) (Math.pow(fullLines, Constants.LINE_POWER) * Constants.LINE_SCORE_BASE));
}

piece = pieceFactory.newRandomPiece();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class UiTexts {

private int numberOfBalls;
private int frameRate;
private int score;

public UiTexts() {
Image backgroundImage = assetManager().getImage(Resources.BACKGROUND_IMG);
Expand All @@ -41,8 +42,9 @@ public void mayRedrawTexts() {
textDataChanged = false;
Canvas canvas = textLayer.canvas();
canvas.clear();
canvas.drawText("Balls: " + numberOfBalls, 550, 50);
canvas.drawText("FPS: " + frameRate, 550, 70);
canvas.drawText("Score: " + score, 550, 50);
canvas.drawText("Balls: " + numberOfBalls, 550, 70);
canvas.drawText("FPS: " + frameRate, 550, 90);
}
}

Expand All @@ -59,5 +61,18 @@ public void updateFrameRate(int frameRate) {
this.frameRate = frameRate;
}
}

public void updateScore(int score) {
if (this.score != score) {
textDataChanged = true;
this.score = score;
}
}

public void resetAll() {
numberOfBalls = 0;
score = 0;
textDataChanged = true;
}

}

0 comments on commit 2b3497f

Please sign in to comment.