Skip to content
Snippets Groups Projects
Commit d550b880 authored by Jan Grewe's avatar Jan Grewe
Browse files

smoother game reset

WIP: fade game out
parent 5603e1cd
No related branches found
No related tags found
No related merge requests found
...@@ -27,6 +27,7 @@ void setupDisplay() ...@@ -27,6 +27,7 @@ void setupDisplay()
matrix.addLayer(&indexedLayer); matrix.addLayer(&indexedLayer);
matrix.begin(); matrix.begin();
matrix.setBrightness(defaultBrightness); matrix.setBrightness(defaultBrightness);
backgroundLayer.setBrightness(255);
backgroundLayer.enableColorCorrection(true); backgroundLayer.enableColorCorrection(true);
} }
...@@ -45,27 +46,32 @@ void displayLoop() ...@@ -45,27 +46,32 @@ void displayLoop()
} }
} }
void fadeOut(int ticks) void showEndScreen(int ticks) {
{ showEvolutions(ticks);
const uint transitionTime = 5000; }
unsigned long currentMillis = millis();
void fadeOutGame(int brightness) {
brightness = lightPowerMap8bit[brightness];
backgroundLayer.setBrightness(brightness);
backgroundLayer.swapBuffers();
}
while (millis() - currentMillis < transitionTime) void showEvolutions(int ticks)
{ {
char evolutions[16]; char msg[15] = "Life has ended";
char evolutions[17];
sprintf(evolutions, "Evolutions: %d", ticks); sprintf(evolutions, "Evolutions: %d", ticks);
char msg[] = "Life has ended";
indexedLayer.setFont(font3x5); indexedLayer.setFont(font3x5);
indexedLayer.drawString((matrix.getScreenWidth() / 2) - (strlen(msg) * 4) / 2, (matrix.getScreenHeight() / 2) - 2 - 5 , 1, msg); indexedLayer.drawString((matrix.getScreenWidth() / 2) - (strlen(msg) * 4) / 2, (matrix.getScreenHeight() / 2) - 2 - 5 , 1, msg);
indexedLayer.drawString((matrix.getScreenWidth() / 2) - (strlen(evolutions) * 4) / 2, (matrix.getScreenHeight() / 2) + 2, 1, evolutions); indexedLayer.drawString((matrix.getScreenWidth() / 2) - (strlen(evolutions) * 4) / 2, (matrix.getScreenHeight() / 2) + 2, 1, evolutions);
indexedLayer.swapBuffers(); indexedLayer.swapBuffers();
indexedLayer.fillScreen(0); indexedLayer.fillScreen(0);
} }
indexedLayer.swapBuffers();
}
void clearDisplay() void clearDisplay()
{ {
indexedLayer.swapBuffers();
backgroundLayer.fillScreen({0, 0, 0}); backgroundLayer.fillScreen({0, 0, 0});
backgroundLayer.setBrightness(255);
backgroundLayer.swapBuffers(); backgroundLayer.swapBuffers();
} }
...@@ -10,7 +10,9 @@ extern int brightnessPercent; ...@@ -10,7 +10,9 @@ extern int brightnessPercent;
void setupDisplay(); void setupDisplay();
void displayLoop(); void displayLoop();
void fadeOut(int ticks); void showEndScreen(int ticks);
void fadeOutGame(int brightness);
void showEvolutions(int ticks);
void clearDisplay(); void clearDisplay();
#endif #endif
...@@ -8,19 +8,27 @@ int g[SCREEN_HEIGHT][SCREEN_WIDTH]; ...@@ -8,19 +8,27 @@ int g[SCREEN_HEIGHT][SCREEN_WIDTH];
int arrayCopy[SCREEN_HEIGHT][SCREEN_WIDTH]; int arrayCopy[SCREEN_HEIGHT][SCREEN_WIDTH];
bool runGame = true; bool runGame = true;
bool gameOver = false;
int currentTick; int currentTick;
int finalTicks;
int cellsAliveBefore; int cellsAliveBefore;
int noEvolutionTicks; int noEvolutionTicks;
const uint scoreScreenTimeout = 5000;
unsigned long currentMillis;
void setupGameOfLife() void setupGameOfLife()
{ {
gameOver = false;
currentTick = 0; currentTick = 0;
finalTicks = 0;
cellsAliveBefore = 0; cellsAliveBefore = 0;
noEvolutionTicks = 0; noEvolutionTicks = 0;
randomSeed(analogRead(34)); randomSeed(analogRead(34));
createRandomMatrix(g); createRandomMatrix(g);
for (int i = 0; i < 10; i++) { for (int i = 0; i < 10; i++)
{
addGlider(random(SCREEN_HEIGHT), random(SCREEN_WIDTH), g); addGlider(random(SCREEN_HEIGHT), random(SCREEN_WIDTH), g);
} }
} }
...@@ -64,6 +72,7 @@ void gameOfLife(int (&a)[SCREEN_HEIGHT][SCREEN_WIDTH]) ...@@ -64,6 +72,7 @@ void gameOfLife(int (&a)[SCREEN_HEIGHT][SCREEN_WIDTH])
if (total < 2 || total > 3) if (total < 2 || total > 3)
{ {
arrayCopy[i][j] = 0; arrayCopy[i][j] = 0;
cellsAliveNow--;
} }
} }
else if (total == 3) else if (total == 3)
...@@ -82,11 +91,13 @@ void gameOfLife(int (&a)[SCREEN_HEIGHT][SCREEN_WIDTH]) ...@@ -82,11 +91,13 @@ void gameOfLife(int (&a)[SCREEN_HEIGHT][SCREEN_WIDTH])
} }
} }
if (currentTick % 2) { if (currentTick % 2)
{
cellsAliveBefore = cellsAliveNow; cellsAliveBefore = cellsAliveNow;
} }
if (cellsAliveNow == cellsAliveBefore) if (cellsAliveNow >= cellsAliveBefore - 3 &&
cellsAliveNow <= cellsAliveBefore + 3)
{ {
noEvolutionTicks++; noEvolutionTicks++;
} }
...@@ -97,7 +108,16 @@ void gameOfLife(int (&a)[SCREEN_HEIGHT][SCREEN_WIDTH]) ...@@ -97,7 +108,16 @@ void gameOfLife(int (&a)[SCREEN_HEIGHT][SCREEN_WIDTH])
if (noEvolutionTicks > noEvolutionTicksLimit) if (noEvolutionTicks > noEvolutionTicksLimit)
{ {
endGame(); if (!gameOver)
{
finalTicks = currentTick - noEvolutionTicksLimit;
currentMillis = millis();
showEndScreen(finalTicks);
gameOver = true;
} else {
int brightness = 255 * (1 - ((millis() - currentMillis) / scoreScreenTimeout));
fadeOutGame(brightness);
}
} }
} }
...@@ -118,17 +138,21 @@ void addGlider(int i1, int j1, int (&a)[SCREEN_HEIGHT][SCREEN_WIDTH]) ...@@ -118,17 +138,21 @@ void addGlider(int i1, int j1, int (&a)[SCREEN_HEIGHT][SCREEN_WIDTH])
void gameLoop() void gameLoop()
{ {
if (runGame) { if (runGame)
{
gameOfLife(g); gameOfLife(g);
if (gameOver)
{
if (millis() - currentMillis > scoreScreenTimeout)
{
resetGame();
}
} }
} }
void endGame() {
fadeOut(currentTick - noEvolutionTicksLimit);
resetGame();
} }
void resetGame() { void resetGame()
clearDisplay(); {
setupGameOfLife(); setupGameOfLife();
clearDisplay();
} }
...@@ -5,14 +5,14 @@ ...@@ -5,14 +5,14 @@
#include "gameoflife.h" #include "gameoflife.h"
int noEvolutionTicksLimit = 100; int noEvolutionTicksLimit = 100;
int brightnessPercent = 20; int brightnessPercent = 100;
void setup() void setup()
{ {
Serial.begin(115200); Serial.begin(115200);
logLine("", true); logLine("", true);
setupNetwork();
setupDisplay(); setupDisplay();
setupNetwork();
setupGameOfLife(); setupGameOfLife();
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment