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
- Committer:
- ale_carb0ni
- Date:
- 2020-05-26
- Revision:
- 3:c61d0c70eda4
- Parent:
- 2:7fa08670b1fc
- Child:
- 4:17d5b53b8815
File content as of revision 3:c61d0c70eda4:
#include "Game.h" #include "mbed.h" #include "Gamepad.h" #include "N5110.h" #include "Menu.h" Game::Game() { x = 4; y = 4; for (int i = 0; i < 16; i++) { fruitX[i] = 3 + i*5; } for (int i = 0; i < 9; i++) { fruitY[i] = 3 + i*5; } score = 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); } void Game::movement(Gamepad &pad) { if (pad.Y_held() && !(a == 2)) { a = 1; } if (pad.A_held() && !(a == 1)) { a = 2; } if (pad.X_held() && !(a == 4)) { a = 3; } if (pad.B_held() && !(a == 3)) { a = 4; } if (a == 1) { x += -5; //wait(0.25); } 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]; int prevY = y_pos[0]; 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 (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]; } x_pos[1] = prevX; y_pos[1] = prevY; } } int Game::death(N5110 &lcd,Gamepad &pad) { 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.start_held()) { return 1; } } } if (y < 1 || y > HEIGHT) { //if you hit the top or bottom walls you die while (1) { gameover(lcd, pad); if (pad.B_held()) { return 2; } if (pad.start_held()) { return 1; } } } for (int i = 1; i < x_pos.size(); i++) { //if you hit 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; } if (pad.start_held()) { return 1; } } } } return 0; } 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 //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); //printf("draw \n"); } //printf("/////////////// \n"); //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); 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); pad.leds_on(); wait(0.1); pad.leds_off(); wait(0.1); 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) { if (x == fruitX1 && y == fruitY1) { //if central coordinate of the snake head is equal to the central coordinate of the food: //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; pad.tone(750.0,1); rx = rand() % 16; //generates a new random coordinate for the food ry = rand() % 9; //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; fruitX1 = fruitX[rx]+1; fruitY1 = fruitY[ry]+1; } } ntail++; lcd.drawRect(fruitX[rx],fruitY[ry],3,3,FILL_BLACK); //_lcd.refresh(); //wait(1/6); } }