AirsoftTimer software based on mbed
Dependencies: mbed TextLCD keypad
games/GameSelector.cpp@10:afc22465169e, 2014-12-14 (annotated)
- Committer:
- sillevl
- Date:
- Sun Dec 14 11:50:19 2014 +0000
- Revision:
- 10:afc22465169e
- Child:
- 17:19dbb1dbb640
Added game selector skeleton + demo
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sillevl | 10:afc22465169e | 1 | #include "GameSelector.h" |
sillevl | 10:afc22465169e | 2 | |
sillevl | 10:afc22465169e | 3 | GameSelector::GameSelector(Board* board){ |
sillevl | 10:afc22465169e | 4 | this->board = board; |
sillevl | 10:afc22465169e | 5 | titles[0] = "Hold it"; |
sillevl | 10:afc22465169e | 6 | titles[1] = "Capture the bomb"; |
sillevl | 10:afc22465169e | 7 | titles[2] = "Hurry up"; |
sillevl | 10:afc22465169e | 8 | titles[3] = "Search & destroy"; |
sillevl | 10:afc22465169e | 9 | titles[4] = "Simple timer"; |
sillevl | 10:afc22465169e | 10 | titles[5] = "DummyGame 6"; |
sillevl | 10:afc22465169e | 11 | titles[6] = "DummyGame 7"; |
sillevl | 10:afc22465169e | 12 | total_selections = 7; |
sillevl | 10:afc22465169e | 13 | current_selection = 0; |
sillevl | 10:afc22465169e | 14 | start_position = 0; |
sillevl | 10:afc22465169e | 15 | } |
sillevl | 10:afc22465169e | 16 | |
sillevl | 10:afc22465169e | 17 | void GameSelector::run(){ |
sillevl | 10:afc22465169e | 18 | print_list(); |
sillevl | 10:afc22465169e | 19 | //demo |
sillevl | 10:afc22465169e | 20 | wait(0.5); |
sillevl | 10:afc22465169e | 21 | while(true){ |
sillevl | 10:afc22465169e | 22 | for(int i = 0; i < 10; i++){ |
sillevl | 10:afc22465169e | 23 | go_down(); |
sillevl | 10:afc22465169e | 24 | wait(0.5); |
sillevl | 10:afc22465169e | 25 | } |
sillevl | 10:afc22465169e | 26 | for(int i = 0; i < 10; i++){ |
sillevl | 10:afc22465169e | 27 | go_up(); |
sillevl | 10:afc22465169e | 28 | wait(0.5); |
sillevl | 10:afc22465169e | 29 | } |
sillevl | 10:afc22465169e | 30 | |
sillevl | 10:afc22465169e | 31 | } // wait until selection is done |
sillevl | 10:afc22465169e | 32 | } |
sillevl | 10:afc22465169e | 33 | |
sillevl | 10:afc22465169e | 34 | void GameSelector::print_list(){ |
sillevl | 10:afc22465169e | 35 | board->lcd->cls(); |
sillevl | 10:afc22465169e | 36 | for(int i = 0; i < 4; i++){ |
sillevl | 10:afc22465169e | 37 | board->lcd->locate(2,i); |
sillevl | 10:afc22465169e | 38 | board->lcd->printf(titles[i+start_position]); |
sillevl | 10:afc22465169e | 39 | } |
sillevl | 10:afc22465169e | 40 | print_selection_arrow(); |
sillevl | 10:afc22465169e | 41 | print_up_down_arrows(); |
sillevl | 10:afc22465169e | 42 | } |
sillevl | 10:afc22465169e | 43 | |
sillevl | 10:afc22465169e | 44 | void GameSelector::print_up_down_arrows(){ |
sillevl | 10:afc22465169e | 45 | if(start_position != 0){ |
sillevl | 10:afc22465169e | 46 | board->lcd->locate(19,0); |
sillevl | 10:afc22465169e | 47 | board->lcd->putc('\x02'); |
sillevl | 10:afc22465169e | 48 | } |
sillevl | 10:afc22465169e | 49 | if(start_position < total_selections - 4){ |
sillevl | 10:afc22465169e | 50 | board->lcd->locate(19,3); |
sillevl | 10:afc22465169e | 51 | board->lcd->putc('\x03'); |
sillevl | 10:afc22465169e | 52 | } |
sillevl | 10:afc22465169e | 53 | } |
sillevl | 10:afc22465169e | 54 | |
sillevl | 10:afc22465169e | 55 | void GameSelector::print_selection_arrow(){ |
sillevl | 10:afc22465169e | 56 | int line = current_selection - start_position; |
sillevl | 10:afc22465169e | 57 | board->lcd->locate(0,line); |
sillevl | 10:afc22465169e | 58 | board->lcd->putc('\x00'); |
sillevl | 10:afc22465169e | 59 | } |
sillevl | 10:afc22465169e | 60 | |
sillevl | 10:afc22465169e | 61 | void GameSelector::go_down(){ |
sillevl | 10:afc22465169e | 62 | if(current_selection < (total_selections - 1)){ |
sillevl | 10:afc22465169e | 63 | current_selection++; |
sillevl | 10:afc22465169e | 64 | } |
sillevl | 10:afc22465169e | 65 | if(current_selection - start_position >= 4){ |
sillevl | 10:afc22465169e | 66 | start_position = current_selection - 3; |
sillevl | 10:afc22465169e | 67 | } |
sillevl | 10:afc22465169e | 68 | print_list(); |
sillevl | 10:afc22465169e | 69 | } |
sillevl | 10:afc22465169e | 70 | |
sillevl | 10:afc22465169e | 71 | void GameSelector::go_up(){ |
sillevl | 10:afc22465169e | 72 | if(current_selection > 0){ |
sillevl | 10:afc22465169e | 73 | current_selection--; |
sillevl | 10:afc22465169e | 74 | } |
sillevl | 10:afc22465169e | 75 | if(current_selection - start_position < 0){ |
sillevl | 10:afc22465169e | 76 | start_position = current_selection; |
sillevl | 10:afc22465169e | 77 | } |
sillevl | 10:afc22465169e | 78 | print_list(); |
sillevl | 10:afc22465169e | 79 | } |