#include #include #include #include #include "Snake.h" using namespace std; bool hi = true; Snake bdy; void Snake::movement(char input){ // simple get input to change direction of snake. if((input == 'w'|| input == 'W')&& direction != 3 ) direction = 1; else if((input == 'a'|| input == 'A')&& direction != 2 ) direction = 4; else if((input == 's'|| input == 'S')&& direction != 1 ) direction = 3; else if((input == 'd'|| input == 'D')&& direction != 4 ) direction = 2; // moves the first body part that exists on top of the head if(direction == 1){ x--; parts[0].changeX(1); } else if(direction == 4){ y--; parts[0].changeY(1); } else if(direction == 3){ x++; parts[0].changeX(0); } else if(direction == 2){ y++; parts[0].changeY(0); } for (int count = 0; count < parts.size(); count++){ if (count != 0) parts[count].follow(count, this); } } bool Snake::draw(){ cout << "Your score is : " << score << endl; for (int i = 0; i < 29; i++){ for (int q = 0; q < 60; q++){ if (x < 1)return false; // end the loop if it hits a wall if (y < 1)return false; if (x > 26)return false; if (y > 57)return false; grid[i][q] = whtSpace; // each square is set to a white space if (h == i || o == q) // displayes the apple grid[h][o] = '*'; if (i == 27 || i == 0)// top and bottom wall grid[i][q] = '-'; if(q == 0 || q == 59)// side walls grid[i][q] = '|'; for ( int count = 0; count < parts.size(); count++){ if (grid[parts[0].getX()][parts[0].getY()] == '-') return false; // if a body connects with another you lose if (count == 0){// display for head if( direction == 2) grid[parts[count].getX()][parts[count].getY()] = '>'; else if ( direction == 3) grid[parts[count].getX()][parts[count].getY()] = 'v'; else if( direction == 4) grid[parts[count].getX()][parts[count].getY()] = '<'; else if( direction == 1) grid[parts[count].getX()][parts[count].getY()] = '^'; else grid[parts[count].getX()][parts[count].getY()] = 'O'; } else // body parts look like grid[parts[count].getX()][parts[count].getY()] = '-'; } } } // this is to draw the board for (int i = 0; i < 29; i++){ cout << endl; for (int q = 0; q < 60; q++){ cout << grid[i][q] << " "; } } if (grid [x][y] == grid[h][o] ){ // randomizes the apples h = (rand() % 26) + 1; o = (rand() % 57) + 1; score++; newBody(); bonusBody = bonusBody + 5; }else if (bonusBody > 0){ // adds body parts every loop after picking up apples newBody(); bonusBody--; } return hi; } int Snake::snekoScore(){ return score; } void Snake::clrScreen(){ for (int p = 0; p < 40; p++){ cout << endl; } } int Snake::getX(){ return x; } int Snake::getY(){ return y; } void Snake::newBody(){ Body segment (x,y); parts.push_back(segment); } void Snake::GameOver(){ for (int p = 0; p < 40; p++){ cout << endl; } cout << " _____ ____"<< endl << " / ____| / __ | " << endl << " | | __ __ _ _ __ ___ ___ | | | |_ _____ _ __" << endl << " | | |_ |/ _` | '_ ` _ | / _ | | | | || | / / _ | '__|" << endl << " | |__| | (_| | | | | | | __/ | |__| || V / __/ |" << endl << " |_____||__,_|_| |_| |_||___| |____/ |_/ |___|_|" << endl; } void Body::changeX(bool updown){ lastX = x; lastY = y; if (updown) x--; else x++; } void Body::changeY(bool updown){ lastX = x; lastY = y; if (updown) y--; else y++; } Body::Body(int xCord, int yCord){ x = xCord; y = yCord; } int Body::getX(){ return x; } int Body::getY(){ return y; } int Body::getlastX(){ return lastX; } int Body::getlastY(){ return lastY; } void Body::follow(int count, Snake* snake){ lastX = x; lastY = y; x = snake->getPart(count - 1).getlastX(); y = snake->getPart(count - 1).getlastY(); }