用c语言写一个切水果的小游戏
时间: 2023-11-19 22:04:26 浏览: 52
好的,下面是用 C 语言实现的切水果小游戏:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int score = 0, fruit;
char choice;
srand(time(NULL)); // 初始化随机数种子
printf("欢迎来到切水果小游戏!\n");
do {
fruit = rand() % 3 + 1; // 生成 1 - 3 的随机数,分别代表苹果、橙子、香蕉
printf("请切开下面的水果:\n");
switch (fruit) {
case 1:
printf(" __ \n");
printf(" / \\ \n");
printf("| | \n");
printf("| |\n");
printf(" \\ __ / \n");
break;
case 2:
printf(" __ \n");
printf(" / \\ \n");
printf("| | \n");
printf("| |\n");
printf("| |\n");
printf(" \\ __ / \n");
break;
case 3:
printf(" __ \n");
printf(" / \\ \n");
printf("| | \n");
printf("| |\n");
printf("|______|\n");
printf(" \\ / \n");
printf(" \\ __/ \n");
break;
}
printf("选择 (A) 水平切开或 (B) 垂直切开?\n");
scanf(" %c", &choice);
switch (choice) {
case 'A':
case 'a':
if (fruit == 1 || fruit == 2) {
printf("恭喜你,切对了!得到 10 分!\n");
score += 10;
} else {
printf("切错了,扣去 5 分!\n");
score -= 5;
}
break;
case 'B':
case 'b':
if (fruit == 3) {
printf("恭喜你,切对了!得到 10 分!\n");
score += 10;
} else {
printf("切错了,扣去 5 分!\n");
score -= 5;
}
break;
default:
printf("无效的选择!\n");
break;
}
printf("你的得分是:%d\n", score);
printf("是否继续游戏?(Y/N)\n");
scanf(" %c", &choice);
} while (choice == 'Y' || choice == 'y');
printf("游戏结束,你的最终得分是:%d\n", score);
return 0;
}
```
这个小游戏会随机生成一个水果(苹果、橙子或香蕉),然后让玩家选择是水平切开还是垂直切开。如果玩家选择正确,得到 10 分;否则扣去 5 分。玩家可以选择继续游戏或退出游戏,最终显示得分。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![torrent](https://img-home.csdnimg.cn/images/20210720083646.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)