|
| 1 | +#include <iostream> |
| 2 | +#include <vector> |
| 3 | +#include <conio.h> |
| 4 | +#include <windows.h> |
| 5 | +#include <cstdlib> |
| 6 | +#include <ctime> |
| 7 | + |
| 8 | +using namespace std; |
| 9 | + |
| 10 | +/* |
| 11 | +Snake Game in C++ (Console Version) |
| 12 | +
|
| 13 | +How to Play: |
| 14 | +- W → Up, A → Left, S → Down, D → Right |
| 15 | +- Eat the food (*) to grow and increase your score |
| 16 | +Snake head = @, body = o, food = * |
| 17 | +- Avoid walls and your own body |
| 18 | +- After Game Over, press Y to play again |
| 19 | +*/ |
| 20 | + |
| 21 | +class Snake { |
| 22 | +private: |
| 23 | + vector<pair<int,int>> body; |
| 24 | + char direction; |
| 25 | +public: |
| 26 | + Snake(int startX, int startY) { |
| 27 | + body.push_back({startX, startY}); |
| 28 | + direction = 'D'; |
| 29 | + } |
| 30 | + pair<int,int> getHead() { return body.front(); } |
| 31 | + vector<pair<int,int>> getBody() { return body; } |
| 32 | + char getDirection() { return direction; } |
| 33 | + void setDirection(char dir) { direction = dir; } |
| 34 | + void move(bool grow=false) { |
| 35 | + pair<int,int> head = body.front(); |
| 36 | + switch(direction) { |
| 37 | + case 'W': head.first--; break; |
| 38 | + case 'S': head.first++; break; |
| 39 | + case 'A': head.second--; break; |
| 40 | + case 'D': head.second++; break; |
| 41 | + } |
| 42 | + body.insert(body.begin(), head); |
| 43 | + if(!grow) body.pop_back(); |
| 44 | + } |
| 45 | + bool checkCollision(int width, int height) { |
| 46 | + pair<int,int> head = getHead(); |
| 47 | + if(head.first < 0 || head.first >= height || head.second < 0 || head.second >= width) |
| 48 | + return true; |
| 49 | + for(size_t i=1; i<body.size(); i++) |
| 50 | + if(body[i] == head) return true; |
| 51 | + return false; |
| 52 | + } |
| 53 | + void reset(int startX, int startY) { |
| 54 | + body.clear(); |
| 55 | + body.push_back({startX, startY}); |
| 56 | + direction = 'D'; |
| 57 | + } |
| 58 | +}; |
| 59 | + |
| 60 | +class Food { |
| 61 | +private: |
| 62 | + pair<int,int> pos; |
| 63 | +public: |
| 64 | + Food(int width, int height) { respawn(width, height, {}); } |
| 65 | + pair<int,int> getPosition() { return pos; } |
| 66 | + void respawn(int width, int height, vector<pair<int,int>> snakeBody) { |
| 67 | + while(true) { |
| 68 | + int x = rand()%height; |
| 69 | + int y = rand()%width; |
| 70 | + pos = {x, y}; |
| 71 | + bool onSnake = false; |
| 72 | + for(auto s: snakeBody) |
| 73 | + if(s==pos) onSnake = true; |
| 74 | + if(!onSnake) break; |
| 75 | + } |
| 76 | + } |
| 77 | +}; |
| 78 | + |
| 79 | +class Game { |
| 80 | +private: |
| 81 | + int width, height, score; |
| 82 | + Snake snake; |
| 83 | + Food food; |
| 84 | + bool gameOver; |
| 85 | +public: |
| 86 | + Game(int w,int h) : width(w), height(h), score(0), snake(w/2,h/2), food(w,h), gameOver(false) {} |
| 87 | + void draw() { |
| 88 | + system("cls"); |
| 89 | + for(int i=0;i<width+2;i++) cout<<"# "; |
| 90 | + cout<<endl; |
| 91 | + for(int i=0;i<height;i++){ |
| 92 | + cout<<"# "; |
| 93 | + for(int j=0;j<width;j++){ |
| 94 | + pair<int,int> head = snake.getHead(); |
| 95 | + if(head.first==i && head.second==j) cout<<"@ "; |
| 96 | + else if(food.getPosition()==make_pair(i,j)) cout<<"* "; |
| 97 | + else { |
| 98 | + bool printed=false; |
| 99 | + for(auto s:snake.getBody()) |
| 100 | + if(s.first==i && s.second==j){cout<<"o "; printed=true; break;} |
| 101 | + if(!printed) cout<<" "; |
| 102 | + } |
| 103 | + } |
| 104 | + cout<<"#"<<endl; |
| 105 | + } |
| 106 | + for(int i=0;i<width+2;i++) cout<<"# "; |
| 107 | + cout<<"\nScore: "<<score<<endl; |
| 108 | + } |
| 109 | + void input() { |
| 110 | + if(_kbhit()){ |
| 111 | + char key = toupper(_getch()); |
| 112 | + char dir = snake.getDirection(); |
| 113 | + if((key=='W' && dir!='S') || (key=='S' && dir!='W') || |
| 114 | + (key=='A' && dir!='D') || (key=='D' && dir!='A')) |
| 115 | + snake.setDirection(key); |
| 116 | + } |
| 117 | + } |
| 118 | + void logic() { |
| 119 | + pair<int,int> nextHead = snake.getHead(); |
| 120 | + switch(snake.getDirection()){ |
| 121 | + case 'W': nextHead.first--; break; |
| 122 | + case 'S': nextHead.first++; break; |
| 123 | + case 'A': nextHead.second--; break; |
| 124 | + case 'D': nextHead.second++; break; |
| 125 | + } |
| 126 | + bool grow = (nextHead == food.getPosition()); |
| 127 | + if(grow){score+=10; food.respawn(width,height,snake.getBody());} |
| 128 | + snake.move(grow); |
| 129 | + if(snake.checkCollision(width,height)) gameOver=true; |
| 130 | + } |
| 131 | + void reset() { |
| 132 | + score = 0; |
| 133 | + snake.reset(width/2, height/2); |
| 134 | + food.respawn(width, height, snake.getBody()); |
| 135 | + gameOver = false; |
| 136 | + } |
| 137 | + void run() { |
| 138 | + do { |
| 139 | + reset(); |
| 140 | + while(!gameOver){ |
| 141 | + draw(); |
| 142 | + input(); |
| 143 | + logic(); |
| 144 | + Sleep(250); |
| 145 | + } |
| 146 | + draw(); |
| 147 | + cout<<"\nGame Over! Final Score: "<<score<<endl; |
| 148 | + cout<<"Play again? (Y/N): "; |
| 149 | + } while(toupper(_getch()) == 'Y'); |
| 150 | + } |
| 151 | +}; |
| 152 | + |
| 153 | +int main(){ |
| 154 | + srand(time(0)); |
| 155 | + Game g(20,20); |
| 156 | + g.run(); |
| 157 | + return 0; |
| 158 | +} |
0 commit comments