文章目录
基本c#语法入门之后的实践
提示:以下是本篇文章正文内容,下面案例可供参考
一、JBJHON营救公主
using System;
namespace CSharp实践
{
class Program
{
static void Main(string[] args)
{
//设置窗口大小和基本界面配置
Console.SetWindowSize(60, 40);
Console.SetBufferSize(60, 50);
Console.CursorVisible = false;
//切换游戏场景 0 - 开始场景 1 - 游戏场景 2 - 游戏结束场景
int index = 0;
//设置结束场景的标语
string endTitle = new string("");
while (true)
{
Console.Clear();
if (index == 0)
{
//进入游戏界面的UI
string title = "JBJHON营救公主";
string start = "开始游戏";
string end = "结束游戏";
Console.SetCursorPosition(33 - title.Length, 5);
Console.Write(title);
//选择按钮
char select;
//选择编号
int selectIndex = 0; //默认为0
bool quit = false;
while (true)
{
if (quit)
break;
//为每个选项添加一个编号 方便管理存储
Console.ForegroundColor = (selectIndex == 0) ? ConsoleColor.Red : ConsoleColor.White;
Console.SetCursorPosition(30 - end.Length, 7);
Console.Write(start);
Console.ForegroundColor = (selectIndex == 1) ? ConsoleColor.Red : ConsoleColor.White;
Console.SetCursorPosition(30 - end.Length, 9);
Console.Write(end);
select = Console.ReadKey(true).KeyChar;
switch (select)
{
case 'w':
case 'W':
selectIndex = 0;
break;
case 's':
case 'S':
selectIndex = 1;
break;
case 'j':
case 'J':
if (selectIndex == 0)
{
index = 1;
quit = true;
}
else
Environment.Exit(0);
break;
}
}
}
if(index == 1)
{
Console.Clear();
Console.ForegroundColor = ConsoleColor.Red;
for (int i = 0; i < Console.WindowWidth - 2; i += 2