This is the first version of a Fishing Mini-game. It uses a navigational switch as a button and a joystick, playing wav files from a SD card, and a uLCD screen to show the fishing game
Dependencies: 4DGL-uLCD-SE SDFileSystem mbed-rtos mbed wave_player
main.cpp@0:5d811b6879d5, 2016-03-17 (annotated)
- Committer:
- jd0205
- Date:
- Thu Mar 17 18:33:52 2016 +0000
- Revision:
- 0:5d811b6879d5
- Child:
- 1:8dd8fafa7fc8
This is the first version of a Fishing Mini-game.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jd0205 | 0:5d811b6879d5 | 1 | #include "mbed.h" |
jd0205 | 0:5d811b6879d5 | 2 | #include "rtos.h" |
jd0205 | 0:5d811b6879d5 | 3 | #include "uLCD_4DGL.h" |
jd0205 | 0:5d811b6879d5 | 4 | #include "Nav_Switch.h" |
jd0205 | 0:5d811b6879d5 | 5 | #include "Fish.h" |
jd0205 | 0:5d811b6879d5 | 6 | #include "Fishing.h" |
jd0205 | 0:5d811b6879d5 | 7 | #include "SDFileSystem.h" |
jd0205 | 0:5d811b6879d5 | 8 | #include "wave_player.h" |
jd0205 | 0:5d811b6879d5 | 9 | |
jd0205 | 0:5d811b6879d5 | 10 | uLCD_4DGL uLCD(p28,p27,p29); //LCD screen |
jd0205 | 0:5d811b6879d5 | 11 | Nav_Switch myNav( p9, p6, p7, p5, p8); //Navigation Switch |
jd0205 | 0:5d811b6879d5 | 12 | |
jd0205 | 0:5d811b6879d5 | 13 | SDFileSystem sd(p11, p12, p13, p10, "sd"); //SD Card Reader |
jd0205 | 0:5d811b6879d5 | 14 | AnalogOut DACout(p18); //Wave Player Output |
jd0205 | 0:5d811b6879d5 | 15 | wave_player waver(&DACout); |
jd0205 | 0:5d811b6879d5 | 16 | |
jd0205 | 0:5d811b6879d5 | 17 | Mutex stdio_mutex; //Mutex for Threads |
jd0205 | 0:5d811b6879d5 | 18 | |
jd0205 | 0:5d811b6879d5 | 19 | BusOut mbedleds(LED1,LED2,LED3,LED4); //BusOut for all LEDs at once |
jd0205 | 0:5d811b6879d5 | 20 | |
jd0205 | 0:5d811b6879d5 | 21 | Fishing fisher; |
jd0205 | 0:5d811b6879d5 | 22 | |
jd0205 | 0:5d811b6879d5 | 23 | int dist; |
jd0205 | 0:5d811b6879d5 | 24 | int rem1; |
jd0205 | 0:5d811b6879d5 | 25 | int rem2; |
jd0205 | 0:5d811b6879d5 | 26 | int old1; |
jd0205 | 0:5d811b6879d5 | 27 | int old2; |
jd0205 | 0:5d811b6879d5 | 28 | |
jd0205 | 0:5d811b6879d5 | 29 | bool fishing = false; |
jd0205 | 0:5d811b6879d5 | 30 | bool lure = false; |
jd0205 | 0:5d811b6879d5 | 31 | bool success = false; |
jd0205 | 0:5d811b6879d5 | 32 | |
jd0205 | 0:5d811b6879d5 | 33 | void lakeside_thread(void const *args) { |
jd0205 | 0:5d811b6879d5 | 34 | while(1) { |
jd0205 | 0:5d811b6879d5 | 35 | stdio_mutex.lock(); |
jd0205 | 0:5d811b6879d5 | 36 | rem1 = dist%20; |
jd0205 | 0:5d811b6879d5 | 37 | rem2 = (dist+10)%20; |
jd0205 | 0:5d811b6879d5 | 38 | if (old1 != rem1 && rem1 != 0) { |
jd0205 | 0:5d811b6879d5 | 39 | uLCD.filled_rectangle(40-3*old1, 10+(old1*6), 40-3*old1+ 4*((old1/5)+1), 10+(old1*6) + 5*((old1/5)+2), BLUE); |
jd0205 | 0:5d811b6879d5 | 40 | uLCD.filled_rectangle(20, 0, 48, 23, BLACK); |
jd0205 | 0:5d811b6879d5 | 41 | uLCD.line(20, 108, 40, 40, RED); |
jd0205 | 0:5d811b6879d5 | 42 | if (old1 < 23) { |
jd0205 | 0:5d811b6879d5 | 43 | for(int i =0; i < 17; i++) { |
jd0205 | 0:5d811b6879d5 | 44 | uLCD.line(20, 23+i, 50, 23+i, i*0x000F); |
jd0205 | 0:5d811b6879d5 | 45 | } |
jd0205 | 0:5d811b6879d5 | 46 | } |
jd0205 | 0:5d811b6879d5 | 47 | old1 = rem1; |
jd0205 | 0:5d811b6879d5 | 48 | fisher.grass(40-3*rem1, 10+(rem1*6), (rem1/5)+1, GREEN); |
jd0205 | 0:5d811b6879d5 | 49 | } |
jd0205 | 0:5d811b6879d5 | 50 | if (old2 != rem2 && rem2 != 0) { |
jd0205 | 0:5d811b6879d5 | 51 | uLCD.filled_rectangle(88+2*old2, 10+(old2*6), 88+2*old2 + 4*((old2/5)+1), 10+(old2*6) + 5*((old2/5)+2), BLUE); |
jd0205 | 0:5d811b6879d5 | 52 | uLCD.filled_rectangle(88, 0 , 108, 23, BLACK); |
jd0205 | 0:5d811b6879d5 | 53 | uLCD.line(88, 40, 108, 108, RED); |
jd0205 | 0:5d811b6879d5 | 54 | if (old2 < 23) { |
jd0205 | 0:5d811b6879d5 | 55 | for(int i =0; i < 17; i++) { |
jd0205 | 0:5d811b6879d5 | 56 | uLCD.line(78, 23+i, 108, 23+i, i*0x000F); |
jd0205 | 0:5d811b6879d5 | 57 | } |
jd0205 | 0:5d811b6879d5 | 58 | } |
jd0205 | 0:5d811b6879d5 | 59 | old2 = rem2; |
jd0205 | 0:5d811b6879d5 | 60 | fisher.grass(88+2*rem2, 10+(rem2*6), (rem2/5)+1, GREEN); |
jd0205 | 0:5d811b6879d5 | 61 | } |
jd0205 | 0:5d811b6879d5 | 62 | stdio_mutex.unlock(); |
jd0205 | 0:5d811b6879d5 | 63 | } |
jd0205 | 0:5d811b6879d5 | 64 | } |
jd0205 | 0:5d811b6879d5 | 65 | |
jd0205 | 0:5d811b6879d5 | 66 | void bgm_thread(void const *args) { |
jd0205 | 0:5d811b6879d5 | 67 | FILE *wave_file; |
jd0205 | 0:5d811b6879d5 | 68 | while(true) { |
jd0205 | 0:5d811b6879d5 | 69 | while(lure) { |
jd0205 | 0:5d811b6879d5 | 70 | wave_file=fopen("/sd/wavfiles/Water Drop.wav","r"); |
jd0205 | 0:5d811b6879d5 | 71 | waver.play(wave_file); |
jd0205 | 0:5d811b6879d5 | 72 | fclose(wave_file); |
jd0205 | 0:5d811b6879d5 | 73 | Thread::wait(5000); |
jd0205 | 0:5d811b6879d5 | 74 | } |
jd0205 | 0:5d811b6879d5 | 75 | while(fishing) { |
jd0205 | 0:5d811b6879d5 | 76 | wave_file=fopen("/sd/wavfiles/Stream Noise.wav","r"); |
jd0205 | 0:5d811b6879d5 | 77 | waver.play(wave_file); |
jd0205 | 0:5d811b6879d5 | 78 | fclose(wave_file); |
jd0205 | 0:5d811b6879d5 | 79 | Thread::wait(50); |
jd0205 | 0:5d811b6879d5 | 80 | } |
jd0205 | 0:5d811b6879d5 | 81 | while(success) { |
jd0205 | 0:5d811b6879d5 | 82 | wave_file=fopen("/sd/wavfiles/Fish Splashing.wav","r"); |
jd0205 | 0:5d811b6879d5 | 83 | waver.play(wave_file); |
jd0205 | 0:5d811b6879d5 | 84 | fclose(wave_file); |
jd0205 | 0:5d811b6879d5 | 85 | Thread::wait(1000); |
jd0205 | 0:5d811b6879d5 | 86 | } |
jd0205 | 0:5d811b6879d5 | 87 | } |
jd0205 | 0:5d811b6879d5 | 88 | } |
jd0205 | 0:5d811b6879d5 | 89 | |
jd0205 | 0:5d811b6879d5 | 90 | int main() { |
jd0205 | 0:5d811b6879d5 | 91 | dist = 0; |
jd0205 | 0:5d811b6879d5 | 92 | uLCD.baudrate(3000000); |
jd0205 | 0:5d811b6879d5 | 93 | fishing = false; |
jd0205 | 0:5d811b6879d5 | 94 | lure = false; |
jd0205 | 0:5d811b6879d5 | 95 | success = false; |
jd0205 | 0:5d811b6879d5 | 96 | |
jd0205 | 0:5d811b6879d5 | 97 | uLCD.background_color(BLACK); |
jd0205 | 0:5d811b6879d5 | 98 | |
jd0205 | 0:5d811b6879d5 | 99 | fisher.start(); |
jd0205 | 0:5d811b6879d5 | 100 | wait(3); |
jd0205 | 0:5d811b6879d5 | 101 | |
jd0205 | 0:5d811b6879d5 | 102 | fisher.lake_init(); |
jd0205 | 0:5d811b6879d5 | 103 | |
jd0205 | 0:5d811b6879d5 | 104 | Thread thread1(lakeside_thread); |
jd0205 | 0:5d811b6879d5 | 105 | Thread thread2(bgm_thread); |
jd0205 | 0:5d811b6879d5 | 106 | Fish fish1; |
jd0205 | 0:5d811b6879d5 | 107 | int rand1 = rand() % 100; |
jd0205 | 0:5d811b6879d5 | 108 | int cnt = 0; |
jd0205 | 0:5d811b6879d5 | 109 | lure = true; |
jd0205 | 0:5d811b6879d5 | 110 | |
jd0205 | 0:5d811b6879d5 | 111 | while(1) { |
jd0205 | 0:5d811b6879d5 | 112 | fisher.draw_fish(64, dist); |
jd0205 | 0:5d811b6879d5 | 113 | lure = false; |
jd0205 | 0:5d811b6879d5 | 114 | fisher.draw_rod(0, 64); |
jd0205 | 0:5d811b6879d5 | 115 | stdio_mutex.lock(); |
jd0205 | 0:5d811b6879d5 | 116 | uLCD.locate(0,0); |
jd0205 | 0:5d811b6879d5 | 117 | uLCD.printf("Press Center when Flashing"); |
jd0205 | 0:5d811b6879d5 | 118 | stdio_mutex.unlock(); |
jd0205 | 0:5d811b6879d5 | 119 | if(rand1 > 80) { |
jd0205 | 0:5d811b6879d5 | 120 | for(cnt = 0; cnt < 30; cnt++) { |
jd0205 | 0:5d811b6879d5 | 121 | mbedleds = 0x0F*(cnt%2); |
jd0205 | 0:5d811b6879d5 | 122 | uLCD.circle(64, 80 , 4+(cnt%8), BLACK ); |
jd0205 | 0:5d811b6879d5 | 123 | uLCD.circle(64, 80 , 5+(cnt%8), BLACK ); |
jd0205 | 0:5d811b6879d5 | 124 | uLCD.circle(64, 80 , 4+(cnt%8), BLUE); |
jd0205 | 0:5d811b6879d5 | 125 | uLCD.circle(64, 80 , 5+(cnt%8), BLUE); |
jd0205 | 0:5d811b6879d5 | 126 | if(myNav.fire()) { |
jd0205 | 0:5d811b6879d5 | 127 | while(dist < 300) { |
jd0205 | 0:5d811b6879d5 | 128 | dist = dist + 5; |
jd0205 | 0:5d811b6879d5 | 129 | } |
jd0205 | 0:5d811b6879d5 | 130 | fishing = true; |
jd0205 | 0:5d811b6879d5 | 131 | fish1.reset(); |
jd0205 | 0:5d811b6879d5 | 132 | cnt = 30; |
jd0205 | 0:5d811b6879d5 | 133 | mbedleds = 0x00; |
jd0205 | 0:5d811b6879d5 | 134 | stdio_mutex.lock(); |
jd0205 | 0:5d811b6879d5 | 135 | uLCD.filled_rectangle(0,0,127,20,BLACK); |
jd0205 | 0:5d811b6879d5 | 136 | stdio_mutex.unlock(); |
jd0205 | 0:5d811b6879d5 | 137 | } |
jd0205 | 0:5d811b6879d5 | 138 | wait(0.1); |
jd0205 | 0:5d811b6879d5 | 139 | } |
jd0205 | 0:5d811b6879d5 | 140 | cnt = 0; |
jd0205 | 0:5d811b6879d5 | 141 | } |
jd0205 | 0:5d811b6879d5 | 142 | while(fishing) { |
jd0205 | 0:5d811b6879d5 | 143 | fisher.draw_fish(fish1.x(), dist); |
jd0205 | 0:5d811b6879d5 | 144 | fish1.movement(); |
jd0205 | 0:5d811b6879d5 | 145 | dist = dist - 2; |
jd0205 | 0:5d811b6879d5 | 146 | if (myNav.left()) { |
jd0205 | 0:5d811b6879d5 | 147 | cnt--; |
jd0205 | 0:5d811b6879d5 | 148 | if (cnt > 0) { |
jd0205 | 0:5d811b6879d5 | 149 | cnt-=2; |
jd0205 | 0:5d811b6879d5 | 150 | } |
jd0205 | 0:5d811b6879d5 | 151 | } else if (myNav.right()) { |
jd0205 | 0:5d811b6879d5 | 152 | cnt++; |
jd0205 | 0:5d811b6879d5 | 153 | if (cnt < 0) { |
jd0205 | 0:5d811b6879d5 | 154 | cnt+=2; |
jd0205 | 0:5d811b6879d5 | 155 | } |
jd0205 | 0:5d811b6879d5 | 156 | } else { |
jd0205 | 0:5d811b6879d5 | 157 | if (cnt > 0) { |
jd0205 | 0:5d811b6879d5 | 158 | cnt--; |
jd0205 | 0:5d811b6879d5 | 159 | } else if (cnt < 0) { |
jd0205 | 0:5d811b6879d5 | 160 | cnt++; |
jd0205 | 0:5d811b6879d5 | 161 | } |
jd0205 | 0:5d811b6879d5 | 162 | } |
jd0205 | 0:5d811b6879d5 | 163 | if (cnt > 6) { |
jd0205 | 0:5d811b6879d5 | 164 | cnt = 6; |
jd0205 | 0:5d811b6879d5 | 165 | } else if (cnt < -6) { |
jd0205 | 0:5d811b6879d5 | 166 | cnt = -6; |
jd0205 | 0:5d811b6879d5 | 167 | } |
jd0205 | 0:5d811b6879d5 | 168 | fish1.move(((64+cnt*6 - fish1.x())/30) + 1); |
jd0205 | 0:5d811b6879d5 | 169 | fisher.draw_rod(cnt, fish1.x()); |
jd0205 | 0:5d811b6879d5 | 170 | |
jd0205 | 0:5d811b6879d5 | 171 | if(dist == 0) { |
jd0205 | 0:5d811b6879d5 | 172 | success = true; |
jd0205 | 0:5d811b6879d5 | 173 | fishing = false; |
jd0205 | 0:5d811b6879d5 | 174 | stdio_mutex.lock(); |
jd0205 | 0:5d811b6879d5 | 175 | fisher.lake_init(); |
jd0205 | 0:5d811b6879d5 | 176 | uLCD.locate(1,1); |
jd0205 | 0:5d811b6879d5 | 177 | uLCD.printf("You Caught It!"); |
jd0205 | 0:5d811b6879d5 | 178 | wait(3.0); |
jd0205 | 0:5d811b6879d5 | 179 | uLCD.filled_rectangle(0,0,127,20,BLACK); |
jd0205 | 0:5d811b6879d5 | 180 | stdio_mutex.unlock(); |
jd0205 | 0:5d811b6879d5 | 181 | success = false; |
jd0205 | 0:5d811b6879d5 | 182 | } |
jd0205 | 0:5d811b6879d5 | 183 | if (!(fish1.failed())) { |
jd0205 | 0:5d811b6879d5 | 184 | fishing = false; |
jd0205 | 0:5d811b6879d5 | 185 | stdio_mutex.lock(); |
jd0205 | 0:5d811b6879d5 | 186 | fisher.lake_init(); |
jd0205 | 0:5d811b6879d5 | 187 | uLCD.locate(1,1); |
jd0205 | 0:5d811b6879d5 | 188 | uLCD.printf("It Got Away..."); |
jd0205 | 0:5d811b6879d5 | 189 | wait(3.0); |
jd0205 | 0:5d811b6879d5 | 190 | uLCD.filled_rectangle(0,0,127,20,BLACK); |
jd0205 | 0:5d811b6879d5 | 191 | stdio_mutex.unlock(); |
jd0205 | 0:5d811b6879d5 | 192 | } |
jd0205 | 0:5d811b6879d5 | 193 | wait(0.25); |
jd0205 | 0:5d811b6879d5 | 194 | } |
jd0205 | 0:5d811b6879d5 | 195 | rand1 = rand() % 100; |
jd0205 | 0:5d811b6879d5 | 196 | dist = 300; |
jd0205 | 0:5d811b6879d5 | 197 | cnt = 0; |
jd0205 | 0:5d811b6879d5 | 198 | wait(0.1); |
jd0205 | 0:5d811b6879d5 | 199 | } |
jd0205 | 0:5d811b6879d5 | 200 | } |