• C#(CSharp)入门实践项目(简易回合制游戏)


    项目名称

    木木夕营救公主

    项目介绍

    这是一个小游戏,你将扮演一个英雄(木木夕),去打败恶龙,拯救出公主,该项目采用回合制战斗模式,由于角色的血量和攻击为随机数,所以需要靠运气才能通关噢!

    简单需求分析

    开始界面:控制台输入输出,控制台颜色变化

    游戏界面:控制台输入输出,控制台颜色变化,回合制战斗(随机数,循环,条件判断

    结束界面: 控制台输入输出,控制台颜色变化

    界面间相互切换 

    项目代码(含详细说明)

    1. using System;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using System.Text;
    5. using System.Threading.Tasks;
    6. namespace CSharp入门_实践
    7. {
    8. class Program
    9. {
    10. static void Main(string[] args)
    11. {
    12. #region 控制台基础设置
    13. int width = 50;
    14. int height = 30;
    15. //隐藏光标
    16. Console.CursorVisible = false;
    17. //设置控制台大小
    18. Console.SetWindowSize(width, height);
    19. //设置缓冲区大小
    20. Console.SetBufferSize(width, height);
    21. #endregion
    22. #region 多场景
    23. //约定 1是开始游戏界面,2是游戏界面,3是结束界面
    24. int nowId = 1;
    25. string gameOverInfo ="" ;
    26. while (true)
    27. {
    28. //不同的场景id,进行不同的逻辑
    29. switch (nowId)
    30. {
    31. #region 开始场景逻辑
    32. //开始场景
    33. case 1:
    34. Console.Clear();//每次场景切换需要把上一次的场景清除
    35. Console.SetCursorPosition(width / 2 - 7, 8);//设置下面文字的位置
    36. Console.Write("木木夕营救公主");
    37. int nowSelIndex = 0;//当前选项编号 0:开始游戏 1:结束游戏
    38. //输入
    39. while (true)
    40. {
    41. bool isQuit = false;//退出循环标志
    42. Console.SetCursorPosition(width / 2 - 4, 12);
    43. //改变开始游戏的颜色
    44. Console.ForegroundColor = nowSelIndex == 0 ? ConsoleColor.Red : ConsoleColor.White;
    45. Console.Write("开始游戏");
    46. Console.SetCursorPosition(width / 2 - 4, 14);
    47. //改变结束游戏的颜色
    48. Console.ForegroundColor = nowSelIndex == 1 ? ConsoleColor.Red : ConsoleColor.White;
    49. Console.Write("退出游戏");
    50. //说明
    51. Console.SetCursorPosition(width / 2 - 16, 16);
    52. Console.ForegroundColor = ConsoleColor.White;
    53. Console.Write("说明:按W,S进行上下选择,J键确定");
    54. Console.SetCursorPosition(width / 2 - 10, 18);
    55. Console.Write("操作时按WASD进行移动");
    56. Console.ForegroundColor = ConsoleColor.Green;
    57. Console.SetCursorPosition(width / 2 - 10, 20);
    58. Console.Write("★");
    59. Console.ForegroundColor = ConsoleColor.White;
    60. Console.Write("为boss");
    61. Console.ForegroundColor = ConsoleColor.Yellow;
    62. Console.SetCursorPosition(width / 2 - 10, 21);
    63. Console.Write("●");
    64. Console.ForegroundColor = ConsoleColor.White;
    65. Console.Write("为玩家木木夕");
    66. Console.ForegroundColor = ConsoleColor.Blue;
    67. Console.SetCursorPosition(width / 2 - 10, 22);
    68. Console.Write("▲");
    69. Console.ForegroundColor = ConsoleColor.White;
    70. Console.Write("为公主");
    71. Console.SetCursorPosition(width / 2 - 18, 23);
    72. Console.Write("找到boss,进行挑战,按J键进行一次攻击");
    73. //检测玩家输入,根据输入内容进行跳转
    74. char input = Console.ReadKey(true).KeyChar;//检测输入并赋值,不显示在控制台
    75. switch (input)
    76. {
    77. case 'w':
    78. case 'W':
    79. --nowSelIndex;
    80. if (nowSelIndex < 0)
    81. {
    82. nowSelIndex = 0;
    83. }
    84. break;
    85. case 's':
    86. case 'S':
    87. ++nowSelIndex;
    88. if (nowSelIndex > 1)
    89. {
    90. nowSelIndex = 1;
    91. }
    92. break;
    93. case 'j':
    94. case 'J':
    95. //确定按钮
    96. if (nowSelIndex == 0)
    97. {
    98. //进入游戏
    99. //改变当前场景id
    100. nowId = 2;
    101. //退出内层while循环
    102. isQuit = true;
    103. }
    104. else
    105. {
    106. //关闭控制台
    107. Environment.Exit(0);
    108. }
    109. break;
    110. }
    111. if (isQuit == true)
    112. {
    113. break;//跳出该层循环
    114. }
    115. }
    116. break;
    117. #endregion
    118. //游戏场景
    119. case 2:
    120. Console.Clear();
    121. #region 红墙
    122. Console.ForegroundColor = ConsoleColor.Red;//设置颜色
    123. //画墙
    124. for (int i = 0; i < width-2; i += 2)
    125. {
    126. //上方
    127. Console.SetCursorPosition(i, 0);//设置位置
    128. Console.Write("■");
    129. //下方
    130. Console.SetCursorPosition(i, height - 2);//设置位置
    131. Console.Write("■");
    132. //中间
    133. Console.SetCursorPosition(i, height - 7);//设置位置
    134. Console.Write("■");
    135. }
    136. for (int i = 0; i -1; i++)
    137. {
    138. //左边
    139. Console.SetCursorPosition(0, i);//设置位置
    140. Console.Write("■");
    141. //右边
    142. Console.SetCursorPosition(width-2, i);//设置位置
    143. Console.Write("■");
    144. }
    145. #endregion
    146. #region boss属性相关
    147. //boss位置
    148. int xBoss = 24;
    149. int yBoss = 15;
    150. //boss攻击范围
    151. int bossAtkMin = 9;
    152. int bossAtkMax = 16;
    153. //boss血量
    154. int bossHP = 100;
    155. //boss图标
    156. string bossIcon = "★";
    157. //boss颜色
    158. ConsoleColor bossColor = ConsoleColor.Green;
    159. #endregion
    160. #region 玩家属性相关
    161. //Player位置
    162. int xPlayer = 4;
    163. int yPlayer = 5;
    164. //Player攻击范围
    165. int playerAtkMin = 8;
    166. int playerAtkMax = 15;
    167. //Player血量
    168. int playerHP = 100;
    169. //Player图标
    170. string playerIcon = "●";
    171. //Player颜色
    172. ConsoleColor playerColor = ConsoleColor.Yellow;
    173. //玩家输入的内容
    174. char playerInput;
    175. #endregion
    176. #region 公主属性
    177. int xprincess = 24;
    178. int yprincess = 5;
    179. string princessIcon = "▲";
    180. ConsoleColor princessColor = ConsoleColor.Blue;
    181. #endregion
    182. #region 玩家战斗状态
    183. bool isFight = false;
    184. bool isOver = false;//跳出while
    185. #endregion
    186. //游戏场景死循环
    187. while (true)
    188. {
    189. //boss活着
    190. if (bossHP > 0)
    191. {
    192. //绘制boss
    193. Console.SetCursorPosition(xBoss, yBoss);
    194. Console.ForegroundColor = bossColor;
    195. Console.Write(bossIcon);
    196. }
    197. #region 公主模块
    198. else
    199. {
    200. Console.SetCursorPosition(xprincess, yprincess);
    201. Console.ForegroundColor = princessColor;
    202. Console.Write(princessIcon);
    203. }
    204. #endregion
    205. #region 玩家移动
    206. //绘制玩家
    207. Console.SetCursorPosition(xPlayer, yPlayer);
    208. Console.ForegroundColor = playerColor;
    209. Console.Write(playerIcon);
    210. //玩家移动
    211. //得到玩家输入
    212. playerInput = Console.ReadKey(true).KeyChar;
    213. //判断是否为战斗状态
    214. if (isFight)
    215. {
    216. //执行战斗状态逻辑
    217. if (playerInput == 'j'|| playerInput == 'J'){
    218. //判断boss和玩家是否死亡
    219. if (playerHP <= 0)
    220. {
    221. //游戏结束
    222. nowId = 3;//结束游戏
    223. gameOverInfo = "游戏失败";
    224. break;
    225. }else if (bossHP <= 0)
    226. {
    227. //营救公主
    228. //先把boss擦除
    229. Console.SetCursorPosition(xBoss, yBoss);
    230. Console.Write(" ");
    231. isFight = false;
    232. }
    233. else
    234. {
    235. //玩家攻击boss
    236. Random rd = new Random();
    237. int atk = rd.Next(playerAtkMin, playerAtkMax);
    238. bossHP -= atk;
    239. //打印信息
    240. Console.ForegroundColor = ConsoleColor.Green;
    241. //擦除信息
    242. Console.SetCursorPosition(2, height - 4);
    243. Console.Write(" ");
    244. //再写新的信息
    245. Console.SetCursorPosition(2, height - 4);
    246. Console.Write("你对boss造成了{0}伤害,boss剩余血量为{1}", atk, bossHP);
    247. //boss攻击玩家
    248. if (bossHP > 0)
    249. {
    250. atk = rd.Next(bossAtkMin, bossAtkMax);
    251. playerHP -= atk;
    252. //打印信息
    253. Console.ForegroundColor = ConsoleColor.Yellow;
    254. //擦除信息
    255. Console.SetCursorPosition(2, height - 3);
    256. Console.Write(" ");
    257. //再写新的信息
    258. //玩家死亡
    259. if (playerHP < 0)
    260. {
    261. Console.SetCursorPosition(2, height - 3);
    262. Console.Write("很遗憾,你未能通过boss的试炼,战败了(按J结束)");
    263. }
    264. else
    265. {
    266. Console.SetCursorPosition(2, height - 3);
    267. Console.Write("boss对你造成了{0}伤害,你的剩余血量为{1}", atk, playerHP);
    268. }
    269. }
    270. else
    271. {
    272. //擦除战斗信息
    273. Console.SetCursorPosition(2, height - 5);
    274. Console.Write(" ");
    275. Console.SetCursorPosition(2, height - 4);
    276. Console.Write(" ");
    277. Console.SetCursorPosition(2, height - 3);
    278. Console.Write(" ");
    279. //显示胜利信息
    280. Console.SetCursorPosition(2, height - 5);
    281. Console.Write("你战胜了boss,快去营救公主(先按J键解除战斗)");
    282. Console.SetCursorPosition(2, height - 4);
    283. Console.Write("请前往公主身边,按J键进行营救");
    284. }
    285. }
    286. }
    287. }
    288. else
    289. {
    290. //执行非战斗状态逻辑
    291. //擦除
    292. Console.SetCursorPosition(xPlayer, yPlayer);//设置光标位置
    293. Console.Write(" ");//擦除
    294. //改位置
    295. switch (playerInput)
    296. {
    297. case 'w':
    298. case 'W':
    299. --yPlayer;
    300. if (yPlayer < 1)
    301. {
    302. yPlayer = 1;
    303. }//主角与boss重合,但是boss没有死
    304. else if (xPlayer == xBoss && yPlayer == yBoss && bossHP > 0)
    305. {
    306. //拉回去
    307. ++yPlayer;
    308. }else if (xPlayer == xprincess && yPlayer == yprincess && bossHP <= 0)
    309. {
    310. //拉回去
    311. ++yPlayer;
    312. }
    313. break;
    314. case 'a':
    315. case 'A':
    316. xPlayer -= 2;
    317. if (xPlayer < 2)
    318. {
    319. xPlayer = 2;
    320. }//主角与boss重合,但是boss没有死
    321. else if (xPlayer == xBoss && yPlayer == yBoss && bossHP > 0)
    322. {
    323. //拉回去
    324. xPlayer += 2;
    325. }
    326. else if (xPlayer == xprincess && yPlayer == yprincess && bossHP <= 0)
    327. {
    328. //拉回去
    329. xPlayer += 2;
    330. }
    331. break;
    332. case 's':
    333. case 'S':
    334. ++yPlayer;
    335. if (yPlayer > height - 8)
    336. {
    337. yPlayer = height - 8;
    338. }//主角与boss重合,但是boss没有死
    339. else if (xPlayer == xBoss && yPlayer == yBoss && bossHP > 0)
    340. {
    341. //拉回去
    342. --yPlayer;
    343. }
    344. else if (xPlayer == xprincess && yPlayer == yprincess && bossHP <= 0)
    345. {
    346. //拉回去
    347. --yPlayer;
    348. }
    349. break;
    350. case 'd':
    351. case 'D':
    352. xPlayer += 2;
    353. if (xPlayer > width - 4)
    354. {
    355. xPlayer = width - 4;
    356. }//主角与boss重合,但是boss没有死
    357. else if (xPlayer == xBoss && yPlayer == yBoss && bossHP > 0)
    358. {
    359. //拉回去
    360. xPlayer -= 2;
    361. }
    362. else if (xPlayer == xprincess && yPlayer == yprincess && bossHP <= 0)
    363. {
    364. //拉回去
    365. xPlayer -= 2;
    366. }
    367. break;
    368. case 'j':
    369. case 'J':
    370. //开始战斗,玩家不再移动,下方显示信息
    371. if ((xPlayer == xBoss && yPlayer == yBoss - 1 ||
    372. xPlayer == xBoss && yPlayer == yBoss + 1 ||
    373. yPlayer == yBoss && xPlayer == xBoss - 2 ||
    374. yPlayer == yBoss && xPlayer == xBoss + 2) && bossHP > 0
    375. )
    376. {
    377. isFight = true;
    378. //满足上述条件可以开始战斗
    379. Console.SetCursorPosition(2, height - 5);
    380. Console.ForegroundColor = ConsoleColor.White;
    381. Console.Write("开始和boss战斗了,按J键继续");
    382. Console.SetCursorPosition(2, height - 4);
    383. Console.Write("玩家当前血量为{0}", playerHP);
    384. Console.SetCursorPosition(2, height - 3);
    385. Console.Write("Boss当前血量为{0}", bossHP);
    386. }
    387. //判断是否在公主身边
    388. else if ((xPlayer == xprincess && yPlayer == yprincess - 1 ||
    389. xPlayer == xprincess && yPlayer == yprincess + 1 ||
    390. yPlayer == yprincess && xPlayer == xprincess - 2 ||
    391. yPlayer == yprincess && xPlayer == xprincess + 2) && bossHP<=0)
    392. {
    393. nowId = 3;
    394. gameOverInfo ="游戏通关";
    395. isOver = true;
    396. break;
    397. }
    398. break;
    399. }
    400. }
    401. #endregion
    402. if (isOver)
    403. {
    404. break;
    405. }
    406. }
    407. break;
    408. //结束场景
    409. case 3:
    410. Console.Clear();
    411. Console.SetCursorPosition(width / 2 - 5, 5);
    412. Console.ForegroundColor = ConsoleColor.White;
    413. Console.Write("GAME OVER");
    414. //失败和成功提示不一样
    415. Console.SetCursorPosition(width / 2 - 4, 7);
    416. Console.ForegroundColor = ConsoleColor.Green;
    417. Console.Write(gameOverInfo);
    418. int nowSelEndId = 0;
    419. while (true)
    420. {
    421. bool isQuitEnd = false;
    422. Console.SetCursorPosition(width / 2 - 6, 9);
    423. Console.ForegroundColor = nowSelEndId == 0 ? ConsoleColor.Red : ConsoleColor.White;
    424. Console.Write("回到开始界面");
    425. Console.SetCursorPosition(width / 2 - 4, 11);
    426. Console.ForegroundColor = nowSelEndId == 1 ? ConsoleColor.Red : ConsoleColor.White;
    427. Console.Write("退出游戏");
    428. char input = Console.ReadKey(true).KeyChar;
    429. switch (input)
    430. {
    431. case 'w':
    432. case 'W':
    433. --nowSelEndId;
    434. if (nowSelEndId < 0)
    435. {
    436. nowSelEndId = 0;
    437. }
    438. break;
    439. case 's':
    440. case 'S':
    441. ++nowSelEndId;
    442. if (nowSelEndId >1)
    443. {
    444. nowSelEndId = 1;
    445. }
    446. break;
    447. case 'j':
    448. case 'J':
    449. if (nowSelEndId == 0)
    450. {
    451. nowId = 1;
    452. isQuitEnd = true;
    453. }
    454. else
    455. {
    456. Environment.Exit(0);
    457. }
    458. break;
    459. }
    460. //为了从switch中跳出while
    461. if (isQuitEnd)
    462. {
    463. break;
    464. }
    465. }
    466. break;
    467. }
    468. }
    469. #endregion
    470. }
    471. }
    472. }

    项目演示 

    木木夕营救公主

  • 相关阅读:
    数据结构-邻接矩阵
    I2C相关实验2
    华为机试真题 C++ 实现【探索地块建立】【2022.11 Q4 新题】
    VUE项目build后自动生成zip压缩包
    Flink 支持三种时间语义
    # Itext Pdf 合并拆分
    计算机系统(11)----- 线程概念
    基于Spring Boot + Vue.js构建的前后端分离的学生社团管理系
    RK3568 SPI子系统–oled屏
    untiy加载一张本地图片到Image
  • 原文地址:https://blog.csdn.net/m0_67995737/article/details/133341013