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
Diff: Game/Game.cpp
- Revision:
- 6:a2c72def99f9
- Parent:
- 4:17d5b53b8815
- Child:
- 7:8d381315f72c
diff -r a919651ebf36 -r a2c72def99f9 Game/Game.cpp --- a/Game/Game.cpp Tue May 26 17:14:44 2020 +0000 +++ b/Game/Game.cpp Tue May 26 18:14:03 2020 +0000 @@ -4,37 +4,37 @@ #include "N5110.h" #include "Menu.h" - +/**Constructor*/ Game::Game() { x = 4; y = 4; - for (int i = 0; i < 16; i++) { - fruitX[i] = 3 + i*5; + 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++) { - fruitY[i] = 3 + i*5; + 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 } - score = 0; + 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 k = 0; - rx = rand() % 16; - ry = rand() % 9; - fruitX1 = fruitX[rx]+1; - fruitY1 = fruitY[ry]+1; - x_pos.push_back(4); - y_pos.push_back(4); + 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)) { - a = 1; - } - if (pad.A_held() && !(a == 1)) { - a = 2; - } + 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 if (pad.X_held() && !(a == 4)) { a = 3; } @@ -45,21 +45,17 @@ void Game::updating_position() { - if (a == 1) { - x += -5; - //wait(0.25); + 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 } if (a == 2) { x += 5; - //wait(0.25); } if (a == 3) { y += -5; - //wait(0.25); } if (a == 4) { y += 5; - //wait(0.25); } int prevX = x_pos[0]; @@ -68,15 +64,15 @@ x_pos[0] = x; y_pos[0] = y; - if (!(ntail + 1 == x_pos.size())) { - x_pos.push_back(x_pos[ntail - 1]); - y_pos.push_back(y_pos[ntail - 1]); + 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 } if (x_pos.size() > 0) { - 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]; + 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 } x_pos[1] = prevX; y_pos[1] = prevY; @@ -88,11 +84,11 @@ if (x < 1 || x > WIDTH-2) { //if you hit the side walls you die while (1) { gameover(lcd, pad); - if (pad.B_held()) { - return 2; + if (pad.B_held()) { //if you press start you will go back to the menu + return 1; } - if (pad.start_held()) { - return 1; + if (pad.start_held()) { //if you press B, you will initiate a new game + return 2; } } } @@ -101,22 +97,22 @@ while (1) { gameover(lcd, pad); if (pad.B_held()) { - return 2; + return 1; } if (pad.start_held()) { - return 1; + return 2; } } } - for (int i = 1; i < x_pos.size(); i++) { //if you hit your tail you die + for (int i = 1; i < x_pos.size(); i++) { //if you hit any part of your tail you die if (x_pos[0] == x_pos[i] && y_pos[0] == y_pos[i]) { while (1) { gameover(lcd, pad); if (pad.B_held()) { - return 2; + return 1; } if (pad.start_held()) { - return 1; + return 2; } } } @@ -132,28 +128,26 @@ 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); + lcd.drawCircle(x_pos[k],y_pos[k],2,FILL_TRANSPARENT); //draws each new segment of the snake //printf("draw \n"); } - //printf("/////////////// \n"); + lcd.refresh(); //printf("Fruit x coordinate: %d\n", fruitX1); //printf("Fruit y coordinate: %d\n", 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]); - lcd.refresh(); } void Game::gameover(N5110 &lcd,Gamepad &pad) { lcd.clear(); - //while (1) { - lcd.printString("GAME OVER",WIDTH/2-25,0); + lcd.printString("GAME OVER",WIDTH/2-25,0); //display of the gameover screen sprintf (buffer, "Score: %d",score); - lcd.printString(buffer,WIDTH/2-26,2); - lcd.printString("TO PLAY AGAIN",3,4); - lcd.printString("PRESS B",17,5); + 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 pad.leds_on(); wait(0.1); pad.leds_off(); @@ -161,15 +155,6 @@ pad.tone(500.0,1); lcd.refresh(); wait(1/6); - // if ( _pad.B_held()) { - //break; - //} - //} - /*wait(1/6); - _lcd.clear(); - //_menu.menu_screen(); - _lcd.refresh(); - wait(1/6);*/ } void Game::point(N5110 &lcd,Gamepad &pad) @@ -181,25 +166,23 @@ //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; + 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 coordinate for the food - ry = rand() % 9; + 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]) { - rx = rand() % 16; - ry = rand() % 9; + 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 fruitX1 = fruitX[rx]+1; fruitY1 = fruitY[ry]+1; } } - ntail++; - lcd.drawRect(fruitX[rx],fruitY[ry],3,3,FILL_BLACK); - //_lcd.refresh(); - //wait(1/6); + 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