c++游戏(躲避障碍) |
您所在的位置:网站首页 › 躲避障碍物的游戏 › c++游戏(躲避障碍) |
#include #include #include #include using namespace std; // 定义游戏窗口大小 const int width = 30; const int height = 20; // 定义游戏角色 class Player { public: int x, y; // 角色坐标 Player(int _x, int _y) : x(_x), y(_y) {} void move(int dx, int dy) { // 移动角色 x += dx; y += dy; if (x < 0) x = 0; if (x >= width) x = width - 1; if (y < 0) y = 0; if (y >= height) y = height - 1; } }; // 定义游戏障碍物 class Obstacle { public: int x, y; // 障碍物坐标 Obstacle(int _x, int _y) : x(_x), y(_y) {} void move() { // 移动障碍物 x--; if (x < 0) { // 障碍物到达左边界,重新生成 x = width - 1; y = rand() % height; } } }; int main() { srand(time(NULL)); // 初始化随机数种子 Player player(width / 2, height / 2); // 初始化角色 Obstacle obstacle(width - 1, rand() % height); // 初始化障碍物 int score = 0; // 初始化得分 while (true) { // 清空屏幕 system("cls"); // 绘制游戏界面 for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { if (i == player.y && j == player.x) cout cout |
今日新闻 |
点击排行 |
|
推荐新闻 |
图片新闻 |
|
专题文章 |
CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭 |