C/C++ 迷宫游戏

游戏介绍

这个迷宫探险游戏有以下功能:

  1. 探险:选择该选项后,玩家会进入地下迷宫进行探险。在随机事件中,可能会遇到陷阱、发现金币或者什么都没有发生。陷阱会使玩家失去一定的生命值,金币可以增加玩家的金币数量。

  2. 休息:选择该选项后,玩家可以休息一会儿。在休息过程中,有50%的概率会丢失随机数量的金币。而体力值将会回复15点,但不超过上限100。

  3. 查看背包:选择该选项后,玩家可以查看背包中的道具。当前游戏中有三种道具:能量药水、地图和食物。能量药水可以增加20点体力值,地图可以指示方向,而食物则可以增加10点生命值。

  4. 结束游戏:选择该选项后,游戏将会结束。

玩家需要通过探险获得足够的金币(100个)以及保持生命值大于0才能获得胜利。如果生命值降到0或者选择结束游戏,则游戏失败。

游戏示例
在这里插入图片描述

在这里插入图片描述

运用的知识点如下:

  • 结构体:使用了结构体player来表示玩家的状态,包括生命值、体力值和金币数量。

  • typedef:使用typedef为结构体类型player定义别名Player,方便在代码中使用。

  • 随机数:使用srand函数设置随机种子。然后使用rand函数生成随机数,用于产生随机事件、陷阱伤害、金币奖励等。

  • 条件语句:使用if-else语句根据不同的条件执行相应的操作,例如判断玩家的选择、判断随机事件类型等。

  • 循环语句:使用while循环控制游戏的进行,直到玩家的生命值小于等于0或金币数量达到200时结束游戏。

  • 指针:使用指针作为函数参数,通过指针修改玩家的状态。

  • 数组:使用数组items来表示玩家的背包道具,下标对应道具编号-1,数组元素表示道具数量。

  • 函数:定义了多个函数来实现不同的功能,如探险、休息、查看背包、使用道具等。

  • switch语句:根据玩家的选择执行不同的操作。

  • 类型转换:使用int类型将字符转换为整数,例如将用户输入的选择转换为整数

结构体定义

struct player {int health;   // 欢乐值int stamina;   // 体力值int gold;   // 金币
};typedef struct player Player;
int items[3] = {0};   // 道具数组,下标分别对应道具编号-1

函数声明

void inif(Player* p);//初始化
void explore(Player* p);//收集
void rest(Player* p);//休息
void view_inventory(Player* p);//查看背包
void use_item(Player* p, int index);//使用道具
void use_item1(Player* p);//能量药水
void use_item2(Player* p);//地图
void use_item3(Player* p);//野果
void show_status(Player* p);//显示状态
void end_of_game(Player* p);//结束收集

源代码

