Tetris Base Game

Dependencies:   4DGL-uLCD-SE SDFileSystem mbed-rtos mbed wave_player

Committer:
jsanchez307
Date:
Wed Nov 30 21:57:25 2022 +0000
Revision:
0:1858f2b100fd
Tetris Skeleton V 1.0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jsanchez307 0:1858f2b100fd 1 #include "mbed.h"
jsanchez307 0:1858f2b100fd 2 #include "Game.h"
jsanchez307 0:1858f2b100fd 3 //#include "PinDetect.h"
jsanchez307 0:1858f2b100fd 4 //#include "Speaker.h"
jsanchez307 0:1858f2b100fd 5 //#include "TextLCD.h"
jsanchez307 0:1858f2b100fd 6 //#include "Shiftbrite.h"
jsanchez307 0:1858f2b100fd 7 //#include "SDFileSystem.h"
jsanchez307 0:1858f2b100fd 8 #include "wave_player.h"
jsanchez307 0:1858f2b100fd 9 #include "rtos.h"
jsanchez307 0:1858f2b100fd 10
jsanchez307 0:1858f2b100fd 11 uLCD_4DGL uLCD(p28, p27, p29);
jsanchez307 0:1858f2b100fd 12 //TextLCD textLCD(p26, p25, p24, p23, p22, p15);
jsanchez307 0:1858f2b100fd 13 //Speaker mySpeaker(p21);
jsanchez307 0:1858f2b100fd 14 //Shiftbrite myShiftbrite(p9, p10, p11, p12, p13);
jsanchez307 0:1858f2b100fd 15 //SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card
jsanchez307 0:1858f2b100fd 16 Serial pc(USBTX, USBRX);
jsanchez307 0:1858f2b100fd 17
jsanchez307 0:1858f2b100fd 18 PinDetect LEFT_KEY(p16); // need to use NavSwitch for this
jsanchez307 0:1858f2b100fd 19 PinDetect RIGHT_KEY(p19);
jsanchez307 0:1858f2b100fd 20 PinDetect ROTATE_KEY(p17);
jsanchez307 0:1858f2b100fd 21 PinDetect DOWN_KEY(p30);
jsanchez307 0:1858f2b100fd 22
jsanchez307 0:1858f2b100fd 23 int SinglePiecePoints = 50;
jsanchez307 0:1858f2b100fd 24 int LineClearPoints = 200;
jsanchez307 0:1858f2b100fd 25 int key_input = 0;
jsanchez307 0:1858f2b100fd 26 bool gameStarted = false;
jsanchez307 0:1858f2b100fd 27 void input_left() // FIX
jsanchez307 0:1858f2b100fd 28 {
jsanchez307 0:1858f2b100fd 29 if(!gameStarted)
jsanchez307 0:1858f2b100fd 30 { gameStarted=true;
jsanchez307 0:1858f2b100fd 31 return;
jsanchez307 0:1858f2b100fd 32 }
jsanchez307 0:1858f2b100fd 33 key_input = 1;
jsanchez307 0:1858f2b100fd 34 //mySpeaker.PlayNote(400.0,0.05,0.025);
jsanchez307 0:1858f2b100fd 35 //mySpeaker.PlayNote(500.0,0.05,0.025);
jsanchez307 0:1858f2b100fd 36 }
jsanchez307 0:1858f2b100fd 37
jsanchez307 0:1858f2b100fd 38 void input_right()
jsanchez307 0:1858f2b100fd 39 {
jsanchez307 0:1858f2b100fd 40 if(!gameStarted)
jsanchez307 0:1858f2b100fd 41 { gameStarted=true;
jsanchez307 0:1858f2b100fd 42 return;
jsanchez307 0:1858f2b100fd 43 }
jsanchez307 0:1858f2b100fd 44 key_input = 2;
jsanchez307 0:1858f2b100fd 45 //mySpeaker.PlayNote(400.0,0.05,0.025);
jsanchez307 0:1858f2b100fd 46 //mySpeaker.PlayNote(500.0,0.05,0.025);
jsanchez307 0:1858f2b100fd 47 }
jsanchez307 0:1858f2b100fd 48
jsanchez307 0:1858f2b100fd 49 void input_rotate()
jsanchez307 0:1858f2b100fd 50 {
jsanchez307 0:1858f2b100fd 51 if(!gameStarted)
jsanchez307 0:1858f2b100fd 52 { gameStarted=true;
jsanchez307 0:1858f2b100fd 53 return;
jsanchez307 0:1858f2b100fd 54 }
jsanchez307 0:1858f2b100fd 55 key_input = 3;
jsanchez307 0:1858f2b100fd 56 /*mySpeaker.PlayNote(600.0,0.05,0.025);
jsanchez307 0:1858f2b100fd 57 mySpeaker.PlayNote(700.0,0.05,0.025);
jsanchez307 0:1858f2b100fd 58 mySpeaker.PlayNote(800.0,0.05,0.025);
jsanchez307 0:1858f2b100fd 59 mySpeaker.PlayNote(900.0,0.05,0.025);*/
jsanchez307 0:1858f2b100fd 60 }
jsanchez307 0:1858f2b100fd 61
jsanchez307 0:1858f2b100fd 62 void input_down()
jsanchez307 0:1858f2b100fd 63 {
jsanchez307 0:1858f2b100fd 64 if(!gameStarted)
jsanchez307 0:1858f2b100fd 65 { gameStarted=true;
jsanchez307 0:1858f2b100fd 66 return;
jsanchez307 0:1858f2b100fd 67 }
jsanchez307 0:1858f2b100fd 68 key_input=4;
jsanchez307 0:1858f2b100fd 69 /*mySpeaker.PlayNote(500.0,0.05,0.025);
jsanchez307 0:1858f2b100fd 70 mySpeaker.PlayNote(1500.0,0.05,0.025);
jsanchez307 0:1858f2b100fd 71 mySpeaker.PlayNote(500.0,0.05,0.025);
jsanchez307 0:1858f2b100fd 72 mySpeaker.PlayNote(1500.0,0.05,0.025);*/
jsanchez307 0:1858f2b100fd 73 }
jsanchez307 0:1858f2b100fd 74
jsanchez307 0:1858f2b100fd 75 void clear_board()
jsanchez307 0:1858f2b100fd 76 {
jsanchez307 0:1858f2b100fd 77 uLCD.filled_rectangle(20,0,79,128,0);
jsanchez307 0:1858f2b100fd 78 }
jsanchez307 0:1858f2b100fd 79
jsanchez307 0:1858f2b100fd 80 void clear_next_piece()
jsanchez307 0:1858f2b100fd 81 {
jsanchez307 0:1858f2b100fd 82 uLCD.filled_rectangle(92,20,122,50,0);
jsanchez307 0:1858f2b100fd 83 }
jsanchez307 0:1858f2b100fd 84
jsanchez307 0:1858f2b100fd 85 /*
jsanchez307 0:1858f2b100fd 86 void UpdateGameStatus(int points,int lines)
jsanchez307 0:1858f2b100fd 87 {
jsanchez307 0:1858f2b100fd 88 textLCD.locate(7,0);
jsanchez307 0:1858f2b100fd 89 textLCD.printf("%d",points);
jsanchez307 0:1858f2b100fd 90 textLCD.locate(13,1);
jsanchez307 0:1858f2b100fd 91 textLCD.printf("%d",lines);
jsanchez307 0:1858f2b100fd 92 }*/
jsanchez307 0:1858f2b100fd 93
jsanchez307 0:1858f2b100fd 94 /* ShiftBright Code
jsanchez307 0:1858f2b100fd 95 void FlashLight(void const *args)
jsanchez307 0:1858f2b100fd 96 {
jsanchez307 0:1858f2b100fd 97 myShiftbrite.RGB(0,0,0);
jsanchez307 0:1858f2b100fd 98 myShiftbrite.RGB(0,0,0);
jsanchez307 0:1858f2b100fd 99
jsanchez307 0:1858f2b100fd 100 myShiftbrite.RGB(0,0,42);
jsanchez307 0:1858f2b100fd 101 myShiftbrite.RGB(0,0,42);
jsanchez307 0:1858f2b100fd 102 Thread::wait(100);
jsanchez307 0:1858f2b100fd 103
jsanchez307 0:1858f2b100fd 104
jsanchez307 0:1858f2b100fd 105 myShiftbrite.RGB(0,0,0);
jsanchez307 0:1858f2b100fd 106 myShiftbrite.RGB(0,42,0);
jsanchez307 0:1858f2b100fd 107 Thread::wait(100);
jsanchez307 0:1858f2b100fd 108
jsanchez307 0:1858f2b100fd 109 myShiftbrite.RGB(0,0,42);
jsanchez307 0:1858f2b100fd 110 myShiftbrite.RGB(0,0,42);
jsanchez307 0:1858f2b100fd 111 Thread::wait(100);
jsanchez307 0:1858f2b100fd 112
jsanchez307 0:1858f2b100fd 113 myShiftbrite.RGB(0,0,0);
jsanchez307 0:1858f2b100fd 114 myShiftbrite.RGB(0,0,0);
jsanchez307 0:1858f2b100fd 115 }
jsanchez307 0:1858f2b100fd 116
jsanchez307 0:1858f2b100fd 117 void GameOverLight(void const *args)
jsanchez307 0:1858f2b100fd 118 {
jsanchez307 0:1858f2b100fd 119
jsanchez307 0:1858f2b100fd 120 myShiftbrite.RGB(0,0,0);
jsanchez307 0:1858f2b100fd 121 myShiftbrite.RGB(0,0,0);
jsanchez307 0:1858f2b100fd 122
jsanchez307 0:1858f2b100fd 123 myShiftbrite.RGB(0,0,200);
jsanchez307 0:1858f2b100fd 124 Thread::wait(100);
jsanchez307 0:1858f2b100fd 125 myShiftbrite.RGB(0,0,0);
jsanchez307 0:1858f2b100fd 126 Thread::wait(100);
jsanchez307 0:1858f2b100fd 127 myShiftbrite.RGB(0,0,200);
jsanchez307 0:1858f2b100fd 128 Thread::wait(100);
jsanchez307 0:1858f2b100fd 129 myShiftbrite.RGB(0,0,0);
jsanchez307 0:1858f2b100fd 130 Thread::wait(100);
jsanchez307 0:1858f2b100fd 131 myShiftbrite.RGB(0,0,200);
jsanchez307 0:1858f2b100fd 132 Thread::wait(100);
jsanchez307 0:1858f2b100fd 133 myShiftbrite.RGB(0,0,0);
jsanchez307 0:1858f2b100fd 134 Thread::wait(100);
jsanchez307 0:1858f2b100fd 135 myShiftbrite.RGB(0,0,200);
jsanchez307 0:1858f2b100fd 136 Thread::wait(100);
jsanchez307 0:1858f2b100fd 137 myShiftbrite.RGB(0,0,0);
jsanchez307 0:1858f2b100fd 138 Thread::wait(100);
jsanchez307 0:1858f2b100fd 139 myShiftbrite.RGB(0,0,200);
jsanchez307 0:1858f2b100fd 140 Thread::wait(100);
jsanchez307 0:1858f2b100fd 141 myShiftbrite.RGB(0,0,0);
jsanchez307 0:1858f2b100fd 142 Thread::wait(100);
jsanchez307 0:1858f2b100fd 143 myShiftbrite.RGB(0,0,200);
jsanchez307 0:1858f2b100fd 144 Thread::wait(100);
jsanchez307 0:1858f2b100fd 145 myShiftbrite.RGB(0,0,0);
jsanchez307 0:1858f2b100fd 146 Thread::wait(100);
jsanchez307 0:1858f2b100fd 147 myShiftbrite.RGB(0,0,200);
jsanchez307 0:1858f2b100fd 148 Thread::wait(100);
jsanchez307 0:1858f2b100fd 149 myShiftbrite.RGB(0,0,0);
jsanchez307 0:1858f2b100fd 150 Thread::wait(100);
jsanchez307 0:1858f2b100fd 151 myShiftbrite.RGB(0,0,200);
jsanchez307 0:1858f2b100fd 152 Thread::wait(100);
jsanchez307 0:1858f2b100fd 153 myShiftbrite.RGB(0,0,0);
jsanchez307 0:1858f2b100fd 154 Thread::wait(100);
jsanchez307 0:1858f2b100fd 155 myShiftbrite.RGB(0,0,200);
jsanchez307 0:1858f2b100fd 156 Thread::wait(100);
jsanchez307 0:1858f2b100fd 157 myShiftbrite.RGB(0,0,0);
jsanchez307 0:1858f2b100fd 158 Thread::wait(100);
jsanchez307 0:1858f2b100fd 159 myShiftbrite.RGB(0,0,200);
jsanchez307 0:1858f2b100fd 160 Thread::wait(100);
jsanchez307 0:1858f2b100fd 161 myShiftbrite.RGB(0,0,0);
jsanchez307 0:1858f2b100fd 162 }
jsanchez307 0:1858f2b100fd 163
jsanchez307 0:1858f2b100fd 164 void GameStartLight(void const *args)
jsanchez307 0:1858f2b100fd 165 {
jsanchez307 0:1858f2b100fd 166
jsanchez307 0:1858f2b100fd 167 myShiftbrite.RGB(0,0,0);
jsanchez307 0:1858f2b100fd 168 myShiftbrite.RGB(0,0,0);
jsanchez307 0:1858f2b100fd 169
jsanchez307 0:1858f2b100fd 170 myShiftbrite.RGB(0,200,0);
jsanchez307 0:1858f2b100fd 171 Thread::wait(100);
jsanchez307 0:1858f2b100fd 172 myShiftbrite.RGB(0,0,0);
jsanchez307 0:1858f2b100fd 173 myShiftbrite.RGB(0,0,0);
jsanchez307 0:1858f2b100fd 174 Thread::wait(100);
jsanchez307 0:1858f2b100fd 175 myShiftbrite.RGB(0,200,0);
jsanchez307 0:1858f2b100fd 176 Thread::wait(100);
jsanchez307 0:1858f2b100fd 177 myShiftbrite.RGB(0,0,0);
jsanchez307 0:1858f2b100fd 178 myShiftbrite.RGB(0,0,0);
jsanchez307 0:1858f2b100fd 179 Thread::wait(100);
jsanchez307 0:1858f2b100fd 180 myShiftbrite.RGB(0,200,0);
jsanchez307 0:1858f2b100fd 181 Thread::wait(100);
jsanchez307 0:1858f2b100fd 182 myShiftbrite.RGB(0,0,0);
jsanchez307 0:1858f2b100fd 183 myShiftbrite.RGB(0,0,0);
jsanchez307 0:1858f2b100fd 184 Thread::wait(100);
jsanchez307 0:1858f2b100fd 185 myShiftbrite.RGB(0,200,0);
jsanchez307 0:1858f2b100fd 186 Thread::wait(100);
jsanchez307 0:1858f2b100fd 187 myShiftbrite.RGB(0,0,0);
jsanchez307 0:1858f2b100fd 188 myShiftbrite.RGB(0,0,0);
jsanchez307 0:1858f2b100fd 189 Thread::wait(100);
jsanchez307 0:1858f2b100fd 190 myShiftbrite.RGB(0,200,0);
jsanchez307 0:1858f2b100fd 191 Thread::wait(100);
jsanchez307 0:1858f2b100fd 192 myShiftbrite.RGB(0,0,0);
jsanchez307 0:1858f2b100fd 193 myShiftbrite.RGB(0,0,0);
jsanchez307 0:1858f2b100fd 194
jsanchez307 0:1858f2b100fd 195 } */
jsanchez307 0:1858f2b100fd 196
jsanchez307 0:1858f2b100fd 197 /* Speaker Code
jsanchez307 0:1858f2b100fd 198 void PlayStartSound()
jsanchez307 0:1858f2b100fd 199 {
jsanchez307 0:1858f2b100fd 200 mySpeaker.PlayNote(500.0,0.05,0.025);
jsanchez307 0:1858f2b100fd 201 mySpeaker.PlayNote(600.0,0.05,0.025);
jsanchez307 0:1858f2b100fd 202 mySpeaker.PlayNote(700.0,0.05,0.025);
jsanchez307 0:1858f2b100fd 203 mySpeaker.PlayNote(800.0,0.05,0.025);
jsanchez307 0:1858f2b100fd 204 mySpeaker.PlayNote(900.0,0.05,0.025);
jsanchez307 0:1858f2b100fd 205 mySpeaker.PlayNote(1000.0,0.05,0.025);
jsanchez307 0:1858f2b100fd 206 mySpeaker.PlayNote(1100.0,0.05,0.025);
jsanchez307 0:1858f2b100fd 207 mySpeaker.PlayNote(1200.0,0.05,0.025);
jsanchez307 0:1858f2b100fd 208 mySpeaker.PlayNote(600.0,0.05,0.025);
jsanchez307 0:1858f2b100fd 209 mySpeaker.PlayNote(700.0,0.05,0.025);
jsanchez307 0:1858f2b100fd 210 mySpeaker.PlayNote(800.0,0.05,0.025);
jsanchez307 0:1858f2b100fd 211 mySpeaker.PlayNote(900.0,0.05,0.025);
jsanchez307 0:1858f2b100fd 212 mySpeaker.PlayNote(1000.0,0.05,0.025);
jsanchez307 0:1858f2b100fd 213 mySpeaker.PlayNote(1100.0,0.05,0.025);
jsanchez307 0:1858f2b100fd 214 mySpeaker.PlayNote(1200.0,0.05,0.025);
jsanchez307 0:1858f2b100fd 215 mySpeaker.PlayNote(1300.0,0.05,0.025);
jsanchez307 0:1858f2b100fd 216 mySpeaker.PlayNote(700.0,0.05,0.025);
jsanchez307 0:1858f2b100fd 217 mySpeaker.PlayNote(800.0,0.05,0.025);
jsanchez307 0:1858f2b100fd 218 mySpeaker.PlayNote(900.0,0.05,0.025);
jsanchez307 0:1858f2b100fd 219 mySpeaker.PlayNote(1000.0,0.05,0.025);
jsanchez307 0:1858f2b100fd 220 mySpeaker.PlayNote(1100.0,0.05,0.025);
jsanchez307 0:1858f2b100fd 221 mySpeaker.PlayNote(1200.0,0.05,0.025);
jsanchez307 0:1858f2b100fd 222 mySpeaker.PlayNote(1300.0,0.05,0.025);
jsanchez307 0:1858f2b100fd 223 mySpeaker.PlayNote(1400.0,0.05,0.025);
jsanchez307 0:1858f2b100fd 224 }
jsanchez307 0:1858f2b100fd 225
jsanchez307 0:1858f2b100fd 226 void PlayOverSound()
jsanchez307 0:1858f2b100fd 227 {
jsanchez307 0:1858f2b100fd 228 mySpeaker.PlayNote(1400.0,0.05,0.025);
jsanchez307 0:1858f2b100fd 229 mySpeaker.PlayNote(1300.0,0.05,0.025);
jsanchez307 0:1858f2b100fd 230 mySpeaker.PlayNote(1200.0,0.05,0.025);
jsanchez307 0:1858f2b100fd 231 mySpeaker.PlayNote(1100.0,0.05,0.025);
jsanchez307 0:1858f2b100fd 232 mySpeaker.PlayNote(1000.0,0.05,0.025);
jsanchez307 0:1858f2b100fd 233 mySpeaker.PlayNote(900.0,0.05,0.025);
jsanchez307 0:1858f2b100fd 234 mySpeaker.PlayNote(800.0,0.05,0.025);
jsanchez307 0:1858f2b100fd 235 mySpeaker.PlayNote(700.0,0.05,0.025);
jsanchez307 0:1858f2b100fd 236 mySpeaker.PlayNote(1300.0,0.05,0.025);
jsanchez307 0:1858f2b100fd 237 mySpeaker.PlayNote(1200.0,0.05,0.025);
jsanchez307 0:1858f2b100fd 238 mySpeaker.PlayNote(1100.0,0.05,0.025);
jsanchez307 0:1858f2b100fd 239 mySpeaker.PlayNote(1000.0,0.05,0.025);
jsanchez307 0:1858f2b100fd 240 mySpeaker.PlayNote(900.0,0.05,0.025);
jsanchez307 0:1858f2b100fd 241 mySpeaker.PlayNote(800.0,0.05,0.025);
jsanchez307 0:1858f2b100fd 242 mySpeaker.PlayNote(700.0,0.05,0.025);
jsanchez307 0:1858f2b100fd 243 mySpeaker.PlayNote(600.0,0.05,0.025);
jsanchez307 0:1858f2b100fd 244 mySpeaker.PlayNote(1200.0,0.05,0.025);
jsanchez307 0:1858f2b100fd 245 mySpeaker.PlayNote(1100.0,0.05,0.025);
jsanchez307 0:1858f2b100fd 246 mySpeaker.PlayNote(1000.0,0.05,0.025);
jsanchez307 0:1858f2b100fd 247 mySpeaker.PlayNote(900.0,0.05,0.025);
jsanchez307 0:1858f2b100fd 248 mySpeaker.PlayNote(800.0,0.05,0.025);
jsanchez307 0:1858f2b100fd 249 mySpeaker.PlayNote(700.0,0.05,0.025);
jsanchez307 0:1858f2b100fd 250 mySpeaker.PlayNote(600.0,0.05,0.025);
jsanchez307 0:1858f2b100fd 251 mySpeaker.PlayNote(500.0,0.05,0.025);
jsanchez307 0:1858f2b100fd 252 }
jsanchez307 0:1858f2b100fd 253
jsanchez307 0:1858f2b100fd 254 void PlayClearSound()
jsanchez307 0:1858f2b100fd 255 {
jsanchez307 0:1858f2b100fd 256 mySpeaker.PlayNote(900.0,0.2,0.025);
jsanchez307 0:1858f2b100fd 257 mySpeaker.PlayNote(1000.0,0.2,0.025);
jsanchez307 0:1858f2b100fd 258 mySpeaker.PlayNote(1100.0,0.2,0.025);
jsanchez307 0:1858f2b100fd 259 mySpeaker.PlayNote(1000.0,0.2,0.025);
jsanchez307 0:1858f2b100fd 260 mySpeaker.PlayNote(9000.0,0.2,0.025);
jsanchez307 0:1858f2b100fd 261 } */
jsanchez307 0:1858f2b100fd 262
jsanchez307 0:1858f2b100fd 263 int RandomGen(char range)
jsanchez307 0:1858f2b100fd 264 {
jsanchez307 0:1858f2b100fd 265 pc.printf("%c",range);
jsanchez307 0:1858f2b100fd 266 while(!pc.readable()) wait(0.5);
jsanchez307 0:1858f2b100fd 267 char buffer[4];
jsanchez307 0:1858f2b100fd 268 pc.gets(buffer,4);
jsanchez307 0:1858f2b100fd 269 int i = buffer[0]-'0';
jsanchez307 0:1858f2b100fd 270 return i;
jsanchez307 0:1858f2b100fd 271 }
jsanchez307 0:1858f2b100fd 272
jsanchez307 0:1858f2b100fd 273 int main()
jsanchez307 0:1858f2b100fd 274 {
jsanchez307 0:1858f2b100fd 275 //hardware setup // FIX
jsanchez307 0:1858f2b100fd 276 LEFT_KEY.mode(PullUp);
jsanchez307 0:1858f2b100fd 277 RIGHT_KEY.mode(PullUp);
jsanchez307 0:1858f2b100fd 278 ROTATE_KEY.mode(PullUp);
jsanchez307 0:1858f2b100fd 279 DOWN_KEY.mode(PullUp);
jsanchez307 0:1858f2b100fd 280
jsanchez307 0:1858f2b100fd 281 LEFT_KEY.attach_deasserted(&input_left);
jsanchez307 0:1858f2b100fd 282 RIGHT_KEY.attach_deasserted(&input_right);
jsanchez307 0:1858f2b100fd 283 ROTATE_KEY.attach_deasserted(&input_rotate);
jsanchez307 0:1858f2b100fd 284 DOWN_KEY.attach_deasserted(&input_down);
jsanchez307 0:1858f2b100fd 285
jsanchez307 0:1858f2b100fd 286 LEFT_KEY.setSampleFrequency();
jsanchez307 0:1858f2b100fd 287 RIGHT_KEY.setSampleFrequency();
jsanchez307 0:1858f2b100fd 288 ROTATE_KEY.setSampleFrequency();
jsanchez307 0:1858f2b100fd 289 DOWN_KEY.setSampleFrequency(); // FIX
jsanchez307 0:1858f2b100fd 290 uLCD.baudrate(3000000);
jsanchez307 0:1858f2b100fd 291
jsanchez307 0:1858f2b100fd 292 //myShiftbrite.RGB(0,0,0);
jsanchez307 0:1858f2b100fd 293 //myShiftbrite.RGB(0,0,0);
jsanchez307 0:1858f2b100fd 294
jsanchez307 0:1858f2b100fd 295 /* SD card used for Startup Image
jsanchez307 0:1858f2b100fd 296 sd.disk_initialize(); // ? might need to delete
jsanchez307 0:1858f2b100fd 297 uLCD.media_init();
jsanchez307 0:1858f2b100fd 298 uLCD.set_sector_address(0x001D, 0x7801);
jsanchez307 0:1858f2b100fd 299 uLCD.display_image(0,0);*/
jsanchez307 0:1858f2b100fd 300
jsanchez307 0:1858f2b100fd 301 /*textLCD.cls();
jsanchez307 0:1858f2b100fd 302 textLCD.locate(0,0);
jsanchez307 0:1858f2b100fd 303 textLCD.printf("Waiting for PC...");*/
jsanchez307 0:1858f2b100fd 304
jsanchez307 0:1858f2b100fd 305 pc.baud(9600);
jsanchez307 0:1858f2b100fd 306 pc.format(8,SerialBase::None,1);
jsanchez307 0:1858f2b100fd 307 pc.printf("0");
jsanchez307 0:1858f2b100fd 308 while(!pc.readable()) wait(0.5);
jsanchez307 0:1858f2b100fd 309 char buffer[4];
jsanchez307 0:1858f2b100fd 310 pc.gets(buffer,4);
jsanchez307 0:1858f2b100fd 311 /*
jsanchez307 0:1858f2b100fd 312 //wait for game start
jsanchez307 0:1858f2b100fd 313 textLCD.cls();
jsanchez307 0:1858f2b100fd 314 textLCD.locate(0,0);
jsanchez307 0:1858f2b100fd 315 textLCD.printf("TETRIS READY!!");
jsanchez307 0:1858f2b100fd 316 textLCD.locate(0,1);
jsanchez307 0:1858f2b100fd 317 textLCD.printf("PRESS ANY KEY...");
jsanchez307 0:1858f2b100fd 318 while(!gameStarted) wait(0.5);
jsanchez307 0:1858f2b100fd 319 PlayStartSound();
jsanchez307 0:1858f2b100fd 320 textLCD.cls();
jsanchez307 0:1858f2b100fd 321 textLCD.printf("Starting game now...");
jsanchez307 0:1858f2b100fd 322 wait(2);
jsanchez307 0:1858f2b100fd 323 textLCD.cls();
jsanchez307 0:1858f2b100fd 324 textLCD.locate(0,0);
jsanchez307 0:1858f2b100fd 325 textLCD.printf("Points");
jsanchez307 0:1858f2b100fd 326 textLCD.locate(0,1);
jsanchez307 0:1858f2b100fd 327 textLCD.printf("Cleared Line");*/
jsanchez307 0:1858f2b100fd 328
jsanchez307 0:1858f2b100fd 329 //Thread t1(GameStartLight);
jsanchez307 0:1858f2b100fd 330 //game classes init
jsanchez307 0:1858f2b100fd 331 bool isGameOver = false;
jsanchez307 0:1858f2b100fd 332 int mScreenHeight = 128;
jsanchez307 0:1858f2b100fd 333 Pieces mPieces;
jsanchez307 0:1858f2b100fd 334 Board mBoard (&mPieces, mScreenHeight);
jsanchez307 0:1858f2b100fd 335 int a = RandomGen('a');
jsanchez307 0:1858f2b100fd 336 int b = RandomGen('b');
jsanchez307 0:1858f2b100fd 337 int c = RandomGen('a');
jsanchez307 0:1858f2b100fd 338 int d = RandomGen('b');
jsanchez307 0:1858f2b100fd 339 Game mGame (&mBoard, &mPieces, mScreenHeight, &uLCD,a,b,c,d);
jsanchez307 0:1858f2b100fd 340
jsanchez307 0:1858f2b100fd 341
jsanchez307 0:1858f2b100fd 342 // ----- Main Loop -----
jsanchez307 0:1858f2b100fd 343 int prevX=0;
jsanchez307 0:1858f2b100fd 344 int prevY=0;
jsanchez307 0:1858f2b100fd 345 int prevPiece=-1;
jsanchez307 0:1858f2b100fd 346 int prevRot=0;
jsanchez307 0:1858f2b100fd 347 Timer timer;
jsanchez307 0:1858f2b100fd 348 timer.start();
jsanchez307 0:1858f2b100fd 349 key_input=0;
jsanchez307 0:1858f2b100fd 350 bool needErase = false;
jsanchez307 0:1858f2b100fd 351 uLCD.cls();
jsanchez307 0:1858f2b100fd 352 while (1)
jsanchez307 0:1858f2b100fd 353 {
jsanchez307 0:1858f2b100fd 354 if(isGameOver)
jsanchez307 0:1858f2b100fd 355 {
jsanchez307 0:1858f2b100fd 356 wait(1);
jsanchez307 0:1858f2b100fd 357 continue;
jsanchez307 0:1858f2b100fd 358 }
jsanchez307 0:1858f2b100fd 359 // ----- Draw ----
jsanchez307 0:1858f2b100fd 360 if(needErase)
jsanchez307 0:1858f2b100fd 361 {
jsanchez307 0:1858f2b100fd 362 mGame.ErasePiece(prevX,prevY,prevPiece,prevRot);
jsanchez307 0:1858f2b100fd 363 needErase=false;
jsanchez307 0:1858f2b100fd 364 }
jsanchez307 0:1858f2b100fd 365 mGame.DrawScene();
jsanchez307 0:1858f2b100fd 366
jsanchez307 0:1858f2b100fd 367 prevX=mGame.mPosX;
jsanchez307 0:1858f2b100fd 368 prevY=mGame.mPosY;
jsanchez307 0:1858f2b100fd 369 prevPiece=mGame.mPiece;
jsanchez307 0:1858f2b100fd 370 prevRot=mGame.mRotation;
jsanchez307 0:1858f2b100fd 371
jsanchez307 0:1858f2b100fd 372 // ----- Input -----
jsanchez307 0:1858f2b100fd 373 switch (key_input)
jsanchez307 0:1858f2b100fd 374 {
jsanchez307 0:1858f2b100fd 375 case (2): //right
jsanchez307 0:1858f2b100fd 376 {
jsanchez307 0:1858f2b100fd 377 if (mBoard.IsPossibleMovement (mGame.mPosX + 1, mGame.mPosY, mGame.mPiece, mGame.mRotation))
jsanchez307 0:1858f2b100fd 378 {mGame.mPosX++;needErase=true;}
jsanchez307 0:1858f2b100fd 379 break;
jsanchez307 0:1858f2b100fd 380 }
jsanchez307 0:1858f2b100fd 381
jsanchez307 0:1858f2b100fd 382 case (1): //left
jsanchez307 0:1858f2b100fd 383 {
jsanchez307 0:1858f2b100fd 384 if (mBoard.IsPossibleMovement (mGame.mPosX - 1, mGame.mPosY, mGame.mPiece, mGame.mRotation))
jsanchez307 0:1858f2b100fd 385 {mGame.mPosX--;needErase=true;}
jsanchez307 0:1858f2b100fd 386 break;
jsanchez307 0:1858f2b100fd 387 }
jsanchez307 0:1858f2b100fd 388
jsanchez307 0:1858f2b100fd 389 case (4)://down
jsanchez307 0:1858f2b100fd 390 {
jsanchez307 0:1858f2b100fd 391 // Check collision from up to down
jsanchez307 0:1858f2b100fd 392 while (mBoard.IsPossibleMovement(mGame.mPosX, mGame.mPosY, mGame.mPiece, mGame.mRotation)) { mGame.mPosY++; }
jsanchez307 0:1858f2b100fd 393 needErase=true;
jsanchez307 0:1858f2b100fd 394 mBoard.StorePiece (mGame.mPosX, mGame.mPosY - 1, mGame.mPiece, mGame.mRotation);
jsanchez307 0:1858f2b100fd 395 mGame.AddPoints(SinglePiecePoints);
jsanchez307 0:1858f2b100fd 396 int linesDeleted = mBoard.DeletePossibleLines ();
jsanchez307 0:1858f2b100fd 397 if(linesDeleted>0)
jsanchez307 0:1858f2b100fd 398 {
jsanchez307 0:1858f2b100fd 399 mGame.AddClearedLines(linesDeleted);
jsanchez307 0:1858f2b100fd 400 mGame.AddPoints(LineClearPoints*linesDeleted);
jsanchez307 0:1858f2b100fd 401 //Thread t1(FlashLight);
jsanchez307 0:1858f2b100fd 402 //PlayClearSound();
jsanchez307 0:1858f2b100fd 403 clear_board();
jsanchez307 0:1858f2b100fd 404 }
jsanchez307 0:1858f2b100fd 405 UpdateGameStatus(mGame.GetPoints(),mGame.GetClearedLines());
jsanchez307 0:1858f2b100fd 406
jsanchez307 0:1858f2b100fd 407 if (mBoard.IsGameOver())
jsanchez307 0:1858f2b100fd 408 {
jsanchez307 0:1858f2b100fd 409 isGameOver=true;
jsanchez307 0:1858f2b100fd 410 uLCD.cls();
jsanchez307 0:1858f2b100fd 411
jsanchez307 0:1858f2b100fd 412 // FIX add Game Over mechanic
jsanchez307 0:1858f2b100fd 413
jsanchez307 0:1858f2b100fd 414 //uLCD.media_init();
jsanchez307 0:1858f2b100fd 415 //uLCD.set_sector_address(0x001D, 0x7842);
jsanchez307 0:1858f2b100fd 416 //uLCD.display_image(0,0);
jsanchez307 0:1858f2b100fd 417 //Thread t2(GameOverLight);
jsanchez307 0:1858f2b100fd 418 //PlayOverSound();
jsanchez307 0:1858f2b100fd 419
jsanchez307 0:1858f2b100fd 420 }
jsanchez307 0:1858f2b100fd 421
jsanchez307 0:1858f2b100fd 422 if(!isGameOver)
jsanchez307 0:1858f2b100fd 423 {
jsanchez307 0:1858f2b100fd 424 mGame.CreateNewPiece(RandomGen('a'),RandomGen('b'));
jsanchez307 0:1858f2b100fd 425 clear_next_piece();
jsanchez307 0:1858f2b100fd 426 }
jsanchez307 0:1858f2b100fd 427 break;
jsanchez307 0:1858f2b100fd 428 }
jsanchez307 0:1858f2b100fd 429
jsanchez307 0:1858f2b100fd 430 case (3)://rotate
jsanchez307 0:1858f2b100fd 431 {
jsanchez307 0:1858f2b100fd 432 if (mBoard.IsPossibleMovement (mGame.mPosX, mGame.mPosY, mGame.mPiece, (mGame.mRotation + 1) % 4))
jsanchez307 0:1858f2b100fd 433 {mGame.mRotation = (mGame.mRotation + 1) % 4;needErase=true;}
jsanchez307 0:1858f2b100fd 434 break;
jsanchez307 0:1858f2b100fd 435 }
jsanchez307 0:1858f2b100fd 436
jsanchez307 0:1858f2b100fd 437 case (0):{break;}
jsanchez307 0:1858f2b100fd 438 }
jsanchez307 0:1858f2b100fd 439 key_input = 0;
jsanchez307 0:1858f2b100fd 440
jsanchez307 0:1858f2b100fd 441 // ----- Vertical movement -----
jsanchez307 0:1858f2b100fd 442
jsanchez307 0:1858f2b100fd 443 if(timer.read_ms()>WAIT_TIME)
jsanchez307 0:1858f2b100fd 444 {
jsanchez307 0:1858f2b100fd 445 needErase=true;
jsanchez307 0:1858f2b100fd 446 if(!isGameOver)
jsanchez307 0:1858f2b100fd 447 {
jsanchez307 0:1858f2b100fd 448 if (mBoard.IsPossibleMovement (mGame.mPosX, mGame.mPosY + 1, mGame.mPiece, mGame.mRotation))
jsanchez307 0:1858f2b100fd 449 {
jsanchez307 0:1858f2b100fd 450 mGame.mPosY++;
jsanchez307 0:1858f2b100fd 451 }
jsanchez307 0:1858f2b100fd 452 else
jsanchez307 0:1858f2b100fd 453 {
jsanchez307 0:1858f2b100fd 454 mBoard.StorePiece (mGame.mPosX, mGame.mPosY, mGame.mPiece, mGame.mRotation);
jsanchez307 0:1858f2b100fd 455 mGame.AddPoints(SinglePiecePoints);
jsanchez307 0:1858f2b100fd 456 int linesDeleted = mBoard.DeletePossibleLines ();
jsanchez307 0:1858f2b100fd 457 if(linesDeleted>0)
jsanchez307 0:1858f2b100fd 458 {
jsanchez307 0:1858f2b100fd 459 mGame.AddClearedLines(linesDeleted);
jsanchez307 0:1858f2b100fd 460 mGame.AddPoints(LineClearPoints*linesDeleted);
jsanchez307 0:1858f2b100fd 461 //Thread t1(FlashLight);
jsanchez307 0:1858f2b100fd 462 //PlayClearSound();
jsanchez307 0:1858f2b100fd 463 clear_board();
jsanchez307 0:1858f2b100fd 464 }
jsanchez307 0:1858f2b100fd 465 UpdateGameStatus(mGame.GetPoints(),mGame.GetClearedLines());
jsanchez307 0:1858f2b100fd 466
jsanchez307 0:1858f2b100fd 467 if (mBoard.IsGameOver())
jsanchez307 0:1858f2b100fd 468 {
jsanchez307 0:1858f2b100fd 469 isGameOver=true;
jsanchez307 0:1858f2b100fd 470 uLCD.cls();
jsanchez307 0:1858f2b100fd 471
jsanchez307 0:1858f2b100fd 472 // FIX add Game Over mechanic
jsanchez307 0:1858f2b100fd 473
jsanchez307 0:1858f2b100fd 474 //uLCD.media_init();
jsanchez307 0:1858f2b100fd 475 //uLCD.set_sector_address(0x001D, 0x7842);
jsanchez307 0:1858f2b100fd 476 //uLCD.display_image(0,0);
jsanchez307 0:1858f2b100fd 477 //Thread t2(GameOverLight);
jsanchez307 0:1858f2b100fd 478 //PlayOverSound();
jsanchez307 0:1858f2b100fd 479 }
jsanchez307 0:1858f2b100fd 480
jsanchez307 0:1858f2b100fd 481 if(!isGameOver)
jsanchez307 0:1858f2b100fd 482 {
jsanchez307 0:1858f2b100fd 483 mGame.CreateNewPiece(RandomGen('a'),RandomGen('b'));
jsanchez307 0:1858f2b100fd 484 clear_next_piece();
jsanchez307 0:1858f2b100fd 485 }
jsanchez307 0:1858f2b100fd 486 }
jsanchez307 0:1858f2b100fd 487 }
jsanchez307 0:1858f2b100fd 488 timer.reset();
jsanchez307 0:1858f2b100fd 489 }
jsanchez307 0:1858f2b100fd 490 wait(0.1);
jsanchez307 0:1858f2b100fd 491 }
jsanchez307 0:1858f2b100fd 492 }