Đây là mã C++ trò chơi rắn săn mồi chạy trên cmd Windows, sử dụng các hàm API Windows để xử lý hiển thị và điều khiển:
cpp
#include <iostream>
#include <conio.h>
#include <windows.h>
#include <vector>
using namespace std;
// Kích thước cửa sổ
const int width = 40;
const int height = 20;
// Hướng di chuyển
enum Direction { STOP = 0, LEFT, RIGHT, UP, DOWN };
Direction dir;
// Cấu trúc tọa độ
struct Point {
int x, y;
};
// Hàm di chuyển con trỏ console
void gotoXY(int x, int y) {
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
// Hàm ẩn con trỏ nhấp nháy
void hideCursor() {
CONSOLE_CURSOR_INFO cursorInfo;
cursorInfo.dwSize = 100;
cursorInfo.bVisible = FALSE;
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursorInfo);
}
// Hàm xử lý va chạm và logic game
bool gameOver;
Point head;
vector<Point> body;
Point fruit;
int score;
void Setup() {
gameOver = false;
dir = STOP;
head = { width / 2, height / 2 };
fruit = { rand() % width, rand() % height };
score = 0;
body.clear();
}
void Draw() {
system("cls"); // Xóa màn hình
// Vẽ tường trên
for (int i = 0; i < width + 2; i++)
cout << "#";
cout << endl;
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
if (x == 0) cout << "#"; // Tường trái
// Vẽ đầu rắn hoặc thân
if (x == head.x && y == head.y)
cout << "O";
else {
bool printBody = false;
for (auto &segment : body) {
if (segment.x == x && segment.y == y) {
cout << "o";
printBody = true;
break;
}
}
if (!printBody) {
if (x == fruit.x && y == fruit.y)
cout << "F";
else
cout << " ";
}
}
if (x == width - 1) cout << "#"; // Tường phải
}
cout << endl;
}
// Vẽ tường dưới
for (int i = 0; i < width + 2; i++)
cout << "#";
cout << endl;
// Hiển thị điểm
cout << "Score: " << score << endl;
}
void Input() {
if (_kbhit()) {
switch (_getch()) {
case 72: // Mũi tên lên
if (dir != DOWN) dir = UP;
break;
case 80: // Mũi tên xuống
if (dir != UP) dir = DOWN;
break;
case 75: // Mũi tên trái
if (dir != RIGHT) dir = LEFT;
break;
case 77: // Mũi tên phải
if (dir != LEFT) dir = RIGHT;
break;
case 'x':
gameOver = true;
break;
}
}
}
void Logic() {
// Di chuyển thân rắn
if (!body.empty()) {
body.insert(body.begin(), head);
body.pop_back();
}
// Di chuyển đầu
switch (dir) {
case LEFT:
head.x--;
break;
case RIGHT:
head.x++;
break;
case UP:
head.y--;
break;
case DOWN:
head.y++;
break;
}
// Kiểm tra va chạm tường
if (head.x < 0 || head.x >= width || head.y < 0 || head.y >= height)
gameOver = true;
// Kiểm tra ăn quả
if (head.x == fruit.x && head.y == fruit.y) {
score += 10;
body.push_back(head); // Thêm đoạn thân mới
// Tạo quả mới
fruit = { rand() % width, rand() % height };
}
// Kiểm tra va chạm thân
for (auto &segment : body)
if (segment.x == head.x && segment.y == head.y)
gameOver = true;
}
int main() {
hideCursor();
Setup();
while (!gameOver) {
Draw();
Input();
Logic();
Sleep(100); // Tốc độ game
}
system("cls");
gotoXY(width/2 - 4, height/2);
cout << "GAME OVER!";
gotoXY(width/2 - 6, height/2 + 1);
cout << "Final Score: " << score;
gotoXY(0, height + 3);
return 0;
}
Để chạy game, biên dịch mã bằng trình biên dịch C++ hỗ trợ thư viện Windows.h như MinGW hoặc Visual Studio.