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

2023-10-13 23:10

本文主要是介绍C#(CSharp)入门实践项目(简易回合制游戏),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

项目名称

木木夕营救公主

项目介绍

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

简单需求分析

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

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

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

界面间相互切换 

项目代码(含详细说明)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace CSharp入门_实践
{class Program{static void Main(string[] args){#region 控制台基础设置int width = 50;int height = 30;//隐藏光标Console.CursorVisible = false;//设置控制台大小Console.SetWindowSize(width, height);//设置缓冲区大小Console.SetBufferSize(width, height);#endregion#region 多场景//约定 1是开始游戏界面,2是游戏界面,3是结束界面int nowId = 1;string gameOverInfo ="" ;while (true){//不同的场景id,进行不同的逻辑switch (nowId){#region 开始场景逻辑//开始场景case 1:Console.Clear();//每次场景切换需要把上一次的场景清除Console.SetCursorPosition(width / 2 - 7, 8);//设置下面文字的位置Console.Write("木木夕营救公主");int nowSelIndex = 0;//当前选项编号 0:开始游戏  1:结束游戏//输入while (true){bool isQuit = false;//退出循环标志Console.SetCursorPosition(width / 2 - 4, 12);//改变开始游戏的颜色Console.ForegroundColor = nowSelIndex == 0 ? ConsoleColor.Red : ConsoleColor.White;Console.Write("开始游戏");Console.SetCursorPosition(width / 2 - 4, 14);//改变结束游戏的颜色Console.ForegroundColor = nowSelIndex == 1 ? ConsoleColor.Red : ConsoleColor.White;Console.Write("退出游戏");//说明Console.SetCursorPosition(width / 2 - 16, 16);Console.ForegroundColor = ConsoleColor.White;Console.Write("说明:按W,S进行上下选择,J键确定");Console.SetCursorPosition(width / 2 - 10, 18);Console.Write("操作时按WASD进行移动");Console.ForegroundColor = ConsoleColor.Green;Console.SetCursorPosition(width / 2 - 10, 20);Console.Write("★");Console.ForegroundColor = ConsoleColor.White;Console.Write("为boss");Console.ForegroundColor = ConsoleColor.Yellow;Console.SetCursorPosition(width / 2 - 10, 21);Console.Write("●");Console.ForegroundColor = ConsoleColor.White;Console.Write("为玩家木木夕");Console.ForegroundColor = ConsoleColor.Blue;Console.SetCursorPosition(width / 2 - 10, 22);Console.Write("▲");Console.ForegroundColor = ConsoleColor.White;Console.Write("为公主");Console.SetCursorPosition(width / 2 - 18, 23);Console.Write("找到boss,进行挑战,按J键进行一次攻击");//检测玩家输入,根据输入内容进行跳转char input = Console.ReadKey(true).KeyChar;//检测输入并赋值,不显示在控制台switch (input){case 'w':case 'W':--nowSelIndex;if (nowSelIndex < 0){nowSelIndex = 0;}break;case 's':case 'S':++nowSelIndex;if (nowSelIndex > 1){nowSelIndex = 1;}break;case 'j':case 'J'://确定按钮if (nowSelIndex == 0){//进入游戏//改变当前场景idnowId = 2;//退出内层while循环isQuit = true;}else{//关闭控制台Environment.Exit(0);}break;}if (isQuit == true){break;//跳出该层循环}}break;#endregion//游戏场景case 2:Console.Clear();#region 红墙Console.ForegroundColor = ConsoleColor.Red;//设置颜色//画墙for (int i = 0; i < width-2; i += 2){//上方Console.SetCursorPosition(i, 0);//设置位置Console.Write("■");//下方Console.SetCursorPosition(i, height - 2);//设置位置Console.Write("■");//中间Console.SetCursorPosition(i, height - 7);//设置位置Console.Write("■");}for (int i = 0; i <height-1; i++){//左边Console.SetCursorPosition(0, i);//设置位置Console.Write("■");//右边Console.SetCursorPosition(width-2, i);//设置位置Console.Write("■");}#endregion#region boss属性相关//boss位置int xBoss = 24;int yBoss = 15;//boss攻击范围int bossAtkMin = 9;int bossAtkMax = 16;//boss血量int bossHP = 100;//boss图标string bossIcon = "★";//boss颜色ConsoleColor bossColor = ConsoleColor.Green;#endregion#region 玩家属性相关//Player位置int xPlayer = 4;int yPlayer = 5;//Player攻击范围int playerAtkMin = 8;int playerAtkMax = 15;//Player血量int playerHP = 100;//Player图标string playerIcon = "●";//Player颜色ConsoleColor playerColor = ConsoleColor.Yellow;//玩家输入的内容char playerInput;#endregion#region 公主属性int xprincess = 24;int yprincess = 5;string princessIcon = "▲";ConsoleColor princessColor = ConsoleColor.Blue;#endregion#region 玩家战斗状态bool isFight = false;bool isOver = false;//跳出while#endregion//游戏场景死循环while (true){//boss活着if (bossHP > 0){//绘制bossConsole.SetCursorPosition(xBoss, yBoss);Console.ForegroundColor = bossColor;Console.Write(bossIcon);}#region 公主模块else{Console.SetCursorPosition(xprincess, yprincess);Console.ForegroundColor = princessColor;Console.Write(princessIcon);}#endregion#region 玩家移动//绘制玩家Console.SetCursorPosition(xPlayer, yPlayer);Console.ForegroundColor = playerColor;Console.Write(playerIcon);//玩家移动//得到玩家输入playerInput = Console.ReadKey(true).KeyChar;//判断是否为战斗状态if (isFight){//执行战斗状态逻辑if (playerInput == 'j'|| playerInput == 'J'){//判断boss和玩家是否死亡if (playerHP <= 0){//游戏结束nowId = 3;//结束游戏gameOverInfo = "游戏失败";break;}else if (bossHP <= 0){//营救公主//先把boss擦除Console.SetCursorPosition(xBoss, yBoss);Console.Write("  ");isFight = false;}else{//玩家攻击bossRandom rd = new Random();int atk = rd.Next(playerAtkMin, playerAtkMax);bossHP -= atk;//打印信息Console.ForegroundColor = ConsoleColor.Green;//擦除信息Console.SetCursorPosition(2, height - 4);Console.Write("                                               ");//再写新的信息Console.SetCursorPosition(2, height - 4);Console.Write("你对boss造成了{0}伤害,boss剩余血量为{1}", atk, bossHP);//boss攻击玩家if (bossHP > 0){atk = rd.Next(bossAtkMin, bossAtkMax);playerHP -= atk;//打印信息Console.ForegroundColor = ConsoleColor.Yellow;//擦除信息Console.SetCursorPosition(2, height - 3);Console.Write("                                               ");//再写新的信息//玩家死亡if (playerHP < 0){Console.SetCursorPosition(2, height - 3);Console.Write("很遗憾,你未能通过boss的试炼,战败了(按J结束)");}else{Console.SetCursorPosition(2, height - 3);Console.Write("boss对你造成了{0}伤害,你的剩余血量为{1}", atk, playerHP);}}else{//擦除战斗信息Console.SetCursorPosition(2, height - 5);Console.Write("                                               ");Console.SetCursorPosition(2, height - 4);Console.Write("                                               ");Console.SetCursorPosition(2, height - 3);Console.Write("                                               ");//显示胜利信息Console.SetCursorPosition(2, height - 5);Console.Write("你战胜了boss,快去营救公主(先按J键解除战斗)");Console.SetCursorPosition(2, height - 4);Console.Write("请前往公主身边,按J键进行营救");}}}}else{//执行非战斗状态逻辑//擦除Console.SetCursorPosition(xPlayer, yPlayer);//设置光标位置Console.Write("  ");//擦除//改位置switch (playerInput){case 'w':case 'W':--yPlayer;if (yPlayer < 1){yPlayer = 1;}//主角与boss重合,但是boss没有死else if (xPlayer == xBoss && yPlayer == yBoss && bossHP > 0){//拉回去++yPlayer;}else if (xPlayer == xprincess && yPlayer == yprincess && bossHP <= 0){//拉回去++yPlayer;}break;case 'a':case 'A':xPlayer -= 2;if (xPlayer < 2){xPlayer = 2;}//主角与boss重合,但是boss没有死else if (xPlayer == xBoss && yPlayer == yBoss && bossHP > 0){//拉回去xPlayer += 2;}else if (xPlayer == xprincess && yPlayer == yprincess && bossHP <= 0){//拉回去xPlayer += 2;}break;case 's':case 'S':++yPlayer;if (yPlayer > height - 8){yPlayer = height - 8;}//主角与boss重合,但是boss没有死else if (xPlayer == xBoss && yPlayer == yBoss && bossHP > 0){//拉回去--yPlayer;}else if (xPlayer == xprincess && yPlayer == yprincess && bossHP <= 0){//拉回去--yPlayer;}break;case 'd':case 'D':xPlayer += 2;if (xPlayer > width - 4){xPlayer = width - 4;}//主角与boss重合,但是boss没有死else if (xPlayer == xBoss && yPlayer == yBoss && bossHP > 0){//拉回去xPlayer -= 2;}else if (xPlayer == xprincess && yPlayer == yprincess && bossHP <= 0){//拉回去xPlayer -= 2;}break;case 'j':case 'J'://开始战斗,玩家不再移动,下方显示信息if ((xPlayer == xBoss && yPlayer == yBoss - 1 ||xPlayer == xBoss && yPlayer == yBoss + 1 ||yPlayer == yBoss && xPlayer == xBoss - 2 ||yPlayer == yBoss && xPlayer == xBoss + 2) && bossHP > 0){isFight = true;//满足上述条件可以开始战斗Console.SetCursorPosition(2, height - 5);Console.ForegroundColor = ConsoleColor.White;Console.Write("开始和boss战斗了,按J键继续");Console.SetCursorPosition(2, height - 4);Console.Write("玩家当前血量为{0}", playerHP);Console.SetCursorPosition(2, height - 3);Console.Write("Boss当前血量为{0}", bossHP);}//判断是否在公主身边else if ((xPlayer == xprincess && yPlayer == yprincess - 1 ||xPlayer == xprincess && yPlayer == yprincess + 1 ||yPlayer == yprincess && xPlayer == xprincess - 2 ||yPlayer == yprincess && xPlayer == xprincess + 2) && bossHP<=0){nowId = 3;gameOverInfo ="游戏通关";isOver = true;break;}break;}}#endregionif (isOver){break;}}break;//结束场景case 3:Console.Clear();Console.SetCursorPosition(width / 2 - 5, 5);Console.ForegroundColor = ConsoleColor.White;Console.Write("GAME  OVER");//失败和成功提示不一样Console.SetCursorPosition(width / 2 - 4, 7);Console.ForegroundColor = ConsoleColor.Green;Console.Write(gameOverInfo);int nowSelEndId = 0;while (true){bool isQuitEnd = false;Console.SetCursorPosition(width / 2 - 6, 9);Console.ForegroundColor = nowSelEndId == 0 ? ConsoleColor.Red : ConsoleColor.White;Console.Write("回到开始界面");Console.SetCursorPosition(width / 2 - 4, 11);Console.ForegroundColor = nowSelEndId == 1 ? ConsoleColor.Red : ConsoleColor.White;Console.Write("退出游戏");char input = Console.ReadKey(true).KeyChar;switch (input){case 'w':case 'W':--nowSelEndId;if (nowSelEndId < 0){nowSelEndId = 0;}break;case 's':case 'S':++nowSelEndId;if (nowSelEndId >1){nowSelEndId = 1;}break;case 'j':case 'J':if (nowSelEndId == 0){nowId = 1;isQuitEnd = true;}else{Environment.Exit(0);}break;}//为了从switch中跳出whileif (isQuitEnd){break;}}break;}}#endregion}}
}

项目演示 

木木夕营救公主

这篇关于C#(CSharp)入门实践项目(简易回合制游戏)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:https://blog.csdn.net/m0_67995737/article/details/133341013
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/206504

相关文章

springboot项目中使用JOSN解析库的方法

《springboot项目中使用JOSN解析库的方法》JSON,全程是JavaScriptObjectNotation,是一种轻量级的数据交换格式,本文给大家介绍springboot项目中使用JOSN... 目录一、jsON解析简介二、Spring Boot项目中使用JSON解析1、pom.XML文件引入依

MyBatis编写嵌套子查询的动态SQL实践详解

《MyBatis编写嵌套子查询的动态SQL实践详解》在Java生态中,MyBatis作为一款优秀的ORM框架,广泛应用于数据库操作,本文将深入探讨如何在MyBatis中编写嵌套子查询的动态SQL,并结... 目录一、Myhttp://www.chinasem.cnBATis动态SQL的核心优势1. 灵活性与可

pytest+allure环境搭建+自动化实践过程

《pytest+allure环境搭建+自动化实践过程》:本文主要介绍pytest+allure环境搭建+自动化实践过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录一、pytest下载安装1.1、安装pytest1.2、检测是否安装成功二、allure下载安装2.

C#代码实现解析WTGPS和BD数据

《C#代码实现解析WTGPS和BD数据》在现代的导航与定位应用中,准确解析GPS和北斗(BD)等卫星定位数据至关重要,本文将使用C#语言实现解析WTGPS和BD数据,需要的可以了解下... 目录一、代码结构概览1. 核心解析方法2. 位置信息解析3. 经纬度转换方法4. 日期和时间戳解析5. 辅助方法二、L

使用vscode搭建pywebview集成vue项目实践

《使用vscode搭建pywebview集成vue项目实践》:本文主要介绍使用vscode搭建pywebview集成vue项目实践,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录环境准备项目源码下载项目说明调试与生成可执行文件核心代码说明总结本节我们使用pythonpywebv

Python中OpenCV与Matplotlib的图像操作入门指南

《Python中OpenCV与Matplotlib的图像操作入门指南》:本文主要介绍Python中OpenCV与Matplotlib的图像操作指南,本文通过实例代码给大家介绍的非常详细,对大家的学... 目录一、环境准备二、图像的基本操作1. 图像读取、显示与保存 使用OpenCV操作2. 像素级操作3.

Mybatis嵌套子查询动态SQL编写实践

《Mybatis嵌套子查询动态SQL编写实践》:本文主要介绍Mybatis嵌套子查询动态SQL编写方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录前言一、实体类1、主类2、子类二、Mapper三、XML四、详解总结前言MyBATis的xml文件编写动态SQL

apache的commons-pool2原理与使用实践记录

《apache的commons-pool2原理与使用实践记录》ApacheCommonsPool2是一个高效的对象池化框架,通过复用昂贵资源(如数据库连接、线程、网络连接)优化系统性能,这篇文章主... 目录一、核心原理与组件二、使用步骤详解(以数据库连接池为例)三、高级配置与优化四、典型应用场景五、注意事

Maven项目中集成数据库文档生成工具的操作步骤

《Maven项目中集成数据库文档生成工具的操作步骤》在Maven项目中,可以通过集成数据库文档生成工具来自动生成数据库文档,本文为大家整理了使用screw-maven-plugin(推荐)的完... 目录1. 添加插件配置到 pom.XML2. 配置数据库信息3. 执行生成命令4. 高级配置选项5. 注意事

使用C#删除Excel表格中的重复行数据的代码详解

《使用C#删除Excel表格中的重复行数据的代码详解》重复行是指在Excel表格中完全相同的多行数据,删除这些重复行至关重要,因为它们不仅会干扰数据分析,还可能导致错误的决策和结论,所以本文给大家介绍... 目录简介使用工具C# 删除Excel工作表中的重复行语法工作原理实现代码C# 删除指定Excel单元