Skip to content

Commit

Permalink
add score display, and game over splash screen
Browse files Browse the repository at this point in the history
  • Loading branch information
dan-sf committed Jan 9, 2017
1 parent 57d360d commit da1c0c8
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 5 deletions.
65 changes: 65 additions & 0 deletions game_over.pde
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
class GameOverSplash {
int rectSize;
int screenWidth;
PVector[] fallingRects;
int hasFallen;
boolean animationOver;

GameOverSplash(int screenWidth, int rectSize) {
this.rectSize = rectSize;
this.screenWidth = screenWidth;

int numRects = screenWidth / rectSize;
fallingRects = new PVector[numRects];
hasFallen = 0;
animationOver = false;

for (int i=0; i < numRects; i++) {
//Create random number from -rectSize to -rectSize*4 <- ???
int rectX = i * rectSize;
int rectY = (int) random(-rectSize * 8, -rectSize);

fallingRects[i] = new PVector(rectX, rectY);

}
}

void animate(int score) {
for (int i=0; i<fallingRects.length; i++) {
if (fallingRects[i].y <= height - rectSize && hasFallen < ((1<<fallingRects.length)-1)) {
fill(51);
rect(fallingRects[i].x, fallingRects[i].y, rectSize, rectSize);

fill(255);
fallingRects[i].y += 8;
if (fallingRects[i].y >= height - rectSize) {
rect(fallingRects[i].x, height - rectSize, rectSize, rectSize);

int mask = 1<<i;
hasFallen = hasFallen | mask;
print("i: "); print(i);
print(", Mask: "); print(Integer.toString(mask, 2));
print(", hasFallen: "); print(Integer.toString(hasFallen, 2));
print(", all ones: "); println(Integer.toString( ((1<<fallingRects.length)-1), 2));
}
else {
rect(fallingRects[i].x, fallingRects[i].y, rectSize, rectSize);
}
}
}

if (hasFallen == ((1<<fallingRects.length)-1)) {
animationOver = true;

fill(255);
textAlign(CENTER);
textSize(32);
text("Game Over", width/2, height/3);
textSize(18);
text("Final score: " + String.valueOf(score) + "\nPress any key to restart",
width/2, height/3 + 40);
rectMode(CORNER);
}

}
}
18 changes: 18 additions & 0 deletions score.pde
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Score {
int score;

Score() {
score = 0;
}

void increase() {
score++;
}

void show() {
fill(255);
textAlign(RIGHT);
textSize(10);
text("Score: " + String.valueOf(score), width-5, 15);
}
}
20 changes: 15 additions & 5 deletions snake.pde
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
/* Todo:
- make game consistent when frame rate changes
- add a score display
- add a game over splash screen or printed word
- figure out why rendering text takes so long on start up
- add a new game splash screen
- add a snake death blink
- add some music/sound effects
- add read me
*/

PlayerSnake snake;
Food food;
Score score;
GameOverSplash gameOverSplash;
boolean gameOver;

int rectSize = 20;
Expand All @@ -22,18 +22,23 @@ void settings() {

void setup() {
background(51);
textSize(32);

snake = new PlayerSnake(rectSize);
food = new Food(rectSize);
score = new Score();
gameOverSplash = new GameOverSplash(width, rectSize);

food.update();
food.show();

score.show();

gameOver = false;
}

void keyPressed() {
if (gameOver) {
if (gameOver && gameOverSplash.animationOver) {
setup();
}
else {
Expand All @@ -47,6 +52,7 @@ void draw() {
if (!gameOver && updateGame) {
if (snake.eat(food)) {
snake.growTail();
score.increase();
do {
food.update();
} while (food.isOnSnake(snake.x, snake.y, snake.tail));
Expand All @@ -57,10 +63,14 @@ void draw() {
background(51);
food.show();
snake.show();
score.show();
}
else {
gameOver = true;
}
}
else if (gameOver) {
gameOverSplash.animate(score.score);
}
}

0 comments on commit da1c0c8

Please sign in to comment.