mirror of
https://codeberg.org/hkzlab/TK2048.git
synced 2025-12-25 18:42:16 +11:00
Show score and moves
This commit is contained in:
parent
4ea5c7147e
commit
1a3635fc9a
5 changed files with 55 additions and 35 deletions
|
|
@ -25,7 +25,6 @@ static uint8_t *back_buf = (uint8_t*)DISPLAY_PAGE_2;
|
|||
// Every square is 35x35 pixels
|
||||
|
||||
void draw_field_borders_on_buffer(uint8_t brd, uint8_t* buf);
|
||||
void swap_display_buffers(void);
|
||||
|
||||
void ddraw_field_borders_on_buffer(uint8_t brd) {
|
||||
draw_field_borders_on_buffer(brd, front_buf);
|
||||
|
|
@ -97,9 +96,6 @@ void draw_tiles(void) {
|
|||
|
||||
// Re-draw the borders, to restore the correct width
|
||||
draw_field_borders_on_buffer(0x0F, back_buf);
|
||||
|
||||
// And finally swap the buffer to show the update
|
||||
swap_display_buffers();
|
||||
}
|
||||
|
||||
void swap_display_buffers(void) {
|
||||
|
|
|
|||
|
|
@ -18,5 +18,6 @@ void draw_game_background(void);
|
|||
void draw_number(uint16_t n, uint8_t len, uint8_t x, uint8_t y);
|
||||
void draw_tiles(void);
|
||||
void ddraw_single_tile(uint8_t offset);
|
||||
void swap_display_buffers(void);
|
||||
|
||||
#endif /* _GAME_GRAPHICS_HEADER_ */
|
||||
|
|
|
|||
|
|
@ -19,14 +19,18 @@ void swap_grids(void) {
|
|||
back_grid = temp;
|
||||
}
|
||||
|
||||
void reset_game(void) {
|
||||
uint8_t reset_game(void) {
|
||||
uint8_t score = 0;
|
||||
|
||||
// Clear the back and front grid
|
||||
memset(back_grid, 0, GRID_SIDE * GRID_SIDE);
|
||||
memset(front_grid, 0, GRID_SIDE * GRID_SIDE);
|
||||
|
||||
// Then add two random tiles
|
||||
add_random_tile();
|
||||
add_random_tile();
|
||||
score += (1 << front_grid[add_random_tile() - 1]);
|
||||
score += (1 << front_grid[add_random_tile() - 1]);
|
||||
|
||||
return score;
|
||||
}
|
||||
|
||||
uint8_t *get_front_grid(void) {
|
||||
|
|
@ -49,12 +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
|
||||
}
|
||||
|
||||
game_state step_game(step_direction dir) {
|
||||
game_state state = {
|
||||
.score = 0,
|
||||
.done = 0
|
||||
};
|
||||
|
||||
uint8_t step_game(step_direction dir) {
|
||||
uint8_t done = 0;
|
||||
uint8_t start_offset;
|
||||
int8_t column_step;
|
||||
int8_t row_step;
|
||||
|
|
@ -104,7 +104,6 @@ game_state step_game(step_direction dir) {
|
|||
back_grid[current_offset] = front_grid[sub_col_offset];
|
||||
front_grid[sub_col_offset] = 0;
|
||||
|
||||
state.score += ((uint16_t)1) << back_grid[current_offset];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
|
@ -119,8 +118,7 @@ game_state step_game(step_direction dir) {
|
|||
back_grid[current_offset]++; // Merge them (by increasing the value of the current square and removing the merged one)
|
||||
front_grid[sub_col_offset] = 0;
|
||||
|
||||
state.score += ((uint16_t)1) << back_grid[current_offset];
|
||||
state.done += back_grid[current_offset] == 11 ? 1 : 0;
|
||||
done += back_grid[current_offset] == 11 ? 1 : 0;
|
||||
break;
|
||||
} else if (front_grid[sub_col_offset]) break;
|
||||
}
|
||||
|
|
@ -129,5 +127,15 @@ game_state step_game(step_direction dir) {
|
|||
|
||||
swap_grids();
|
||||
|
||||
return state;
|
||||
return done;
|
||||
}
|
||||
|
||||
uint16_t calculate_score(void) {
|
||||
uint16_t score = 0;
|
||||
|
||||
for(uint8_t offset = 0; offset < GRID_SIDE * GRID_SIDE; offset++) {
|
||||
if(front_grid[offset]) score += ((uint16_t)1 << front_grid[offset]);
|
||||
}
|
||||
|
||||
return score;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,14 +12,10 @@ typedef enum {
|
|||
RIGHT
|
||||
} step_direction;
|
||||
|
||||
typedef struct {
|
||||
uint16_t score;
|
||||
uint8_t done;
|
||||
} game_state;
|
||||
|
||||
void reset_game(void);
|
||||
uint8_t reset_game(void);
|
||||
uint8_t *get_front_grid(void);
|
||||
game_state step_game(step_direction dir);
|
||||
uint8_t step_game(step_direction dir);
|
||||
uint16_t calculate_score(void);
|
||||
uint8_t add_random_tile(void);
|
||||
|
||||
#endif /* _GAME_LOGIC_HEADER_ */
|
||||
|
|
|
|||
41
src/main.c
41
src/main.c
|
|
@ -30,20 +30,31 @@ void init(void) {
|
|||
#define MOVES_TEXT_Y 14
|
||||
#define MOVES_TEXT_WIDTH 4
|
||||
|
||||
#define SCORE_TEXT_X 28
|
||||
#define SCORE_TEXT_Y 22
|
||||
#define SCORE_TEXT_WIDTH 5
|
||||
|
||||
__task int main(void) {
|
||||
uint16_t moves_count = 0;
|
||||
uint16_t moves_count;
|
||||
uint16_t score;
|
||||
uint8_t done;
|
||||
|
||||
init();
|
||||
|
||||
game_state state;
|
||||
|
||||
|
||||
while(1){ // Outer loop
|
||||
reset_game();
|
||||
|
||||
moves_count = 0;
|
||||
|
||||
score = reset_game();
|
||||
|
||||
// Draw the initial state of the game
|
||||
draw_game_background();
|
||||
draw_number(moves_count, MOVES_TEXT_WIDTH, MOVES_TEXT_X, MOVES_TEXT_Y);
|
||||
draw_number(score, SCORE_TEXT_WIDTH, SCORE_TEXT_X, SCORE_TEXT_Y);
|
||||
draw_tiles();
|
||||
|
||||
// Swap graphical buffers
|
||||
swap_display_buffers();
|
||||
|
||||
while(1) { // Game loop
|
||||
lfsr_update();
|
||||
|
||||
|
|
@ -51,22 +62,22 @@ __task int main(void) {
|
|||
case K_UP:
|
||||
BELL1();
|
||||
ddraw_field_borders_on_buffer(0x0E);
|
||||
state = step_game(UP);
|
||||
done = step_game(UP);
|
||||
break;
|
||||
case K_DOWN:
|
||||
BELL1();
|
||||
ddraw_field_borders_on_buffer(0x0D);
|
||||
state = step_game(DOWN);
|
||||
done = step_game(DOWN);
|
||||
break;
|
||||
case K_LEFT:
|
||||
BELL1();
|
||||
ddraw_field_borders_on_buffer(0x0B);
|
||||
state = step_game(LEFT);
|
||||
done = step_game(LEFT);
|
||||
break;
|
||||
case K_RIGHT:
|
||||
BELL1();
|
||||
ddraw_field_borders_on_buffer(0x07);
|
||||
state = step_game(RIGHT);
|
||||
done = step_game(RIGHT);
|
||||
break;
|
||||
default:
|
||||
continue; // Do nothing, loop again
|
||||
|
|
@ -76,11 +87,12 @@ __task int main(void) {
|
|||
moves_count++;
|
||||
|
||||
// If we have won, break out of this loop
|
||||
if(state.done) break;
|
||||
if(done) break;
|
||||
|
||||
// Draw the number of moves
|
||||
draw_number(moves_count, MOVES_TEXT_WIDTH, MOVES_TEXT_X, MOVES_TEXT_Y);
|
||||
|
||||
|
||||
// Draw the moved tiles
|
||||
draw_tiles();
|
||||
|
||||
|
|
@ -88,6 +100,13 @@ __task int main(void) {
|
|||
uint8_t random_tile_off = add_random_tile();
|
||||
if(!random_tile_off) break;
|
||||
|
||||
score = calculate_score();
|
||||
|
||||
// Draw the score
|
||||
draw_number(score, SCORE_TEXT_WIDTH, SCORE_TEXT_X, SCORE_TEXT_Y);
|
||||
|
||||
swap_display_buffers();
|
||||
|
||||
// Draw the new tile directly on the front buffer, this way we make it appear with an "animation"
|
||||
ddraw_single_tile(random_tile_off - 1);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue