baseline features - a little buggy

Dependencies:   mbed wave_player 4DGL-uLCD-SE MMA8452

Committer:
robbiehuey
Date:
Sat Apr 10 02:08:02 2021 +0000
Revision:
2:cf4e61c051a4
Parent:
1:4421c1e849e9
hi

Who changed what in which revision?

UserRevisionLine numberNew contents of line
DCchico 0:95264f964374 1 //=================================================================
DCchico 0:95264f964374 2 // The main program file.
DCchico 0:95264f964374 3 //
DCchico 0:95264f964374 4 // Copyright 2020 Georgia Tech. All rights reserved.
DCchico 0:95264f964374 5 // The materials provided by the instructor in this course are for
DCchico 0:95264f964374 6 // the use of the students currently enrolled in the course.
DCchico 0:95264f964374 7 // Copyrighted course materials may not be further disseminated.
DCchico 0:95264f964374 8 // This file must not be made publicly available anywhere.
DCchico 0:95264f964374 9 //==================================================================
DCchico 0:95264f964374 10
DCchico 0:95264f964374 11 // External libs
DCchico 0:95264f964374 12 #include <stdlib.h>
DCchico 0:95264f964374 13
DCchico 0:95264f964374 14 // Project includes
DCchico 0:95264f964374 15 #include "globals.h"
DCchico 0:95264f964374 16 #include "hardware.h"
DCchico 0:95264f964374 17 #include "compost_pile_public.h"
DCchico 0:95264f964374 18 #include "fruit_public.h"
DCchico 0:95264f964374 19 #include "player_public.h"
robbiehuey 1:4421c1e849e9 20 #include "doubly_linked_list.h"
DCchico 0:95264f964374 21
DCchico 0:95264f964374 22 //For sound components
DCchico 0:95264f964374 23 ////AnalogOut DACout(p18);
DCchico 0:95264f964374 24 //PwmOut pwmout(p26);
DCchico 0:95264f964374 25 ////PwmOut speaker(p25);
DCchico 0:95264f964374 26
DCchico 0:95264f964374 27 ////wave_player waver(&DACout);
DCchico 0:95264f964374 28 //SDFileSystem sd(p5, p6, p7, p8, "sd"); // mosi, miso, sck, cs
DCchico 0:95264f964374 29
DCchico 0:95264f964374 30 DigitalOut myled1(LED1);
DCchico 0:95264f964374 31 DigitalOut myled2(LED2);
DCchico 0:95264f964374 32 DigitalOut myled3(LED3);
DCchico 0:95264f964374 33 DigitalOut myled4(LED4);
DCchico 0:95264f964374 34
DCchico 0:95264f964374 35 // ===User implementations start===
robbiehuey 1:4421c1e849e9 36 bool touch(boundingBox knife, boundingBox fruit);
DCchico 0:95264f964374 37 int fruit_contact(void); // iterate through fruits and see if any collided
DCchico 0:95264f964374 38 void compost_pile_update(void);
robbiehuey 1:4421c1e849e9 39 void incScore(void);
DCchico 0:95264f964374 40
DCchico 0:95264f964374 41 void playSound(char * wav);
DCchico 0:95264f964374 42 void playNotes(void);
DCchico 0:95264f964374 43 void set_random_seed(Timer);
robbiehuey 1:4421c1e849e9 44 int tempScore = 0;
robbiehuey 1:4421c1e849e9 45 bool gameWon = false;
robbiehuey 1:4421c1e849e9 46 bool gameLost = false;
robbiehuey 1:4421c1e849e9 47 bool thrown = false;
DCchico 0:95264f964374 48
DCchico 0:95264f964374 49 int main()
DCchico 0:95264f964374 50 {
DCchico 0:95264f964374 51 GameInputs inputs;
DCchico 0:95264f964374 52 // First things first: initialize hardware
DCchico 0:95264f964374 53 ASSERT_P(hardware_init() == ERROR_NONE, "Hardware init failed!");
DCchico 0:95264f964374 54 pc.printf("Program Starting\n");
DCchico 0:95264f964374 55
DCchico 0:95264f964374 56 // ===User implementations start===
DCchico 0:95264f964374 57 // Game state variables
DCchico 0:95264f964374 58
DCchico 0:95264f964374 59 // Timer to measure game update speed (secondarily used to generate random seed)
DCchico 0:95264f964374 60 Timer t;
DCchico 0:95264f964374 61 int dt; // delta time
DCchico 0:95264f964374 62 set_random_seed(t);
DCchico 0:95264f964374 63
DCchico 0:95264f964374 64 //pwmout.period(1.0/4000.0);
DCchico 0:95264f964374 65 //playSound("/sd/wavfiles/BUZZER.wav");//test the sound convert to thread
DCchico 0:95264f964374 66 //wait(0.1);
DCchico 0:95264f964374 67 //playNotes();
DCchico 0:95264f964374 68
DCchico 0:95264f964374 69 //initialize functions
DCchico 0:95264f964374 70 compost_pile_init();
DCchico 0:95264f964374 71 fruit_init();
DCchico 0:95264f964374 72 player_init();
DCchico 0:95264f964374 73 pc.printf("Initialization complete\n");
robbiehuey 1:4421c1e849e9 74 uLCD.printf("SCORE:");
robbiehuey 1:4421c1e849e9 75 uLCD.filled_rectangle(70, 0, 120, 8, RED);
robbiehuey 1:4421c1e849e9 76 int is_bomb;
DCchico 0:95264f964374 77
robbiehuey 1:4421c1e849e9 78 while(!gameWon && !gameLost)
DCchico 0:95264f964374 79 {
DCchico 0:95264f964374 80 t.start();
DCchico 0:95264f964374 81 fruit_generator();
DCchico 0:95264f964374 82 player_draw(0x0000FF);
robbiehuey 1:4421c1e849e9 83 player_knife_draw();
DCchico 0:95264f964374 84 inputs = read_inputs();
robbiehuey 1:4421c1e849e9 85 if (inputs.ay>=0.5) player_moveUp();
robbiehuey 1:4421c1e849e9 86 if (inputs.ay<= -0.5) player_moveDown();
robbiehuey 1:4421c1e849e9 87 if (inputs.b3) {
robbiehuey 1:4421c1e849e9 88 player_throw();
robbiehuey 1:4421c1e849e9 89 }
robbiehuey 1:4421c1e849e9 90 knife_move();
robbiehuey 1:4421c1e849e9 91 if (player_get_info().knife_boundingBox.topLeft.x >= 127) {
robbiehuey 1:4421c1e849e9 92 player_knife_return();
robbiehuey 1:4421c1e849e9 93 }
DCchico 0:95264f964374 94 compost_pile_update();
robbiehuey 1:4421c1e849e9 95 is_bomb = fruit_contact();
robbiehuey 1:4421c1e849e9 96 if (tempScore == 5) {
robbiehuey 1:4421c1e849e9 97 gameWon = true;
robbiehuey 1:4421c1e849e9 98 continue;
robbiehuey 1:4421c1e849e9 99 }
robbiehuey 1:4421c1e849e9 100 if (is_bomb == 1) {
robbiehuey 1:4421c1e849e9 101 gameLost = true;
robbiehuey 1:4421c1e849e9 102 continue;
robbiehuey 1:4421c1e849e9 103 }
robbiehuey 1:4421c1e849e9 104 if (get_compost_tallest_height() > 60) {
robbiehuey 1:4421c1e849e9 105 gameLost = true;
robbiehuey 1:4421c1e849e9 106 continue;
robbiehuey 1:4421c1e849e9 107 }
DCchico 0:95264f964374 108
DCchico 0:95264f964374 109 t.stop();
DCchico 0:95264f964374 110 dt = t.read_ms();
DCchico 0:95264f964374 111 if (dt < 100) wait_ms(100 - dt);
DCchico 0:95264f964374 112 }
DCchico 0:95264f964374 113 destroyList(get_fruit_list());
DCchico 0:95264f964374 114
robbiehuey 1:4421c1e849e9 115 if (gameLost) {
robbiehuey 1:4421c1e849e9 116 uLCD.cls();
robbiehuey 1:4421c1e849e9 117 uLCD.printf("YOU LOSE!\n\n\n\n\nRESTART TO \nPLAY AGAIN");
robbiehuey 1:4421c1e849e9 118 }
robbiehuey 1:4421c1e849e9 119
robbiehuey 1:4421c1e849e9 120 if (gameWon) {
robbiehuey 1:4421c1e849e9 121 uLCD.cls();
robbiehuey 1:4421c1e849e9 122 uLCD.printf("YOU WIN! \n\n\n\n\nRESTART TO \nPLAY AGAIN");
robbiehuey 1:4421c1e849e9 123 }
DCchico 0:95264f964374 124 return 0;
robbiehuey 1:4421c1e849e9 125 }
DCchico 0:95264f964374 126 // ===User implementations end===
DCchico 0:95264f964374 127
DCchico 0:95264f964374 128 // ===User implementations start===
robbiehuey 1:4421c1e849e9 129 void incScore(void) {
robbiehuey 1:4421c1e849e9 130 tempScore++;
robbiehuey 1:4421c1e849e9 131 if (tempScore == 10) {
robbiehuey 1:4421c1e849e9 132 gameWon = true;
robbiehuey 1:4421c1e849e9 133 return;
robbiehuey 1:4421c1e849e9 134 }
robbiehuey 1:4421c1e849e9 135 uLCD.filled_rectangle(70, 0, tempScore*10 + 70, 8, GREEN);
robbiehuey 1:4421c1e849e9 136 }
robbiehuey 1:4421c1e849e9 137
robbiehuey 1:4421c1e849e9 138
DCchico 0:95264f964374 139
robbiehuey 1:4421c1e849e9 140
robbiehuey 1:4421c1e849e9 141 bool touch(boundingBox knife, boundingBox fruit) {
robbiehuey 1:4421c1e849e9 142 if (fruit.topLeft.x > knife.bottomRight.x+3){
robbiehuey 1:4421c1e849e9 143 return false;
robbiehuey 1:4421c1e849e9 144 } else if (fruit.topLeft.y > knife.bottomRight.y+3) {
robbiehuey 1:4421c1e849e9 145 return false;
robbiehuey 1:4421c1e849e9 146 } else if (knife.topLeft.x > fruit.bottomRight.x+3) {
robbiehuey 1:4421c1e849e9 147 return false;
robbiehuey 1:4421c1e849e9 148 } else if (knife.topLeft.y > fruit.bottomRight.y+3) {
robbiehuey 1:4421c1e849e9 149 return false;
robbiehuey 1:4421c1e849e9 150 }
robbiehuey 1:4421c1e849e9 151 return true;
robbiehuey 1:4421c1e849e9 152 }
robbiehuey 1:4421c1e849e9 153
DCchico 0:95264f964374 154 /* This function iterate through the fruit list, checking if anyone one of them is contacting
DCchico 0:95264f964374 155 * with the player's knife. Don't forget to check if the "fruit" is the bomb, since we
DCchico 0:95264f964374 156 * generated bombs using the fruit_generator.
DCchico 0:95264f964374 157 */
DCchico 0:95264f964374 158 int fruit_contact(void) {
robbiehuey 1:4421c1e849e9 159 DLinkedList* fruitList = get_fruit_list();
robbiehuey 1:4421c1e849e9 160 LLNode* curr = fruitList -> head;
robbiehuey 1:4421c1e849e9 161 FRUIT* fruit;
robbiehuey 1:4421c1e849e9 162 while (curr != NULL) {
robbiehuey 1:4421c1e849e9 163 fruit = (FRUIT*)(curr->data);
robbiehuey 1:4421c1e849e9 164 if ((touch(player_get_info().knife_boundingBox, fruit->box))&&(player_get_info().player_has_knife == 0)){
robbiehuey 1:4421c1e849e9 165 if ((fruit->type) == 0) {
robbiehuey 1:4421c1e849e9 166 return 1;
robbiehuey 1:4421c1e849e9 167 }
robbiehuey 1:4421c1e849e9 168 fruit->status = FRUIT_SLICED;
robbiehuey 1:4421c1e849e9 169 player_knife_return();
robbiehuey 1:4421c1e849e9 170 incScore();
robbiehuey 1:4421c1e849e9 171 return 0;
robbiehuey 1:4421c1e849e9 172 }
robbiehuey 1:4421c1e849e9 173 curr = curr-> next;
robbiehuey 1:4421c1e849e9 174 }
robbiehuey 1:4421c1e849e9 175 return 0;
DCchico 0:95264f964374 176 }
DCchico 0:95264f964374 177
DCchico 0:95264f964374 178 /** Call compost_pile_update() repeatedly in your game-loop. ex: main()
DCchico 0:95264f964374 179 This function iterate through the fruit list. Add fruits that reach the bottom of the screen
DCchico 0:95264f964374 180 to the compost pile.
DCchico 0:95264f964374 181 @return Number of remaining cities. You might end the game when all cities are demolished.
DCchico 0:95264f964374 182 */
DCchico 0:95264f964374 183 void compost_pile_update(void){
robbiehuey 1:4421c1e849e9 184 DLinkedList* fruitList = get_fruit_list();
robbiehuey 1:4421c1e849e9 185 LLNode* curr = fruitList -> head;
robbiehuey 1:4421c1e849e9 186 FRUIT* fruit;
robbiehuey 1:4421c1e849e9 187 int index;
robbiehuey 1:4421c1e849e9 188 while (curr != NULL) {
robbiehuey 1:4421c1e849e9 189 fruit = (FRUIT*)(curr->data);
robbiehuey 2:cf4e61c051a4 190 if ((fruit->box.topLeft.y > 127 - get_compost_tallest_height())&&(fruit->type != 0)) {
robbiehuey 1:4421c1e849e9 191 index = (fruit->box.topLeft.x)/11;
robbiehuey 1:4421c1e849e9 192 compost_add(index);
robbiehuey 1:4421c1e849e9 193 fruit->status = FRUIT_SLICED;
robbiehuey 1:4421c1e849e9 194 }
robbiehuey 1:4421c1e849e9 195 curr = curr -> next;
robbiehuey 1:4421c1e849e9 196 }
robbiehuey 1:4421c1e849e9 197 draw_compost();
DCchico 0:95264f964374 198 }
DCchico 0:95264f964374 199
DCchico 0:95264f964374 200 //fcn to play a wav
DCchico 0:95264f964374 201 void playSound(char* wav)
DCchico 0:95264f964374 202 {
DCchico 0:95264f964374 203 //open wav file
DCchico 0:95264f964374 204 FILE *wave_file;
DCchico 0:95264f964374 205 wave_file=fopen(wav,"r");
DCchico 0:95264f964374 206
DCchico 0:95264f964374 207 if(wave_file != NULL)
DCchico 0:95264f964374 208 {
DCchico 0:95264f964374 209 printf("File opened successfully\n");
DCchico 0:95264f964374 210
DCchico 0:95264f964374 211 //play wav file
DCchico 0:95264f964374 212 printf("Sound playing...\n");
DCchico 0:95264f964374 213 waver.play(wave_file);
DCchico 0:95264f964374 214
DCchico 0:95264f964374 215 //close wav file
DCchico 0:95264f964374 216 printf("Sound stopped...\n");
DCchico 0:95264f964374 217 fclose(wave_file);
DCchico 0:95264f964374 218 return;
DCchico 0:95264f964374 219 }
DCchico 0:95264f964374 220
DCchico 0:95264f964374 221 printf("Could not open file for reading - %s\n", wav);
DCchico 0:95264f964374 222 return;
DCchico 0:95264f964374 223 }
DCchico 0:95264f964374 224
DCchico 0:95264f964374 225 /* Wanna hear some annoying sounds?*/
DCchico 0:95264f964374 226 void playNotes(void)
DCchico 0:95264f964374 227 {
DCchico 0:95264f964374 228 int i;
DCchico 0:95264f964374 229 // generate a 500Hz tone using PWM hardware output
DCchico 0:95264f964374 230 speaker.period(1.0/500.0); // 500hz period
DCchico 0:95264f964374 231 speaker =0.5; //50% duty cycle - max volume
DCchico 0:95264f964374 232 wait(3);
DCchico 0:95264f964374 233 speaker=0.0; // turn off audio
DCchico 0:95264f964374 234 wait(2);
DCchico 0:95264f964374 235 // generate a short 150Hz tone using PWM hardware output
DCchico 0:95264f964374 236 // something like this can be used for a button click effect for feedback
DCchico 0:95264f964374 237 for (i=0; i<10; i++) {
DCchico 0:95264f964374 238 speaker.period(1.0/150.0); // 500hz period
DCchico 0:95264f964374 239 speaker =0.25; //25% duty cycle - mid range volume
DCchico 0:95264f964374 240 wait(.02);
DCchico 0:95264f964374 241 speaker=0.0; // turn off audio
DCchico 0:95264f964374 242 wait(0.5);
DCchico 0:95264f964374 243 }
DCchico 0:95264f964374 244
DCchico 0:95264f964374 245 // sweep up in frequency by changing the PWM period
DCchico 0:95264f964374 246 for (i=0; i<8000; i=i+100) {
DCchico 0:95264f964374 247 speaker.period(1.0/float(i));
DCchico 0:95264f964374 248 speaker=0.25;
DCchico 0:95264f964374 249 wait(.1);
DCchico 0:95264f964374 250 }
DCchico 0:95264f964374 251 wait(2);
DCchico 0:95264f964374 252
DCchico 0:95264f964374 253 // two tone police siren effect -  two periods or two frequencies
DCchico 0:95264f964374 254 // increase volume - by changing the PWM duty cycle
DCchico 0:95264f964374 255 for (i=0; i<26; i=i+2) {
DCchico 0:95264f964374 256 speaker.period(1.0/969.0);
DCchico 0:95264f964374 257 speaker = float(i)/50.0;
DCchico 0:95264f964374 258 wait(.5);
DCchico 0:95264f964374 259 speaker.period(1.0/800.0);
DCchico 0:95264f964374 260 wait(.5);
DCchico 0:95264f964374 261 }
DCchico 0:95264f964374 262 // decrease volume
DCchico 0:95264f964374 263 for (i=25; i>=0; i=i-2) {
DCchico 0:95264f964374 264 speaker.period(1.0/969.0);
DCchico 0:95264f964374 265 speaker = float(i)/50.0;
DCchico 0:95264f964374 266 wait(.5);
DCchico 0:95264f964374 267 speaker.period(1.0/800.0);
DCchico 0:95264f964374 268 wait(.5);
DCchico 0:95264f964374 269 }
DCchico 0:95264f964374 270 speaker =0.0;
DCchico 0:95264f964374 271 wait(2);
DCchico 0:95264f964374 272 }
DCchico 0:95264f964374 273
DCchico 0:95264f964374 274 void set_random_seed(Timer t) {
DCchico 0:95264f964374 275 GameInputs inputs;
DCchico 0:95264f964374 276 t.start();
DCchico 0:95264f964374 277 uLCD.printf("Push any button to start.\n");
DCchico 0:95264f964374 278 while(1){
DCchico 0:95264f964374 279 inputs = read_inputs();
DCchico 0:95264f964374 280 if (inputs.b1 || inputs.b2 || inputs.b3) break;
DCchico 0:95264f964374 281 }
DCchico 0:95264f964374 282 uLCD.cls();
DCchico 0:95264f964374 283 t.stop();
DCchico 0:95264f964374 284 int seed = t.read_ms();
DCchico 0:95264f964374 285 srand(seed);
robbiehuey 1:4421c1e849e9 286 }
robbiehuey 1:4421c1e849e9 287
DCchico 0:95264f964374 288 // ===User implementations end===