#include <stdio.h>
#include <stdlib.h>
#include <time.h>struct player {int health;   // 欢乐值int stamina;   // 体力值int gold;   // 金币
};typedef struct player Player;int items[3] = {0};   // 道具数组,下标分别对应道具编号-1void inif(Player* p);//初始化
void explore(Player* p);//收集
void rest(Player* p);//休息
void view_inventory(Player* p);//查看背包
void use_item(Player* p, int index);//使用道具
void use_item1(Player* p);//能量药水
void use_item2(Player* p);//地图
void use_item3(Player* p);//野果
void show_status(Player* p);//显示状态
void end_of_game(Player* p);//结束收集int main() {srand(time(NULL));   // 每次运行产生不同的随机种子Player player;// 初始化玩家状态inif(&player);printf("欢迎进行迷宫探险之旅!\n");while (player.health > 0 && player.gold < 100) {printf("\n-----------------------------------\n");printf("你现在的状态:\n");show_status(&player);int choice;scanf("%d", &choice);switch (choice) {case 1:explore(&player);   // 进行探险break;case 2:rest(&player);   // 休息break;case 3:view_inventory(&player);   // 查看背包中的道具break;case 4:end_of_game(&player);return 0;default:printf("无效选项,请重新输入。\n");}}if (player.health <= 0) {printf("\n你精疲力尽!收集失败...\n");} else {printf("\n你拥有%d个金币,有足够的信心离开迷宫。收集成功!\n", player.gold);}return 0;
}void inif(Player* p){p->health = 100;p->stamina = 100;p->gold = 0;
}void explore(Player* p) {if(p->stamina<=0){printf("\n体力过低请补充体力...\n");return;}printf("\n你开始了收集之旅...\n");int random_event = (rand() % 10) + 1;   // 随机事件if (random_event < 3 && p->health > 0 && p->stamina >= 15) {   // 遇到陷阱p->stamina -= 15;int damage = rand() % 31 + 10;   // 随机减少10-20点生命值printf("你踩到了陷阱,失去了%d点欢乐值\n", damage);p->health -= damage;} else if (random_event < 6 && p->health > 0 && p->stamina >= 10) {   // 发现金币p->stamina -= 10;int gold = rand() % 36 + 5;   // 随机获得金币printf("你发现了%d个金币\n", gold);p->gold += gold;}else if(random_event < 9){if(random_event==6){p->stamina -=5;printf("你获得了能量药水\n");items[0]++;}else if(random_event==7){p->stamina -=5;printf("你获得了地图\n");items[1]++;}else {p->stamina -=5;printf("你获得了野果\n");items[2]++;}}else {   // 什么都没发生,但是消耗体力值p->stamina -= 5;printf("你在迷宫徘徊...\n");}}void show_status(Player* p) {printf("\n体力:%d,欢乐值:%d,金币:%d\n\n", p->stamina, p->health, p->gold);printf("请选择下一步操作:\n");printf("1. 收集\n");printf("2. 休息\n");printf("3. 查看背包\n");printf("4. 结束收集\n");
}void rest(Player* p) {printf("\n你决定休息一会儿...\n");int lost_gold = rand() % 6;   // 50% 的概率丢失随机数量的金币if (lost_gold < 3 && p->gold > 0) {int gold_loss = rand() % (p->gold + 1);   // 丢失随机数量的金币printf("你在休息过程中丢失了%d个金币\n", gold_loss);p->gold -= gold_loss;}p->stamina += 15;   // 回复15点体力值if (p->stamina > 100) {p->stamina = 100;}
}void view_inventory(Player* p) {printf("\n你打开了背包...\n");for (int i = 0; i < 3; i++) {if (items[i] > 0) {switch (i+1) {case 1:printf("1. 能量药水(增加20点体力):%d\n", items[0]);break;case 2:printf("2. 地图(告诉你方向):%d\n", items[1]);break;case 3:printf("3. 野果(增加10点欢乐值):%d\n", items[2]);break;}} else {switch (i+1) {case 1:printf("1. 能量药水(增加20点体力):0\n");break;case 2:printf("2. 地图(告诉你方向):0\n");break;case 3:printf("3. 野果(增加10点欢乐值):0\n");break;}}}printf("\n请选择要使用的道具,或者输入其他选项返回游戏:\n");int choice;scanf("%d", &choice);if (choice != 1 && choice != 2 && choice != 3) {printf("\n你选择了返回游戏。\n");return;}use_item(p,choice);
}void use_item(Player* p, int index) {if (items[index-1] <= 0) {   // 没有该道具printf("\n你没有这个道具!\n");return;}switch (index) {case 1:use_item1(p);break;case 2:use_item2(p);break;case 3:use_item3(p);break;default:printf("\n该选项无效!\n");}
}void end_of_game(Player* p) {printf("\n游戏结束...\n");printf("你的最终状态为:\n");printf("体力:%d,欢乐值:%d,金币:%d\n", p->stamina, p->health, p->gold);if (p->health <= 0) {printf("你已经死亡!\n");}else if (p->gold >= 100) {printf("你找到了100枚个金币,收集成功!\n");}else {printf("你没有找到足够的金币,收集失败!\n");}
}void use_item1(Player* p){if (p->stamina == 100) {   // 体力值已满printf("\n你现在的体力已经是满的了!\n");return;}p->stamina += 20;   // 增加20点体力值if (p->stamina > 100) {   // 不超过上限100p->stamina = 100;}printf("\n你使用了一瓶能量药水,增加了20点体力值!\n");items[0]--;
}void use_item2(Player* p){printf("\n你打开了地图,发现...还需要继续收集才能找到金币。\n");items[1]--;
}void use_item3(Player* p){if (p->health == 100) {   // 生命值已满printf("\n你现在的欢乐值已经是满的了!\n");return;}p->health += 10;   // 增加10点生命值if (p->health > 100) {   // 不超过上限100p->health = 100;}printf("\n你食用了一份美味佳肴,增加了10点欢乐值!\n");items[2]--;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/502017.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

springboot yml方式多环境切换 dev,test,prod环境

第一步,创建对应的yml文件 application.yml application-dev.yml application-prod.yml application-test.yml 第二步,在application.yml中指定对一个的 环境文件 spring:profiles:active: prod记住 只用指定 dev,test,prod 就可以了 第三步 在不同的配置文件,配置不一样…

边缘计算网关在机场生产中的应用-天拓四方

随着工业4.0的推进&#xff0c;物联网&#xff08;IoT&#xff09;技术在各个工业领域中的应用日益广泛。特别是在机床行业&#xff0c;物联网技术的引入不仅提高了生产效率&#xff0c;还实现了对机床设备的实时监控和远程维护。在这一背景下&#xff0c;边缘计算网关的角色愈…

LeetCode刷题--- 乘积为正数的最长子数组长度

个人主页&#xff1a;元清加油_【C】,【C语言】,【数据结构与算法】-CSDN博客 个人专栏 力扣递归算法题 http://t.csdnimg.cn/yUl2I 【C】 ​​​​​​http://t.csdnimg.cn/6AbpV 数据结构与算法 ​​​http://t.csdnimg.cn/hKh2l 前言&#xff1a;这个专栏主要讲述动…

GEE:计算NDVI时间序列和谐波拟合曲线之间的残差时间序列

作者:CSDN @ _养乐多_ 本文将介绍在谷歌地球引擎(Google Earth Engine)使用 Landsat 时间序列数据数据进行时间序列谐波(harmonic)拟合建模,并对模型的残差(residual)序列(计算NDVI时间序列和谐波拟合曲线之间的残差时间序列)进行分析。具体流程为使用Landsat计算ND…

【leetcode】环形链表✚环形链表II

大家好&#xff0c;我是苏貝&#xff0c;本篇博客带大家刷题&#xff0c;如果你觉得我写的还不错的话&#xff0c;可以给我一个赞&#x1f44d;吗&#xff0c;感谢❤️ 目录 1.环形链表解题拓展&#xff1a; 2.环形链表II 1.环形链表 点击查看题目 解题 思路: bool hasCycle…

C++基于多设计模式下的同步异步日志系统day2

&#x1f4df;作者主页&#xff1a;慢热的陕西人 &#x1f334;专栏链接&#xff1a;C基于多设计模式下的同步&异步日志系统 &#x1f4e3;欢迎各位大佬&#x1f44d;点赞&#x1f525;关注&#x1f693;收藏&#xff0c;&#x1f349;留言 主要内容实现了日志代码设计的实…

11.网络游戏逆向分析与漏洞攻防-游戏网络架构逆向分析-接管游戏接收网络数据包的操作

内容参考于&#xff1a;易道云信息技术研究院VIP课 上一个内容&#xff1a;接管游戏发送数据的操作 码云地址&#xff08;master 分支&#xff09;&#xff1a;https://gitee.com/dye_your_fingers/titan 码云版本号&#xff1a;8256eb53e8c16281bc1a29cb8d26d352bb5bbf4c 代…

软件测试最重要的事之【编写用例】

软件测试用例得出软件测试用例的内容&#xff0c;其次&#xff0c;按照软件测试写作方法&#xff0c;落实到文档中&#xff0c;两者是形式和内容的关系&#xff0c;好的测试用例不仅方便自己和别人查看&#xff0c;而且能帮助设计的时候考虑的更周。 一个好的测试用例必须包含…

【HTML】HTML基础系列文章小小总结,运用标签写出网页!

宇宙级声明&#xff01;这次只运用了一些基础标签&#xff0c;希望不要丑到大家~ 目录 效果预览&#xff1a;​编辑​编辑 点击百度百科 点击图片 点击下载 标签说明 源代码 效果预览&#xff1a; 点击百度百科 点击图片 点击下载 标签说明 标题 加粗文字 下划线 换行 分…

IO-DAY2

用发write、fread将一张随意图片修改成德国国旗 #include<stdio.h> #include<string.h> #include<stdlib.h> #include<unistd.h> int main(int argc, char *argv[]) {FILE* fpfopen("./122.bmp","r");int w 0,h 0;fseek(fp,18,…

leetcode:860.柠檬水找零

题意&#xff1a;按照支付顺序&#xff0c;进行支付&#xff0c;能够正确找零。 解题思路&#xff1a;贪心策略&#xff1a;针对支付20的客人&#xff0c;优先选择消耗10而不是消耗5&#xff0c;因为5可以用来找零10或20. 代码实现&#xff1a;有三种情况&#xff08;代表三种…

tomcat 搭建博客 及破解数据库密码

一 tomcat 搭建博客 &#xff08;一&#xff09;博客安装包 1&#xff0c; 把博客war包 放到 webapps 文件夹下 2&#xff0c;会自动解压 3&#xff0c;做个软连接 方便后续操作 可以注意到 因为war包 是又tomcat 自己解压的 所以属主数组还是 tomcat &#xff08…