mirror of
https://codeberg.org/hkzlab/TK2048.git
synced 2026-01-22 15:40:51 +11:00
Update the game logic to take note of high scores
This commit is contained in:
parent
fc11d95fbb
commit
f18b23b08a
4 changed files with 32 additions and 18 deletions
|
|
@ -53,8 +53,8 @@ uint8_t add_random_tile(void) {
|
||||||
return 0; // Return 0 if we were not able to place the tile, else we return (offset + 1) to indicate where the tile was placed
|
return 0; // Return 0 if we were not able to place the tile, else we return (offset + 1) to indicate where the tile was placed
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t step_game(step_direction dir) {
|
int8_t step_game(step_direction dir) {
|
||||||
uint8_t done = 0;
|
int8_t done = 0;
|
||||||
uint8_t start_offset;
|
uint8_t start_offset;
|
||||||
int8_t column_step;
|
int8_t column_step;
|
||||||
int8_t row_step;
|
int8_t row_step;
|
||||||
|
|
|
||||||
|
|
@ -14,7 +14,7 @@ typedef enum {
|
||||||
|
|
||||||
uint8_t reset_game(void);
|
uint8_t reset_game(void);
|
||||||
uint8_t *get_front_grid(void);
|
uint8_t *get_front_grid(void);
|
||||||
uint8_t step_game(step_direction dir);
|
int8_t step_game(step_direction dir);
|
||||||
uint16_t calculate_score(void);
|
uint16_t calculate_score(void);
|
||||||
uint8_t add_random_tile(void);
|
uint8_t add_random_tile(void);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -207,5 +207,4 @@ __attribute__((section("loadscreen"))) const uint8_t __loading_screen[] = {
|
||||||
0x00, 0x00, 0x18, 0x00, 0x3C, 0x00, 0x3E, 0x00, 0x08, 0x00, 0x02, 0x00, 0x00, 0x00, 0x70, 0x01, 0x00, 0x0C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x47,
|
0x00, 0x00, 0x18, 0x00, 0x3C, 0x00, 0x3E, 0x00, 0x08, 0x00, 0x02, 0x00, 0x00, 0x00, 0x70, 0x01, 0x00, 0x0C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x47,
|
||||||
0x7F, 0x7F, 0x3F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x18, 0x0E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7F, 0x00, 0x4E, 0x61, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
|
0x7F, 0x7F, 0x3F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x18, 0x0E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7F, 0x00, 0x4E, 0x61, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
|
||||||
};
|
};
|
||||||
#pragma require __loading_screen
|
|
||||||
|
|
||||||
|
|
|
||||||
41
src/main.c
41
src/main.c
|
|
@ -11,6 +11,19 @@
|
||||||
#include "game_graphics.h"
|
#include "game_graphics.h"
|
||||||
#include "monitor_subroutines.h"
|
#include "monitor_subroutines.h"
|
||||||
|
|
||||||
|
// Make sure the loading screen is included
|
||||||
|
#pragma require __loading_screen
|
||||||
|
|
||||||
|
#define MOVES_TEXT_X 32
|
||||||
|
#define MOVES_TEXT_Y 61
|
||||||
|
#define MOVES_TEXT_WIDTH 5
|
||||||
|
|
||||||
|
#define SCORE_TEXT_X 32
|
||||||
|
#define SCORE_TEXT_Y 29
|
||||||
|
#define SCORE_TEXT_WIDTH 5
|
||||||
|
|
||||||
|
#define WIN_SCORE_BONUS 10000
|
||||||
|
|
||||||
void init(void);
|
void init(void);
|
||||||
|
|
||||||
// Low level initialization
|
// Low level initialization
|
||||||
|
|
@ -26,25 +39,21 @@ void init(void) {
|
||||||
memset((void*)DISPLAY_PAGE_2, 0, DISPLAY_PAGE_SIZE);
|
memset((void*)DISPLAY_PAGE_2, 0, DISPLAY_PAGE_SIZE);
|
||||||
}
|
}
|
||||||
|
|
||||||
#define MOVES_TEXT_X 32
|
|
||||||
#define MOVES_TEXT_Y 61
|
|
||||||
#define MOVES_TEXT_WIDTH 5
|
|
||||||
|
|
||||||
#define SCORE_TEXT_X 32
|
|
||||||
#define SCORE_TEXT_Y 29
|
|
||||||
#define SCORE_TEXT_WIDTH 5
|
|
||||||
#pragma require __loading_screen
|
|
||||||
|
|
||||||
__task int main(void) {
|
__task int main(void) {
|
||||||
uint16_t moves_count;
|
uint16_t moves_count;
|
||||||
uint16_t score;
|
uint16_t score = 0;
|
||||||
uint8_t done;
|
uint16_t hi_score = 0;
|
||||||
|
int8_t done = 0;
|
||||||
|
|
||||||
init();
|
init();
|
||||||
|
|
||||||
while(1){ // Outer loop
|
while(1){ // Outer loop
|
||||||
moves_count = 0;
|
moves_count = 0;
|
||||||
|
|
||||||
|
// Check if we have a new high-score
|
||||||
|
if (score > hi_score) hi_score = score;
|
||||||
|
|
||||||
|
// Reset the game, calculate the initial score depending on which tiles we randomly get
|
||||||
score = reset_game();
|
score = reset_game();
|
||||||
|
|
||||||
// Draw the initial state of the game
|
// Draw the initial state of the game
|
||||||
|
|
@ -88,7 +97,10 @@ __task int main(void) {
|
||||||
moves_count++;
|
moves_count++;
|
||||||
|
|
||||||
// If we have won, break out of this loop
|
// If we have won, break out of this loop
|
||||||
if(done) break;
|
if(done) {
|
||||||
|
score += WIN_SCORE_BONUS;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
// Draw the number of moves
|
// Draw the number of moves
|
||||||
draw_number(moves_count, MOVES_TEXT_WIDTH, MOVES_TEXT_X, MOVES_TEXT_Y);
|
draw_number(moves_count, MOVES_TEXT_WIDTH, MOVES_TEXT_X, MOVES_TEXT_Y);
|
||||||
|
|
@ -98,7 +110,10 @@ __task int main(void) {
|
||||||
|
|
||||||
// Unable to add a tile: we ran out of space and lost!!!
|
// Unable to add a tile: we ran out of space and lost!!!
|
||||||
uint8_t random_tile_off = add_random_tile();
|
uint8_t random_tile_off = add_random_tile();
|
||||||
if(!random_tile_off) break;
|
if(!random_tile_off) {
|
||||||
|
done = -1; // Lost the game
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
score = calculate_score();
|
score = calculate_score();
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue