What means these sentences? LCD Game

Hi! I’ve been using Arduino for a few years and done some projects with different Arduino and tools, but now i want to make games on a LCD display, so i looked for examples and ideas and i found a game about a runner who jumps and avoids random terrains, however when a look the code i couldn’t understand a lot of sentences that I’ve never seen before. Could you help me what means the marked ones? Thanks!

Here’s the entire code:

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27,16,2);
static char terrainUpper[TERRAIN_WIDTH + 1];
static char terrainLower[TERRAIN_WIDTH + 1];
static bool buttonPushed = false;
static int melodyPin = 3;

void initializeGraphics() {
static byte graphics = {
// Run position 1
B01100,
B01100,
B00000,
B01110,
B11100,
B01100,
B11010,
B10011,
// Run position 2
B01100,
B01100,
B00000,
B01100,
B01100,
B01100,
B01100,
B01110,
// Jump
B01100,
B01100,
B00000,
B11110,
B01101,
B11111,
B10000,
B00000,
// Jump lower
B11110,
B01101,
B11111,
B10000,
B00000,
B00000,
B00000,
B00000,
// Ground
B11111,
B11111,
B11111,
B11111,
B11111,
B11111,
B11111,
B11111,
// Ground right
B00011,
B00011,
B00011,
B00011,
B00011,
B00011,
B00011,
B00011,
// Ground left
B11000,
B11000,
B11000,
B11000,
B11000,
B11000,
B11000,
B11000,
};

int i;
// Skip using character 0, this allows lcd.print() to be used to
// quickly draw multiple characters

for (i = 0; i < 7; ++i) {
lcd.createChar(i + 1, &graphics[i * 8]); // Creates the character
}

for (i = 0; i < TERRAIN_WIDTH; ++i) {
terrainUpper = SPRITE_TERRAIN_EMPTY; //cleans the entire screen with empty terrains
terrainLower = SPRITE_TERRAIN_EMPTY;
}
}
// Slide the terrain to the left in half-character increments
//
void advanceTerrain(char* terrain, byte newTerrain) {
for (int i = 0; i < TERRAIN_WIDTH; ++i) {
_ char current = terrain*;_
char next = (i == TERRAIN_WIDTH - 1) ? newTerrain : terrain[i + 1];
_ switch (current) {_
case SPRITE_TERRAIN_EMPTY:
terrain = (next == SPRITE_TERRAIN_SOLID) ? SPRITE_TERRAIN_SOLID_RIGHT : [/color]SPRITE_TERRAIN_EMPTY;
_ break;_
case SPRITE_TERRAIN_SOLID:
terrain = (next == SPRITE_TERRAIN_EMPTY) ? SPRITE_TERRAIN_SOLID_LEFT : SPRITE_TERRAIN_SOLID;[/color]
_ break;_
case SPRITE_TERRAIN_SOLID_RIGHT:
terrain = SPRITE_TERRAIN_SOLID;
_ break;_
case SPRITE_TERRAIN_SOLID_LEFT:
terrain = SPRITE_TERRAIN_EMPTY;
_ break;
}
}
}_

