an mbed tile music game using a capacitive touchpad and uLCD
Dependencies: SDFileSystem mbed wave_player
main.cpp@0:a1c374b9a4fe, 2016-03-14 (annotated)
- Committer:
- clu67
- Date:
- Mon Mar 14 00:26:24 2016 +0000
- Revision:
- 0:a1c374b9a4fe
Initial Release
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
clu67 | 0:a1c374b9a4fe | 1 | //ECE 4180 Piano Tiles |
clu67 | 0:a1c374b9a4fe | 2 | //Spring 2016 Georgia Tech |
clu67 | 0:a1c374b9a4fe | 3 | //Kushagra Brahmbhatt |
clu67 | 0:a1c374b9a4fe | 4 | //Christopher Lu |
clu67 | 0:a1c374b9a4fe | 5 | |
clu67 | 0:a1c374b9a4fe | 6 | #include "mbed.h" |
clu67 | 0:a1c374b9a4fe | 7 | #include "uLCD_4DGL.h" |
clu67 | 0:a1c374b9a4fe | 8 | #include <mpr121.h> |
clu67 | 0:a1c374b9a4fe | 9 | #include "SongPlayer.h" |
clu67 | 0:a1c374b9a4fe | 10 | #include "SDFileSystem.h" |
clu67 | 0:a1c374b9a4fe | 11 | #include "wave_player.h" |
clu67 | 0:a1c374b9a4fe | 12 | |
clu67 | 0:a1c374b9a4fe | 13 | //LCD better |
clu67 | 0:a1c374b9a4fe | 14 | uLCD_4DGL uLCD(p28,p27,p29); |
clu67 | 0:a1c374b9a4fe | 15 | |
clu67 | 0:a1c374b9a4fe | 16 | //Speaker |
clu67 | 0:a1c374b9a4fe | 17 | SongPlayer mySpeaker(p21); |
clu67 | 0:a1c374b9a4fe | 18 | AnalogOut Speaker(p18); |
clu67 | 0:a1c374b9a4fe | 19 | |
clu67 | 0:a1c374b9a4fe | 20 | // Setup the i2c bus on pins 9 and 10 |
clu67 | 0:a1c374b9a4fe | 21 | I2C i2c(p9, p10); |
clu67 | 0:a1c374b9a4fe | 22 | |
clu67 | 0:a1c374b9a4fe | 23 | // Setup the Mpr121: |
clu67 | 0:a1c374b9a4fe | 24 | // Create the interrupt receiver object on pin 26 |
clu67 | 0:a1c374b9a4fe | 25 | InterruptIn interrupt(p26); |
clu67 | 0:a1c374b9a4fe | 26 | // constructor(i2c object, i2c address of the mpr121) |
clu67 | 0:a1c374b9a4fe | 27 | Mpr121 mpr121(&i2c, Mpr121::ADD_VSS); |
clu67 | 0:a1c374b9a4fe | 28 | |
clu67 | 0:a1c374b9a4fe | 29 | // the pinout on the mbed Cool Components workshop board |
clu67 | 0:a1c374b9a4fe | 30 | SDFileSystem sd(p5, p6, p7, p8, "sd"); |
clu67 | 0:a1c374b9a4fe | 31 | |
clu67 | 0:a1c374b9a4fe | 32 | //timer |
clu67 | 0:a1c374b9a4fe | 33 | Timer t; |
clu67 | 0:a1c374b9a4fe | 34 | |
clu67 | 0:a1c374b9a4fe | 35 | //bool key0Press, key4Press, key8Press; |
clu67 | 0:a1c374b9a4fe | 36 | int vals; |
clu67 | 0:a1c374b9a4fe | 37 | bool interruptBool; |
clu67 | 0:a1c374b9a4fe | 38 | int score, timer; |
clu67 | 0:a1c374b9a4fe | 39 | |
clu67 | 0:a1c374b9a4fe | 40 | // Key hit/release interrupt routine |
clu67 | 0:a1c374b9a4fe | 41 | void fallInterrupt() { //void |
clu67 | 0:a1c374b9a4fe | 42 | |
clu67 | 0:a1c374b9a4fe | 43 | int value=mpr121.read(0x00); |
clu67 | 0:a1c374b9a4fe | 44 | |
clu67 | 0:a1c374b9a4fe | 45 | //wait(2); |
clu67 | 0:a1c374b9a4fe | 46 | value +=mpr121.read(0x01)<<8; |
clu67 | 0:a1c374b9a4fe | 47 | interruptBool = true; |
clu67 | 0:a1c374b9a4fe | 48 | vals = value; |
clu67 | 0:a1c374b9a4fe | 49 | } |
clu67 | 0:a1c374b9a4fe | 50 | |
clu67 | 0:a1c374b9a4fe | 51 | |
clu67 | 0:a1c374b9a4fe | 52 | int press() { |
clu67 | 0:a1c374b9a4fe | 53 | int value=mpr121.read(0x00); |
clu67 | 0:a1c374b9a4fe | 54 | value +=mpr121.read(0x01)<<8; |
clu67 | 0:a1c374b9a4fe | 55 | //1(key 0) 2(key 1) 4 8 16 32 64 128 256 512 1024 2048(key 11) |
clu67 | 0:a1c374b9a4fe | 56 | return value; |
clu67 | 0:a1c374b9a4fe | 57 | } |
clu67 | 0:a1c374b9a4fe | 58 | |
clu67 | 0:a1c374b9a4fe | 59 | bool check(int value) { |
clu67 | 0:a1c374b9a4fe | 60 | if (value == 1) { //key0 |
clu67 | 0:a1c374b9a4fe | 61 | if(uLCD.read_pixel(15,90)==BLACK) { |
clu67 | 0:a1c374b9a4fe | 62 | return true; |
clu67 | 0:a1c374b9a4fe | 63 | } |
clu67 | 0:a1c374b9a4fe | 64 | } |
clu67 | 0:a1c374b9a4fe | 65 | if (value == 16) { //key4 |
clu67 | 0:a1c374b9a4fe | 66 | if(uLCD.read_pixel(50,90)==BLACK) { |
clu67 | 0:a1c374b9a4fe | 67 | return true; |
clu67 | 0:a1c374b9a4fe | 68 | } |
clu67 | 0:a1c374b9a4fe | 69 | } |
clu67 | 0:a1c374b9a4fe | 70 | if (value == 256) { //key8 |
clu67 | 0:a1c374b9a4fe | 71 | if(uLCD.read_pixel(90,90)==BLACK) { |
clu67 | 0:a1c374b9a4fe | 72 | return true; |
clu67 | 0:a1c374b9a4fe | 73 | } |
clu67 | 0:a1c374b9a4fe | 74 | } |
clu67 | 0:a1c374b9a4fe | 75 | return false; |
clu67 | 0:a1c374b9a4fe | 76 | } |
clu67 | 0:a1c374b9a4fe | 77 | |
clu67 | 0:a1c374b9a4fe | 78 | //initialize the graphics for the game |
clu67 | 0:a1c374b9a4fe | 79 | void startGame(){ |
clu67 | 0:a1c374b9a4fe | 80 | uLCD.background_color(WHITE); |
clu67 | 0:a1c374b9a4fe | 81 | uLCD.cls(); |
clu67 | 0:a1c374b9a4fe | 82 | //draw walls |
clu67 | 0:a1c374b9a4fe | 83 | uLCD.filled_rectangle(0,103,10,0, BLUE); |
clu67 | 0:a1c374b9a4fe | 84 | uLCD.filled_rectangle(117,103,127,0, BLUE); |
clu67 | 0:a1c374b9a4fe | 85 | uLCD.filled_rectangle(10,78,117,103, 0xFDF983); |
clu67 | 0:a1c374b9a4fe | 86 | uLCD.locate(0,13); |
clu67 | 0:a1c374b9a4fe | 87 | uLCD.textbackground_color(WHITE); |
clu67 | 0:a1c374b9a4fe | 88 | uLCD.color(RED); |
clu67 | 0:a1c374b9a4fe | 89 | uLCD.printf("Score: "); |
clu67 | 0:a1c374b9a4fe | 90 | uLCD.locate(9,13); |
clu67 | 0:a1c374b9a4fe | 91 | uLCD.printf("Time:"); |
clu67 | 0:a1c374b9a4fe | 92 | uLCD.locate(4,14); |
clu67 | 0:a1c374b9a4fe | 93 | uLCD.text_width(1); //4X size text |
clu67 | 0:a1c374b9a4fe | 94 | uLCD.text_height(2); |
clu67 | 0:a1c374b9a4fe | 95 | uLCD.printf("Piano Tiles"); |
clu67 | 0:a1c374b9a4fe | 96 | |
clu67 | 0:a1c374b9a4fe | 97 | |
clu67 | 0:a1c374b9a4fe | 98 | //draw the lines |
clu67 | 0:a1c374b9a4fe | 99 | uLCD.line(10, 26, 117, 26, BLACK); |
clu67 | 0:a1c374b9a4fe | 100 | uLCD.line(10, 52, 117, 52, BLACK); |
clu67 | 0:a1c374b9a4fe | 101 | uLCD.line(10, 78, 117, 78, BLACK); |
clu67 | 0:a1c374b9a4fe | 102 | uLCD.line(10, 103, 117, 103, BLACK); |
clu67 | 0:a1c374b9a4fe | 103 | |
clu67 | 0:a1c374b9a4fe | 104 | uLCD.line(46, 0, 46, 103, BLACK); |
clu67 | 0:a1c374b9a4fe | 105 | uLCD.line(82, 0, 82, 103, BLACK); |
clu67 | 0:a1c374b9a4fe | 106 | |
clu67 | 0:a1c374b9a4fe | 107 | } |
clu67 | 0:a1c374b9a4fe | 108 | |
clu67 | 0:a1c374b9a4fe | 109 | //moves block down |
clu67 | 0:a1c374b9a4fe | 110 | int updateBlockY1(int y) { |
clu67 | 0:a1c374b9a4fe | 111 | y = y + 26; |
clu67 | 0:a1c374b9a4fe | 112 | if (y > 81) { |
clu67 | 0:a1c374b9a4fe | 113 | y = 3; |
clu67 | 0:a1c374b9a4fe | 114 | } |
clu67 | 0:a1c374b9a4fe | 115 | return y; |
clu67 | 0:a1c374b9a4fe | 116 | } |
clu67 | 0:a1c374b9a4fe | 117 | |
clu67 | 0:a1c374b9a4fe | 118 | int updateBlockY2(int y) { |
clu67 | 0:a1c374b9a4fe | 119 | y = y + 26; |
clu67 | 0:a1c374b9a4fe | 120 | if (y > 101) { |
clu67 | 0:a1c374b9a4fe | 121 | y = 23; |
clu67 | 0:a1c374b9a4fe | 122 | } |
clu67 | 0:a1c374b9a4fe | 123 | return y; |
clu67 | 0:a1c374b9a4fe | 124 | } |
clu67 | 0:a1c374b9a4fe | 125 | |
clu67 | 0:a1c374b9a4fe | 126 | //Frequency of notes for Fur Elise |
clu67 | 0:a1c374b9a4fe | 127 | float note[85]= {329.63,311.13,329.63,311.13,329.63,246.94,293.66,261.63,220.00, |
clu67 | 0:a1c374b9a4fe | 128 | 146.83,174.61,220.0,246.94,174.61,233.08,246.94,261.63, 329.63, |
clu67 | 0:a1c374b9a4fe | 129 | 311.13, 329.63, 246.94, 293.66, 261.63, 220.00, 146.83, 174.61, |
clu67 | 0:a1c374b9a4fe | 130 | 220.00, 246.94, 174.61, 261.63, 246.94, 220.00, 246.94, 261.63, |
clu67 | 0:a1c374b9a4fe | 131 | 293.66, 329.63, 196.00, 329.23, 329.23, 293.63, 164.81, 329.63, |
clu67 | 0:a1c374b9a4fe | 132 | 293.63, 261.63, 146.83, 293.63, 261.63, 246.94, 329.63, 311.13, |
clu67 | 0:a1c374b9a4fe | 133 | 329.63, 311.13, 329.63, 246.94, 293.66, 261.63, 220.00, 146.83, |
clu67 | 0:a1c374b9a4fe | 134 | 174.61, 220.00, 246.94, 174.61, 233.08, 246.94, 261.63, 329.63, |
clu67 | 0:a1c374b9a4fe | 135 | 311.13, 329.63, 311.13, 329.63, 311.13, 329.63, 246.94, 293.66, |
clu67 | 0:a1c374b9a4fe | 136 | 261.63, 220.00, 146.83, 174.61, 220.00, 246.94, 174.61, 261.63, |
clu67 | 0:a1c374b9a4fe | 137 | 246.94, 220.0, 0.0}; |
clu67 | 0:a1c374b9a4fe | 138 | //How long each note plays for |
clu67 | 0:a1c374b9a4fe | 139 | float duration[85]= {0.3,0.3,0.3,0.3,0.30,0.30,0.30,0.30,0.30, |
clu67 | 0:a1c374b9a4fe | 140 | 0.30,0.30,0.30,0.30,0.30,0.30,0.30,0.30, |
clu67 | 0:a1c374b9a4fe | 141 | 0.30, 0.30, 0.30, 0.30, 0.30, 0.30, 0.30, |
clu67 | 0:a1c374b9a4fe | 142 | 0.30, 0.30, 0.30, 0.30, 0.30, 0.30, 0.30, |
clu67 | 0:a1c374b9a4fe | 143 | 0.30, 0.30, 0.30, 0.30, 0.30, 0.30, 0.30, |
clu67 | 0:a1c374b9a4fe | 144 | 0.30, 0.30, 0.30, 0.30, 0.30, 0.30, 0.30, |
clu67 | 0:a1c374b9a4fe | 145 | 0.30, 0.30, 0.30, 0.30, 0.30, 0.30, 0.30, |
clu67 | 0:a1c374b9a4fe | 146 | 0.30, 0.30, 0.30, 0.30, 0.30, 0.30, 0.30, |
clu67 | 0:a1c374b9a4fe | 147 | 0.30, 0.30, 0.30, 0.30, 0.30, 0.30, 0.30, |
clu67 | 0:a1c374b9a4fe | 148 | 0.30, 0.30, 0.30, 0.30, 0.30, 0.30, 0.30, |
clu67 | 0:a1c374b9a4fe | 149 | 0.30, 0.30, 0.30, 0.30, 0.30, 0.30, 0.30, |
clu67 | 0:a1c374b9a4fe | 150 | 0.30, 0.30, 0.30, 0.30, 0.0}; |
clu67 | 0:a1c374b9a4fe | 151 | |
clu67 | 0:a1c374b9a4fe | 152 | |
clu67 | 0:a1c374b9a4fe | 153 | int main() { |
clu67 | 0:a1c374b9a4fe | 154 | |
clu67 | 0:a1c374b9a4fe | 155 | //wait 4 seconds then moves on to graphics of the game |
clu67 | 0:a1c374b9a4fe | 156 | uLCD.locate(3,4); |
clu67 | 0:a1c374b9a4fe | 157 | uLCD.text_width(1); |
clu67 | 0:a1c374b9a4fe | 158 | uLCD.text_height(2); |
clu67 | 0:a1c374b9a4fe | 159 | uLCD.printf("Piano Tiles!!!\n"); |
clu67 | 0:a1c374b9a4fe | 160 | uLCD.text_height(1); |
clu67 | 0:a1c374b9a4fe | 161 | uLCD.locate(5,8); |
clu67 | 0:a1c374b9a4fe | 162 | uLCD.printf("Playing:"); |
clu67 | 0:a1c374b9a4fe | 163 | uLCD.locate(5,9); |
clu67 | 0:a1c374b9a4fe | 164 | uLCD.printf("Beethoven"); |
clu67 | 0:a1c374b9a4fe | 165 | uLCD.locate(5,10); |
clu67 | 0:a1c374b9a4fe | 166 | uLCD.printf("Fur Elise"); |
clu67 | 0:a1c374b9a4fe | 167 | uLCD.locate(0,12); |
clu67 | 0:a1c374b9a4fe | 168 | uLCD.printf("Get a score above "); |
clu67 | 0:a1c374b9a4fe | 169 | uLCD.locate(4,13); |
clu67 | 0:a1c374b9a4fe | 170 | uLCD.printf("100 to win!"); |
clu67 | 0:a1c374b9a4fe | 171 | wait(4); |
clu67 | 0:a1c374b9a4fe | 172 | |
clu67 | 0:a1c374b9a4fe | 173 | interrupt.fall(&fallInterrupt); |
clu67 | 0:a1c374b9a4fe | 174 | interrupt.mode(PullUp); |
clu67 | 0:a1c374b9a4fe | 175 | startGame(); |
clu67 | 0:a1c374b9a4fe | 176 | |
clu67 | 0:a1c374b9a4fe | 177 | //initialization to track tiles |
clu67 | 0:a1c374b9a4fe | 178 | int a[16]; |
clu67 | 0:a1c374b9a4fe | 179 | int y1 = 81; //starting y coordinate to fill in rectangle from the bottom |
clu67 | 0:a1c374b9a4fe | 180 | int y2 = 101; |
clu67 | 0:a1c374b9a4fe | 181 | int position = 1; //tracks placement of the previous tile for tone so tiles are placed by tones |
clu67 | 0:a1c374b9a4fe | 182 | |
clu67 | 0:a1c374b9a4fe | 183 | |
clu67 | 0:a1c374b9a4fe | 184 | //initializing the first four set of piano tiles |
clu67 | 0:a1c374b9a4fe | 185 | for (int i = 0; i < 4; i++) { |
clu67 | 0:a1c374b9a4fe | 186 | if (note[i] < note[i-1] && i != 0){ //if (note<previousnote) then next tile is to the left |
clu67 | 0:a1c374b9a4fe | 187 | uLCD.filled_rectangle(49,y1,79,y2, BLACK); |
clu67 | 0:a1c374b9a4fe | 188 | a[4*i + 0] = 49; |
clu67 | 0:a1c374b9a4fe | 189 | a[4*i + 1] = y1; |
clu67 | 0:a1c374b9a4fe | 190 | a[4*i + 2] = 79; |
clu67 | 0:a1c374b9a4fe | 191 | a[4*i + 3] = y2; |
clu67 | 0:a1c374b9a4fe | 192 | position = 2; |
clu67 | 0:a1c374b9a4fe | 193 | } |
clu67 | 0:a1c374b9a4fe | 194 | else if (i == 0 || note[i] > note[i-1]) { //initial start is on right side of screen |
clu67 | 0:a1c374b9a4fe | 195 | uLCD.filled_rectangle(85,y1,115,y2, BLACK); |
clu67 | 0:a1c374b9a4fe | 196 | a[4*i + 0] = 85; |
clu67 | 0:a1c374b9a4fe | 197 | a[4*i + 1] = y1; |
clu67 | 0:a1c374b9a4fe | 198 | a[4*i + 2] = 115; |
clu67 | 0:a1c374b9a4fe | 199 | a[4*i + 3] = y2; |
clu67 | 0:a1c374b9a4fe | 200 | position = 3; |
clu67 | 0:a1c374b9a4fe | 201 | } |
clu67 | 0:a1c374b9a4fe | 202 | //move from bottom of the screen to the top |
clu67 | 0:a1c374b9a4fe | 203 | y1 = y1 - 26; |
clu67 | 0:a1c374b9a4fe | 204 | y2 = y2 - 26; |
clu67 | 0:a1c374b9a4fe | 205 | } |
clu67 | 0:a1c374b9a4fe | 206 | //array used for holding all the tiles |
clu67 | 0:a1c374b9a4fe | 207 | int block1_x1 = a[0]; |
clu67 | 0:a1c374b9a4fe | 208 | int block1_y1 = a[1]; |
clu67 | 0:a1c374b9a4fe | 209 | int block1_x2 = a[2]; |
clu67 | 0:a1c374b9a4fe | 210 | int block1_y2 = a[3]; |
clu67 | 0:a1c374b9a4fe | 211 | int block2_x1 = a[4]; |
clu67 | 0:a1c374b9a4fe | 212 | int block2_y1 = a[5]; |
clu67 | 0:a1c374b9a4fe | 213 | int block2_x2 = a[6]; |
clu67 | 0:a1c374b9a4fe | 214 | int block2_y2 = a[7]; |
clu67 | 0:a1c374b9a4fe | 215 | int block3_x1 = a[8]; |
clu67 | 0:a1c374b9a4fe | 216 | int block3_y1 = a[9]; |
clu67 | 0:a1c374b9a4fe | 217 | int block3_x2 = a[10]; |
clu67 | 0:a1c374b9a4fe | 218 | int block3_y2 = a[11]; |
clu67 | 0:a1c374b9a4fe | 219 | int block4_x1 = a[12]; |
clu67 | 0:a1c374b9a4fe | 220 | int block4_y1 = a[13]; |
clu67 | 0:a1c374b9a4fe | 221 | int block4_x2 = a[14]; |
clu67 | 0:a1c374b9a4fe | 222 | int block4_y2 = a[15]; |
clu67 | 0:a1c374b9a4fe | 223 | |
clu67 | 0:a1c374b9a4fe | 224 | t.start(); |
clu67 | 0:a1c374b9a4fe | 225 | |
clu67 | 0:a1c374b9a4fe | 226 | //starting the game |
clu67 | 0:a1c374b9a4fe | 227 | while(1) { |
clu67 | 0:a1c374b9a4fe | 228 | if (interruptBool) { //key is pressed |
clu67 | 0:a1c374b9a4fe | 229 | uLCD.locate(0,0); |
clu67 | 0:a1c374b9a4fe | 230 | bool checkTrue = check(vals); |
clu67 | 0:a1c374b9a4fe | 231 | uLCD.locate(0,0); |
clu67 | 0:a1c374b9a4fe | 232 | |
clu67 | 0:a1c374b9a4fe | 233 | if (checkTrue) { //if right correct key is pressed, tiles are replaced and move down |
clu67 | 0:a1c374b9a4fe | 234 | uLCD.filled_rectangle(block1_x1, block1_y1, block1_x2, block1_y2, WHITE); |
clu67 | 0:a1c374b9a4fe | 235 | uLCD.filled_rectangle(block2_x1, block2_y1, block2_x2, block2_y2, WHITE); |
clu67 | 0:a1c374b9a4fe | 236 | uLCD.filled_rectangle(block3_x1, block3_y1, block3_x2, block3_y2, WHITE); |
clu67 | 0:a1c374b9a4fe | 237 | uLCD.filled_rectangle(block4_x1, block4_y1, block4_x2, block4_y2, WHITE); |
clu67 | 0:a1c374b9a4fe | 238 | block1_y1 = updateBlockY1(block1_y1); |
clu67 | 0:a1c374b9a4fe | 239 | block1_y2 = updateBlockY2(block1_y2); |
clu67 | 0:a1c374b9a4fe | 240 | block2_y1 = updateBlockY1(block2_y1); |
clu67 | 0:a1c374b9a4fe | 241 | block2_y2 = updateBlockY2(block2_y2); |
clu67 | 0:a1c374b9a4fe | 242 | block3_y1 = updateBlockY1(block3_y1); |
clu67 | 0:a1c374b9a4fe | 243 | block3_y2 = updateBlockY2(block3_y2); |
clu67 | 0:a1c374b9a4fe | 244 | block4_y1 = updateBlockY1(block4_y1); |
clu67 | 0:a1c374b9a4fe | 245 | block4_y2 = updateBlockY2(block4_y2); |
clu67 | 0:a1c374b9a4fe | 246 | |
clu67 | 0:a1c374b9a4fe | 247 | //the following if statements determine position of the following tiles based on the |
clu67 | 0:a1c374b9a4fe | 248 | //frequencies of the note that is being played |
clu67 | 0:a1c374b9a4fe | 249 | if(block1_y1 == 3) |
clu67 | 0:a1c374b9a4fe | 250 | { |
clu67 | 0:a1c374b9a4fe | 251 | if (note[score%85+4] < note[score%85+3] && (position == 1 || position == 2)) { |
clu67 | 0:a1c374b9a4fe | 252 | block1_x1 = 13; |
clu67 | 0:a1c374b9a4fe | 253 | block1_x2 = 43; |
clu67 | 0:a1c374b9a4fe | 254 | position = 1; |
clu67 | 0:a1c374b9a4fe | 255 | } else if ((note[score%85+4] < note[score%85+3] && position == 3) || (note[score%85+4] > note[score%85+3] && position == 1 )) { |
clu67 | 0:a1c374b9a4fe | 256 | block1_x1 = 49; |
clu67 | 0:a1c374b9a4fe | 257 | block1_x2 = 79; |
clu67 | 0:a1c374b9a4fe | 258 | position = 2; |
clu67 | 0:a1c374b9a4fe | 259 | } else if (note[score%85+4] > note[score%85+3] && (position == 2 || position == 3)) { |
clu67 | 0:a1c374b9a4fe | 260 | block1_x1 = 85; |
clu67 | 0:a1c374b9a4fe | 261 | block1_x2 = 115; |
clu67 | 0:a1c374b9a4fe | 262 | position = 3; |
clu67 | 0:a1c374b9a4fe | 263 | } |
clu67 | 0:a1c374b9a4fe | 264 | } |
clu67 | 0:a1c374b9a4fe | 265 | if(block2_y1 == 3) |
clu67 | 0:a1c374b9a4fe | 266 | { |
clu67 | 0:a1c374b9a4fe | 267 | if (note[score%85+4] < note[score%85+3] && (position == 1 || position == 2)) { |
clu67 | 0:a1c374b9a4fe | 268 | block2_x1 = 13; |
clu67 | 0:a1c374b9a4fe | 269 | block2_x2 = 43; |
clu67 | 0:a1c374b9a4fe | 270 | position = 1; |
clu67 | 0:a1c374b9a4fe | 271 | } else if ((note[score%85+4] < note[score%85+3] && position == 3) || (note[score%85+4] > note[score%85+3] && position == 1 )) { |
clu67 | 0:a1c374b9a4fe | 272 | block2_x1 = 49; |
clu67 | 0:a1c374b9a4fe | 273 | block2_x2 = 79; |
clu67 | 0:a1c374b9a4fe | 274 | position = 2; |
clu67 | 0:a1c374b9a4fe | 275 | } else if (note[score%85+4] > note[score%85+3] && (position == 2 || position == 3)) { |
clu67 | 0:a1c374b9a4fe | 276 | block2_x1 = 85; |
clu67 | 0:a1c374b9a4fe | 277 | block2_x2 = 115; |
clu67 | 0:a1c374b9a4fe | 278 | position = 3; |
clu67 | 0:a1c374b9a4fe | 279 | } |
clu67 | 0:a1c374b9a4fe | 280 | } |
clu67 | 0:a1c374b9a4fe | 281 | if(block3_y1 == 3) |
clu67 | 0:a1c374b9a4fe | 282 | { |
clu67 | 0:a1c374b9a4fe | 283 | if (note[score%85+4] < note[score%85+3] && (position == 1 || position == 2)) { |
clu67 | 0:a1c374b9a4fe | 284 | block3_x1 = 13; |
clu67 | 0:a1c374b9a4fe | 285 | block3_x2 = 43; |
clu67 | 0:a1c374b9a4fe | 286 | position = 1; |
clu67 | 0:a1c374b9a4fe | 287 | } else if ((note[score%85+4] < note[score%85+3] && position == 3) || (note[score%85+4] > note[score%85+3] && position == 1 )) { |
clu67 | 0:a1c374b9a4fe | 288 | block3_x1 = 49; |
clu67 | 0:a1c374b9a4fe | 289 | block3_x2 = 79; |
clu67 | 0:a1c374b9a4fe | 290 | position = 2; |
clu67 | 0:a1c374b9a4fe | 291 | } else if (note[score%85+4] > note[score%85+3] && (position == 2 || position == 3)) { |
clu67 | 0:a1c374b9a4fe | 292 | block3_x1 = 85; |
clu67 | 0:a1c374b9a4fe | 293 | block3_x2 = 115; |
clu67 | 0:a1c374b9a4fe | 294 | position = 3; |
clu67 | 0:a1c374b9a4fe | 295 | } |
clu67 | 0:a1c374b9a4fe | 296 | } |
clu67 | 0:a1c374b9a4fe | 297 | if(block4_y1 == 3) |
clu67 | 0:a1c374b9a4fe | 298 | { |
clu67 | 0:a1c374b9a4fe | 299 | if (note[score%85+4] < note[score%85+3] && (position == 1 || position == 2)) { |
clu67 | 0:a1c374b9a4fe | 300 | block4_x1 = 13; |
clu67 | 0:a1c374b9a4fe | 301 | block4_x2 = 43; |
clu67 | 0:a1c374b9a4fe | 302 | position = 1; |
clu67 | 0:a1c374b9a4fe | 303 | } else if ((note[score%85+4] < note[score%85+3] && position == 3) || (note[score%85+4] > note[score%85+3] && position == 1 )) { |
clu67 | 0:a1c374b9a4fe | 304 | block4_x1 = 49; |
clu67 | 0:a1c374b9a4fe | 305 | block4_x2 = 79; |
clu67 | 0:a1c374b9a4fe | 306 | position = 2; |
clu67 | 0:a1c374b9a4fe | 307 | } else if (note[score%85+4] > note[score%85+3] && (position == 2 || position == 3)) { |
clu67 | 0:a1c374b9a4fe | 308 | block4_x1 = 85; |
clu67 | 0:a1c374b9a4fe | 309 | block4_x2 = 115; |
clu67 | 0:a1c374b9a4fe | 310 | position = 3; |
clu67 | 0:a1c374b9a4fe | 311 | } |
clu67 | 0:a1c374b9a4fe | 312 | } |
clu67 | 0:a1c374b9a4fe | 313 | |
clu67 | 0:a1c374b9a4fe | 314 | //fill in the tiles |
clu67 | 0:a1c374b9a4fe | 315 | uLCD.filled_rectangle(block1_x1, block1_y1, block1_x2, block1_y2, BLACK); |
clu67 | 0:a1c374b9a4fe | 316 | uLCD.filled_rectangle(block2_x1, block2_y1, block2_x2, block2_y2, BLACK); |
clu67 | 0:a1c374b9a4fe | 317 | uLCD.filled_rectangle(block3_x1, block3_y1, block3_x2, block3_y2, BLACK); |
clu67 | 0:a1c374b9a4fe | 318 | uLCD.filled_rectangle(block4_x1, block4_y1, block4_x2, block4_y2, BLACK); |
clu67 | 0:a1c374b9a4fe | 319 | interruptBool = false; |
clu67 | 0:a1c374b9a4fe | 320 | |
clu67 | 0:a1c374b9a4fe | 321 | //plays the note as the keypad is being pressed |
clu67 | 0:a1c374b9a4fe | 322 | float note1[] = {note[score%85]}; |
clu67 | 0:a1c374b9a4fe | 323 | float duration1[] = {duration[score%85]}; |
clu67 | 0:a1c374b9a4fe | 324 | mySpeaker.PlaySong(note1,duration1); |
clu67 | 0:a1c374b9a4fe | 325 | score++; |
clu67 | 0:a1c374b9a4fe | 326 | |
clu67 | 0:a1c374b9a4fe | 327 | //updates the time and score |
clu67 | 0:a1c374b9a4fe | 328 | uLCD.text_width(1); |
clu67 | 0:a1c374b9a4fe | 329 | uLCD.text_height(1); |
clu67 | 0:a1c374b9a4fe | 330 | uLCD.locate(0,13); |
clu67 | 0:a1c374b9a4fe | 331 | uLCD.textbackground_color(WHITE); |
clu67 | 0:a1c374b9a4fe | 332 | uLCD.color(RED); |
clu67 | 0:a1c374b9a4fe | 333 | uLCD.printf("Score:%d", score); |
clu67 | 0:a1c374b9a4fe | 334 | uLCD.locate(9,13); |
clu67 | 0:a1c374b9a4fe | 335 | uLCD.printf("Time:%0.1f", t.read()); |
clu67 | 0:a1c374b9a4fe | 336 | |
clu67 | 0:a1c374b9a4fe | 337 | |
clu67 | 0:a1c374b9a4fe | 338 | } |
clu67 | 0:a1c374b9a4fe | 339 | else { //while loop breaks when the wrong key is pressed |
clu67 | 0:a1c374b9a4fe | 340 | break; |
clu67 | 0:a1c374b9a4fe | 341 | //Game Over Screen outside while loop |
clu67 | 0:a1c374b9a4fe | 342 | } |
clu67 | 0:a1c374b9a4fe | 343 | } //end of interruptBool |
clu67 | 0:a1c374b9a4fe | 344 | }//end of while loop |
clu67 | 0:a1c374b9a4fe | 345 | |
clu67 | 0:a1c374b9a4fe | 346 | |
clu67 | 0:a1c374b9a4fe | 347 | //win or lose music |
clu67 | 0:a1c374b9a4fe | 348 | wave_player waver(&Speaker); |
clu67 | 0:a1c374b9a4fe | 349 | //vector<string> filenames; //filenames are stored in a vector string |
clu67 | 0:a1c374b9a4fe | 350 | bool PLAY = true; |
clu67 | 0:a1c374b9a4fe | 351 | bool *PlayStopPtr = &PLAY; |
clu67 | 0:a1c374b9a4fe | 352 | FILE *wave_file; |
clu67 | 0:a1c374b9a4fe | 353 | |
clu67 | 0:a1c374b9a4fe | 354 | //victory and game over screen |
clu67 | 0:a1c374b9a4fe | 355 | if (score >= 100) |
clu67 | 0:a1c374b9a4fe | 356 | { |
clu67 | 0:a1c374b9a4fe | 357 | uLCD.cls(); |
clu67 | 0:a1c374b9a4fe | 358 | uLCD.locate(0,4); |
clu67 | 0:a1c374b9a4fe | 359 | uLCD.text_width(2); //4X size text |
clu67 | 0:a1c374b9a4fe | 360 | uLCD.text_height(2); |
clu67 | 0:a1c374b9a4fe | 361 | uLCD.printf("Victory!!"); |
clu67 | 0:a1c374b9a4fe | 362 | uLCD.text_width(1); //4X size text |
clu67 | 0:a1c374b9a4fe | 363 | uLCD.text_height(1); |
clu67 | 0:a1c374b9a4fe | 364 | uLCD.locate(4,7); |
clu67 | 0:a1c374b9a4fe | 365 | uLCD.printf("SCORE:%d", score); |
clu67 | 0:a1c374b9a4fe | 366 | t.stop(); |
clu67 | 0:a1c374b9a4fe | 367 | uLCD.locate(4,8); |
clu67 | 0:a1c374b9a4fe | 368 | uLCD.printf("Time:%0.2f", t.read()); |
clu67 | 0:a1c374b9a4fe | 369 | uLCD.locate(0,10); |
clu67 | 0:a1c374b9a4fe | 370 | uLCD.printf("Thanks for playing"); |
clu67 | 0:a1c374b9a4fe | 371 | |
clu67 | 0:a1c374b9a4fe | 372 | FILE *wave_file; |
clu67 | 0:a1c374b9a4fe | 373 | wave_file=fopen("/sd/wavfiles/victory.wav","r"); |
clu67 | 0:a1c374b9a4fe | 374 | waver.play(wave_file,PlayStopPtr); |
clu67 | 0:a1c374b9a4fe | 375 | } |
clu67 | 0:a1c374b9a4fe | 376 | else |
clu67 | 0:a1c374b9a4fe | 377 | { |
clu67 | 0:a1c374b9a4fe | 378 | uLCD.cls(); |
clu67 | 0:a1c374b9a4fe | 379 | uLCD.locate(0,4); |
clu67 | 0:a1c374b9a4fe | 380 | uLCD.text_width(2); //4X size text |
clu67 | 0:a1c374b9a4fe | 381 | uLCD.text_height(2); |
clu67 | 0:a1c374b9a4fe | 382 | uLCD.printf("Game Over"); |
clu67 | 0:a1c374b9a4fe | 383 | uLCD.text_width(1); //4X size text |
clu67 | 0:a1c374b9a4fe | 384 | uLCD.text_height(1); |
clu67 | 0:a1c374b9a4fe | 385 | uLCD.locate(4,7); |
clu67 | 0:a1c374b9a4fe | 386 | uLCD.printf("SCORE:%d", score); |
clu67 | 0:a1c374b9a4fe | 387 | t.stop(); |
clu67 | 0:a1c374b9a4fe | 388 | uLCD.locate(4,8); |
clu67 | 0:a1c374b9a4fe | 389 | uLCD.printf("Time:%0.2f", t.read()); |
clu67 | 0:a1c374b9a4fe | 390 | uLCD.locate(0,10); |
clu67 | 0:a1c374b9a4fe | 391 | uLCD.printf("Thanks for playing"); |
clu67 | 0:a1c374b9a4fe | 392 | |
clu67 | 0:a1c374b9a4fe | 393 | FILE *wave_file; |
clu67 | 0:a1c374b9a4fe | 394 | wave_file=fopen("/sd/wavfiles/defeat.wav","r"); |
clu67 | 0:a1c374b9a4fe | 395 | waver.play(wave_file,PlayStopPtr); |
clu67 | 0:a1c374b9a4fe | 396 | } |
clu67 | 0:a1c374b9a4fe | 397 | } |