|
| 1 | +#include <conio.h> |
| 2 | +#include <iostream> |
| 3 | +#include <windows.h> |
| 4 | + |
| 5 | +using namespace std; |
| 6 | + |
| 7 | +bool gameover; |
| 8 | +const int width = 20; |
| 9 | +const int height = 10; |
| 10 | +int x, y, fruitX, fruitY, score; |
| 11 | +int tailX[100], tailY[100]; |
| 12 | +int nTail; |
| 13 | +enum eDirecton { STOP = 0, LEFT, RIGHT, UP, DOWN }; |
| 14 | +eDirecton dir; |
| 15 | + |
| 16 | +void Setup() { |
| 17 | + gameover = false; |
| 18 | + dir = STOP; |
| 19 | + x = width / 2; |
| 20 | + y = height / 2; |
| 21 | + fruitX = rand() % width; |
| 22 | + fruitY = rand() % height; |
| 23 | + score = 0; |
| 24 | +} |
| 25 | + |
| 26 | +void Draw() { |
| 27 | + system("cls"); // Clear the console window |
| 28 | + |
| 29 | + for (int i = 0; i < width + 2; i++) |
| 30 | + cout << "#"; |
| 31 | + cout << endl; |
| 32 | + |
| 33 | + for (int i = 0; i < height; i++) { |
| 34 | + for (int j = 0; j < width; j++) { |
| 35 | + if (j == 0) |
| 36 | + cout << "#"; // Left wall |
| 37 | + if (i == y && j == x) |
| 38 | + cout << "O"; // Snake's head |
| 39 | + else if (i == fruitY && j == fruitX) |
| 40 | + cout << "F"; // Fruit |
| 41 | + else { |
| 42 | + bool print = false; |
| 43 | + for (int k = 0; k < nTail; k++) { |
| 44 | + if (tailX[k] == j && tailY[k] == i) { |
| 45 | + cout << "o"; // Snake's tail |
| 46 | + print = true; |
| 47 | + } |
| 48 | + } |
| 49 | + if (!print) |
| 50 | + cout << " "; |
| 51 | + } |
| 52 | + |
| 53 | + if (j == width - 1) |
| 54 | + cout << "#"; // Right wall |
| 55 | + } |
| 56 | + cout << endl; |
| 57 | + } |
| 58 | + |
| 59 | + for (int i = 0; i < width + 2; i++) |
| 60 | + cout << "#"; |
| 61 | + cout << endl; |
| 62 | + |
| 63 | + cout << "Score:" << score << endl; |
| 64 | +} |
| 65 | + |
| 66 | +void Input() { |
| 67 | + if (_kbhit()) { |
| 68 | + switch (_getch()) { |
| 69 | + case 'a': |
| 70 | + dir = LEFT; |
| 71 | + break; |
| 72 | + case 'd': |
| 73 | + dir = RIGHT; |
| 74 | + break; |
| 75 | + case 'w': |
| 76 | + dir = UP; |
| 77 | + break; |
| 78 | + case 's': |
| 79 | + dir = DOWN; |
| 80 | + break; |
| 81 | + case 'x': |
| 82 | + gameover = true; |
| 83 | + break; |
| 84 | + } |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +void Logic() { |
| 89 | + int prevX = tailX[0]; |
| 90 | + int prevY = tailY[0]; |
| 91 | + int prev2X, prev2Y; |
| 92 | + tailX[0] = x; |
| 93 | + tailY[0] = y; |
| 94 | + for (int i = 1; i < nTail; i++) { |
| 95 | + prev2X = tailX[i]; |
| 96 | + prev2Y = tailY[i]; |
| 97 | + tailX[i] = prevX; |
| 98 | + tailY[i] = prevY; |
| 99 | + prevX = prev2X; |
| 100 | + prevY = prev2Y; |
| 101 | + } |
| 102 | + |
| 103 | + switch (dir) { |
| 104 | + case LEFT: |
| 105 | + x--; |
| 106 | + break; |
| 107 | + case RIGHT: |
| 108 | + x++; |
| 109 | + break; |
| 110 | + case UP: |
| 111 | + y--; |
| 112 | + break; |
| 113 | + case DOWN: |
| 114 | + y++; |
| 115 | + break; |
| 116 | + default: |
| 117 | + break; |
| 118 | + } |
| 119 | + |
| 120 | + if (x >= width) |
| 121 | + x = 0; |
| 122 | + else if (x < 0) |
| 123 | + x = width - 1; |
| 124 | + |
| 125 | + if (y >= height) |
| 126 | + y = 0; |
| 127 | + else if (y < 0) |
| 128 | + y = height - 1; |
| 129 | + |
| 130 | + for (int i = 0; i < nTail; i++) |
| 131 | + if (tailX[i] == x && tailY[i] == y) |
| 132 | + gameover = true; |
| 133 | + |
| 134 | + if (x == fruitX && y == fruitY) { |
| 135 | + score += 10; |
| 136 | + fruitX = rand() % width; |
| 137 | + fruitY = rand() % height; |
| 138 | + nTail++; |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +int main() { |
| 143 | + Setup(); |
| 144 | + while (!gameover) { |
| 145 | + Draw(); |
| 146 | + Input(); |
| 147 | + Logic(); |
| 148 | + Sleep(10); // Add a slight delay to control the speed of the game |
| 149 | + } |
| 150 | + return 0; |
| 151 | +} |
0 commit comments