_bool drawHero(byte position, char terrainUpper, char* terrainLower, unsigned int score) {
bool collide = false;
char upperSave = terrainUpper[HERO_HORIZONTAL_POSITION];
char lowerSave = terrainLower[HERO_HORIZONTAL_POSITION];
byte upper, lower;
switch (position) {
* case HERO_POSITION_OFF:
upper = lower = SPRITE_TERRAIN_EMPTY;*

* break;_
case HERO_POSITION_RUN_LOWER_1:
upper = SPRITE_TERRAIN_EMPTY;
lower = SPRITE_RUN1;
_ break;_
case HERO_POSITION_RUN_LOWER_2:
upper = SPRITE_TERRAIN_EMPTY;
lower = SPRITE_RUN2;
_ break;_
case HERO_POSITION_JUMP_1:
case HERO_POSITION_JUMP_8:
upper = SPRITE_TERRAIN_EMPTY;
lower = SPRITE_JUMP;
_ break;_
case HERO_POSITION_JUMP_2:
case HERO_POSITION_JUMP_7:
upper = SPRITE_JUMP_UPPER;
lower = SPRITE_JUMP_LOWER;
_ break;_
case HERO_POSITION_JUMP_3:
case HERO_POSITION_JUMP_4:
case HERO_POSITION_JUMP_5:
case HERO_POSITION_JUMP_6:
upper = SPRITE_JUMP;
lower = SPRITE_TERRAIN_EMPTY;
_ break;_
case HERO_POSITION_RUN_UPPER_1:
upper = SPRITE_RUN1;
lower = SPRITE_TERRAIN_EMPTY;
_ break;_
case HERO_POSITION_RUN_UPPER_2:
upper = SPRITE_RUN2;
lower = SPRITE_TERRAIN_EMPTY;
_ break;
}
if (upper != ’ ') {_

terrainUpper[HERO_HORIZONTAL_POSITION] = upper;
collide = (upperSave == SPRITE_TERRAIN_EMPTY) ? false : true;
_}
if (lower != ’ ') {_

terrainLower[HERO_HORIZONTAL_POSITION] = lower;
collide |= (lowerSave == SPRITE_TERRAIN_EMPTY) ? false : true;
_}
byte digits = (score > 9999) ? 5 : (score > 999) ? 4 : (score > 99) ? 3 : (score > 9) ? 2 : 1;
// Draw the scene*

terrainUpper[TERRAIN_WIDTH] = ’ ';
terrainLower[TERRAIN_WIDTH] = ’ ';
char temp = terrainUpper[16 - digits];
terrainUpper[16 - digits] = ’ ';
lcd.setCursor(0, 0);
lcd.print(terrainUpper);
terrainUpper[16 - digits] = temp;
lcd.setCursor(0, 1);
lcd.print(terrainLower);
lcd.setCursor(16 - digits, 0);
lcd.print(score);
terrainUpper[HERO_HORIZONTAL_POSITION] = upperSave;
terrainLower[HERO_HORIZONTAL_POSITION] = lowerSave;
return collide;
}
// Handle the button push as an interrupt
void buttonPush() {
buttonPushed = true;
}
void setup() {
initializeGraphics();
* lcd.init();
lcd.init();
lcd.backlight();
}
void loop() {
buttonCheck();
static byte heroPos = HERO_POSITION_RUN_LOWER_1;
static byte newTerrainType = TERRAIN_EMPTY;
static byte newTerrainDuration = 1;
static bool playing = false;
static bool blink = false;
static unsigned int distance = 0;
if (!playing) {_

drawHero((blink) ? HERO_POSITION_OFF : heroPos, terrainUpper, terrainLower, distance >> 3);
_ if (blink) {
lcd.setCursor(0, 0);
lcd.print(“Press Start”);
}
delay(250);
blink = !blink;
if (buttonPushed) {
initializeGraphics();_

heroPos = HERO_POSITION_RUN_LOWER_1;
_ playing = true;
buttonPushed = false;
distance = 0;
}
return;
}
// Shift the terrain to the left*

advanceTerrain(terrainLower, newTerrainType == TERRAIN_LOWER_BLOCK ? SPRITE_TERRAIN_SOLID : SPRITE_TERRAIN_EMPTY);
advanceTerrain(terrainUpper, newTerrainType == TERRAIN_UPPER_BLOCK ? SPRITE_TERRAIN_SOLID : SPRITE_TERRAIN_EMPTY);
// Make new terrain to enter on the right
if (–newTerrainDuration == 0) {
* if (newTerrainType == TERRAIN_EMPTY) {
newTerrainType = (random(3) == 0) ? TERRAIN_UPPER_BLOCK : TERRAIN_LOWER_BLOCK;*

* newTerrainDuration = 2 + random(10);
} else {_

newTerrainType = TERRAIN_EMPTY;
_ newTerrainDuration = 10 + random(10);
}
}
if (buttonPushed) {_

if (heroPos <= HERO_POSITION_RUN_LOWER_2) heroPos = HERO_POSITION_JUMP_1;
_ buttonPushed = false;
}
if (drawHero(heroPos, terrainUpper, terrainLower, distance >> 3)) {
playing = false; // The hero collided with something. Too bad.
} else {_

if (heroPos == HERO_POSITION_RUN_LOWER_2 || heroPos == HERO_POSITION_JUMP_8) {
heroPos = HERO_POSITION_RUN_LOWER_1;
} else if ((heroPos >= HERO_POSITION_JUMP_3 && heroPos <= HERO_POSITION_JUMP_5) && terrainLower[HERO_HORIZONTAL_POSITION] != SPRITE_TERRAIN_EMPTY) {
heroPos = HERO_POSITION_RUN_UPPER_1;
} else if (heroPos >= HERO_POSITION_RUN_UPPER_1 && terrainLower[HERO_HORIZONTAL_POSITION] == SPRITE_TERRAIN_EMPTY) {
heroPos = HERO_POSITION_JUMP_5;
} else if (heroPos == HERO_POSITION_RUN_UPPER_2) {
heroPos = HERO_POSITION_RUN_UPPER_1;
_ } else {
++heroPos;
}
++distance;
}
delay(100);
}
void buttonCheck() {
int b = analogRead(A0); // I’m using a Joystick in A0, so that’s why i don’t use a digital pin for a button
if (b > 850) {
buttonPushed = true;
}
}*_

