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:
- 7:8d381315f72c
- Parent:
- 6:a2c72def99f9
--- 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