Dependencies: mbed
Diff: Game/Game.cpp
- Revision:
- 20:c697902b844f
- Parent:
- 17:2fbe40177b9c
- Child:
- 21:d5b1160f349f
--- a/Game/Game.cpp Fri May 22 23:51:06 2020 +0000 +++ b/Game/Game.cpp Sun May 24 17:40:56 2020 +0000 @@ -1,4 +1,10 @@ #include "Game.h" +Ticker ticker; + +volatile int sound_timer_flag = 0; +volatile int sound_sample = 0; + +void timer_isr(); Game::Game(N5110 &lcd, Gamepad &pad, Ball &ball) { _lcd = &lcd; @@ -35,12 +41,14 @@ // ball should not go through obstacle hence y value limited to 21 // this is done after calculations to ensure accurate results if(_shot_y >= 9) { _shot_y = 21; } + if(_shot_y <= 5) {_shot_y = 3; } _lives--; } _ball->playShot(_shot_x, _shot_y); print_goal_message((int)_is_goal); - if(_score > _highscore) { set_highscore(_score); } + playGoalSound((int)_is_goal + 1); } + if(_score > _highscore) { set_highscore(_score);} _pad->leds(0.0); //turn of all leds } @@ -58,10 +66,10 @@ convert_to_shot_x(); convert_to_shot_y(); _is_goal = _ball->isGoal(_level, _shot_x, _shot_y); - printf("x val = %.2f \n",_x_val); - printf("shot_x val = %d \n",_shot_x); - printf("y val = %.2f \n",_y_val); - printf("shot_y val = %d \n",_shot_y); + //printf("x val = %.2f \n",_x_val); + //printf("shot_x val = %d \n",_shot_x); + //printf("y val = %.2f \n",_y_val); + //printf("shot_y val = %d \n",_shot_y); } void Game::updateLives() { int val [6] = {0,0,0,0,0,0}; @@ -71,7 +79,7 @@ _lives++; _new_lives_threshold = _score; } - printf("Lives = %d \n", _lives); + //printf("Lives = %d \n", _lives); switch(_lives){ case 0: break; @@ -103,13 +111,13 @@ _new_speed_threshold = _score; } } - printf("speed = %.2f \n", _speed); - printf("score = %d \n", _score); + //printf("speed = %.2f \n", _speed); + //printf("score = %d \n", _score); } void Game::updateLevel() { _level = random_level_gen(10); //generate random level _ball->set_level(_level); //set level - printf("Level (in game) = %d \n", _level); + //printf("Level (in game) = %d \n", _level); } void Game::updateLeds( int val[6]) { _pad->led(1,val[0]); @@ -119,6 +127,28 @@ _pad->led(5,val[4]); _pad->led(6,val[5]); } +void Game::playGoalSound(int sound) { + sound_sample = 0; + float val = 0.0f; + int NUM_ELEMENTS = 0; + ticker.attach(&timer_isr,226e-7); + if(sound == 1) {NUM_ELEMENTS = NUM_ELEMENTS_1;} + else if(sound == 2) {NUM_ELEMENTS = NUM_ELEMENTS_3;} + else if(sound == 3) {NUM_ELEMENTS = NUM_ELEMENTS_2;} + while(sound_sample <= NUM_ELEMENTS) { + if (sound_timer_flag == 1) { + sound_timer_flag = 0; + // convert from 0 to 255 to 0.0 to 1.0 + if(sound == 1) {val = float(miss[sound_sample])/ 256.0f;} + else if(sound == 2) {val = float(goall[sound_sample])/ 256.0f;} + else if(sound == 3) {val = float(over[sound_sample])/ 256.0f;} + // write to DAC + _pad->write_dac(val); + // move onto next sample + sound_sample++; + } + } +} void Game::set_highscore (int score) { _highscore = score; } @@ -192,5 +222,8 @@ _lcd->refresh(); wait(1.5); } +void timer_isr() { + sound_timer_flag = 1; // set flag in the isr +}