当前位置: 首页 > news >正文

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)入门实践项目(简易回合制游戏)

项目名称 木木夕营救公主 项目介绍 这是一个小游戏&#xff0c;你将扮演一个英雄&#xff08;木木夕&#xff09;&#xff0c;去打败恶龙&#xff0c;拯救出公主&#xff0c;该项目采用回合制战斗模式&#xff0c;由于角色的血量和攻击为随机数&#xff0c;所以需要靠运气才…...

GEO生信数据挖掘(五)提取临床信息构建分组,分组数据可视化(绘制层次聚类图,绘制PCA图)

检索到目标数据集后&#xff0c;开始数据挖掘&#xff0c;本文以阿尔兹海默症数据集GSE1297为例 上节做了很多的基因数据清洗&#xff08;离群值处理、低表达基因、归一化、log2处理&#xff09;操作&#xff0c;本节介绍构建临床分组信息。 我们已经学习了提取表达矩阵的临床…...

golang时间问题汇总(用法常见问题:插入数据库时间自动+8)

golang时间问题汇总&#xff08;用法&常见问题&#xff09; 1 用法 1.1 time.Parse() func main() {timeStr : "2023-09-26 20:56:23"allDate, _ : time.Parse("2006-01-02 15:04:05", timeStr)fmt.Println("全部解析", allDate) timeStr…...

TCP网络连接中的三次握手和四次挥手

作者&#xff1a;逍遥Sean 简介&#xff1a;一个主修Java的Web网站\游戏服务器后端开发者 主页&#xff1a;https://blog.csdn.net/Ureliable 觉得博主文章不错的话&#xff0c;可以三连支持一下~ 如有需要我的支持&#xff0c;请私信或评论留言&#xff01; TCP网络连接中的三…...

游戏服务商Latis Global参展2023 ChinaJoy B2B

第20届ChinaJoy于2023年7月在上海举行了为期四天的博览会,参展观众达到了33.8万人次。ChinaJoy是全球最具知名度与影响力的年度盛会之一,涵盖了包括游戏、动漫、互联网影视、电子竞技、潮流玩具、智能娱乐在内的多个数字娱乐领域。ChinaJoy不仅仅代表了数字娱乐领域的最新风向,…...

oracle常用sql

