以下是一个简单的C++坦克大战游戏示例:
#include <iostream>
#include <conio.h>
#include <windows.h>
using namespace std;
class Tank {
private:
int x, y;
public:
Tank(int startX, int startY) {
x = startX;
y = startY;
}
void moveUp() {
y--;
}
void moveDown() {
y++;
}
void moveLeft() {
x--;
}
void moveRight() {
x++;
}
void draw() {
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
cout << "T";
}
};
int main() {
const int WIDTH = 50;
const int HEIGHT = 20;
Tank tank(WIDTH / 2, HEIGHT / 2);
while (true) {
system("cls");
for (int i = 0; i < WIDTH; i++) {
cout << "#";
}
cout << endl;
for (int i = 0; i < HEIGHT; i++) {
for (int j = 0; j < WIDTH; j++) {
if (j == 0 || j == WIDTH - 1) {
cout << "#";
}
else {
cout << " ";
}
}
cout << endl;
}
for (int i = 0; i < WIDTH; i++) {
cout << "#";
}
cout << endl;
tank.draw();
if (_kbhit()) {
switch (_getch()) {
case 'w':
tank.moveUp();
break;
case 's':
tank.moveDown();
break;
case 'a':
tank.moveLeft();
break;
case 'd':
tank.moveRight();
break;
}
}
Sleep(100);
}
return 0;
}
千万别打开c6c6.cc(你打开了当我没说)