代码
using System;
using System.Runtime.InteropServices.Marshalling;
using System.Security.Cryptography.X509Certificates;
namespace 实践
{
enum E_SceneType
{
Begin,
Game,
End,
}
enum E_Grid_Type
{
Normal,
Bomb,
Pause,
Tunnel,
}
struct Vector2
{
public int x;
public int y;
public Vector2(int x, int y)
{
this.x = x;
this.y = y;
}
}
struct Grid
{
public E_Grid_Type type;
public Vector2 pos;
public Grid (E_Grid_Type type,int x,int y )
{
this.type = type;
pos.x = x;
pos.y = y;
}
public void Draw()
{
Console.SetCursorPosition(pos.x, pos.y);
switch (type)
{
case E_Grid_Type.Normal:
Console.ForegroundColor = ConsoleColor.White;
Console.Write("□");
break;
case E_Grid_Type.Bomb:
Console.ForegroundColor = ConsoleColor.Red;
Console.Write("×");
break;
case E_Grid_Type.Pause:
Console.ForegroundColor = ConsoleColor.Blue;
Console.Write("||");
break;
case E_Grid_Type.Tunnel:
Console.ForegroundColor = ConsoleColor.DarkYellow;
Console.Write("※");
break;
}
}
}
struct Map
{
public Grid[] grids;
public Map(int x ,int y ,int num)
{
grids = new Grid[num];
int indexX = 0, indexY = 0, xStep = 2;
for(int i = 0; i < num; i++)
{
Random r = new Random();
int prop = r.Next(0,101);
if (prop < 80||i==0||i==num-1)
{
grids[i].type =E_Grid_Type.Normal;
}
else if (prop >= 80&&prop<90)
{
grids[i].type = E_Grid_Type.Bomb;
}
else if (prop >=90&&prop<95)
{
grids[i].type = E_Grid_Type.Tunnel;
}
else
{
grids[i].type = E_Grid_Type.Pause;
}
grids[i].pos=new Vector2 (x,y);
if (indexX == 10)
{
y++;
indexY++;
if (indexY == 2)
{
indexY = 0;
indexX = 0;
xStep=-xStep;
}
}
else
{
x += xStep;
indexX++;
}
}
}
public void Draw()
{
for(int i = 0;i<grids.Length;i++)
{
grids[i].Draw();
}
}
}
enum E_PlayerType
{
Player,
Com,
}
struct Player
{
public E_PlayerType type;
public int nowIndex;
public bool isPause;
public Player(int index,E_PlayerType type)
{
this.type = type;
nowIndex = index;
this.isPause = false;
}
public void Draw(Map mapInfo)
{
Grid grid = mapInfo.grids[nowIndex];
Console.SetCursorPosition(grid.pos.x,grid.pos.y);
switch (type)
{
case E_PlayerType.Player:
Console.ForegroundColor = ConsoleColor.Cyan;
Console.Write("☆");
break;
case E_PlayerType.Com:
Console.ForegroundColor = ConsoleColor.DarkRed;
Console.Write("▲");
break;
}
}
}
internal class Program
{
static void Main(string[] args)
{
int w = 50, h = 32;
Console.CursorVisible=false;
Console.SetWindowSize(w, h);
Console.SetBufferSize(w, h);
E_SceneType nowGame = E_SceneType.Begin;
while (true)
{
switch (nowGame)
{
case E_SceneType.Begin:
BeginS(w, h, ref nowGame);
break;
case E_SceneType.Game:
GameS(w, h, ref nowGame);
break;
case E_SceneType.End:
EndS(w, h, ref nowGame);
break;
}
}
}
static void BeginS(int w,int h ,ref E_SceneType nowGame)
{
Console.Clear();
int beginSelect = 0;
bool isQuitBegin=false;
while (true)
{
if (isQuitBegin)
{
break;
}
Console.SetCursorPosition(w / 2 - 3, 5);
Console.ForegroundColor = ConsoleColor.White;
Console.Write("飞行棋");
Console.SetCursorPosition(w / 2 - 4, 10);
Console.ForegroundColor = beginSelect == 0 ? ConsoleColor.Red : ConsoleColor.White;
Console.Write("开始游戏");
Console.ForegroundColor = beginSelect == 1 ? ConsoleColor.Red : ConsoleColor.White;
Console.SetCursorPosition(w / 2 - 4, 12);
Console.Write("退出游戏");
switch (Console.ReadKey(true).Key)
{
case ConsoleKey.W:
beginSelect--;
if(beginSelect < 0)
{
beginSelect = 0;
}
break;
case ConsoleKey.S:
beginSelect++;
if (beginSelect>1)
{
beginSelect = 1;
}
break;
case ConsoleKey.J:
if(beginSelect==0)
{
nowGame = E_SceneType.Game;
isQuitBegin = true;
}
else
{
Environment.Exit(0);
}
break;
}
}
}
static void DrawWall(int w, int h)
{
Console.Clear();
Console.ForegroundColor = ConsoleColor.Red;
for (int i = 0; i < w; i+=2)
{
Console.SetCursorPosition(i, 0);
Console.Write("■");
Console.SetCursorPosition(i, h-1);
Console.Write("■");
Console.SetCursorPosition(i, h - 5);
Console.Write("■");
Console.SetCursorPosition(i, h - 9);
Console.Write("■");
}
for (int i = 0;i < h;i++)
{
Console.SetCursorPosition(0, i);
Console.Write("■");
Console.SetCursorPosition(w-2, i);
Console.Write("■");
}
Console.SetCursorPosition(2, h-8);
Console.ForegroundColor= ConsoleColor.White;
Console.Write("□:安全位置");
Console.SetCursorPosition(17, h - 8);
Console.ForegroundColor = ConsoleColor.Blue;
Console.Write("||:时空裂缝,暂停一回合");
Console.SetCursorPosition(2, h - 7);
Console.ForegroundColor = ConsoleColor.Red;
Console.Write("×:炸弹,倒退五格");
Console.SetCursorPosition(2, h - 6);
Console.ForegroundColor = ConsoleColor.DarkYellow;
Console.Write("※:隧道,交换双方位置");
Console.SetCursorPosition(23, h - 7);
Console.ForegroundColor = ConsoleColor.Cyan;
Console.Write("☆:玩家");
Console.SetCursorPosition(33, h - 7);
Console.ForegroundColor = ConsoleColor.DarkRed;
Console.Write("▲:电脑");
Console.SetCursorPosition(26, h - 6);
Console.ForegroundColor = ConsoleColor.DarkGreen;
Console.Write("★:双方重合");
}
static void DrawPlayer(Player player,Player com,Map map)
{
if (player.nowIndex == com.nowIndex)
{
Grid grid = map.grids[com.nowIndex];
Console.SetCursorPosition(grid.pos.x,grid.pos.y);
Console.ForegroundColor = ConsoleColor.DarkGreen;
Console.Write("★");
}
else
{
player.Draw(map);
com.Draw(map);
}
}
static bool RollDice(int w,int h,ref Player p1,ref Player p2,Map map)
{
if (p1.isPause)
{
Console.ForegroundColor = p1.type == E_PlayerType.Player ? ConsoleColor.Cyan : ConsoleColor.DarkRed;
Console.SetCursorPosition(2, h - 4);
Console.Write("{0}受到了时空裂缝的影响,暂停一回合", p1.type == E_PlayerType.Player ? "你" : "电脑");
Console.SetCursorPosition(2, h - 3);
Console.Write("按任意键继续");
p1.isPause = false;
return false;
}
Random r= new Random();
int dice = r.Next(1,7);
p1.nowIndex += dice;
Console.ForegroundColor = p1.type == E_PlayerType.Player ? ConsoleColor.Cyan : ConsoleColor.DarkRed;
Console.SetCursorPosition(2, h - 4);
Console.Write("{0}掷出了{1}点",p1.type== E_PlayerType.Player?"你":"电脑",dice);
if (p1.nowIndex >= map.grids.Length-1)
{
p1.nowIndex = map.grids.Length-1;
if(p1.type== E_PlayerType.Player)
{
Console.ForegroundColor = p1.type == E_PlayerType.Player ? ConsoleColor.Cyan : ConsoleColor.DarkRed;
Console.SetCursorPosition(2, h - 4);
Console.Write("恭喜你率先到达终点");
Console.SetCursorPosition(2, h - 3);
Console.Write("按任意键退出游戏");
}
else
{
Console.ForegroundColor = p1.type == E_PlayerType.Player ? ConsoleColor.Cyan : ConsoleColor.DarkRed;
Console.SetCursorPosition(2, h - 4);
Console.Write("很遗憾,你输掉了游戏");
Console.SetCursorPosition(2, h - 3);
Console.Write("按任意键退出游戏");
}
return true;
}
else
{
Grid grid=map.grids[p1.nowIndex];
switch (grid.type)
{
case E_Grid_Type.Normal:
Console.ForegroundColor = p1.type == E_PlayerType.Player ? ConsoleColor.Cyan : ConsoleColor.DarkRed;
Console.SetCursorPosition(2, h - 3);
Console.Write("{0}到达了安全区域", p1.type == E_PlayerType.Player ? "你" : "电脑");
Console.SetCursorPosition(2, h - 2);
Console.Write("按任意键继续");
break;
case E_Grid_Type.Bomb:
p1.nowIndex -= 5;
if (p1.nowIndex <= 0)
{
p1.nowIndex = 0;
}
Console.ForegroundColor = p1.type == E_PlayerType.Player ? ConsoleColor.Cyan : ConsoleColor.DarkRed;
Console.SetCursorPosition(2, h - 3);
Console.Write("{0}踩到了炸弹,倒退五格", p1.type == E_PlayerType.Player ? "你" : "电脑");
Console.SetCursorPosition(2, h - 2);
Console.Write("按任意键继续");
break;
case E_Grid_Type.Pause:
p1.isPause = true;
Console.ForegroundColor = p1.type == E_PlayerType.Player ? ConsoleColor.Cyan : ConsoleColor.DarkRed;
Console.SetCursorPosition(2, h - 3);
Console.Write("{0}受到了时空裂缝的影响", p1.type == E_PlayerType.Player ? "你" : "电脑");
Console.SetCursorPosition(2, h - 2);
Console.Write("按任意键继续");
break;
case E_Grid_Type.Tunnel:
int temp = p1.nowIndex;
p1.nowIndex = p2.nowIndex;
p2.nowIndex = temp;
Console.ForegroundColor = p1.type == E_PlayerType.Player ? ConsoleColor.Cyan : ConsoleColor.DarkRed;
Console.SetCursorPosition(2, h - 3);
Console.Write("{0}进入了隧道,双方位置交换", p1.type == E_PlayerType.Player ? "你" : "电脑");
Console.SetCursorPosition(2, h - 2);
Console.Write("按任意键继续");
break;
}
}
return false;
}
static void InfoClear(int w ,int h)
{
Console.SetCursorPosition(2, h - 2);
Console.Write(" ");
Console.SetCursorPosition(2,h-3);
Console.Write(" ");
Console.SetCursorPosition(2, h - 4);
Console.Write(" ");
}
static void GameS(int w ,int h,ref E_SceneType nowGame)
{
Console.Clear();
DrawWall(w, h);
Map map = new Map(14, 4, 80);
map.Draw();
Player player = new Player(0, E_PlayerType.Player);
Player com = new Player(0, E_PlayerType.Com);
DrawPlayer(player, com, map);
bool isGameOver = false;
while (true)
{
Console.ReadKey(true);
InfoClear(w, h);
isGameOver = RollDice(w, h, ref player, ref com, map);
map.Draw();
DrawPlayer(player, com, map);
if (isGameOver)
{
Console.ReadKey(true);
nowGame = E_SceneType.End;
break;
}
Console.ReadKey(true);
InfoClear(w, h);
isGameOver = RollDice(w, h, ref com, ref player, map);
map.Draw();
DrawPlayer(player, com, map);
if (isGameOver)
{
Console.ReadKey(true);
nowGame = E_SceneType.End;
break;
}
}
}
static void EndS(int w, int h, ref E_SceneType nowGame)
{
Console.Clear();
int endSelect = 0;
bool isQuitEnd = false;
while (true)
{
if (isQuitEnd)
{
break;
}
Console.SetCursorPosition(w / 2 - 4, 5);
Console.ForegroundColor = ConsoleColor.White;
Console.Write("游戏结束");
Console.SetCursorPosition(w / 2 - 4, 10);
Console.ForegroundColor = endSelect == 0 ? ConsoleColor.Red : ConsoleColor.White;
Console.Write("重新开始");
Console.ForegroundColor = endSelect == 1 ? ConsoleColor.Red : ConsoleColor.White;
Console.SetCursorPosition(w / 2 - 4, 12);
Console.Write("退出游戏");
switch (Console.ReadKey(true).Key)
{
case ConsoleKey.W:
endSelect--;
if (endSelect < 0)
{
endSelect = 0;
}
break;
case ConsoleKey.S:
endSelect++;
if (endSelect > 1)
{
endSelect = 1;
}
break;
case ConsoleKey.J:
if (endSelect == 0)
{
nowGame = E_SceneType.Begin;
isQuitEnd = true;
}
else
{
Environment.Exit(0);
}
break;
}
}
}
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!