Fork Copy #include #include #include #include #include #include using namespace std; const int WIDTH = 20; const int HEIGHT = 15; bool gameOver; int score; int headX, headY; vector> tail; int foodX, foodY; enum eDirection { STOP = 0, LEFT, RIGHT, UP, DOWN }; eDirection dir; void Setup() { gameOver = false; dir = RIGHT; // Bắt đầu di chuyển sang phải ngay headX = WIDTH / 2; headY = HEIGHT / 2; score = 0; tail.clear(); srand(static_cast(time(nullptr))); foodX = rand() % WIDTH; foodY = rand() % HEIGHT; } void Draw() { // Di chuyển con trỏ về đầu màn hình thay vì xóa toàn bộ HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE); COORD coord = {0, 0}; SetConsoleCursorPosition(hStdout, coord); // Vẽ khung trên for (int i = 0; i < WIDTH + 2; i++) cout << "#"; cout << endl; for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH + 2; j++) { if (j == 0 || j == WIDTH + 1) cout << "#"; else if (i == headY && j - 1 == headX) cout << "O"; else if (i == foodY && j - 1 == foodX) cout << "@"; else { bool isTail = false; for (const auto& segment : tail) { if (segment.first == j - 1 && segment.second == i) { cout << "o"; isTail = true; break; } } if (!isTail) cout << " "; } } cout << endl; } // Vẽ khung dưới for (int i = 0; i < WIDTH + 2; i++) cout << "#"; cout << endl; cout << "Score: " << score << endl; cout << "Controls: WASD (Move) | X (Exit)" << endl; } void Input() { if (_kbhit()) { switch (_getch()) { case 'a': if (dir != RIGHT) dir = LEFT; break; case 'd': if (dir != LEFT) dir = RIGHT; break; case 'w': if (dir != DOWN) dir = UP; break; case 's': if (dir != UP) dir = DOWN; break; case 'x': gameOver = true; break; } } } void Logic() { // Lưu vị trí cũ pair prev = {headX, headY}; // Di chuyển đầu switch (dir) { case LEFT: headX--; break; case RIGHT: headX++; break; case UP: headY--; break; case DOWN: headY++; break; } // Kiểm tra va chạm tường if (headX < 0 || headX >= WIDTH || headY < 0 || headY >= HEIGHT) gameOver = true; // Kiểm tra va chạm đuôi for (const auto& segment : tail) if (headX == segment.first && headY == segment.second) gameOver = true; // Di chuyển đuôi if (!tail.empty()) { tail.insert(tail.begin(), prev); tail.pop_back(); } // Kiểm tra ăn mồi if (headX == foodX && headY == foodY) { score += 10; // Thêm đốt đuôi if (tail.empty()) tail.push_back(prev); else tail.push_back(tail.back()); // Tạo mồi mới bool valid; do { valid = true; foodX = rand() % WIDTH; foodY = rand() % HEIGHT; // Kiểm tra không trùng với rắn if (headX == foodX && headY == foodY) valid = false; for (const auto& segment : tail) if (segment.first == foodX && segment.second == foodY) valid = false; } while (!valid); } } int main() { // Ẩn con trỏ nhấp nháy HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); CONSOLE_CURSOR_INFO cursorInfo; GetConsoleCursorInfo(hConsole, &cursorInfo); cursorInfo.bVisible = false; SetConsoleCursorInfo(hConsole, &cursorInfo); Setup(); while (!gameOver) { Draw(); Input(); Logic(); Sleep(100); } // Hiển thị khi kết thúc system("cls"); cout << "GAME OVER!\n"; cout << "Final Score: " << score << "\n\n"; system("pause"); return 0; }