下面是一个使用C++编写的简单的人机游戏,可以使用W、A、S、D键来控制玩家的移动,空格键来进行攻击。游戏中有一个玩家和一个敌人,玩家可以通过移动和攻击来击败敌人。

#include <iostream>
#include <conio.h>

using namespace std;

const int WIDTH = 10;
const int HEIGHT = 10;
const char PLAYER = 'P';
const char ENEMY = 'E';
const char EMPTY = ' ';

struct Position {
    int x;
    int y;
};

class Game {
private:
    char grid[WIDTH][HEIGHT];
    Position playerPos;
    Position enemyPos;
    bool gameOver;
public:
    Game() {
        // 初始化游戏地图
        for (int i = 0; i < WIDTH; i++) {
            for (int j = 0; j < HEIGHT; j++) {
                grid[i][j] = EMPTY;
            }
        }
        
        // 初始化玩家和敌人的位置
        playerPos.x = 0;
        playerPos.y = 0;
        enemyPos.x = WIDTH - 1;
        enemyPos.y = HEIGHT - 1;
        
        // 在地图上放置玩家和敌人
        grid[playerPos.x][playerPos.y] = PLAYER;
        grid[enemyPos.x][enemyPos.y] = ENEMY;
        
        gameOver = false;
    }
    
    void printGrid() {
        system("cls");
        
        for (int i = 0; i < WIDTH; i++) {
            for (int j = 0; j < HEIGHT; j++) {
                cout << grid[i][j] << " ";
            }
            cout << endl;
        }
    }
    
    void update() {
        char input = _getch();
        
        // 根据用户输入更新玩家的位置
        switch (input) {
            case 'w':
                if (playerPos.y > 0) {
                    grid[playerPos.x][playerPos.y] = EMPTY;
                    playerPos.y--;
                    grid[playerPos.x][playerPos.y] = PLAYER;
                }
                break;
            case 'a':
                if (playerPos.x > 0) {
                    grid[playerPos.x][playerPos.y] = EMPTY;
                    playerPos.x--;
                    grid[playerPos.x][playerPos.y] = PLAYER;
                }
                break;
            case 's':
                if (playerPos.y < HEIGHT - 1) {
                    grid[playerPos.x][playerPos.y] = EMPTY;
                    playerPos.y++;
                    grid[playerPos.x][playerPos.y] = PLAYER;
                }
                break;
            case 'd':
                if (playerPos.x < WIDTH - 1) {
                    grid[playerPos.x][playerPos.y] = EMPTY;
                    playerPos.x++;
                    grid[playerPos.x][playerPos.y] = PLAYER;
                }
                break;
            case ' ':
                // 玩家攻击敌人
                if (playerPos.x == enemyPos.x && playerPos.y == enemyPos.y) {
                    grid[enemyPos.x][enemyPos.y] = EMPTY;
                    gameOver = true;
                }
                break;
        }
    }
    
    bool isGameOver() {
        return gameOver;
    }
};

int main() {
    Game game;
    
    while (!game.isGameOver()) {
        game.printGrid();
        game.update();
    }
    
    cout << "游戏结束!" << endl;
    
    return 0;
}

你可以在命令行中运行此程序,并使用W、A、S、D键来控制玩家的移动,空格键来进行攻击。游戏地图是一个10x10的网格,玩家和敌人分别用字母P和E表示。玩家初始位置在左上角,敌人初始位置在右下角。当玩家攻击敌人时,敌人将被消灭,游戏结束

给我用c++511写wasd和 操控的人机游戏

原文地址: https://www.cveoy.top/t/topic/hTB3 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录