1010
Check-in [9f2d78daf6]
Not logged in
Public Repositories
mwm's Repositories

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Rewrite the menu code so that the DONE state can use it, so we now get a New/Help menu when the game is over.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:9f2d78daf676ffed1db68b141a0d04138de15de7
User & Date: mwm 2016-11-06 10:01:30
Context
2016-11-06
11:31
Update some comments. check-in: f3ecf097c2 user: mwm tags: trunk
10:01
Rewrite the menu code so that the DONE state can use it, so we now get a New/Help menu when the game is over. check-in: 9f2d78daf6 user: mwm tags: trunk
2016-11-05
13:28
Initial version of help system. Needs more work. check-in: 44de8814a6 user: mwm tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to Tiny-1010.ino.

126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
...
244
245
246
247
248
249
250

251
252
253
254
255
256
257
...
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
...
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
...
414
415
416
417
418
419
420
421
422
423
424
425
426
427

428

429
430
431
432
433


434
435
436

437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
...
498
499
500
501
502
503
504
505









506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
     {2, 2, fours[4]},
     {2, 2, fours[5]},
     {2, 2, fours[6]},
};


// The game
typedef enum {MENU, DONE, SELECTING, MOVING} game_state;
const byte WAIT_LEN = 3;

struct game_data {
     unsigned long high, score;
     byte delay;
     int8_t waiting[WAIT_LEN];	// Shapes ready to be played.
     byte x, y;	// And it's location on the board.
................................................................................

     for (byte w = 0; w < WAIT_LEN; w += 1)
          if (game.waiting[w] >= 0 && find_clear(&shapes[game.waiting[w]])) {
               state = SELECTING;
               return;
          }
     state = DONE;

}


void
reset_game(void) {
     game.delay = 100;
     game.score = 0;
................................................................................

     // use pollButtons since we do what the button asks.
     do arduboy.pollButtons();
     while (!arduboy.justPressed(A_BUTTON));
}

void
draw_menu() {
     draw_msg("  Paused\n\n");
     arduboy.print("\33 ");
     arduboy.print(menu[selected]);
     arduboy.print(" \32");
}

byte
draw_game(void) {
     byte can_place = 0;
................................................................................
     arduboy.print("High\n");
     arduboy.print(game.high);


     switch (state) {
     case DONE:
          blue = RGB_ON;
          draw_msg("Game over!\n\nA to start");
          break;
     case MENU:
          draw_menu();
          break;
     default:
          for (byte i = 0; i < WAIT_LEN; i += 1) {
               arduboy.setCursor(BLOCK_SIZE, (i + 3) * BLOCK_SIZE);
               arduboy.write(i == selected ? '+' : game.waiting[i] < 0 ? ' ' : '-');
          }
          color_shape(s, 32 - s->width * BLOCK_SIZE / 2,
................................................................................
/* Arduino API */
const int store = 266;	// 10 (base 256) + 10. Because why not?
const int saved_game = store + 2;
const int flagbyte = 10;

void
setup(void) {
     boolean reset = true;

     arduboy.begin();
     arduboy.setTextWrap(true);

     if (EEPROM.read(store) != flagbyte || EEPROM.read(store + 1) != flagbyte) {
          // No saved game

          game.high = 0;

          EEPROM.put(saved_game, game);
          EEPROM.write(store, flagbyte);
          EEPROM.write(store + 1, flagbyte);
     } else {
          EEPROM.get(saved_game, game);


          for (byte i = 0; i < WAIT_LEN; i += 1) {
               if (game.waiting[i] >= 0) {
                    reset = false;

                    selected = RESUME;
                    break;
               }
          }
     }

     draw_board();
     draw_page_msg("\n\n1010 v1.0\n by mwm", false);
     arduboy.initRandomSeed();

     if (reset)
          reset_game();
     state = MENU;
}

void
loop(void) {
     const shape *const s = &shapes[game.waiting[selected]];
     const byte can_place = draw_game();

     arduboy.pollButtons();

     if (arduboy.justPressed(B_BUTTON)) {
          state = MENU;
          selected = RESUME;
          return;
     }

     switch (state) {
     case SELECTING:
          if (arduboy.justPressed(UP_BUTTON))
................................................................................
          else if (arduboy.pressed(LEFT_BUTTON)) {
               if (game.x > 0)
                    game.x -= 1;
               else
                    state = SELECTING;
          }
          break;
     case MENU:









          if (arduboy.justPressed(A_BUTTON)) {
               do_menu();
          } else if (arduboy.justPressed(LEFT_BUTTON)) {
               if (selected)
                    selected -= 1;
               else
                    selected = sizeof(menu) / sizeof(char *) - 2;
          } else if (arduboy.justPressed(RIGHT_BUTTON)) {
               selected = (selected + 1) % (sizeof(menu) / sizeof(char *) - 1);
          }
          break;
     case DONE:
          for (byte i = 0; i < WAIT_LEN; i += 1)
               game.waiting[i] = -1;
          EEPROM.put(saved_game, game);
          if (arduboy.justPressed(A_BUTTON))
               reset_game();
          break;
     }

     EEPROM.put(saved_game, game);
     if (state == MOVING) {
          unsigned long end = millis() + game.delay;
          // Used pressed as the button just exists the loop.
          while (millis() < end)
               if (arduboy.pressed(A_BUTTON) || arduboy.pressed(B_BUTTON))
                    break;
     }
}








|







 







>







 







|
|
|







 







|

|
|







 







<
<





>

>





>
>


<
>









<
<
<
<








<

|







 







|
>
>
>
>
>
>
>
>
>











<
<
<
<
<
<
<












126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
...
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
...
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
...
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
...
415
416
417
418
419
420
421


422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438

439
440
441
442
443
444
445
446
447
448




449
450
451
452
453
454
455
456

457
458
459
460
461
462
463
464
465
...
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523







524
525
526
527
528
529
530
531
532
533
534
535
     {2, 2, fours[4]},
     {2, 2, fours[5]},
     {2, 2, fours[6]},
};


// The game
typedef enum {PAUSED, DONE, SELECTING, MOVING} game_state;
const byte WAIT_LEN = 3;

struct game_data {
     unsigned long high, score;
     byte delay;
     int8_t waiting[WAIT_LEN];	// Shapes ready to be played.
     byte x, y;	// And it's location on the board.
................................................................................

     for (byte w = 0; w < WAIT_LEN; w += 1)
          if (game.waiting[w] >= 0 && find_clear(&shapes[game.waiting[w]])) {
               state = SELECTING;
               return;
          }
     state = DONE;
     selected = NEW;
}


void
reset_game(void) {
     game.delay = 100;
     game.score = 0;
................................................................................

     // use pollButtons since we do what the button asks.
     do arduboy.pollButtons();
     while (!arduboy.justPressed(A_BUTTON));
}

void
draw_menu(const char *const title) {
     draw_msg(title);
     arduboy.print("\n\n\33 ");
     arduboy.print(menu[selected]);
     arduboy.print(" \32");
}

byte
draw_game(void) {
     byte can_place = 0;
................................................................................
     arduboy.print("High\n");
     arduboy.print(game.high);


     switch (state) {
     case DONE:
          blue = RGB_ON;
          draw_menu("Game over!");
          break;
     case PAUSED:
          draw_menu("Paused");
          break;
     default:
          for (byte i = 0; i < WAIT_LEN; i += 1) {
               arduboy.setCursor(BLOCK_SIZE, (i + 3) * BLOCK_SIZE);
               arduboy.write(i == selected ? '+' : game.waiting[i] < 0 ? ' ' : '-');
          }
          color_shape(s, 32 - s->width * BLOCK_SIZE / 2,
................................................................................
/* Arduino API */
const int store = 266;	// 10 (base 256) + 10. Because why not?
const int saved_game = store + 2;
const int flagbyte = 10;

void
setup(void) {


     arduboy.begin();
     arduboy.setTextWrap(true);

     if (EEPROM.read(store) != flagbyte || EEPROM.read(store + 1) != flagbyte) {
          // No saved game
          reset_game();
          game.high = 0;
          selected = HELP;
          EEPROM.put(saved_game, game);
          EEPROM.write(store, flagbyte);
          EEPROM.write(store + 1, flagbyte);
     } else {
          EEPROM.get(saved_game, game);
          state = DONE;
          selected = NEW;
          for (byte i = 0; i < WAIT_LEN; i += 1) {
               if (game.waiting[i] >= 0) {

                    state = PAUSED;
                    selected = RESUME;
                    break;
               }
          }
     }

     draw_board();
     draw_page_msg("\n\n1010 v1.0\n by mwm", false);
     arduboy.initRandomSeed();




}

void
loop(void) {
     const shape *const s = &shapes[game.waiting[selected]];
     const byte can_place = draw_game();

     arduboy.pollButtons();

     if (arduboy.justPressed(B_BUTTON)) {
          state = PAUSED;
          selected = RESUME;
          return;
     }

     switch (state) {
     case SELECTING:
          if (arduboy.justPressed(UP_BUTTON))
................................................................................
          else if (arduboy.pressed(LEFT_BUTTON)) {
               if (game.x > 0)
                    game.x -= 1;
               else
                    state = SELECTING;
          }
          break;
     case DONE:
          for (byte i = 0; i < WAIT_LEN; i += 1)
               game.waiting[i] = -1;
          if (arduboy.justPressed(A_BUTTON))
               do_menu();
          else if (arduboy.justPressed(LEFT_BUTTON)
                   || arduboy.justPressed(RIGHT_BUTTON))
               selected = selected == NEW ? HELP : NEW;
          break;
     case PAUSED:
          if (arduboy.justPressed(A_BUTTON)) {
               do_menu();
          } else if (arduboy.justPressed(LEFT_BUTTON)) {
               if (selected)
                    selected -= 1;
               else
                    selected = sizeof(menu) / sizeof(char *) - 2;
          } else if (arduboy.justPressed(RIGHT_BUTTON)) {
               selected = (selected + 1) % (sizeof(menu) / sizeof(char *) - 1);
          }
          break;







     }

     EEPROM.put(saved_game, game);
     if (state == MOVING) {
          unsigned long end = millis() + game.delay;
          // Used pressed as the button just exists the loop.
          while (millis() < end)
               if (arduboy.pressed(A_BUTTON) || arduboy.pressed(B_BUTTON))
                    break;
     }
}