用C++代码写一个游戏
时间: 2024-05-14 19:16:25 浏览: 10
好的,我可以给你一个简单的游戏示例,这是一个猜数字的小游戏,你可以试着运行它:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int num, guess, count = 0;
srand(time(NULL));
num = rand() % 100 + 1;
printf("猜数字游戏!\n");
printf("猜一个 1-100 之间的数字:\n");
do {
scanf("%d", &guess);
count++;
if (guess > num) {
printf("猜大了!再试一次:\n");
} else if (guess < num) {
printf("猜小了!再试一次:\n");
}
} while (guess != num);
printf("恭喜你猜对了!你一共猜了 %d 次。\n", count);
return 0;
}
```
这个游戏的规则很简单,程序会生成一个随机的 1-100 之间的数字,玩家需要猜这个数字。程序会提示玩家每次猜的数字是大了还是小了,直到玩家猜对为止。最后程序会输出猜的次数。
相关问题
用c++代码写一个滑雪游戏
以下是一个简单的滑雪游戏的C++代码示例:
```c++
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
// 游戏区域大小
const int WIDTH = 20;
const int HEIGHT = 10;
// 游戏角色类型
const char PLAYER_CHAR = 'P';
const char TREE_CHAR = 'T';
const char ROCK_CHAR = 'R';
// 游戏角色位置
int playerX = WIDTH / 2;
int playerY = HEIGHT - 1;
// 游戏角色状态
bool isAlive = true;
int score = 0;
// 随机生成游戏角色
char generateCharacter() {
int r = rand() % 10;
if (r < 3) {
return TREE_CHAR;
} else if (r < 6) {
return ROCK_CHAR;
} else {
return ' ';
}
}
// 打印游戏区域
void printGameArea() {
// 清空屏幕
system("cls");
// 打印游戏区域
for (int y = 0; y < HEIGHT; y++) {
for (int x = 0; x < WIDTH; x++) {
if (x == playerX && y == playerY) {
cout << PLAYER_CHAR;
} else {
cout << generateCharacter();
}
}
cout << endl;
}
// 打印得分
cout << "Score: " << score << endl;
}
// 处理用户输入
void handleInput() {
if (GetAsyncKeyState(VK_LEFT) && playerX > 0) {
playerX--;
}
if (GetAsyncKeyState(VK_RIGHT) && playerX < WIDTH - 1) {
playerX++;
}
}
// 更新游戏状态
void updateGame() {
// 检测是否撞到障碍物
char c = generateCharacter();
if (c == TREE_CHAR || c == ROCK_CHAR) {
if (playerX == WIDTH / 2 && playerY == HEIGHT - 1) {
isAlive = false;
}
}
// 更新得分
score++;
}
int main() {
// 设置随机数种子
srand(time(NULL));
// 游戏主循环
while (isAlive) {
handleInput();
updateGame();
printGameArea();
// 暂停一段时间
Sleep(50);
}
// 打印游戏结束信息
cout << "Game Over! Your score is " << score << endl;
return 0;
}
```
上述代码使用了Windows API中的GetAsyncKeyState函数来检测用户输入,需要在Windows环境下编译运行。游戏区域大小、角色类型和位置等信息可以根据需要进行调整。
用c++代码写一个扫雷小游戏
以下是用C++代码实现扫雷小游戏的基本步骤:
```C++
//定义常量
const int N = 105;
const int M = 105;
const int dx[8] = {0, 0, 1, -1, 1, 1, -1, -1};
const int dy[8] = {1, -1, 0, 0, 1, -1, 1, -1};
//定义变量
int n, m, k, cnt;
char a[N][M], b[N][M];
bool vis[N][M];
//初始化
void init() {
memset(a, '.', sizeof(a));
memset(b, '0', sizeof(b));
memset(vis, false, sizeof(vis));
cnt = 0;
}
//生成雷区
void build() {
while (cnt < k) {
int x = rand() % n + 1;
int y = rand() % m + 1;
if (a[x][y] == '*') continue;
a[x][y] = '*';
cnt++;
}
}
//计算周围雷数
void calc() {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (a[i][j] == '*') continue;
for (int k = 0; k < 8; k++) {
int x = i + dx[k];
int y = j + dy[k];
if (x < 1 || x > n || y < 1 || y > m) continue;
if (a[x][y] == '*') b[i][j]++;
}
}
}
}
//递归展开
void dfs(int x, int y) {
vis[x][y] = true;
if (b[x][y] != '0') return;
for (int i = 0; i < 8; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if (nx < 1 || nx > n || ny < 1 || ny > m) continue;
if (vis[nx][ny]) continue;
dfs(nx, ny);
}
}
//主函数
int main() {
srand(time(NULL));
cin >> n >> m >> k;
init();
build();
calc();
dfs(1, 1);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (vis[i][j]) cout << b[i][j];
else cout << ".";
}
cout << endl;
}
return 0;
}
```
相关推荐
![exe](https://img-home.csdnimg.cn/images/20210720083343.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)