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
Game/Game.cpp@6:a2c72def99f9, 2020-05-26 (annotated)
- Committer:
- ale_carb0ni
- Date:
- Tue May 26 18:14:03 2020 +0000
- Revision:
- 6:a2c72def99f9
- Parent:
- 4:17d5b53b8815
- Child:
- 7:8d381315f72c
commented version;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ale_carb0ni | 2:7fa08670b1fc | 1 | #include "Game.h" |
ale_carb0ni | 2:7fa08670b1fc | 2 | #include "mbed.h" |
ale_carb0ni | 2:7fa08670b1fc | 3 | #include "Gamepad.h" |
ale_carb0ni | 2:7fa08670b1fc | 4 | #include "N5110.h" |
ale_carb0ni | 2:7fa08670b1fc | 5 | #include "Menu.h" |
ale_carb0ni | 2:7fa08670b1fc | 6 | |
ale_carb0ni | 6:a2c72def99f9 | 7 | /**Constructor*/ |
ale_carb0ni | 2:7fa08670b1fc | 8 | Game::Game() |
ale_carb0ni | 2:7fa08670b1fc | 9 | { |
ale_carb0ni | 2:7fa08670b1fc | 10 | x = 4; |
ale_carb0ni | 2:7fa08670b1fc | 11 | y = 4; |
ale_carb0ni | 6:a2c72def99f9 | 12 | for (int i = 0; i < 16; i++) { //this creates the possible x coordinates the fruit can have |
ale_carb0ni | 6:a2c72def99f9 | 13 | fruitX[i] = 3 + i*5; //in order to be always alligned with the snake and stay inside the field |
ale_carb0ni | 2:7fa08670b1fc | 14 | } |
ale_carb0ni | 6:a2c72def99f9 | 15 | for (int i = 0; i < 9; i++) { //this creates the possible y coordinates the fruit can have |
ale_carb0ni | 6:a2c72def99f9 | 16 | fruitY[i] = 3 + i*5; //in order to be always alligned with the snake and stay inside the field |
ale_carb0ni | 2:7fa08670b1fc | 17 | } |
ale_carb0ni | 6:a2c72def99f9 | 18 | score = 0; //initial score is 0 |
ale_carb0ni | 2:7fa08670b1fc | 19 | a = 0; //used to select the direction based ont what button is pressed |
ale_carb0ni | 2:7fa08670b1fc | 20 | ntail = 0; //used to increase lenght of the tail |
ale_carb0ni | 2:7fa08670b1fc | 21 | k = 0; |
ale_carb0ni | 6:a2c72def99f9 | 22 | rx = rand() % 16; //choses a random number between 1 and 16, assigning it to the corresponding value in the for loop (x axis) |
ale_carb0ni | 6:a2c72def99f9 | 23 | ry = rand() % 9; //same as the previous line but between 1 and 9 for the y axis |
ale_carb0ni | 6:a2c72def99f9 | 24 | fruitX1 = fruitX[rx]+1; //this line and the following calculate the center |
ale_carb0ni | 6:a2c72def99f9 | 25 | fruitY1 = fruitY[ry]+1; // point of the fruit needed to register the overlap with the head |
ale_carb0ni | 6:a2c72def99f9 | 26 | x_pos.push_back(4); //adds initial x coordinate to the movement vector |
ale_carb0ni | 6:a2c72def99f9 | 27 | y_pos.push_back(4); //adds initial y coordinate to the movement vector |
ale_carb0ni | 2:7fa08670b1fc | 28 | } |
ale_carb0ni | 2:7fa08670b1fc | 29 | |
ale_carb0ni | 2:7fa08670b1fc | 30 | void Game::movement(Gamepad &pad) |
ale_carb0ni | 2:7fa08670b1fc | 31 | { |
ale_carb0ni | 6:a2c72def99f9 | 32 | if (pad.Y_held() && !(a == 2)) { //registers what button is being pressed, |
ale_carb0ni | 6:a2c72def99f9 | 33 | a = 1; //associating it to an integer value |
ale_carb0ni | 6:a2c72def99f9 | 34 | } //between 1 and 4 |
ale_carb0ni | 6:a2c72def99f9 | 35 | if (pad.A_held() && !(a == 1)) { //the second condition in each loop |
ale_carb0ni | 6:a2c72def99f9 | 36 | a = 2; //is needed to prevent the snake from |
ale_carb0ni | 6:a2c72def99f9 | 37 | } //going backwards on itself |
ale_carb0ni | 2:7fa08670b1fc | 38 | if (pad.X_held() && !(a == 4)) { |
ale_carb0ni | 2:7fa08670b1fc | 39 | a = 3; |
ale_carb0ni | 2:7fa08670b1fc | 40 | } |
ale_carb0ni | 2:7fa08670b1fc | 41 | if (pad.B_held() && !(a == 3)) { |
ale_carb0ni | 2:7fa08670b1fc | 42 | a = 4; |
ale_carb0ni | 2:7fa08670b1fc | 43 | } |
ale_carb0ni | 4:17d5b53b8815 | 44 | } |
ale_carb0ni | 4:17d5b53b8815 | 45 | |
ale_carb0ni | 4:17d5b53b8815 | 46 | void Game::updating_position() |
ale_carb0ni | 4:17d5b53b8815 | 47 | { |
ale_carb0ni | 6:a2c72def99f9 | 48 | if (a == 1) { //reads the integer value stored in the prevoius function |
ale_carb0ni | 6:a2c72def99f9 | 49 | x += -5; //and depending on its value decides which way the snake moves |
ale_carb0ni | 2:7fa08670b1fc | 50 | } |
ale_carb0ni | 2:7fa08670b1fc | 51 | if (a == 2) { |
ale_carb0ni | 2:7fa08670b1fc | 52 | x += 5; |
ale_carb0ni | 2:7fa08670b1fc | 53 | } |
ale_carb0ni | 2:7fa08670b1fc | 54 | if (a == 3) { |
ale_carb0ni | 2:7fa08670b1fc | 55 | y += -5; |
ale_carb0ni | 2:7fa08670b1fc | 56 | } |
ale_carb0ni | 2:7fa08670b1fc | 57 | if (a == 4) { |
ale_carb0ni | 2:7fa08670b1fc | 58 | y += 5; |
ale_carb0ni | 2:7fa08670b1fc | 59 | } |
ale_carb0ni | 2:7fa08670b1fc | 60 | |
ale_carb0ni | 2:7fa08670b1fc | 61 | int prevX = x_pos[0]; |
ale_carb0ni | 2:7fa08670b1fc | 62 | int prevY = y_pos[0]; |
ale_carb0ni | 2:7fa08670b1fc | 63 | |
ale_carb0ni | 2:7fa08670b1fc | 64 | x_pos[0] = x; |
ale_carb0ni | 2:7fa08670b1fc | 65 | y_pos[0] = y; |
ale_carb0ni | 2:7fa08670b1fc | 66 | |
ale_carb0ni | 6:a2c72def99f9 | 67 | if (!(ntail + 1 == x_pos.size())) { //adds each new segment's coordinates to the |
ale_carb0ni | 6:a2c72def99f9 | 68 | x_pos.push_back(x_pos[ntail - 1]); //movement vector in order to give eache |
ale_carb0ni | 6:a2c72def99f9 | 69 | y_pos.push_back(y_pos[ntail - 1]); //section the same movement |
ale_carb0ni | 2:7fa08670b1fc | 70 | } |
ale_carb0ni | 2:7fa08670b1fc | 71 | |
ale_carb0ni | 2:7fa08670b1fc | 72 | if (x_pos.size() > 0) { |
ale_carb0ni | 6:a2c72def99f9 | 73 | for (int i = 0; i < x_pos.size() - 1; i++) { //regulates the movement of the snake |
ale_carb0ni | 6:a2c72def99f9 | 74 | x_pos[x_pos.size() - i - 1] = x_pos[x_pos.size() - i - 2]; //having each section move to the previous |
ale_carb0ni | 6:a2c72def99f9 | 75 | y_pos[y_pos.size() - i - 1] = y_pos[y_pos.size() - i - 2]; //coordinates of the segment in front of it |
ale_carb0ni | 2:7fa08670b1fc | 76 | } |
ale_carb0ni | 2:7fa08670b1fc | 77 | x_pos[1] = prevX; |
ale_carb0ni | 2:7fa08670b1fc | 78 | y_pos[1] = prevY; |
ale_carb0ni | 2:7fa08670b1fc | 79 | } |
ale_carb0ni | 2:7fa08670b1fc | 80 | } |
ale_carb0ni | 2:7fa08670b1fc | 81 | |
ale_carb0ni | 3:c61d0c70eda4 | 82 | int Game::death(N5110 &lcd,Gamepad &pad) |
ale_carb0ni | 2:7fa08670b1fc | 83 | { |
ale_carb0ni | 2:7fa08670b1fc | 84 | if (x < 1 || x > WIDTH-2) { //if you hit the side walls you die |
ale_carb0ni | 2:7fa08670b1fc | 85 | while (1) { |
ale_carb0ni | 2:7fa08670b1fc | 86 | gameover(lcd, pad); |
ale_carb0ni | 6:a2c72def99f9 | 87 | if (pad.B_held()) { //if you press start you will go back to the menu |
ale_carb0ni | 6:a2c72def99f9 | 88 | return 1; |
ale_carb0ni | 2:7fa08670b1fc | 89 | } |
ale_carb0ni | 6:a2c72def99f9 | 90 | if (pad.start_held()) { //if you press B, you will initiate a new game |
ale_carb0ni | 6:a2c72def99f9 | 91 | return 2; |
ale_carb0ni | 2:7fa08670b1fc | 92 | } |
ale_carb0ni | 2:7fa08670b1fc | 93 | } |
ale_carb0ni | 2:7fa08670b1fc | 94 | } |
ale_carb0ni | 2:7fa08670b1fc | 95 | |
ale_carb0ni | 2:7fa08670b1fc | 96 | if (y < 1 || y > HEIGHT) { //if you hit the top or bottom walls you die |
ale_carb0ni | 2:7fa08670b1fc | 97 | while (1) { |
ale_carb0ni | 2:7fa08670b1fc | 98 | gameover(lcd, pad); |
ale_carb0ni | 2:7fa08670b1fc | 99 | if (pad.B_held()) { |
ale_carb0ni | 6:a2c72def99f9 | 100 | return 1; |
ale_carb0ni | 2:7fa08670b1fc | 101 | } |
ale_carb0ni | 2:7fa08670b1fc | 102 | if (pad.start_held()) { |
ale_carb0ni | 6:a2c72def99f9 | 103 | return 2; |
ale_carb0ni | 2:7fa08670b1fc | 104 | } |
ale_carb0ni | 2:7fa08670b1fc | 105 | } |
ale_carb0ni | 2:7fa08670b1fc | 106 | } |
ale_carb0ni | 6:a2c72def99f9 | 107 | for (int i = 1; i < x_pos.size(); i++) { //if you hit any part of your tail you die |
ale_carb0ni | 2:7fa08670b1fc | 108 | if (x_pos[0] == x_pos[i] && y_pos[0] == y_pos[i]) { |
ale_carb0ni | 2:7fa08670b1fc | 109 | while (1) { |
ale_carb0ni | 2:7fa08670b1fc | 110 | gameover(lcd, pad); |
ale_carb0ni | 2:7fa08670b1fc | 111 | if (pad.B_held()) { |
ale_carb0ni | 6:a2c72def99f9 | 112 | return 1; |
ale_carb0ni | 2:7fa08670b1fc | 113 | } |
ale_carb0ni | 2:7fa08670b1fc | 114 | if (pad.start_held()) { |
ale_carb0ni | 6:a2c72def99f9 | 115 | return 2; |
ale_carb0ni | 2:7fa08670b1fc | 116 | } |
ale_carb0ni | 2:7fa08670b1fc | 117 | } |
ale_carb0ni | 2:7fa08670b1fc | 118 | } |
ale_carb0ni | 2:7fa08670b1fc | 119 | } |
ale_carb0ni | 3:c61d0c70eda4 | 120 | return 0; |
ale_carb0ni | 2:7fa08670b1fc | 121 | } |
ale_carb0ni | 2:7fa08670b1fc | 122 | |
ale_carb0ni | 2:7fa08670b1fc | 123 | void Game::draw(N5110 &lcd,Gamepad &pad) |
ale_carb0ni | 2:7fa08670b1fc | 124 | { |
ale_carb0ni | 2:7fa08670b1fc | 125 | lcd.clear(); |
ale_carb0ni | 2:7fa08670b1fc | 126 | lcd.drawRect(1,1,WIDTH-2,HEIGHT-1,FILL_TRANSPARENT); //rectangle around border of field |
ale_carb0ni | 2:7fa08670b1fc | 127 | lcd.drawRect(fruitX[rx],fruitY[ry],3,3,FILL_BLACK); //fruit on the field |
ale_carb0ni | 2:7fa08670b1fc | 128 | lcd.drawCircle(x,y,2,FILL_TRANSPARENT); //initial snake design |
ale_carb0ni | 2:7fa08670b1fc | 129 | //printf("size of vector %d\n", x_pos.size()); |
ale_carb0ni | 2:7fa08670b1fc | 130 | for (k = 1; k <= ntail; k++) { |
ale_carb0ni | 6:a2c72def99f9 | 131 | lcd.drawCircle(x_pos[k],y_pos[k],2,FILL_TRANSPARENT); //draws each new segment of the snake |
ale_carb0ni | 2:7fa08670b1fc | 132 | //printf("draw \n"); |
ale_carb0ni | 2:7fa08670b1fc | 133 | } |
ale_carb0ni | 6:a2c72def99f9 | 134 | lcd.refresh(); |
ale_carb0ni | 2:7fa08670b1fc | 135 | //printf("Fruit x coordinate: %d\n", fruitX1); |
ale_carb0ni | 2:7fa08670b1fc | 136 | //printf("Fruit y coordinate: %d\n", fruitY1); |
ale_carb0ni | 2:7fa08670b1fc | 137 | //printf("Snake x: %d\n", x); |
ale_carb0ni | 2:7fa08670b1fc | 138 | //printf("Snake y: %d\n", y); |
ale_carb0ni | 2:7fa08670b1fc | 139 | //printf("seg1 x: %d\n", x_pos[1]); |
ale_carb0ni | 2:7fa08670b1fc | 140 | //printf("seg1 y: %d\n", y_pos[1]); |
ale_carb0ni | 2:7fa08670b1fc | 141 | } |
ale_carb0ni | 2:7fa08670b1fc | 142 | |
ale_carb0ni | 2:7fa08670b1fc | 143 | void Game::gameover(N5110 &lcd,Gamepad &pad) |
ale_carb0ni | 2:7fa08670b1fc | 144 | { |
ale_carb0ni | 2:7fa08670b1fc | 145 | lcd.clear(); |
ale_carb0ni | 6:a2c72def99f9 | 146 | lcd.printString("GAME OVER",WIDTH/2-25,0); //display of the gameover screen |
ale_carb0ni | 2:7fa08670b1fc | 147 | sprintf (buffer, "Score: %d",score); |
ale_carb0ni | 6:a2c72def99f9 | 148 | lcd.printString(buffer,WIDTH/2-26,2); //displays your score |
ale_carb0ni | 6:a2c72def99f9 | 149 | lcd.printString("PLAY AGAIN: B",0,4); //if you want to play again press B |
ale_carb0ni | 6:a2c72def99f9 | 150 | lcd.printString("MENU: START",17,5); //if you want to access the main menu press start |
ale_carb0ni | 2:7fa08670b1fc | 151 | pad.leds_on(); |
ale_carb0ni | 2:7fa08670b1fc | 152 | wait(0.1); |
ale_carb0ni | 2:7fa08670b1fc | 153 | pad.leds_off(); |
ale_carb0ni | 2:7fa08670b1fc | 154 | wait(0.1); |
ale_carb0ni | 2:7fa08670b1fc | 155 | pad.tone(500.0,1); |
ale_carb0ni | 2:7fa08670b1fc | 156 | lcd.refresh(); |
ale_carb0ni | 2:7fa08670b1fc | 157 | wait(1/6); |
ale_carb0ni | 2:7fa08670b1fc | 158 | } |
ale_carb0ni | 2:7fa08670b1fc | 159 | |
ale_carb0ni | 2:7fa08670b1fc | 160 | void Game::point(N5110 &lcd,Gamepad &pad) |
ale_carb0ni | 2:7fa08670b1fc | 161 | { |
ale_carb0ni | 2:7fa08670b1fc | 162 | if (x == fruitX1 && y == fruitY1) { //if central coordinate of the snake head is equal to the central coordinate of the food: |
ale_carb0ni | 2:7fa08670b1fc | 163 | //printf("Snake x: %d\n", x); |
ale_carb0ni | 2:7fa08670b1fc | 164 | //printf("Snake y: %d\n", y); |
ale_carb0ni | 2:7fa08670b1fc | 165 | //printf("seg1 x: %d\n", x_pos[1]); |
ale_carb0ni | 2:7fa08670b1fc | 166 | //printf("seg1 y: %d\n", y_pos[1]); |
ale_carb0ni | 2:7fa08670b1fc | 167 | //printf("Fruit x coordinate: %d\n", fruitX1); |
ale_carb0ni | 2:7fa08670b1fc | 168 | //printf("Fruit y coordinate: %d\n", fruitY1); |
ale_carb0ni | 6:a2c72def99f9 | 169 | score = score + 10; //adds 10 to the score each time you eat a fruit |
ale_carb0ni | 2:7fa08670b1fc | 170 | pad.tone(750.0,1); |
ale_carb0ni | 6:a2c72def99f9 | 171 | rx = rand() % 16; //generates a new random x coordinate for the food |
ale_carb0ni | 6:a2c72def99f9 | 172 | ry = rand() % 9; //generates a new random x coordinate for the food |
ale_carb0ni | 2:7fa08670b1fc | 173 | //printf("rx: %d\n", rx); |
ale_carb0ni | 2:7fa08670b1fc | 174 | //printf("ry: %d\n", ry); |
ale_carb0ni | 2:7fa08670b1fc | 175 | fruitX1 = fruitX[rx]+1; |
ale_carb0ni | 2:7fa08670b1fc | 176 | fruitY1 = fruitY[ry]+1; |
ale_carb0ni | 2:7fa08670b1fc | 177 | for (int i = 0; i < x_pos.size(); i++) { |
ale_carb0ni | 6:a2c72def99f9 | 178 | if (fruitX1 == x_pos[i] && fruitY1 == y_pos[i]) { //if the random set of coordinates |
ale_carb0ni | 6:a2c72def99f9 | 179 | rx = rand() % 16; //corresponds to the position of one of the segments of the snake |
ale_carb0ni | 6:a2c72def99f9 | 180 | ry = rand() % 9; //it finds a new set of coordinates |
ale_carb0ni | 2:7fa08670b1fc | 181 | fruitX1 = fruitX[rx]+1; |
ale_carb0ni | 2:7fa08670b1fc | 182 | fruitY1 = fruitY[ry]+1; |
ale_carb0ni | 2:7fa08670b1fc | 183 | } |
ale_carb0ni | 2:7fa08670b1fc | 184 | } |
ale_carb0ni | 6:a2c72def99f9 | 185 | ntail++; //increases the lenght of the tail |
ale_carb0ni | 6:a2c72def99f9 | 186 | lcd.drawRect(fruitX[rx],fruitY[ry],3,3,FILL_BLACK); //draws the new fruit |
ale_carb0ni | 2:7fa08670b1fc | 187 | } |
ale_carb0ni | 2:7fa08670b1fc | 188 | } |