370 lines
16 KiB
C#
370 lines
16 KiB
C#
|
|
#region 控制台基础设施
|
||
|
|
using System.ComponentModel.Design;
|
||
|
|
|
||
|
|
Console.BackgroundColor = ConsoleColor.Black;
|
||
|
|
Console.Clear();
|
||
|
|
int w = 50;
|
||
|
|
int h = 30;
|
||
|
|
Console.SetWindowSize(w, h);
|
||
|
|
Console.SetBufferSize(w, h);
|
||
|
|
Console.CursorVisible = false;
|
||
|
|
#endregion
|
||
|
|
|
||
|
|
#region 多个场景
|
||
|
|
int nowScene = 1;
|
||
|
|
try
|
||
|
|
{
|
||
|
|
while (true)
|
||
|
|
{
|
||
|
|
switch (nowScene)
|
||
|
|
{
|
||
|
|
//主界面
|
||
|
|
case 1:
|
||
|
|
Console.Clear();
|
||
|
|
Console.WriteLine("开始场景");
|
||
|
|
Console.ForegroundColor = ConsoleColor.White;
|
||
|
|
Console.SetCursorPosition(w / 2 - 3, h / 4);
|
||
|
|
Console.Write("寄掰游戏");
|
||
|
|
bool notExit = true;
|
||
|
|
int nowSceneIndex = 1;
|
||
|
|
char choice = 'w';
|
||
|
|
nowScene = 2;
|
||
|
|
while (notExit)
|
||
|
|
{
|
||
|
|
Console.SetCursorPosition(w / 2 - 3, h / 3 + 1);
|
||
|
|
Console.ForegroundColor = nowSceneIndex == 1 ? ConsoleColor.Red : ConsoleColor.White;
|
||
|
|
Console.Write("开始游戏");
|
||
|
|
Console.ForegroundColor = nowSceneIndex == 0 ? ConsoleColor.Red : ConsoleColor.White;
|
||
|
|
Console.SetCursorPosition(w / 2 - 3, h / 3 + 3);
|
||
|
|
Console.Write("退出游戏");
|
||
|
|
|
||
|
|
choice = Console.ReadKey(true).KeyChar;
|
||
|
|
switch (choice)
|
||
|
|
{
|
||
|
|
case '↑':
|
||
|
|
case 'w':
|
||
|
|
case 'W':
|
||
|
|
nowScene = 2;
|
||
|
|
nowSceneIndex = 1;
|
||
|
|
break;
|
||
|
|
case '↓':
|
||
|
|
case 's':
|
||
|
|
case 'S':
|
||
|
|
nowSceneIndex = 0;
|
||
|
|
nowScene = 3;
|
||
|
|
break;
|
||
|
|
case '\r':
|
||
|
|
notExit = false;
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
break;
|
||
|
|
|
||
|
|
//游戏场景
|
||
|
|
case 2:
|
||
|
|
#region 游戏场景初始化
|
||
|
|
Console.Clear();
|
||
|
|
Console.ForegroundColor = ConsoleColor.White;
|
||
|
|
Console.WriteLine("游戏场景");
|
||
|
|
Console.SetCursorPosition(2, h - 5);
|
||
|
|
Console.Write("1.WASD移动 2.靠近BOSS按J攻击");
|
||
|
|
#region 画框
|
||
|
|
|
||
|
|
Console.ForegroundColor = ConsoleColor.Red;
|
||
|
|
for (int i = 0; i < w; i += 2)
|
||
|
|
{
|
||
|
|
for (int j = 0; j < h; j++)
|
||
|
|
{
|
||
|
|
//Console.SetCursorPosition(25, 12);
|
||
|
|
//Console.WriteLine("当前i{0},j{1}", i, j);//测试用
|
||
|
|
if (i == 0 || j == 0)
|
||
|
|
{
|
||
|
|
Console.SetCursorPosition(i, j);
|
||
|
|
Console.Write("■");
|
||
|
|
}
|
||
|
|
else if (j == Console.BufferHeight - 6)
|
||
|
|
{
|
||
|
|
Console.SetCursorPosition(i, j);
|
||
|
|
Console.Write("■");
|
||
|
|
}
|
||
|
|
else if (i == Console.BufferWidth - 2 || j == Console.BufferHeight - 1)
|
||
|
|
{
|
||
|
|
Console.SetCursorPosition(i, j);
|
||
|
|
Console.Write("■");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
#endregion
|
||
|
|
#endregion
|
||
|
|
|
||
|
|
#region 参数初始化
|
||
|
|
Random r = new Random();
|
||
|
|
notExit = true;
|
||
|
|
bool isBossDefeated = false;
|
||
|
|
bool isPlayerAlive = true;
|
||
|
|
bool isPlayerApproaching = false;
|
||
|
|
char movement;
|
||
|
|
|
||
|
|
|
||
|
|
//BOSS参数
|
||
|
|
string bossIcon = "■";
|
||
|
|
int bossHP = 100;
|
||
|
|
int bossAttackMax = 120;
|
||
|
|
int bossAttackMin = 100;
|
||
|
|
int bossDefense = 5;
|
||
|
|
int bossLocationX = r.Next(2, w - 2);
|
||
|
|
int bossLocationY = r.Next(1, h - 7);
|
||
|
|
while (bossLocationX < 2 || bossLocationY < 2 || bossLocationX > w - 4 || bossLocationY > h - 8)
|
||
|
|
{
|
||
|
|
bossLocationX = r.Next(2, w - 2);
|
||
|
|
bossLocationY = r.Next(1, h - 7);
|
||
|
|
}
|
||
|
|
|
||
|
|
//玩家参数
|
||
|
|
string playerIcon = "●";
|
||
|
|
int playerHP = 100;
|
||
|
|
int playerAttackMax = 12;
|
||
|
|
int playerAttackMin = 7;
|
||
|
|
int playerLocationX = r.Next(2, w - 2);
|
||
|
|
int playerLocationY = r.Next(1, h - 7);
|
||
|
|
while (playerLocationX == bossLocationX && playerLocationY == bossLocationY)
|
||
|
|
{
|
||
|
|
playerLocationX = r.Next(2, w - 2);
|
||
|
|
playerLocationY = r.Next(1, h - 7);
|
||
|
|
}
|
||
|
|
#endregion
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
while (notExit)
|
||
|
|
{
|
||
|
|
#region 画玩家
|
||
|
|
if (playerHP > 0)
|
||
|
|
{
|
||
|
|
Console.ForegroundColor = ConsoleColor.Green;
|
||
|
|
Console.SetCursorPosition(playerLocationX, playerLocationY);
|
||
|
|
Console.WriteLine(playerIcon);
|
||
|
|
}
|
||
|
|
#endregion
|
||
|
|
|
||
|
|
#region 画BOSS
|
||
|
|
if (bossHP > 0)
|
||
|
|
{
|
||
|
|
Console.SetCursorPosition(bossLocationX, bossLocationY);
|
||
|
|
Console.ForegroundColor = ConsoleColor.Yellow;
|
||
|
|
Console.Write(bossIcon);
|
||
|
|
}
|
||
|
|
#endregion
|
||
|
|
|
||
|
|
#region 判断玩家是否到达BOSS位置
|
||
|
|
if (playerLocationY == bossLocationY && playerLocationX == bossLocationX ||
|
||
|
|
playerLocationY == bossLocationY && playerLocationX == bossLocationX - 1 ||
|
||
|
|
playerLocationY == bossLocationY && playerLocationX == bossLocationX + 1)
|
||
|
|
{
|
||
|
|
isPlayerApproaching = true;
|
||
|
|
while (isPlayerAlive)
|
||
|
|
{
|
||
|
|
Console.SetCursorPosition(2, h - 4);
|
||
|
|
Console.ForegroundColor = ConsoleColor.Red;
|
||
|
|
Console.WriteLine("Press J to join fight!");
|
||
|
|
movement = Console.ReadKey(true).KeyChar;
|
||
|
|
if (movement == 'J' || movement == 'j')
|
||
|
|
{
|
||
|
|
int round = 1;
|
||
|
|
bool isDefend = false;
|
||
|
|
int damage;
|
||
|
|
#region 初始化辅助界面
|
||
|
|
Console.SetCursorPosition(2, h - 4);
|
||
|
|
Console.WriteLine(" ");
|
||
|
|
Console.SetCursorPosition(2, h - 4);
|
||
|
|
Console.WriteLine("Combat start!");
|
||
|
|
Thread.Sleep(2000);
|
||
|
|
Console.SetCursorPosition(2, h - 4);
|
||
|
|
Console.WriteLine(" ");
|
||
|
|
#endregion
|
||
|
|
while (isPlayerAlive)
|
||
|
|
{
|
||
|
|
damage = 0;
|
||
|
|
Console.SetCursorPosition(2, h - 5);
|
||
|
|
Console.WriteLine(" ");
|
||
|
|
if (bossHP <= 0)
|
||
|
|
{
|
||
|
|
isBossDefeated = true;
|
||
|
|
Console.SetCursorPosition(2, h - 5);
|
||
|
|
Console.WriteLine("你击败了BOSS,按下任何按键继续");
|
||
|
|
Thread.Sleep(1000);
|
||
|
|
isBossDefeated = true;
|
||
|
|
notExit = true;
|
||
|
|
nowScene = 3;
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
if (playerHP <= 0)
|
||
|
|
{
|
||
|
|
isPlayerAlive = false;
|
||
|
|
Console.SetCursorPosition(2, h - 5);
|
||
|
|
Console.WriteLine("你被BOSS打败了,按下任何按键继续");
|
||
|
|
Thread.Sleep(1000);
|
||
|
|
notExit = true;
|
||
|
|
nowScene = 3;
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (round % 2 != 0)
|
||
|
|
{
|
||
|
|
Console.SetCursorPosition(2, h - 5);
|
||
|
|
Console.WriteLine("现在是你的回合,按下1防御,按下2攻击");
|
||
|
|
movement = Console.ReadKey(true).KeyChar;
|
||
|
|
if (movement == '1')
|
||
|
|
{
|
||
|
|
isDefend = true;
|
||
|
|
Console.SetCursorPosition(2, h - 5);
|
||
|
|
Console.WriteLine("你选择了防御");
|
||
|
|
Thread.Sleep(2000);
|
||
|
|
}
|
||
|
|
else if (movement == '2')
|
||
|
|
{
|
||
|
|
isDefend = false;
|
||
|
|
damage = r.Next(playerAttackMin, playerAttackMax);
|
||
|
|
bossHP -= damage;
|
||
|
|
Console.SetCursorPosition(2, h - 4);
|
||
|
|
Console.WriteLine("你攻击了BOSS,造成了{0}点伤害,BOSS剩余血量{1}", damage, bossHP);
|
||
|
|
Thread.Sleep(2000);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
Console.SetCursorPosition(2, h - 5);
|
||
|
|
Console.WriteLine("现在是BOSS的回合");
|
||
|
|
Thread.Sleep(2000);
|
||
|
|
if (isDefend = false)
|
||
|
|
{
|
||
|
|
damage = r.Next(bossAttackMin, bossAttackMax);
|
||
|
|
playerHP -= damage;
|
||
|
|
Console.SetCursorPosition(2, h - 4);
|
||
|
|
Console.WriteLine("boss攻击了你{0}点血,剩余{1}点血",damage,playerHP);
|
||
|
|
Thread.Sleep(2400);
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
damage = r.Next(bossAttackMin, bossAttackMax) / 2; //防御时伤害减半
|
||
|
|
playerHP -= damage;
|
||
|
|
Console.SetCursorPosition(2, h - 4);
|
||
|
|
Console.WriteLine("boss攻击了你{0}点血,剩余{1}点血", damage, playerHP);
|
||
|
|
Thread.Sleep(2500);
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
round++;
|
||
|
|
}
|
||
|
|
if (isBossDefeated)
|
||
|
|
{
|
||
|
|
notExit = false; //退出游戏场景
|
||
|
|
nowScene = 3; //进入结算场景
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
Console.SetCursorPosition(2, h - 4);
|
||
|
|
Console.WriteLine(" ");
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
notExit = false; //退出游戏场景
|
||
|
|
nowScene = 3; //进入结算场景
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
#endregion
|
||
|
|
|
||
|
|
#region 移动读取和处理
|
||
|
|
movement = Console.ReadKey(true).KeyChar;
|
||
|
|
//test
|
||
|
|
Console.SetCursorPosition(2, h - 2);
|
||
|
|
Console.WriteLine(movement);
|
||
|
|
#region 清除玩家当前位置
|
||
|
|
Console.SetCursorPosition(playerLocationX, playerLocationY);
|
||
|
|
Console.WriteLine(" ");
|
||
|
|
#endregion
|
||
|
|
switch (movement)
|
||
|
|
{
|
||
|
|
case 'w':
|
||
|
|
case 'W':
|
||
|
|
if (playerLocationY > 1)
|
||
|
|
{
|
||
|
|
playerLocationY--;
|
||
|
|
}
|
||
|
|
break;
|
||
|
|
case 's':
|
||
|
|
case 'S':
|
||
|
|
if (playerLocationY < h - 7)
|
||
|
|
{
|
||
|
|
playerLocationY++;
|
||
|
|
}
|
||
|
|
break;
|
||
|
|
case 'a':
|
||
|
|
case 'A':
|
||
|
|
if (playerLocationX > 2)
|
||
|
|
{
|
||
|
|
playerLocationX -= 1;
|
||
|
|
}
|
||
|
|
break;
|
||
|
|
case 'd':
|
||
|
|
case 'D':
|
||
|
|
if (playerLocationX < w - 4)
|
||
|
|
{
|
||
|
|
playerLocationX += 1;
|
||
|
|
}
|
||
|
|
break;
|
||
|
|
case 'j':
|
||
|
|
case 'J':
|
||
|
|
if (isPlayerApproaching == false)
|
||
|
|
{
|
||
|
|
Console.SetCursorPosition(2, h - 4);
|
||
|
|
Console.WriteLine("距离不足");
|
||
|
|
}
|
||
|
|
break;
|
||
|
|
case 'q':
|
||
|
|
case 'Q':
|
||
|
|
nowScene = 3; //退出游戏
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
#endregion
|
||
|
|
|
||
|
|
#region 场景更新
|
||
|
|
if (nowScene == 3)
|
||
|
|
{
|
||
|
|
notExit = false;
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
#endregion
|
||
|
|
|
||
|
|
}
|
||
|
|
break;
|
||
|
|
|
||
|
|
//结算场景
|
||
|
|
case 3:
|
||
|
|
Console.Clear();
|
||
|
|
Console.ForegroundColor = ConsoleColor.White;
|
||
|
|
Console.WriteLine("结算场景");
|
||
|
|
//Console.WriteLine("恭喜你,成功营救公主!");
|
||
|
|
Console.WriteLine("Press any key to quit");
|
||
|
|
int block = Console.ReadKey(true).KeyChar;
|
||
|
|
Environment.Exit(0);
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
catch (Exception e)
|
||
|
|
{
|
||
|
|
Console.WriteLine("Error code:{0}",e);
|
||
|
|
}
|
||
|
|
finally
|
||
|
|
{
|
||
|
|
Console.ResetColor(); //重置颜色为默认颜色
|
||
|
|
Console.Clear(); //清空控制台内容
|
||
|
|
Environment.Exit(0);
|
||
|
|
}
|
||
|
|
#endregion
|