
Ellis Blackford Stroud 201155309
Dependencies: mbed FATFileSystem
Revision 17:98127ac75195, committed 2019-05-09
- Comitter:
- ellisbhastroud
- Date:
- Thu May 09 12:01:42 2019 +0000
- Parent:
- 16:c8d68cbd1ae2
- Commit message:
- Final Submission. I have read and agreed with Statement of Academic Integrity.
Changed in this revision
--- a/Ball/Ball.cpp Thu May 09 09:58:51 2019 +0000 +++ b/Ball/Ball.cpp Thu May 09 12:01:42 2019 +0000 @@ -153,6 +153,7 @@ void Ball::left_bounce(Coord start, Coord end) //checks if there is a collision with left wall using coordinates and bounces accordingly { //if statement bounce conditions use coordinates from function and determine whether the ball will pass line in next frame - if so then bounces if(_x_pos + _x_vel*10.0f/_frame_rate - 1 < start.x && _x_pos + _x_vel*10.0f/_frame_rate > start.x - 5 && _y_pos >= start.y && _y_pos + 1 <= end.y && _x_vel < 0){ + _x_pos = start.x + 1; _x_vel = -_x_vel; //left wall bounce causes x velocity to change sign _bounce_flag = true; @@ -162,6 +163,7 @@ void Ball::right_bounce(Coord start, Coord end) //checks if there is a collision with a right wall using coordinates and bounces accordingly { //if statement bounce conditions use coordinates from function and determine whether the ball will pass line in next frame - if so then bounces if(_x_pos + _x_vel*10.0f/_frame_rate + 2 > start.x && _x_pos + _x_vel*10.0f/_frame_rate < start.x + 5 && _y_pos >= start.y && _y_pos + 1 <= end.y && _x_vel > 0){ //right wall x + 1 + _x_pos = start.x - 1; _x_vel = -_x_vel; //right wall bounce causes x velocity to change sign _bounce_flag = true; @@ -171,6 +173,7 @@ void Ball::top_bounce(Coord start, Coord end) //checks if there is a collision with a top wall using coordinates and bounces accordingly { //if statement bounce conditions use coordinates from function and determine whether the ball will pass line in next frame - if so then bounces if(_y_pos + _y_vel*10.0f/_frame_rate - 1 < start.y && _y_pos + _y_vel*10.0f/_frame_rate > start.y - 5 && _x_pos >= start.x && _x_pos + 1 <= end.x && _y_vel < 0){ //top wall y -1 + _y_pos = start.y + 1; _y_vel = -_y_vel; //top wall bounce causes y velocity to change sign _bounce_flag = true; @@ -180,6 +183,7 @@ void Ball::bottom_bounce(Coord start, Coord end) //checks if there is a collision with a bottom wall using coordinates and bounces accordingly { //if statement bounce conditions use coordinates from function and determine whether the ball will pass line in next frame - if so then bounces if(_y_pos + _y_vel*10.0f/_frame_rate + 2 > start.y && _y_pos + _y_vel*10.0f/_frame_rate < start.y + 5 && _x_pos >= start.x && _x_pos + 1 <= end.x && _y_vel > 0){ //bottom wall + _y_pos = start.y - 1; _y_vel = -_y_vel; //bottom wall bounce causes y velocity to change sign _bounce_flag = true; @@ -191,6 +195,9 @@ if((_y_pos + _y_vel*10.0f/_frame_rate + 1) > (_x_pos + _x_vel*10.0f/_frame_rate - 1) + (start.y-start.x) && //bottom left bounce conditions (_x_pos + _x_vel*10.0f/_frame_rate - 1) >= start.x && (_x_pos + _x_vel*10.0f/_frame_rate - 1) <= end.x && (_y_pos + _y_vel*10.0f/_frame_rate + 1) >= start.y && (_y_pos + _y_vel*10.0f/_frame_rate + 1) <= end.y ) { + + _x_pos = _x_pos + 0.5f; //move ball slightly away from wall - stops ball getting stuck behind wall + _y_pos = _y_pos - 0.5f; swap(_x_vel, _y_vel); //reflects from wall with velocity directions swapped _bounce_flag = true; } @@ -201,7 +208,9 @@ if((_x_pos + _x_vel*10.0f/_frame_rate + 1) > -(_y_pos + _y_vel*10.0f/_frame_rate + 1) + (start.x+start.y) //bottom right bounce conditions && (_x_pos + _x_vel*10.0f/_frame_rate + 1) >= start.x - 1 && (_x_pos + _x_vel*10.0f/_frame_rate + 1) <= end.x + 1 && (_y_pos + _y_vel*10.0f/_frame_rate + 1) >= end.y - 1 && (_y_pos + _y_vel*10.0f/_frame_rate + 1) <= start.y + 1) { - + + _x_pos = _x_pos - 0.5f; //move ball away from wall - stops ball getting stuck behind wall + _y_pos = _y_pos - 0.5f; _x_vel = -_x_vel; //negative wall causes both velocities to change sign then swap _y_vel = -_y_vel; swap(_x_vel, _y_vel); //reflects from wall with velocity directions swapped @@ -214,7 +223,9 @@ if((_x_pos + _x_vel*10.0f/_frame_rate - 1) < -(_y_pos + _y_vel*10.0f/_frame_rate + 1) + (start.x+start.y) //top left bounce conditions && (_x_pos + _x_vel*10.0f/_frame_rate - 1) >= start.x - 1&& (_x_pos + _x_vel*10.0f/_frame_rate - 1) <= end.x + 1 && (_y_pos + _y_vel*10.0f/_frame_rate + 1) >= end.y - 1 && (_y_pos + _y_vel*10.0f/_frame_rate + 1) <= start.y + 1) { - + + _x_pos = _x_pos + 0.5f; //move ball away from wall - stops ball getting stuck behind wall + _y_pos = _y_pos + 0.5f; _x_vel = -_x_vel; //negative wall causes both velocities to change sign then swap _y_vel = -_y_vel; swap(_x_vel, _y_vel); //reflects from wall with velocity directions swapped @@ -228,6 +239,8 @@ && (_x_pos + _x_vel*10.0f/_frame_rate + 1) >= start.x - 1 && (_x_pos + _x_vel*10.0f/_frame_rate + 1) <= end.x + 1 && (_y_pos + _y_vel*10.0f/_frame_rate - 1) >= start.y - 1 && (_y_pos + _y_vel*10.0f/_frame_rate - 1) <= end.y + 1) { + _x_pos = _x_pos - 0.5f; //move ball away from wall - stops ball getting stuck behind wall + _y_pos = _y_pos + 0.5f; swap(_x_vel, _y_vel); //reflects from wall with velocity directions swapped _bounce_flag = true; //used to trigger beep }
--- a/Ball/Ball.h Thu May 09 09:58:51 2019 +0000 +++ b/Ball/Ball.h Thu May 09 12:01:42 2019 +0000 @@ -41,7 +41,7 @@ Coord ball; /**< start position of ball */ Coord hole; /**< position of hole */ - WallMap walls[16]; /**< array of walls */ + WallMap walls[18]; /**< array of walls */ int wall_count; /**< number of walls in level */
--- a/GolfEngine/GolfEngine.cpp Thu May 09 09:58:51 2019 +0000 +++ b/GolfEngine/GolfEngine.cpp Thu May 09 12:01:42 2019 +0000 @@ -56,12 +56,7 @@ void GolfEngine::check_end_level(N5110 &lcd, Gamepad &pad, SDFileSystem &sd) { if(_ball.check_hole(levels[_level].hole) == true) { //End of level sequence when level completed - /* UNCOMMENT FOR TESTING - printf("Level %i Complete\n",_level+1); - print("Shots Taken = %i\n",_ball.get_shot_count()); - printf("Total Shot Count = %i\n",_ball_get_total_shot_count()); - printf("Begin Level %i /n",_level+2); - */ + drawCourseWalls(lcd,levels[_level].walls,levels[_level].wall_count); //draw walls for flash animation _level++; //increment level counter g_maj7_flash(pad, lcd); //flashes screen with g maj chord @@ -111,7 +106,7 @@ FILE *fp; fp = fopen("/sd/highscores.txt", "a"); //attempts to open highscores.txt if (fp == NULL) { // if file does not exist then create new highscores.txt file - printf("Error! Unable to open file!\n"); + printf("Error! Unable to open file!\n"); //print error message } else { fprintf(fp, "\n%i", value); //appends total shot count to highscores file fclose(fp); //close file @@ -120,11 +115,12 @@ void GolfEngine::new_level(N5110 &lcd, SDFileSystem &sd) //called after ball is enters hole { - if(_level < 6) { //if all levels are completed + if(_level < 7) { //if all levels are completed printLevel(lcd); _ball.init(levels[_level].ball); //initialises ball in position for new level - - } else if (_level >= 6) { //if all levels not completed + } else if (_level >= 7) { //if all levels not completed + + _ball.init(levels[_level].ball); //to add the last levels shot count to the toal tally - ball position irrelevant lcd.printString("COURSE ",26,2); lcd.printString("COMPLETE",22,3); lcd.refresh(); @@ -132,12 +128,8 @@ lcd.clear(); char buffer[14]; sprintf(buffer," %i",_ball.get_total_shot_count()); - lcd.printString("YOUR SCORE",14,1); - lcd.printString(buffer,30,2); //print final score - /*UNCOMMENT FOR DEBUG - printf("Total Score = %i", _ball_get_total_shot_count()); - printf("Game Over - Returning to menu"); - */ + lcd.printString("YOUR SCORE:",14,1); + lcd.printString(buffer,30,3); //print final score lcd.refresh(); wait(5); file_append(_ball.get_total_shot_count(), sd); //appends final score to highscores.txt file @@ -168,11 +160,11 @@ void GolfEngine::g_maj7_flash(Gamepad &pad, N5110 &lcd) // Screen inverse flashing and buzzer plays G7 Chord with leds on { pad.leds_on(); - lcd.inverseMode(); + lcd.inverseMode(); //pixels on = off & off = on lcd.refresh(); pad.tone(783.99, 0.25); //play note (G5) wait(0.25); - lcd.normalMode(); + lcd.normalMode(); lcd.refresh(); pad.tone(987.77, 0.25); //play note (B5) wait(0.25);
--- a/GolfEngine/GolfEngine.h Thu May 09 09:58:51 2019 +0000 +++ b/GolfEngine/GolfEngine.h Thu May 09 12:01:42 2019 +0000 @@ -82,7 +82,8 @@ //Array of Levels struct contains all information needed to draw and run all levels //Contains coordinates of ball start position, hole position and all walls with types. The game engine uses //these coordinates to draw the ball and hole for each level, to check for bounces and to check if the ball is in the hole and the level is over. -const Levels levels[6] = { +//To add new levels enter line and ball/hole coordinates for new map. +const Levels levels[7] = { {{15,32}, {68,31}, //level one - //element one is coordinate of ball and element two is coordinate of hole {{LEFT,{9,24},{9,39}}, //wall types with coordinates used to check for bounces @@ -153,6 +154,26 @@ {RIGHT,{60,25},{60,36}}, {BOTTOM,{60,25},{67,25}}, {LEFT,{67,25},{67,36}}, - {TOP,{60,36},{67,36}}}, 16} + {TOP,{60,36},{67,36}}}, 16}, + + {{41,20}, {41,33}, //level seven + {{LEFT,{10,9},{10,45}}, + {TOPLEFT,{10,14},{15,9}}, + {TOP,{10,9},{73,9}}, + {TOPRIGHT,{68,9},{73,14}}, + {RIGHT,{73,9},{73,45}}, + {BOTTOM,{10,45},{73,45}}, + {BOTTOM,{34,23},{50,23}}, + {RIGHT,{50,15},{50,23}}, + {BOTTOM,{50,15},{64,15}}, + {LEFT,{64,15},{64,39}}, + {TOP,{50,39},{64,39}}, + {RIGHT,{50,31},{50,39}}, + {TOP,{34,31},{50,31}}, + {LEFT,{34,31},{34,39}}, + {TOP,{19,39},{34,39}}, + {RIGHT,{19,15},{19,39}}, + {BOTTOM,{19,15},{34,15}}, + {LEFT,{34,15},{34,23}}}, 18} }; #endif \ No newline at end of file
--- a/Menu/Menu.cpp Thu May 09 09:58:51 2019 +0000 +++ b/Menu/Menu.cpp Thu May 09 12:01:42 2019 +0000 @@ -85,7 +85,7 @@ void Menu::highscores_loop(Gamepad &pad, N5110 &lcd, SDFileSystem &sd) //highscores screen loop { - sd_read(sd); //reads values from highscores.txt file and sorts in ascending order + sd_read(sd); //reads values from highscores.txt file into array and sorts in ascending order while(pad.check_event(Gamepad::BACK_PRESSED) == false) { //loops until back pressed lcd.clear(); @@ -93,7 +93,7 @@ for(int i = 0; i < _n ; i++) { //displays highscores char buffer[14]; - sprintf(buffer,"%i - %i", i + 1, _highscores_array[i]); + sprintf(buffer,"%i - %i", i + 1, _highscores_array[i]); //prints values from highscores array lcd.printString(buffer,0,i+1); }
--- a/main.cpp Thu May 09 09:58:51 2019 +0000 +++ b/main.cpp Thu May 09 12:01:42 2019 +0000 @@ -61,11 +61,7 @@ lcd.refresh(); wait(3.0f); golf.reset_game_over_flag(); - /* UNCOMMENT FOR TESTING - printf("Game Initialised\n",_level-1); - print("Frame Rate = %i Hz\n",menu.get_frame_rate())); - print("Begin Level 1 \n",menu.get_frame_rate())); - */ + while(golf.get_game_over_flag() == false){ //Main Game Loop - When game is over returns to start at menu screen if(g_frame_flag) { //If ticker calls isr and game not over