给我用c++511写wsad和 操控的人机游戏
下面是一个使用C++编写的基本的人机游戏,可以通过键盘的W、S、A、D来控制玩家的移动,空格键用于攻击。
#include <iostream>
#include <conio.h>
using namespace std;
const int MAP_SIZE = 10;
struct Position {
int x;
int y;
};
class Game {
private:
Position playerPosition;
Position enemyPosition;
bool isGameOver;
public:
Game() {
playerPosition.x = 0;
playerPosition.y = 0;
enemyPosition.x = MAP_SIZE - 1;
enemyPosition.y = MAP_SIZE - 1;
isGameOver = false;
}
void drawMap() {
cout << " ";
for (int i = 0; i < MAP_SIZE; i++) {
cout << "-";
}
cout << " " << endl;
for (int i = 0; i < MAP_SIZE; i++) {
cout << "|";
for (int j = 0; j < MAP_SIZE; j++) {
if (i == playerPosition.y && j == playerPosition.x) {
cout << "P";
} else if (i == enemyPosition.y && j == enemyPosition.x) {
cout << "E";
} else {
cout << " ";
}
}
cout << "|" << endl;
}
cout << " ";
for (int i = 0; i < MAP_SIZE; i++) {
cout << "-";
}
cout << " " << endl;
}
void updatePlayerPosition(char key) {
switch (key) {
case 'w':
if (playerPosition.y > 0) {
playerPosition.y--;
}
break;
case 's':
if (playerPosition.y < MAP_SIZE - 1) {
playerPosition.y++;
}
break;
case 'a':
if (playerPosition.x > 0) {
playerPosition.x--;
}
break;
case 'd':
if (playerPosition.x < MAP_SIZE - 1) {
playerPosition.x++;
}
break;
}
}
void updateGame(char key) {
updatePlayerPosition(key);
if (playerPosition.x == enemyPosition.x && playerPosition.y == enemyPosition.y) {
isGameOver = true;
}
}
bool getGameOver() {
return isGameOver;
}
};
int main() {
Game game;
while (!game.getGameOver()) {
system("cls");
game.drawMap();
char key = _getch();
if (key == ' ') {
cout << "Player attacked!" << endl;
} else {
game.updateGame(key);
}
}
cout << "Game Over!" << endl;
return 0;
}
这个游戏会在控制台中显示一个地图,玩家用键盘上的W、S、A、D键来移动,空格键用于攻击。玩家的位置用字母P表示,敌人的位置用字母E表示。当玩家和敌人的位置重叠时,游戏结束。
请注意,这只是一个非常基本的示例,没有包含太多游戏逻辑和交互。您可以根据自己的需求进行修改和扩展
原文地址: https://www.cveoy.top/t/topic/hTCa 著作权归作者所有。请勿转载和采集!