Please click into this link first https://circuits.io/circuits/1023084-arduino-l
ID: 3875586 • Letter: P
Question
Please click into this link first
https://circuits.io/circuits/1023084-arduino-lcd-game?
After a minite, you will see a button "Code Editor" and click it.
Could you explain the following code:
#define SPRITE_RUN1 1
#define SPRITE_RUN2 2
#define SPRITE_JUMP 3
#define SPRITE_JUMP_UPPER '.' // Use the '.' character for the head
#define SPRITE_JUMP_LOWER 4
#define SPRITE_TERRAIN_EMPTY ' ' // User the ' ' character
#define SPRITE_TERRAIN_SOLID 5
#define SPRITE_TERRAIN_SOLID_RIGHT 6
#define SPRITE_TERRAIN_SOLID_LEFT 7
#define HERO_HORIZONTAL_POSITION 1 // Horizontal position of hero on screen
#define TERRAIN_WIDTH 16
#define TERRAIN_EMPTY 0
#define TERRAIN_LOWER_BLOCK 1
#define TERRAIN_UPPER_BLOCK 2
#define HERO_POSITION_OFF 0 // Hero is invisible
#define HERO_POSITION_RUN_LOWER_1 1 // Hero is running on lower row (pose 1)
#define HERO_POSITION_RUN_LOWER_2 2 // (pose 2)
#define HERO_POSITION_JUMP_1 3 // Starting a jump
#define HERO_POSITION_JUMP_2 4 // Half-way up
#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 // Half-way down
#define HERO_POSITION_JUMP_8 10 // About to land
#define HERO_POSITION_RUN_UPPER_1 11 // Hero is running on upper row (pose 1)
#define HERO_POSITION_RUN_UPPER_2 12 //
How do the code make the hero with different movements presenting in the LCD display by defining the variable name as number?
For example, SPRITE_RUN1 = 1
Explanation / Answer
Above program has been written in Arduino Integrated Development Environment - or Arduino Software (IDE) and file extension should be ino.
And as we know every prog languages has its own grammer and character handling . In below explaination we talk for the above mentioned code . This code is for use in developing endless runner games like [Temple Run] .
All the position e.g. HERO_HORIZONTAL_POSITION width vars e.g. TERRAIN_WIDTH are set as number.
While you proceed in the code further where it actually draws the hero and defines its position , there must be one function whose one of argument is character pointer and which is used to convert such num variables to character and used them in switch cases.
// sample code to draw the hero
bool collide = false;
char upperSave = terrainUpper[HERO_HORIZONTAL_POSITION];
char lowerSave = terrainLower[HERO_HORIZONTAL_POSITION];
byte upper, lower;
// Draw the appropriate sprite for the hero (run or jump)
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;
//******
//*****
//etc etc many switch acses what all you want in your program
}
}
so at every step or in cases you will see those num vars get converted to char vars and then being used.
P.S. In the question text only variables are declared .. there should be a huge program beneath that . Any question remains, Happy to Help.
bool collide = false;
char upperSave = terrainUpper[HERO_HORIZONTAL_POSITION];
char lowerSave = terrainLower[HERO_HORIZONTAL_POSITION];
byte upper, lower;
// Draw the appropriate sprite for the hero (run or jump)
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;
//******
//*****
//etc etc many switch acses what all you want in your program
}
}
so at every step or in cases you will see those num vars get converted to char vars and then being used.
P.S. In the question text only variables are declared .. there should be a huge program beneath that . Any question remains, Happy to Help.