Sorry, i had to write the code without the “#define” sentences because i exceed the limit of words.

#define SPRITE_RUN1 1
#define SPRITE_RUN2 2
#define SPRITE_JUMP 3
#define SPRITE_JUMP_UPPER ‘.’ //
#define SPRITE_JUMP_LOWER 4
#define SPRITE_TERRAIN_EMPTY ’ ’ //
#define SPRITE_TERRAIN_SOLID 5
#define SPRITE_TERRAIN_SOLID_RIGHT 6
#define SPRITE_TERRAIN_SOLID_LEFT 7
#define HERO_HORIZONTAL_POSITION 1 //
#define TERRAIN_WIDTH 16
#define TERRAIN_EMPTY 0
#define TERRAIN_LOWER_BLOCK 1
#define TERRAIN_UPPER_BLOCK 2
#define HERO_POSITION_OFF 0 //
#define HERO_POSITION_RUN_LOWER_1 1 // (pose 1)
#define HERO_POSITION_RUN_LOWER_2 2 // (pose 2)
#define HERO_POSITION_JUMP_1 3 //
#define HERO_POSITION_JUMP_2 4 //
#define HERO_POSITION_JUMP_3 5 // Jump is on upper row
#define HERO_POSITION_JUMP_4 6 // Jump is on upper row
#define HERO_POSITION_JUMP_5 7 // Jump is on upper row
#define HERO_POSITION_JUMP_6 8 // Jump is on upper row
#define HERO_POSITION_JUMP_7 9 //
#define HERO_POSITION_JUMP_8 10 //
#define HERO_POSITION_RUN_UPPER_1 11 // (pose 1)
#define HERO_POSITION_RUN_UPPER_2 12 // (pose 2)

This is not a good program to learn from. It's written in very "tekky" monolithic style with few comments. You would be better served by spending some time exploring simpler programs and writing your own from scratch, to gain some experience.

That's what it would take for you to understand an answer to your question, in any case. Of course someone may leap in and explain "ternary operators" and other elements of it, but I suspect it would be lost on you in such a complex context.

Erik_Baas:

Thanks so much! That information helped me a lot.

aarg:
This is not a good program to learn from. It's written in very "tekky" monolithic style with few comments. You would be better served by spending some time exploring simpler programs and writing your own from scratch, to gain some experience.

That's what it would take for you to understand an answer to your question, in any case. Of course someone may leap in and explain "ternary operators" and other elements of it, but I suspect it would be lost on you in such a complex context.

You're right, i found that program from a page that explains each step for do the connections, but it doesn't focus on the code, which is annoying; there were a few comments in the code but is not a great difference. Besides, there are more complex games in the same page (with the same issues as the runner game), but as you said, it's not a good start for make that kind of projects.

Anyway, i'm still looking for more information and making little sketches for finally write my own game.
Thanks for the recommendation!

The very first programs I wrote were games. Sprite animation is advanced stuff. I suggest some text based games so you can get results (hence, feedback) faster. Try some gambling games, like dice games... avoid real time stuff and animation until you can at least handle static graphics.

 advanceTerrain(terrainLower, newTerrainType == TERRAIN_LOWER_BLOCK ? SPRITE_TERRAIN_SOLID : SPRITE_TERRAIN_EMPTY);

I always thought the ternary operator required a '(' ')' pair around the condition. I guess not. But, leaving it out seems to make a potentially confusing operator even more so.

Perhaps the program is deliberately obfuscated.