下面是一个简单的用C++编写的基于WSAD和空格键操作的坦克游戏示例代码:

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

const int width = 20;
const int height = 10;

int tankX = width / 2;
int tankY = height / 2;

void DrawTank()
{
    system("cls"); // 清空屏幕

    for (int y = 0; y < height; y++)
    {
        for (int x = 0; x < width; x++)
        {
            if (x == tankX && y == tankY) // 绘制坦克
                std::cout << "T";
            else
                std::cout << " ";
        }
        std::cout << std::endl;
    }
}

int main()
{
    char key = ' ';

    while (key != 'q')
    {
        if (_kbhit()) // 检查是否有按键被按下
        {
            key = _getch(); // 获取按下的键值

            switch (key)
            {
                case 'w': // 上
                    tankY--;
                    break;
                case 's': // 下
                    tankY++;
                    break;
                case 'a': // 左
                    tankX--;
                    break;
                case 'd': // 右
                    tankX++;
                    break;
                default:
                    break;
            }

            DrawTank();
        }
    }

    return 0;
}

这个示例代码使用了Windows的conio.h头文件中的_kbhit()和_getch()函数来实现获取键盘输入。程序会在控制台绘制一个大小为20x10的坦克,并且可以用WSAD键来控制坦克的移动,按下空格键退出游戏

给我用c++511写wsad和 操控的坦克游戏

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

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