Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
Revision 7:8d381315f72c, committed 2020-05-26
- Comitter:
- ale_carb0ni
- Date:
- Tue May 26 22:53:42 2020 +0000
- Parent:
- 6:a2c72def99f9
- Commit message:
- Final Submission. I have read and agreed with Statement of Academic Integrity.
Changed in this revision
--- a/Game/Game.cpp Tue May 26 18:14:03 2020 +0000 +++ b/Game/Game.cpp Tue May 26 22:53:42 2020 +0000 @@ -4,37 +4,47 @@ #include "N5110.h" #include "Menu.h" -/**Constructor*/ +//initialise the game Game::Game() { - x = 4; - y = 4; - for (int i = 0; i < 16; i++) { //this creates the possible x coordinates the fruit can have - fruitX[i] = 3 + i*5; //in order to be always alligned with the snake and stay inside the field - } - for (int i = 0; i < 9; i++) { //this creates the possible y coordinates the fruit can have - fruitY[i] = 3 + i*5; //in order to be always alligned with the snake and stay inside the field + x = 4; //sets initial x position to 4 + y = 4; //sets initial y position to 4 + /*creates the array for the possible x coordinates the fruit can have + in order to be always alligned with the snake and stay inside the field*/ + for (int i = 0; i < 16; i++) { + fruitX[i] = 3 + i*5; } - score = 0; //initial score is 0 - a = 0; //used to select the direction based ont what button is pressed - ntail = 0; //used to increase lenght of the tail + /*creates the array for the possible y coordinates the fruit can have + in order to be always alligned with the snake and stay inside the field*/ + for (int i = 0; i < 9; i++) { + fruitY[i] = 3 + i*5; + } + score = 0; //initial score is 0 + a = 0; //used to select the direction based ont what button is pressed + ntail = 0; //used to increase lenght of the tail, initially set to 0 k = 0; - rx = rand() % 16; //choses a random number between 1 and 16, assigning it to the corresponding value in the for loop (x axis) - ry = rand() % 9; //same as the previous line but between 1 and 9 for the y axis - fruitX1 = fruitX[rx]+1; //this line and the following calculate the center - fruitY1 = fruitY[ry]+1; // point of the fruit needed to register the overlap with the head - x_pos.push_back(4); //adds initial x coordinate to the movement vector - y_pos.push_back(4); //adds initial y coordinate to the movement vector + rx = rand() % 16; //choses a random number between 1 and 16, assigning it to the corresponding value in the for loop (x axis) + ry = rand() % 9; //same as the previous line but between 1 and 9 for the y axis + fruitX1 = fruitX[rx]+1; //this line and the following calculate the center + fruitY1 = fruitY[ry]+1; // point of the fruit needed to register the overlap with the head + x_pos.push_back(4); //adds initial x coordinate to the movement vector + y_pos.push_back(4); //adds initial y coordinate to the movement vector } void Game::movement(Gamepad &pad) { - if (pad.Y_held() && !(a == 2)) { //registers what button is being pressed, - a = 1; //associating it to an integer value - } //between 1 and 4 - if (pad.A_held() && !(a == 1)) { //the second condition in each loop - a = 2; //is needed to prevent the snake from - } //going backwards on itself + /*registers what button is being pressed, + associating it to an integer value + between 1 and 4 + the second condition in each loop + is needed to prevent the snake from + going backwards on itself*/ + if (pad.Y_held() && !(a == 2)) { + a = 1; + } + if (pad.A_held() && !(a == 1)) { + a = 2; + } if (pad.X_held() && !(a == 4)) { a = 3; } @@ -45,17 +55,19 @@ void Game::updating_position() { - if (a == 1) { //reads the integer value stored in the prevoius function - x += -5; //and depending on its value decides which way the snake moves + /*reads the integer value stored in the prevoius function + and depending on its value decides which way the snake moves*/ + if (a == 1) { + x += -5; //left } if (a == 2) { - x += 5; + x += 5; //right } if (a == 3) { - y += -5; + y += -5; //up } if (a == 4) { - y += 5; + y += 5; //down } int prevX = x_pos[0]; @@ -64,15 +76,21 @@ x_pos[0] = x; y_pos[0] = y; - if (!(ntail + 1 == x_pos.size())) { //adds each new segment's coordinates to the - x_pos.push_back(x_pos[ntail - 1]); //movement vector in order to give eache - y_pos.push_back(y_pos[ntail - 1]); //section the same movement + /*adds each new segment's coordinates to the + movement vector in order to give each + section the same movement*/ + if (!(ntail + 1 == x_pos.size())) { + x_pos.push_back(x_pos[ntail - 1]); + y_pos.push_back(y_pos[ntail - 1]); } if (x_pos.size() > 0) { - for (int i = 0; i < x_pos.size() - 1; i++) { //regulates the movement of the snake - x_pos[x_pos.size() - i - 1] = x_pos[x_pos.size() - i - 2]; //having each section move to the previous - y_pos[y_pos.size() - i - 1] = y_pos[y_pos.size() - i - 2]; //coordinates of the segment in front of it + /*regulates the movement of the snake + having each section move to the previous + coordinates of the segment in front of it*/ + for (int i = 0; i < x_pos.size() - 1; i++) { + x_pos[x_pos.size() - i - 1] = x_pos[x_pos.size() - i - 2]; + y_pos[y_pos.size() - i - 1] = y_pos[y_pos.size() - i - 2]; } x_pos[1] = prevX; y_pos[1] = prevY; @@ -81,19 +99,23 @@ int Game::death(N5110 &lcd,Gamepad &pad) { - if (x < 1 || x > WIDTH-2) { //if you hit the side walls you die + //if you hit the side walls you die + if (x < 1 || x > WIDTH-2) { while (1) { gameover(lcd, pad); - if (pad.B_held()) { //if you press start you will go back to the menu + if (pad.B_held()) { + //if you press start you will go back to the menu return 1; } - if (pad.start_held()) { //if you press B, you will initiate a new game + if (pad.start_held()) { + //if you press B, you will initiate a new game return 2; } } } - - if (y < 1 || y > HEIGHT) { //if you hit the top or bottom walls you die + + //if you hit the top or bottom walls you die + if (y < 1 || y > HEIGHT) { while (1) { gameover(lcd, pad); if (pad.B_held()) { @@ -104,7 +126,8 @@ } } } - for (int i = 1; i < x_pos.size(); i++) { //if you hit any part of your tail you die + //if you hit any part of your tail you die + for (int i = 1; i < x_pos.size(); i++) { if (x_pos[0] == x_pos[i] && y_pos[0] == y_pos[i]) { while (1) { gameover(lcd, pad); @@ -117,18 +140,18 @@ } } } - return 0; + return 0; //if 0 is returned, there is no gamover and the game goes on } void Game::draw(N5110 &lcd,Gamepad &pad) { lcd.clear(); - lcd.drawRect(1,1,WIDTH-2,HEIGHT-1,FILL_TRANSPARENT); //rectangle around border of field - lcd.drawRect(fruitX[rx],fruitY[ry],3,3,FILL_BLACK); //fruit on the field - lcd.drawCircle(x,y,2,FILL_TRANSPARENT); //initial snake design + lcd.drawRect(1,1,WIDTH-2,HEIGHT-1,FILL_TRANSPARENT); //rectangle around border of field + lcd.drawRect(fruitX[rx],fruitY[ry],3,3,FILL_BLACK); //fruit on the field + lcd.drawCircle(x,y,2,FILL_TRANSPARENT); //initial snake design //printf("size of vector %d\n", x_pos.size()); for (k = 1; k <= ntail; k++) { - lcd.drawCircle(x_pos[k],y_pos[k],2,FILL_TRANSPARENT); //draws each new segment of the snake + lcd.drawCircle(x_pos[k],y_pos[k],2,FILL_TRANSPARENT); //draws each new segment of the snake //printf("draw \n"); } lcd.refresh(); @@ -142,12 +165,13 @@ void Game::gameover(N5110 &lcd,Gamepad &pad) { + //display of the gameover screen lcd.clear(); - lcd.printString("GAME OVER",WIDTH/2-25,0); //display of the gameover screen + lcd.printString("GAME OVER",WIDTH/2-25,0); sprintf (buffer, "Score: %d",score); - lcd.printString(buffer,WIDTH/2-26,2); //displays your score - lcd.printString("PLAY AGAIN: B",0,4); //if you want to play again press B - lcd.printString("MENU: START",17,5); //if you want to access the main menu press start + lcd.printString(buffer,WIDTH/2-26,2); //displays your score + lcd.printString("Play Again: B",0,4); //if you want to play again press B + lcd.printString("Menu: Start",0,5); //if you want to access the main menu press start pad.leds_on(); wait(0.1); pad.leds_off(); @@ -159,30 +183,36 @@ void Game::point(N5110 &lcd,Gamepad &pad) { - if (x == fruitX1 && y == fruitY1) { //if central coordinate of the snake head is equal to the central coordinate of the food: + /*if central coordinate of the snake head + is equal to the central coordinate of the food:*/ + if (x == fruitX1 && y == fruitY1) { //printf("Snake x: %d\n", x); //printf("Snake y: %d\n", y); //printf("seg1 x: %d\n", x_pos[1]); //printf("seg1 y: %d\n", y_pos[1]); //printf("Fruit x coordinate: %d\n", fruitX1); //printf("Fruit y coordinate: %d\n", fruitY1); - score = score + 10; //adds 10 to the score each time you eat a fruit + score = score + 10; //adds 10 to the score each time you eat a fruit pad.tone(750.0,1); - rx = rand() % 16; //generates a new random x coordinate for the food - ry = rand() % 9; //generates a new random x coordinate for the food + rx = rand() % 16; //generates a new random x coordinate for the food + ry = rand() % 9; //generates a new random x coordinate for the food //printf("rx: %d\n", rx); //printf("ry: %d\n", ry); fruitX1 = fruitX[rx]+1; fruitY1 = fruitY[ry]+1; for (int i = 0; i < x_pos.size(); i++) { - if (fruitX1 == x_pos[i] && fruitY1 == y_pos[i]) { //if the random set of coordinates - rx = rand() % 16; //corresponds to the position of one of the segments of the snake - ry = rand() % 9; //it finds a new set of coordinates + /*if the random set of coordinates + corresponds to the position of one of + the segments of the snake it finds + a new set of coordinates*/ + if (fruitX1 == x_pos[i] && fruitY1 == y_pos[i]) { + rx = rand() % 16; + ry = rand() % 9; fruitX1 = fruitX[rx]+1; fruitY1 = fruitY[ry]+1; } } - ntail++; //increases the lenght of the tail - lcd.drawRect(fruitX[rx],fruitY[ry],3,3,FILL_BLACK); //draws the new fruit + ntail++; //increases the lenght of the tail + lcd.drawRect(fruitX[rx],fruitY[ry],3,3,FILL_BLACK); //draws the new fruit } } \ No newline at end of file
--- a/Game/Game.h Tue May 26 18:14:03 2020 +0000 +++ b/Game/Game.h Tue May 26 22:53:42 2020 +0000 @@ -23,8 +23,8 @@ int fruitX [16]; int fruitY [9]; int score; - int a; //used to select the direction based ont what button is pressed - int ntail; //used to increase lenght of the tail + int a; //used to select the direction based ont what button is pressed + int ntail; //used to increase lenght of the tail int k; int rx; int ry; @@ -35,14 +35,41 @@ vector <int> y_pos; public: + /**Constructor*/ + Game();//initialises the game - Game(); - + /**Reguates the diretion of movement of the snake + * @param Gamepad class object + */ void movement(Gamepad &pad); + + /**Regulates the way the snake moves + */ void updating_position(); + + /**Get value of which button is pressed + *@ param N5110 class object + *@ param Gamepad class object + * @return death value (0,1,2) + */ int death(N5110 &lcd,Gamepad &pad); + + /**Draws the game field, the snake and the fruit + *@ param N5110 class object + *@ param Gamepad class object + */ void draw(N5110 &lcd,Gamepad &pad); + + /**Executes what happens when you die + *@ param N5110 class object + *@ param Gamepad class object + */ void gameover(N5110 &lcd,Gamepad &pad); + + /**Increases your points and your lenght when you score + *@ param N5110 class object + *@ param Gamepad class object + */ void point(N5110 &lcd,Gamepad &pad); };
--- a/Menu/Menu.cpp Tue May 26 18:14:03 2020 +0000 +++ b/Menu/Menu.cpp Tue May 26 22:53:42 2020 +0000 @@ -4,7 +4,7 @@ #include "N5110.h" //design of snake on initial page -int snake[28][26] { +int snake[28][26] { { 0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, { 0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, { 0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0 }, @@ -36,7 +36,7 @@ }; //design of skull in the help page -int skull[23][23] { +int skull[23][23] { { 0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0 }, { 0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0 }, { 1,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,1 }, @@ -71,7 +71,7 @@ { 0,0,0,0,1,0,0 }, }; - +//initialising the gamepad and the screen Menu::Menu(N5110 &lcd,Gamepad &pad) { _lcd = lcd; @@ -81,13 +81,14 @@ void Menu::initscreen() { while( _pad.start_held() == false) { + //initial page _lcd.clear(); - _lcd.drawSprite(29,1,28,26,(int*)snake); //initial page + _lcd.drawSprite(29,1,28,26,(int*)snake); _lcd.printString("SNAKE",WIDTH/2-15,4); _lcd.printString("Press Start",WIDTH/2-30,5); _lcd.setContrast( _pad.read_pot1()); _lcd.refresh(); - wait(1/6); + wait(0.2); } _lcd.clear(); } @@ -97,103 +98,142 @@ wait(0.1); _lcd.drawSprite(WIDTH/2-26,25,5,7,(int*)selector); _lcd.refresh(); - while(_pad.A_held() == false) { //unitl A is pressed, the code - arrow(); //stays on the menu, - _lcd.refresh(); //executing only the code of the arrow - wait(1/6); + while(_pad.A_held() == false) { + /*unitl A is pressed, the code + stays on the menu, + executing only the code of the arrow*/ + arrow(); + _lcd.refresh(); + wait(0.2); } - if (_lcd.getPixel(20,35)) { //if A is pressed and the arrow - help(); //is corresponding to HELP, - _lcd.refresh(); //the code executes help function - wait(1/6); + /*if A is pressed and the arrow + is corresponding to HELP, + the code executes help function*/ + if (_lcd.getPixel(20,35)) { + help(); + _lcd.refresh(); + wait(0.2); } } void Menu::arrow() { - _lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT); //main menu design + //main menu design + _lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT); _lcd.printString("MENU",WIDTH/2-15,1); _lcd.printString("Play",WIDTH/2-13,3); _lcd.printString("Help",WIDTH/2-13,4); _lcd.refresh(); - wait(1/6); + wait(0.2); - if ( _pad.X_held() == true) { //position of arrow in the meun - _lcd.clear(); //according to what button is pressed + /*position of arrow in the meun + according to what button is pressed*/ + if ( _pad.X_held() == true) { + _lcd.clear(); _lcd.drawSprite(WIDTH/2-26,25,5,7,(int*)selector); _lcd.refresh(); - wait(1/6); + wait(0.2); } - if ( _pad.B_held() == true) { //position of arrow in the meun - _lcd.clear(); //according to what button is pressed + /*position of arrow in the meun + position of arrow in the meun + according to what button is pressed*/ + if ( _pad.B_held() == true) { + _lcd.clear(); _lcd.drawSprite(WIDTH/2-26,33,5,7,(int*)selector); _lcd.refresh(); - wait(1/6); + wait(0.2); } } void Menu::help() { while(1) { - _lcd.clear(); //help instructions, page 1 + //help instructions, page 1 + _lcd.clear(); _lcd.printString("Use the",21,0); _lcd.printString("buttons",21,1); _lcd.printString("to move",21,2); _lcd.printString("(press B)",15,5); _lcd.refresh(); - wait(1/6); + wait(0.2); if (_pad.B_held()) { + //if B is pressed, go to next page break; } } wait(0.2); while(1) { - _lcd.clear(); //help instructions, page 2 - _lcd.printString("Eat the fruit",5,0); - _lcd.printString("to score",15,1); - _lcd.printString("(press B)",16,5); + //help instructions, page 2 + _lcd.clear(); + _lcd.printString("Use the",21,0); + _lcd.printString("potentiometer",3,1); + _lcd.printString("to control",13,2); + _lcd.printString("the speed",17,3); + _lcd.printString("(press B)",15,5); _lcd.refresh(); - wait(1/6); + wait(0.2); if (_pad.B_held()) { + //if B is pressed, go to next page break; } } wait(0.2); while(1) { - _lcd.clear(); //help instructions, page 3 - _lcd.printString("If you hit",13,0); - _lcd.printString("a wall",23,1); - _lcd.printString("or your tail",5,2); - _lcd.printString("...",WIDTH/2-7,3); + //help instructions, page 3 + _lcd.clear(); + _lcd.printString("Eat the fruit",5,0); + _lcd.printString("to score",15,1); _lcd.printString("(press B)",16,5); _lcd.refresh(); - wait(1/6); + wait(0.2); if (_pad.B_held()) { + //if B is pressed, go to next page break; } } wait(0.2); while(1) { - _lcd.clear(); //help instructions, final page + //help instructions, page 4 + _lcd.clear(); + _lcd.printString("If you hit",13,0); + _lcd.printString("a wall",23,1); + _lcd.printString("or your tail",5,2); + _lcd.printString("...",WIDTH/2-7,3); + _lcd.printString("(press B)",16,5); + _lcd.refresh(); + wait(0.2); + + if (_pad.B_held()) { + //if B is pressed, go to next page + break; + } + } + wait(0.2); + + while(1) { + //help instructions, final page + _lcd.clear(); _lcd.printString("YOU DIE!",20,1); _lcd.drawSprite(30,16,23,23,(int*)skull); _lcd.refresh(); - wait(1/6); + wait(0.2); if (_pad.B_held()) { + //if B is pressed, go back to the menu break; } } - wait(1/6); + wait(0.2); + //after help instructions, returns to main menu _lcd.clear(); - menu_screen(); //after help instructions, returns to main menu + menu_screen(); _lcd.refresh(); - wait(1/6); + wait(0.2); } \ No newline at end of file
--- a/Menu/Menu.h Tue May 26 18:14:03 2020 +0000 +++ b/Menu/Menu.h Tue May 26 22:53:42 2020 +0000 @@ -15,17 +15,33 @@ { private: - + //classes N5110 _lcd; Gamepad _pad; public: - + //functions + + /**Constructor + *@param N5110 object + *@param Gamepad object + */ Menu(N5110 &lcd,Gamepad &pad); + /**Make initial screen + */ void initscreen(); + + /**Make the menu screen + */ void menu_screen(); + + /**Make the arrow in the menu + */ void arrow(); + + /**Make the help function + */ void help(); };
--- a/main.cpp Tue May 26 18:14:03 2020 +0000 +++ b/main.cpp Tue May 26 22:53:42 2020 +0000 @@ -1,4 +1,4 @@ -/* +/* ELEC2645 Embedded Systems Project School of Electronic & Electrical Engineering University of Leeds @@ -10,6 +10,7 @@ Date: 10/03/2020 */ +// includes #include "mbed.h" #include "Gamepad.h" #include "N5110.h" @@ -17,27 +18,30 @@ #include "Menu.h" #include "Game.h" +//objects N5110 lcd; Gamepad pad; Menu menu(lcd,pad); -//prototypes +//functions void init(); void game_function(); //variables int g_check = 0; -// functions + int main() { + //initialises the lcd and the gamepad init(); - lcd.clear(); + //creates the initial screen menu.initscreen(); - + //infinite loop for the game and menu while(1) { + //checks if the gameover function is returning new game or menu if (g_check == 2 || g_check == 0) { menu.menu_screen(); game_function(); @@ -48,12 +52,13 @@ } void init() { - lcd.init(); - pad.init(); + lcd.init();//initialises the screen + pad.init();//initialises the gamepad } void game_function() { + //creates class objects Game game; game.movement(pad); game.draw(lcd, pad); @@ -66,19 +71,26 @@ //printf("updating_position"); game.draw(lcd, pad); //printf("draw\n"); + /*this part is executed if you die, and + chose to return to the main menu*/ if (game.death(lcd, pad) == 2) { - g_check = 2; //this part is executed if you die, and - break; //chose to return to the main menu + g_check = 2; + break; + /*this part is executed if you die, and + chose to play again*/ } else if (game.death(lcd, pad) == 1) { - g_check = 1; //this part is executed if you die, and - break; //chose to play again + g_check = 1; + break; } //printf("death\n"); game.point(lcd, pad); //printf("point\n"); lcd.refresh(); - for(int i = 0; i < 25; i++) { - wait(pad.read_pot2()/50); + /*based on the value of the potentiometer + the refresh rate can be increased and + by doing so you can increase the speed*/ + for(int i = 0; i < pad.read_pot2()*50; i++) { + wait(0.01); game.movement(pad); } }