mirror of
https://codeberg.org/hkzlab/TK2048.git
synced 2025-12-25 18:52:17 +11:00
Fix logic
This commit is contained in:
parent
dd61eae8d1
commit
38a40eb2e4
4 changed files with 37 additions and 13 deletions
|
|
@ -8,6 +8,7 @@
|
|||
#include "line_data.h"
|
||||
#include "game_logic.h"
|
||||
#include "tiles.h"
|
||||
#include "monitor_subroutines.h"
|
||||
|
||||
#define SCREEN_WIDTH_B 40
|
||||
#define GRID_CELL_SIDE 35
|
||||
|
|
@ -45,13 +46,31 @@ void draw_game_background(void) {
|
|||
|
||||
}
|
||||
|
||||
// This will draw directly to the front buffer
|
||||
void ddraw_single_tile(uint8_t offset) {
|
||||
uint8_t* grid = get_front_grid();
|
||||
if(!grid[offset]) return; // The tile is not there, nothing to do
|
||||
|
||||
const uint8_t *tile_data = tiles + (TILE_WIDTH_BYTES * TILE_HEIGHT * (grid[offset] - 1));
|
||||
|
||||
uint8_t col = offset % GRID_SIDE;
|
||||
uint8_t row = offset / GRID_SIDE;
|
||||
|
||||
uint8_t delay = 0xFF;
|
||||
|
||||
for(uint8_t h = 0; h < TILE_HEIGHT; h++) {
|
||||
memcpy(front_buf + line_offset_map[TOP_OFFSET + 7 + (row * GRID_CELL_SIDE) + h] + LEFT_OFFSET_B + 1 + (col * GRID_CELL_SIDE/7),
|
||||
tile_data + (TILE_WIDTH_BYTES * h), TILE_WIDTH_BYTES);
|
||||
WAIT(48);
|
||||
}
|
||||
}
|
||||
|
||||
void draw_tiles(void) {
|
||||
uint8_t* grid = get_front_grid();
|
||||
|
||||
// Clear the grid so we'll be able to draw the boxes on
|
||||
clear_box(GRID_SIDE * (GRID_CELL_SIDE/7) + 1, (GRID_SIDE * GRID_CELL_SIDE) + 6, LEFT_OFFSET_B, TOP_OFFSET + 1, back_buf);
|
||||
|
||||
|
||||
for (uint8_t tile = 0; tile < GRID_SIDE * GRID_SIDE; tile++) {
|
||||
if(grid[tile]) {
|
||||
const uint8_t *tile_data = tiles + (TILE_WIDTH_BYTES * TILE_HEIGHT * (grid[tile] - 1));
|
||||
|
|
|
|||
|
|
@ -5,5 +5,6 @@
|
|||
|
||||
void draw_game_background(void);
|
||||
void draw_tiles(void);
|
||||
void ddraw_single_tile(uint8_t offset);
|
||||
|
||||
#endif /* _GAME_GRAPHICS_HEADER_ */
|
||||
|
|
|
|||
|
|
@ -35,19 +35,18 @@ uint8_t *get_front_grid(void) {
|
|||
|
||||
uint8_t add_random_tile(void) {
|
||||
uint16_t rand = lfsr_update();
|
||||
uint8_t tile_val = (rand & 0x000F) > 0x0D ? 2 : 1; // 90% chance of a tile of type 1 (a "2"), 10% of a type 2 (a "4")
|
||||
uint8_t tile_val = (rand & 0x000F) > 0x0D ? 2 : 1; // ~90% chance of a tile of type 1 (a "2"), 10% of a type 2 (a "4")
|
||||
uint8_t rand_offset = rand >> 8;
|
||||
uint8_t tile_placed = 0;
|
||||
|
||||
for (int8_t offset = 0; offset < GRID_SIDE * GRID_SIDE; offset++) {
|
||||
if (!front_grid[(offset + rand_offset)%(GRID_SIDE * GRID_SIDE)]) {
|
||||
front_grid[(offset + rand_offset)%(GRID_SIDE * GRID_SIDE)] = tile_val;
|
||||
tile_placed = 1;
|
||||
break;
|
||||
for (uint8_t offset = 0; offset < GRID_SIDE * GRID_SIDE; offset++) {
|
||||
uint8_t rand_off = (offset + rand_offset)%(GRID_SIDE * GRID_SIDE);
|
||||
if (!front_grid[rand_off]) {
|
||||
front_grid[rand_off] = tile_val;
|
||||
return rand_off + 1;
|
||||
}
|
||||
}
|
||||
|
||||
return tile_placed; // Return 0 if we were not able to place the tile
|
||||
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) {
|
||||
|
|
@ -123,7 +122,7 @@ game_state step_game(step_direction dir) {
|
|||
state.score += ((uint16_t)1) << back_grid[current_offset];
|
||||
state.done += back_grid[current_offset] == 11 ? 1 : 0;
|
||||
break;
|
||||
}
|
||||
} else if (front_grid[sub_col_offset]) break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
11
src/main.c
11
src/main.c
|
|
@ -64,10 +64,15 @@ __task int main(void) {
|
|||
// If we have finished, break out of this loop
|
||||
if(state.done) break;
|
||||
|
||||
// Unable to add a tile. We lost!!!
|
||||
if(!add_random_tile()) break;
|
||||
|
||||
// Draw the moved sprites
|
||||
draw_tiles();
|
||||
|
||||
// Unable to add a tile. We lost!!!
|
||||
uint8_t random_tile_off = add_random_tile();
|
||||
if(!random_tile_off) break;
|
||||
|
||||
// Draw the new tile directly on the front buffer
|
||||
ddraw_single_tile(random_tile_off - 1);
|
||||
}
|
||||
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue