fixed btn repeat for memgame, option to get smaller board

pull/1/head
Ondřej Hruška 10 years ago
parent 3e897aeee9
commit 154d69b5c4
  1. 24
      projects/color-memory-game/main.c

@ -20,8 +20,8 @@
// #define BOARD_WIDTH 6 // #define BOARD_WIDTH 6
// #define BOARD_HEIGHT 5 // #define BOARD_HEIGHT 5
#define BOARD_WIDTH 4 #define BOARD_WIDTH 6
#define BOARD_HEIGHT 4 #define BOARD_HEIGHT 5
// number of cards // number of cards
#define CARD_COUNT (BOARD_WIDTH * BOARD_HEIGHT) #define CARD_COUNT (BOARD_WIDTH * BOARD_HEIGHT)
@ -29,6 +29,9 @@
// number of pairs // number of pairs
#define PAIR_COUNT (CARD_COUNT / 2) #define PAIR_COUNT (CARD_COUNT / 2)
// when the "small" pin is DOWN, only this many cards are dealt - on the same board size
#define CARD_COUNT_SMALL 18
// color palette // color palette
const xrgb_t COLORS[] = { const xrgb_t COLORS[] = {
rgb24_xrgbc(0x00FF99), // emerald rgb24_xrgbc(0x00FF99), // emerald
@ -68,6 +71,9 @@ const xrgb_t COLORS[] = {
#define BTN_SELECT D6 #define BTN_SELECT D6
#define BTN_RESTART D7 #define BTN_RESTART D7
// connect this pin to ground to get smaller board size (for kids ^^)
#define FLAG_SMALL D12
// Debouncer channels for buttons // Debouncer channels for buttons
// (Must be added in this order to debouncer) // (Must be added in this order to debouncer)
#define D_LEFT 0 #define D_LEFT 0
@ -106,6 +112,8 @@ void SECTION(".init8") init()
as_input_pu(BTN_SELECT); as_input_pu(BTN_SELECT);
as_input_pu(BTN_RESTART); as_input_pu(BTN_RESTART);
as_input_pu(FLAG_SMALL); // when LOW, use smaller board.
// add buttons to debouncer // add buttons to debouncer
debo_add_rev(BTN_LEFT); debo_add_rev(BTN_LEFT);
debo_add_rev(BTN_RIGHT); debo_add_rev(BTN_RIGHT);
@ -156,13 +164,15 @@ void deal_cards()
board[i] = (tile_t) { .color = 0, .state = GONE }; board[i] = (tile_t) { .color = 0, .state = GONE };
} }
const uint8_t dealt_cards = get_pin(FLAG_SMALL) ? CARD_COUNT : CARD_COUNT_SMALL;
// for all pair_COUNT // for all pair_COUNT
for (uint8_t i = 0; i < PAIR_COUNT; ++i) { for (uint8_t i = 0; i < (dealt_cards / 2); ++i) {
// for both cards in pair // for both cards in pair
for (uint8_t j = 0; j < 2; j++) { for (uint8_t j = 0; j < 2; j++) {
// loop until empty slot is found // loop until empty slot is found
while(1) { while(1) {
uint8_t pos = rand() % CARD_COUNT; const uint8_t pos = rand() % dealt_cards;
if (board[pos].state == GONE) { if (board[pos].state == GONE) {
board[pos] = (tile_t) { .color = i, .state = SECRET }; board[pos] = (tile_t) { .color = i, .state = SECRET };
@ -200,7 +210,7 @@ uint8_t tile2;
#define HIDE_TIME 100 #define HIDE_TIME 100
// length of button holding before it's repeated (in 10ms) // length of button holding before it's repeated (in 10ms)
#define BTNHOLD_REPEAT 20 #define BTNHOLD_REPEAT 15
uint8_t btn_hold_cnt[DEBO_CHANNELS]; uint8_t btn_hold_cnt[DEBO_CHANNELS];
@ -306,7 +316,7 @@ void update()
} }
// non-arrows wrap to 1 -> do not generate repeated clicks // non-arrows wrap to 1 -> do not generate repeated clicks
inc_wrap(btn_hold_cnt[i], is_arrow_key(i) ? 1 : 0, BTNHOLD_REPEAT); inc_wrap(btn_hold_cnt[i], is_arrow_key(i) ? 0 : 1, BTNHOLD_REPEAT);
} else { } else {
btn_hold_cnt[i] = 0; btn_hold_cnt[i] = 0;
@ -324,7 +334,7 @@ void update()
if (board[cursor].state == GONE) { if (board[cursor].state == GONE) {
// move to some other tile // move to some other tile
// try not to change row if possible // try not to change row if possible
if ((cursor % BOARD_WIDTH) == (BOARD_WIDTH-1)) if ((cursor % BOARD_WIDTH) == (BOARD_WIDTH - 1))
safe_press_arrow_key(D_LEFT); safe_press_arrow_key(D_LEFT);
else else
safe_press_arrow_key(D_RIGHT); safe_press_arrow_key(D_RIGHT);

Loading…
Cancel
Save