oracle常用sql oracle常用sql查询当前会话id(sid),会话序列号(serial#),操作系统进程id(spid)查询数据库信息查询实例信息查询字符集查看回收站情况数据库系统PSU信息数据库大小查看表空间状况常规库表空间情况查询,非CDBCBD表空间情况查询当前客户端信息资源使用情况…...

手游模拟器长时间运行后,游戏掉帧且不恢复

1&#xff09;手游模拟器长时间运行后&#xff0c;游戏掉帧且不恢复 2&#xff09;FrameBuffer Fetch无论哪种模式在确定支持的手机上显示全紫 3&#xff09;协程中yield return CoFunction()和yield return StartCoroutine(CoFunction())的区别 这是第353篇UWA技术知识分享的推…...

linux下离线安装telnet

安装过程概要&#xff1a; &#xff08;一&#xff09;互联网端下载rpm包&#xff1b; &#xff08;二&#xff09;上传到服务器root目录下&#xff1b; &#xff08;三&#xff09;安装telnet服务和测试&#xff1a; 详细内容&#xff1a; &#xff08;一&#xff09;互联…...

Unity 发布WebGL平台,C#与JavaScript交互

发布H5平台&#xff0c;接入SDK&#xff0c;比如微信等&#xff0c;涉及到C#与JS的交互。 jslib&#xff08;JavaScript Library&#xff09;是Unity的一种机制&#xff0c;允许你在C#中通过JavaScript代码来执行一些操作。这是一种高级的技巧&#xff0c;主要用于一些特殊情况…...

利用 Forcing InnoDB Recovery 特性解决 MySQL 重启失败的问题

问题 由于异常断电或者系统异常重启时 MySQL 没有正常退出导致 MySQL 无法启动&#xff0c;启动时报错如下&#xff1a; [System] [Server] /usr/sbin/mysqld (mysqld 8.0.30) starting as process 2665 [System] [InnoDB] InnoDB initialization has started. [System] [Inn…...

windows修改键位F11变insert(改键盘映射)

这里是通过改变windows的注册表来实现的 1.按住winr打开运行&#xff0c;在运行中输入“regedit”&#xff0c;再点击“确定”按钮。如下图 2.找到注册表的目录 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Keyboard Layout 3.在Keyboard Layout右击新建 -> 二进…...

安装gpu版本的paddle和paddleclas

安装gpu版本的paddle python -m pip install paddlepaddle-gpu2.3.2.post111 -f https://www.paddlepaddle.org.cn/whl/windows/mkl/avx/stable.html以上支持cuda11.1版本 其他需求可查阅文档在这里 安装paddleclas 1 在虚拟环境中安装所需的Python库&#xff1a; pip inst…...

61从零开始学Java之处理大数字相关的类有哪些?

作者&#xff1a;孙玉昌&#xff0c;昵称【一一哥】&#xff0c;另外【壹壹哥】也是我哦 千锋教育高级教研员、CSDN博客专家、万粉博主、阿里云专家博主、掘金优质作者 前言 我们知道&#xff0c;在现实世界里&#xff0c;实际上数字是有无穷个的&#xff0c;就比如0和1之间&a…...

vscode 搜索界面的files to include files to exclude 是什么功能?

在VSCode&#xff08;Visual Studio Code&#xff09;中&#xff0c;搜索功能是一个强大的工具&#xff0c;可以帮助你在项目中快速查找特定的文本、代码或其他内容。搜索界面的 “files to include” 和 “files to exclude” 提供了一种方式来定制你的搜索范围。 files to in…...

数据计算-第15届蓝桥杯第一次STEMA测评Scratch真题精选

[导读]&#xff1a;超平老师的《Scratch蓝桥杯真题解析100讲》已经全部完成&#xff0c;后续会不定期解读蓝桥杯真题&#xff0c;这是Scratch蓝桥杯真题解析第154讲。 第15届蓝桥杯第1次STEMA测评已于2023年8月20日落下帷幕&#xff0c;编程题一共有6题&#xff0c;分别如下&a…...

谈谈前端和后端的选择

引言 在我的印象中&#xff0c;也是视线里&#xff0c;后端都是在一个黑屏的页面&#xff0c;左边一个文件类&#xff0c;右边在不停的写sql,一只手放在键盘上&#xff0c;一边写&#xff0c;一遍不停的关联进入&#xff0c;感觉很无趣&#xff0c;他们的分享不是什么java集成&…...

Vue3最佳实践 第六章 Pinia,Vuex与axios,VueUse 1(Pinia)

Pinia状态管理 在 Vue3 中项目中组件之间传递数据时&#xff0c;可以使用 Props 和 Emit&#xff0c;还可以使用 Provide/Inject 来代替 Props 和 Emit。Props 和 Emit 只能在具有父子关系的组件之间传递数据&#xff0c;所以层级越深&#xff0c;过程就越复杂。为了解决此类问…...

Java比较器之equals、comparable、comparator

文章目录 前言一、基本类型比较1.2.equals3.和equals的区别 二、对象的比较1.覆写基类的equals2.基于Comparable接口类的比较3.基于Comparator比较器比较4.三种方式对比 前言 在Java中&#xff0c;基本类型的对象可以直接比较&#xff0c;而自定义类型&#xff0c;默认是用equ…...

Virtio-user使用简介

一、简述​ DPDK支持几种方式让用户空间的报文重新进入内核协议栈(这种dpdk和kernel直接通信的路径叫做exception path)&#xff0c;例如tap/tun设备使用&#xff0c;kni&#xff0c;Virtio-user。这里主要讲Virtio-user使用&#xff0c;Virtio-user是virtio PMD的虚拟设备&…...

点云从入门到精通技术详解100篇-基于深度学习的三维植物点云分割网络

目录 前言 研究现状及趋势 传统的植物表型分割方法 现行的植物表型分割方法...

(十)学生端搭建

本次旨在将之前的已完成的部分功能进行拼装到学生端&#xff0c;同时完善学生端的构建。本次工作主要包括&#xff1a; 1.学生端整体界面布局 2.模拟考场与部分个人画像流程的串联 3.整体学生端逻辑 一、学生端 在主界面可以选择自己的用户角色 选择学生则进入学生登录界面…...

Golang 面试经典题:map 的 key 可以是什么类型?哪些不可以?

Golang 面试经典题&#xff1a;map 的 key 可以是什么类型&#xff1f;哪些不可以&#xff1f; 在 Golang 的面试中&#xff0c;map 类型的使用是一个常见的考点&#xff0c;其中对 key 类型的合法性 是一道常被提及的基础却很容易被忽视的问题。本文将带你深入理解 Golang 中…...

[ICLR 2022]How Much Can CLIP Benefit Vision-and-Language Tasks?

论文网址&#xff1a;pdf 英文是纯手打的&#xff01;论文原文的summarizing and paraphrasing。可能会出现难以避免的拼写错误和语法错误&#xff0c;若有发现欢迎评论指正&#xff01;文章偏向于笔记&#xff0c;谨慎食用 目录 1. 心得 2. 论文逐段精读 2.1. Abstract 2…...

屋顶变身“发电站” ,中天合创屋面分布式光伏发电项目顺利并网!

5月28日&#xff0c;中天合创屋面分布式光伏发电项目顺利并网发电&#xff0c;该项目位于内蒙古自治区鄂尔多斯市乌审旗&#xff0c;项目利用中天合创聚乙烯、聚丙烯仓库屋面作为场地建设光伏电站&#xff0c;总装机容量为9.96MWp。 项目投运后&#xff0c;每年可节约标煤3670…...

P3 QT项目----记事本(3.8)

3.8 记事本项目总结 项目源码 1.main.cpp #include "widget.h" #include <QApplication> int main(int argc, char *argv[]) {QApplication a(argc, argv);Widget w;w.show();return a.exec(); } 2.widget.cpp #include "widget.h" #include &q…...

学校时钟系统,标准考场时钟系统,AI亮相2025高考,赛思时钟系统为教育公平筑起“精准防线”

2025年#高考 将在近日拉开帷幕&#xff0c;#AI 监考一度冲上热搜。当AI深度融入高考&#xff0c;#时间同步 不再是辅助功能&#xff0c;而是决定AI监考系统成败的“生命线”。 AI亮相2025高考&#xff0c;40种异常行为0.5秒精准识别 2025年高考即将拉开帷幕&#xff0c;江西、…...

让回归模型不再被异常值“带跑偏“,MSE和Cauchy损失函数在噪声数据环境下的实战对比

在机器学习的回归分析中&#xff0c;损失函数的选择对模型性能具有决定性影响。均方误差&#xff08;MSE&#xff09;作为经典的损失函数&#xff0c;在处理干净数据时表现优异&#xff0c;但在面对包含异常值的噪声数据时&#xff0c;其对大误差的二次惩罚机制往往导致模型参数…...

【生成模型】视频生成论文调研

工作清单 上游应用方向&#xff1a;控制、速度、时长、高动态、多主体驱动 类型工作基础模型WAN / WAN-VACE / HunyuanVideo控制条件轨迹控制ATI~镜头控制ReCamMaster~多主体驱动Phantom~音频驱动Let Them Talk: Audio-Driven Multi-Person Conversational Video Generation速…...

JS设计模式(4):观察者模式

JS设计模式(4):观察者模式 一、引入 在开发中&#xff0c;我们经常会遇到这样的场景&#xff1a;一个对象的状态变化需要自动通知其他对象&#xff0c;比如&#xff1a; 电商平台中&#xff0c;商品库存变化时需要通知所有订阅该商品的用户&#xff1b;新闻网站中&#xff0…...

JS手写代码篇----使用Promise封装AJAX请求

15、使用Promise封装AJAX请求 promise就有reject和resolve了&#xff0c;就不必写成功和失败的回调函数了 const BASEURL ./手写ajax/test.jsonfunction promiseAjax() {return new Promise((resolve, reject) > {const xhr new XMLHttpRequest();xhr.open("get&quo…...