Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
Matrix of Life
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Jan Grewe
Matrix of Life
Commits
d550b880
Commit
d550b880
authored
2 years ago
by
Jan Grewe
Browse files
Options
Downloads
Patches
Plain Diff
smoother game reset
WIP: fade game out
parent
5603e1cd
No related branches found
No related tags found
No related merge requests found
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
src/display.cpp
+21
-15
21 additions, 15 deletions
src/display.cpp
src/display.h
+3
-1
3 additions, 1 deletion
src/display.h
src/gameoflife.cpp
+39
-15
39 additions, 15 deletions
src/gameoflife.cpp
src/main.cpp
+2
-2
2 additions, 2 deletions
src/main.cpp
with
65 additions
and
33 deletions
src/display.cpp
+
21
−
15
View file @
d550b880
...
@@ -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
();
}
}
This diff is collapsed.
Click to expand it.
src/display.h
+
3
−
1
View file @
d550b880
...
@@ -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
This diff is collapsed.
Click to expand it.
src/gameoflife.cpp
+
39
−
15
View file @
d550b880
...
@@ -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
();
}
}
This diff is collapsed.
Click to expand it.
src/main.cpp
+
2
−
2
View file @
d550b880
...
@@ -5,14 +5,14 @@
...
@@ -5,14 +5,14 @@
#include
"gameoflife.h"
#include
"gameoflife.h"
int
noEvolutionTicksLimit
=
100
;
int
noEvolutionTicksLimit
=
100
;
int
brightnessPercent
=
2
0
;
int
brightnessPercent
=
10
0
;
void
setup
()
void
setup
()
{
{
Serial
.
begin
(
115200
);
Serial
.
begin
(
115200
);
logLine
(
""
,
true
);
logLine
(
""
,
true
);
setupNetwork
();
setupDisplay
();
setupDisplay
();
setupNetwork
();
setupGameOfLife
();
setupGameOfLife
();
}
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment