控制方向的是:WSAD
确定键是:J
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Csharp基础实践小项目_自个先写_
{
#region 1.开始场景的实现
//开始游戏结构体(类)
class Start
{
//变量
string str1;
string str2;
string str3;
//颜色ID
int colorID;
//接收字符的盒子
char input;
//跳出内层循环循环标识符
bool going;
//对接 nowSeneID
int nowSeneID007;
//构造函数
public Start()
{
str1 = "飞行棋";
str2 = "开始游戏";
str3 = "退出游戏";
//颜色ID默认值
colorID = 0;
//接收字符的盒子默认值
input = 'W';
//跳出内层循环标识符默认值
going = false;
//nowSeneID007 默认值
nowSeneID007 = 1;
}
//函数
public int Show()
{
Console.Clear();
Console.SetCursorPosition(36, 10);
Console.WriteLine(str1);
while (true)
{
Console.SetCursorPosition(35, 14);
Console.ForegroundColor = colorID == 0 ? ConsoleColor.Red : ConsoleColor.White;
//用三元运算符替换
//if (colorID == 0)
//{
// Console.ForegroundColor = ConsoleColor.Red;
//}
//else
//{
// Console.ForegroundColor = ConsoleColor.White;
//}
Console.WriteLine(str2);
Console.SetCursorPosition(35, 16);
Console.ForegroundColor = colorID == 1 ? ConsoleColor.Red : ConsoleColor.White;
//用三元运算符替换
//if (colorID == 1)
//{
// Console.ForegroundColor = ConsoleColor.Red;
//}
//else
//{
// Console.ForegroundColor = ConsoleColor.White;
//}
Console.WriteLine(str3);
input = Console.ReadKey(true).KeyChar;
switch (input)
{
case 'W':
case 'w':
colorID = 0;
break;
case 'S':
case 's':
colorID = 1;
break;
case 'J':
case 'j':
if (colorID == 0)
{
nowSeneID007 = 2;
}
else
{
nowSeneID007 = 4;
}
going = true;
break;
default:
break;
}
//跳出内层循环
if (going)
{
return nowSeneID007;
break;
}
}
}
}
#endregion
#region 2.不变红墙
class Wool
{
//变量
string wool;
//构造函数
public Wool()
{
wool = "■";
}
//函数方法
public void ShowWool(int x, int y)
{
Console.Clear();
Console.ForegroundColor = ConsoleColor.Red;
//横墙
for (int j = 0; j < x - 2; j++)
{
Console.SetCursorPosition(j, 0);
Console.Write(wool);
Console.SetCursorPosition(j, y - 12);
Console.Write(wool);
Console.SetCursorPosition(j, y - 6);
Console.Write(wool);
Console.SetCursorPosition(j, y - 1);
Console.Write(wool);
}
//竖墙
for (int i = 0; i < y - 1; i++)
{
Console.SetCursorPosition(0, i);
Console.Write(wool);
Console.SetCursorPosition(x - 2, i);
Console.Write(wool);
}
Console.SetCursorPosition(2, y - 11);
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("□:普通格子");
Console.SetCursorPosition(2, y - 10);
Console.ForegroundColor = ConsoleColor.Blue;
Console.Write("||:暂停,一回合不动 ");
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("●:炸弹,倒退5格");
Console.SetCursorPosition(2, y - 9);
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("¤:时空隧道,随机倒退,暂停,换位置");
Console.SetCursorPosition(2, y - 8);
Console.ForegroundColor = ConsoleColor.Cyan;
Console.Write("★:玩家 ");
Console.ForegroundColor = ConsoleColor.Magenta;
Console.Write("▲:电脑 ");
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("◎:玩家电脑重合");
Console.SetCursorPosition(2, y - 5);
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("按任意键开始掷骰子");
}
}
#endregion
#region 3.地图绘制
class Map
{
//变量
public string[,] map;
//构造函数
public Map()
{
map = new string[,] { {"□","□","□","□","□","□","□","||","□","||"},
{" "," "," "," "," "," "," "," "," ","□"},
{"□","¤","□","□","□","□","□","□","□","□"},
{"□"," "," "," "," "," "," "," "," "," "},
{"||","□","□","□","□","□","□","□","□","□"},
{" "," "," "," "," "," "," "," "," ","□"},
{ "□","□","□","□","□","□","□","□","□","||"},
{"□"," "," "," "," "," "," "," "," "," "},
{"●","□","□","□","□","□","□","¤","□","□"},
{" "," "," "," "," "," "," "," "," ","□"},
{ "□","□","□","¤","□","□","||","□","□","□"},
{"□"," "," "," "," "," "," "," "," "," "},
{ "□","□","□","□","□","□","●","□","□","□"},
{" "," "," "," "," "," "," "," "," ","●"},
{ " "," "," "," "," "," "," "," ","□","●"}};
}
//函数方法
public void MapShow(string[,] str)
{
//map = str;
for (int i = 0; i < str.GetLength(0); i++)
{
Console.SetCursorPosition(30, 6 + i);
for (int j = 0; j < str.GetLength(1); j++)
{
if(str[i,j] == "||")
{
Console.ForegroundColor = ConsoleColor.Blue;
}
else if(str[i, j] == "¤")
{
Console.ForegroundColor = ConsoleColor.Yellow;
}
else if (str[i, j] == "●")
{
Console.ForegroundColor = ConsoleColor.Red;
}
else if (str[i, j] == "★")
{
Console.ForegroundColor = ConsoleColor.Cyan;
}
else if (str[i, j] == "▲")
{
Console.ForegroundColor = ConsoleColor.Magenta;
}
else if (str[i, j] == "◎")
{
Console.ForegroundColor = ConsoleColor.Green;
}
else
{
Console.ForegroundColor = ConsoleColor.White;
}
Console.Write(str[i,j]);
}
Console.WriteLine();
}
}
}
#endregion
#region 4.玩家绘制
class Play
{
//枚举
//enum E_Player
//{
// player,
// computer,
//}
//变量
//玩家
int player;
//电脑
int computer;
int x;
int y;
//构造函数
public Play()
{
player = 0;
computer = 1;
x = 80;
y = 40;
}
//函数方法
public int getPlayer(Map map)
{
string[,] str = new string[15, 10];
for (int i = 0; i < map.map.GetLength(0); i++)
{
for (int j = 0; j < map.map.GetLength(1); j++)
{
str[i, j] = map.map[i, j];
}
}
map.MapShow(str);
Console.SetCursorPosition(28,6);
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("◎");
Random r = new Random();
int box = 0;
int box2 = 0;
int playerbox = 0;
int computerbox = 0;
//标识符
bool bo1 = true;
bool bo2 = true;
while (true)
{
//接收输入
//Console.ReadKey(true);
因为数组是引用类型,所以不能:str = map.map 不然他们地址就会相同
//for (int i = 0; i < map.map.GetLength(0); i++)
//{
// for (int j = 0; j < map.map.GetLength(1); j++)
// {
// str[i, j] = map.map[i, j];
// }
//}
//Console.SetCursorPosition(28, 6);
//Console.WriteLine(" ");
//玩家画自己的方法
if (bo1)
{
//接收输入
Console.ReadKey(true);
//因为数组是引用类型,所以不能:str = map.map 不然他们地址就会相同
for (int i = 0; i < map.map.GetLength(0); i++)
{
for (int j = 0; j < map.map.GetLength(1); j++)
{
str[i, j] = map.map[i, j];
}
}
Console.SetCursorPosition(28, 6);
Console.WriteLine(" ");
box = r.Next(1, 7);
if ((playerbox - 1) / 10 == 2 || (playerbox - 1) / 10 == 6 || (playerbox - 1) / 10 == 10 || (playerbox - 1) / 10 == 14)
{
playerbox = (playerbox - 1) / 10 * 10 + (10 - (playerbox - 1) % 10);
}
Console.ForegroundColor = ConsoleColor.Cyan;
Console.SetCursorPosition(2, y - 5);
Console.WriteLine("玩家扔出点数为:" + box + " ");
playerbox += box;
if ((playerbox - 1) / 10 == 1 || (playerbox - 1) / 10 == 5 || (playerbox - 1) / 10 == 9 || (playerbox - 1) / 10 == 13)
{
playerbox += 9;
}
if ((playerbox - 1) / 10 == 2 || (playerbox - 1) / 10 == 6 || (playerbox - 1) / 10 == 10 || (playerbox - 1) / 10 == 14)
{
playerbox = playerbox / 10 * 10 + (10 - (playerbox - 1) % 10);
}
if (((playerbox - 1) / 10 == 3 || (playerbox - 1) / 10 == 7 || (playerbox - 1) / 10 == 11) && playerbox % 10 != 1)
{
playerbox += 9;
}
if (playerbox > 140 && playerbox < 150)
{
playerbox = 142;
playerbox = playerbox / 10 * 10 + (10 - (playerbox - 1) % 10);
}
if (playerbox == 150)
{
Console.SetCursorPosition(2, y - 4);
Console.WriteLine("踩到炸弹倒退5格后又踩到炸弹再倒退5格 ");
Console.SetCursorPosition(2, y - 3);
Console.WriteLine("请按任意键继续 ");
playerbox = 122;
}
else if (str[(playerbox - 1) / 10, (playerbox - 1) % 10] == "||")
{
Console.SetCursorPosition(2, y - 4);
Console.WriteLine("踩到暂停键,暂停一回合 ");
Console.SetCursorPosition(2, y - 3);
Console.WriteLine("请按任意键继续 ");
bo1 = false;
}
else if (str[(playerbox - 1) / 10, (playerbox - 1) % 10] == "●")
{
Console.SetCursorPosition(2, y - 4);
Console.WriteLine("踩到炸弹倒退5格 ");
Console.SetCursorPosition(2, y - 3);
Console.WriteLine("请按任意键继续 ");
playerbox -= 5;
if (((playerbox - 1) / 10 == 3 || (playerbox - 1) / 10 == 7 || (playerbox - 1) / 10 == 11) && playerbox % 10 != 1)
{
playerbox -= 9;
}
if ((playerbox - 1) / 10 == 1 || (playerbox - 1) / 10 == 5 || (playerbox - 1) / 10 == 9 || (playerbox - 1) / 10 == 13)
{
playerbox -= 9;
}
if ((playerbox - 1) / 10 == 2 || (playerbox - 1) / 10 == 6 || (playerbox - 1) / 10 == 10 || (playerbox - 1) / 10 == 14)
{
playerbox = playerbox / 10 * 10 + (10 - (playerbox - 1) % 10);
}
}
else if (str[(playerbox - 1) / 10, (playerbox - 1) % 10] == "¤")
{
Console.SetCursorPosition(2, y - 4);
Console.WriteLine("踩到时光隧道(将发生随机事件) ");
box2 = r.Next(0, 3);
switch (box2)
{
case 0:
Console.SetCursorPosition(2, y - 3);
Console.WriteLine("随机事件发生--倒退5格 ");
Console.SetCursorPosition(2, y - 2);
Console.WriteLine("请按任意键继续 ");
if ((playerbox - 1) / 10 == 2 || (playerbox - 1) / 10 == 10)
{
playerbox += 5;
}
else
{
playerbox -= 5;
}
break;
case 1:
Console.SetCursorPosition(2, y - 3);
Console.WriteLine("随机事件发生--暂停一回合 ");
Console.SetCursorPosition(2, y - 2);
Console.WriteLine("请按任意键继续 ");
bo1 = false;
break;
case 2:
Console.SetCursorPosition(2, y - 3);
Console.WriteLine("随机事件发生--与电脑玩家交换位置 ");
Console.SetCursorPosition(2, y - 2);
Console.WriteLine("请按任意键继续 ");
box2 = playerbox;
playerbox = computerbox;
computerbox = box2;
break;
default:
break;
}
}
else
{
Console.SetCursorPosition(2, y - 4);
Console.WriteLine("玩家到达一个安全位置 ");
Console.SetCursorPosition(2, y - 3);
Console.WriteLine("请按任意键继续 ");
Console.SetCursorPosition(2, y - 2);
Console.WriteLine(" ");
}
}
else
{
//接收输入
Console.ReadKey(true);
Console.ForegroundColor = ConsoleColor.Cyan;
Console.SetCursorPosition(2, y - 5);
Console.WriteLine("暂停一回合 ");
Console.SetCursorPosition(2, y - 4);
Console.WriteLine("请按任意键继续 ");
Console.SetCursorPosition(2, y - 3);
Console.WriteLine(" ");
Console.SetCursorPosition(2, y - 2);
Console.WriteLine(" ");
bo1 = true;
}
str[(playerbox - 1) / 10, (playerbox - 1) % 10] = "★";
if (computerbox > 0)
{
str[(computerbox - 1) / 10, (computerbox - 1) % 10] = "▲";
}
if (playerbox == computerbox)
{
str[(computerbox - 1) / 10, (computerbox - 1) % 10] = "◎";
}
map.MapShow(str);
if(playerbox == 149)
{
Console.SetCursorPosition(2, y - 5);
Console.WriteLine("玩家胜利--游戏结束 ");
Console.SetCursorPosition(2, y - 4);
Console.WriteLine("按任意键继续 ");
Console.SetCursorPosition(2, y - 3);
Console.WriteLine(" ");
Console.SetCursorPosition(2, y - 2);
Console.WriteLine(" ");
break;
}
//if ((playerbox - 1) / 10 == 2 || (playerbox - 1) / 10 == 6 || (playerbox - 1) / 10 == 10 || (playerbox - 1) / 10 == 14)
//{
// playerbox = (playerbox - 1) / 10 * 10 + (10 - (playerbox - 1) % 10);
//}
if (bo2)
{
//接收输入
Console.ReadKey(true);
//因为数组是引用类型,所以不能:str = map.map 不然他们地址就会相同
for (int i = 0; i < map.map.GetLength(0); i++)
{
for (int j = 0; j < map.map.GetLength(1); j++)
{
str[i, j] = map.map[i, j];
}
}
//电脑画自己的方法
box = r.Next(1, 7);
Console.ForegroundColor = ConsoleColor.Magenta;
Console.SetCursorPosition(2, y - 5);
Console.WriteLine("电脑扔出点数为:" + box + " ");
if ((computerbox - 1) / 10 == 2 || (computerbox - 1) / 10 == 6 || (computerbox - 1) / 10 == 10 || (computerbox - 1) / 10 == 14)
{
computerbox = (computerbox - 1) / 10 * 10 + (10 - (computerbox - 1) % 10);
}
computerbox += box;
if ((computerbox - 1) / 10 == 1 || (computerbox - 1) / 10 == 5 || (computerbox - 1) / 10 == 9 || (computerbox - 1) / 10 == 13)
{
computerbox += 9;
}
if ((computerbox - 1) / 10 == 2 || (computerbox - 1) / 10 == 6 || (computerbox - 1) / 10 == 10 || (computerbox - 1) / 10 == 14)
{
computerbox = computerbox / 10 * 10 + (10 - (computerbox - 1) % 10);
}
if (((computerbox - 1) / 10 == 3 || (computerbox - 1) / 10 == 7 || (computerbox - 1) / 10 == 11) && computerbox % 10 != 1)
{
computerbox += 9;
}
if (computerbox > 140 && computerbox < 150)
{
computerbox = 142;
computerbox = computerbox / 10 * 10 + (10 - (computerbox - 1) % 10);
}
if (computerbox == 150)
{
Console.SetCursorPosition(2, y - 4);
Console.WriteLine("踩到炸弹倒退5格后又踩到炸弹再倒退5格 ");
Console.SetCursorPosition(2, y - 3);
Console.WriteLine("请按任意键继续 ");
computerbox = 122;
}
else if(str[(computerbox - 1) / 10, (computerbox - 1) % 10] == "||")
{
Console.SetCursorPosition(2, y - 4);
Console.WriteLine("踩到暂停键,暂停一回合 ");
Console.SetCursorPosition(2, y - 3);
Console.WriteLine("请按任意键继续 ");
bo2 = false;
}
else if(str[(computerbox - 1) / 10, (computerbox - 1) % 10] == "●")
{
Console.SetCursorPosition(2, y - 4);
Console.WriteLine("踩到炸弹倒退5格 ");
Console.SetCursorPosition(2, y - 3);
Console.WriteLine("请按任意键继续 ");
computerbox -= 5;
if (((computerbox - 1) / 10 == 3 || (computerbox - 1) / 10 == 7 || (computerbox - 1) / 10 == 11) && computerbox % 10 != 1)
{
computerbox -= 9;
}
if ((computerbox - 1) / 10 == 1 || (computerbox - 1) / 10 == 5 || (computerbox - 1) / 10 == 9 || (computerbox - 1) / 10 == 13)
{
computerbox -= 9;
}
if ((computerbox - 1) / 10 == 2 || (computerbox - 1) / 10 == 6 || (computerbox - 1) / 10 == 10 || (computerbox - 1) / 10 == 14)
{
computerbox = computerbox / 10 * 10 + (10 - (computerbox - 1) % 10);
}
}
else if(str[(computerbox - 1) / 10, (computerbox - 1) % 10] == "¤")
{
Console.SetCursorPosition(2, y - 4);
Console.WriteLine("踩到时光隧道(将发生随机事件) ");
box2 = r.Next(0, 3);
switch (box2)
{
case 0:
Console.SetCursorPosition(2, y - 3);
Console.WriteLine("随机事件发生--倒退5格 ");
Console.SetCursorPosition(2, y - 2);
Console.WriteLine("请按任意键继续 ");
if ((computerbox - 1) / 10 == 2 || (computerbox - 1) / 10 == 10)
{
computerbox += 5;
}
else
{
computerbox -= 5;
}
break;
case 1:
Console.SetCursorPosition(2, y - 3);
Console.WriteLine("随机事件发生--暂停一回合 ");
Console.SetCursorPosition(2, y - 2);
Console.WriteLine("请按任意键继续 ");
bo2 = false;
break;
case 2:
Console.SetCursorPosition(2, y - 3);
Console.WriteLine("随机事件发生--与玩家交换位置 ");
Console.SetCursorPosition(2, y - 2);
Console.WriteLine("请按任意键继续 ");
box2 = playerbox;
playerbox = computerbox;
computerbox = box2;
break;
default:
break;
}
}
else
{
Console.SetCursorPosition(2, y - 4);
Console.WriteLine("电脑到达一个安全位置 ");
Console.SetCursorPosition(2, y - 3);
Console.WriteLine("请按任意键继续 ");
Console.SetCursorPosition(2, y - 2);
Console.WriteLine(" ");
}
}
else
{
//接收输入
Console.ReadKey(true);
Console.ForegroundColor = ConsoleColor.Magenta;
Console.SetCursorPosition(2, y - 5);
Console.WriteLine("暂停一回合 ");
Console.SetCursorPosition(2, y - 4);
Console.WriteLine("请按任意键继续 ");
Console.SetCursorPosition(2, y - 3);
Console.WriteLine(" ");
Console.SetCursorPosition(2, y - 2);
Console.WriteLine(" ");
bo2 = true;
}
str[(computerbox - 1) / 10, (computerbox - 1) % 10] = "▲";
str[(playerbox - 1) / 10, (playerbox - 1) % 10] = "★";
//if ((playerbox - 1) / 10 == 2 || (playerbox - 1) / 10 == 6 || (playerbox - 1) / 10 == 10 || (playerbox - 1) / 10 == 14)
//{
// playerbox = (playerbox - 1) / 10 * 10 + (10 - (playerbox - 1) % 10);
//}
//if ((computerbox - 1) / 10 == 2 || (computerbox - 1) / 10 == 6 || (computerbox - 1) / 10 == 10 || (computerbox - 1) / 10 == 14)
//{
// computerbox = (computerbox - 1) / 10 * 10 + (10 - (computerbox - 1) % 10);
//}
if (playerbox == computerbox)
{
str[(computerbox - 1) / 10, (computerbox - 1) % 10] = "◎";
}
map.MapShow(str);
if (computerbox == 149)
{
Console.SetCursorPosition(2, y - 5);
Console.WriteLine("电脑胜利--游戏结束 ");
Console.SetCursorPosition(2, y - 4);
Console.WriteLine("按任意键继续 ");
Console.SetCursorPosition(2, y - 3);
Console.WriteLine(" ");
Console.SetCursorPosition(2, y - 2);
Console.WriteLine(" ");
break;
}
}
return 3;
}
}
#endregion
#region 5.结束场景
class Finish
{
int nowSeneID;
string str1;
string str2;
string str3;
int box;
bool bo1;
public Finish()
{
nowSeneID = 3;
str1 = "游戏结束";
str2 = "返回主菜单";
str3 = "退出游戏";
box = 1;
bo1 = false;
}
public int Final()
{
Console.Clear();
Console.SetCursorPosition(36, 10);
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine(str1);
while (true)
{
Console.SetCursorPosition(35, 14);
Console.ForegroundColor = box == 1 ? ConsoleColor.Red : ConsoleColor.White;
Console.WriteLine(str2);
Console.SetCursorPosition(36, 16);
Console.ForegroundColor = box == 4 ? ConsoleColor.Red : ConsoleColor.White;
Console.WriteLine(str3);
char keybox = Console.ReadKey(true).KeyChar;
switch (keybox)
{
case 'W':
case 'w':
box = 1;
break;
case 'S':
case 's':
box = 4;
break;
case 'J':
case 'j':
bo1 = true;
break;
default:
break;
}
if (bo1)
{
nowSeneID = box;
break;
}
}
return nowSeneID;
}
}
#endregion
class Program
{
//场景类型枚举
enum E_nowSeneID
{
start = 1,
game,
finish,
off,
}
static void Main(string[] args)
{
//基础设置
//隐藏光标
Console.CursorVisible = false;
//设置舞台大小
int x = 80;
int y = 40;
Console.SetWindowSize(x, y);
Console.SetBufferSize(x, y);
//尽量用OOP 面向对象
//所有的代码都是在大循环中进行的
//主循环
//场景ID
E_nowSeneID nowSeneID = E_nowSeneID.start;
while (true)
{
switch (nowSeneID)
{
case E_nowSeneID.start:
#region 1.开始场景的实现
Start start = new Start();
nowSeneID = (E_nowSeneID)start.Show();
#endregion
break;
case E_nowSeneID.game:
#region 2.不变红墙
Wool wool = new Wool();
wool.ShowWool(x,y);
#endregion
#region 3.地图绘制
Map map = new Map();
//string[,] str = map.map;
//map.MapShow();
#endregion
#region 4.玩家和电脑的绘制
Play plays = new Play();
nowSeneID = (E_nowSeneID)plays.getPlayer(map);
#endregion
Console.ReadKey(true);
break;
case E_nowSeneID.finish:
#region 5.游戏结束场景
Finish finish = new Finish();
nowSeneID = (E_nowSeneID)finish.Final();
#endregion
break;
case E_nowSeneID.off:
//退出程序
Environment.Exit(0);
break;
default:
break;
}
}
}
}
}
C#基础实践项目--飞行棋(自己实现)
第一部分
第二部分--游戏场景相关
#region 8.扔骰子 函数
//擦除提示的函数
static void ClearInfo(int h)
{
Console.SetCursorPosition(2, h - 5);
Console.Write(" ");
Console.SetCursorPosition(2, h - 4);
Console.Write(" ");
Console.SetCursorPosition(2, h - 3);
Console.Write(" ");
Console.SetCursorPosition(2, h - 2);
Console.Write(" ");
}
///
/// 扔骰子函数
///
/// 窗口的宽
/// 窗口的高
/// 扔骰子的对象
/// 地图信息
/// 默认返回false 代表没有结束
static bool RandomMove(int w, int h, ref Player p, ref Player otherP, Map map)
{
//擦除之前显示的提示信息
ClearInfo(h);
//根据扔骰子的玩家类型 决定信息的颜色
Console.ForegroundColor = p.type == E_PlayerType.Player ? ConsoleColor.Cyan : ConsoleColor.Magenta;
//扔骰子之前 判断 玩家是否处于暂停状态
if (p.isPause)
{
Console.SetCursorPosition(2, h - 5);
Console.Write("处于暂停,{0}需要暂停一回合", p.type == E_PlayerType.Player ? "你" : "电脑");
Console.SetCursorPosition(2, h - 4);
Console.Write("请按任意键,让{0}开始扔骰子", p.type != E_PlayerType.Player ? "你" : "电脑");
//停止暂停
p.isPause = false;
return false;
}
//扔骰子目的 是改变 玩家或者电脑的位置 计算位置变化
//扔骰子 随机一个1到6的数 加上去
Random r = new Random();
int randomNum = r.Next(1, 7);
p.nowIndex += randomNum;
//打印扔的点数
Console.SetCursorPosition(2, h - 5);
Console.Write("{0}扔出的点数为:{1}", p.type == E_PlayerType.Player ? "你" : "电脑",randomNum);
//首先判断是否到终点了
if (p.nowIndex >= map.grids.Length - 1)
{
p.nowIndex = map.grids.Length - 1;
Console.SetCursorPosition(2, h - 4);
if (p.type == E_PlayerType.Player)
{
Console.Write("恭喜你, 你率先到达了终点!");
}
else
{
Console.Write("很遗憾,电脑先到达了终点。");
}
Console.SetCursorPosition(2, h - 3);
Console.Write("请按任意键结束游戏。");
return true;
}
else
{
//没有到终点 就判断 当前对象 到了一个怎样的格子
Grid grid = map.grids[p.nowIndex];
switch (grid.type)
{
case E_Grid_Type.Normal:
//普通格子不用处理
Console.SetCursorPosition(2, h - 4);
Console.Write("{0}到达了一个安全位置", p.type == E_PlayerType.Player ? "你" : "电脑");
Console.SetCursorPosition(2, h - 3);
Console.Write("请按任意键,让{0}开始扔骰子", p.type != E_PlayerType.Player ? "你" : "电脑");
break;
case E_Grid_Type.Boom:
//炸弹退格
p.nowIndex -= 5;
//不能比起点小
if(p.nowIndex < 0)
{
p.nowIndex = 0;
}
Console.SetCursorPosition(2, h - 4);
Console.Write("{0}踩到了炸弹,退回5格", p.type == E_PlayerType.Player ? "你" : "电脑");
Console.SetCursorPosition(2, h - 3);
Console.Write("请按任意键,让{0}开始扔骰子", p.type != E_PlayerType.Player ? "你" : "电脑");
break;
case E_Grid_Type.Pause:
//暂停一回合
//目前 只有加一个对象的暂停标识 才能知道 下一回合它是不是能扔骰子
//下回合要暂停
p.isPause = true;
Console.SetCursorPosition(2, h - 4);
Console.Write("{0}踩到暂停键,暂停一回合", p.type == E_PlayerType.Player ? "你" : "电脑");
Console.SetCursorPosition(2, h - 3);
Console.Write("请按任意键,让{0}开始扔骰子", p.type != E_PlayerType.Player ? "你" : "电脑");
break;
case E_Grid_Type.Tunnel:
Console.SetCursorPosition(2, h - 4);
Console.Write("{0}踩到了时空隧道", p.type == E_PlayerType.Player ? "你" : "电脑");
//随机
randomNum = r.Next(1, 91);
//触发倒退
if (randomNum <= 30)
{
p.nowIndex -= 5;
if(p.nowIndex < 0)
{
p.nowIndex = 0;
}
Console.SetCursorPosition(2, h - 3);
Console.Write("触发倒退5格");
}
//触发暂停
else if(randomNum <= 60)
{
p.isPause = true;
Console.SetCursorPosition(2, h - 3);
Console.Write("触发暂停一回合");
}
else
{
int temp = p.nowIndex;
p.nowIndex = otherP.nowIndex;
otherP.nowIndex = temp;
Console.SetCursorPosition(2, h - 3);
Console.Write("触发交换位置");
}
Console.SetCursorPosition(2, h - 2 );
Console.Write("请按任意键,让{0}开始扔骰子", p.type != E_PlayerType.Player ? "你" : "电脑");
break;
default:
break;
}
}
//默认没有结束
return false;
}
#endregion
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 1.控制台初始化
//基础设置
//设置舞台大小
int w = 80;
int h = 40;
ConsoleInit(w, h);
#endregion
#region 2.场景选择相关
//申明一个 表示场景标识的 变量
E_SceneType nowSceneType = E_SceneType.Begin;
while (true)
{
switch (nowSceneType)
{
case E_SceneType.Begin:
//开始场景逻辑
Console.Clear();
BeginOrEndScene(w,h,ref nowSceneType);
break;
case E_SceneType.Game:
//游戏场景逻辑
Console.Clear();
GameScene(w, h, ref nowSceneType);
break;
case E_SceneType.End:
//结束场景逻辑
Console.Clear();
BeginOrEndScene(w, h, ref nowSceneType);
break;
default:
break;
}
}
#endregion
}
#region 1.控制台初始化
static void ConsoleInit(int w,int h)
{
//隐藏光标
Console.CursorVisible = false;
//设置舞台大小
Console.SetWindowSize(w, h);
Console.SetBufferSize(w, h);
}
#endregion
#region 3.开始场景逻辑 + 9.结束场景逻辑
static void BeginOrEndScene(int w,int h,ref E_SceneType nowSceneType)
{
Console.ForegroundColor = ConsoleColor.White;
Console.SetCursorPosition(nowSceneType == E_SceneType.Begin ? w / 2 - 3 : w / 2 - 4, 8);
Console.WriteLine(nowSceneType == E_SceneType.Begin ? "飞行棋" : "游戏结束");
//当前选项的编号
int nowSelIndex = 0;
bool isQuitBegin = false;
//开始逻辑
while (true)
{
isQuitBegin = false;
Console.SetCursorPosition(nowSceneType == E_SceneType.Begin ? w / 2 - 4 : w / 2 - 5, 13);
Console.ForegroundColor = nowSelIndex == 0 ? ConsoleColor.Red : ConsoleColor.White;
Console.WriteLine(nowSceneType == E_SceneType.Begin ? "开始游戏" : "回到主菜单");
Console.SetCursorPosition(w / 2 - 4, 15);
Console.ForegroundColor = nowSelIndex == 1 ? ConsoleColor.Red : ConsoleColor.White;
Console.WriteLine("退出游戏");
//通过ReadKey可以得到一个输入的枚举类型
switch (Console.ReadKey(true).Key)
{
case ConsoleKey.W:
nowSelIndex = 0;
break;
case ConsoleKey.S:
nowSelIndex = 1;
break;
case ConsoleKey.J:
if(nowSelIndex == 0)
{
//进入游戏场景
//1.改变当前场景ID
nowSceneType = nowSceneType == E_SceneType.Begin ? E_SceneType.Game : E_SceneType.Begin;
//2.退出当前循环
isQuitBegin = true;
}
else
{
Environment.Exit(0);
}
break;
default:
break;
}
//通过标识决定 是否退出循环
if (isQuitBegin)
{
break;
}
}
}
#endregion
#region 游戏场景逻辑
static void GameScene(int w, int h, ref E_SceneType nowSceneType)
{
//绘制不变的基本信息
DrawWall(w,h);
//绘制地图
Map map = new Map(30, 6, 90);
map.Draw();
//绘制玩家
Player player = new Player(0, E_PlayerType.Player);
Player computer = new Player(0, E_PlayerType.Computer);
DrawPlayer(player, computer, map);
bool isGameOver = false;
//游戏场景循环
while (true)
{
//之后的游戏逻辑
//玩家扔骰子逻辑
//检测输入
Console.ReadKey(true);
//扔骰子的逻辑
isGameOver = RandomMove(w, h, ref player, ref computer, map);
//绘制地图
map.Draw();
//绘制玩家
DrawPlayer(player, computer, map);
//判断是否要结束游戏
if (isGameOver)
{
//卡住程序 让玩家按任意键
Console.ReadKey(true);
//改变场景ID
nowSceneType = E_SceneType.End;
//直接跳出循环
break;
}
//电脑扔骰子逻辑
//检测输入
Console.ReadKey(true);
//扔骰子的逻辑
isGameOver = RandomMove(w, h, ref computer, ref player, map);
//绘制地图
map.Draw();
//绘制电脑
DrawPlayer(player, computer, map);
//判断是否要结束游戏
if (isGameOver)
{
//卡住程序 让玩家按任意键
Console.ReadKey(true);
//改变场景ID
nowSceneType = E_SceneType.End;
//直接跳出循环
break;
}
}
}
#endregion
#region 4.绘制不变内容(红墙 提示等等)
static void DrawWall(int w,int h)
{
Console.ForegroundColor = ConsoleColor.Red;
//画墙
//横着的墙
for (int i = 0; i < w - 2; i += 2)
{
//最上方的墙
Console.SetCursorPosition(i, 0);
Console.Write("■");
//最下方的墙
Console.SetCursorPosition(i, h - 1);
Console.Write("■");
//中间的墙
Console.SetCursorPosition(i, h - 6);
Console.Write("■");
Console.SetCursorPosition(i, h - 11);
Console.Write("■");
}
//竖着的墙
for (int i = 0; i < h - 1; i++)
{
//最左边
Console.SetCursorPosition(0, i);
Console.Write("■");
//最右边
Console.SetCursorPosition(w - 2, i);
Console.Write("■");
}
//文字信息
Console.ForegroundColor = ConsoleColor.White;
Console.SetCursorPosition(2, h - 10);
Console.Write("□:普通格子");
Console.ForegroundColor = ConsoleColor.Blue;
Console.SetCursorPosition(2, h - 9);
Console.Write("||:暂停,一回合不动");
Console.ForegroundColor = ConsoleColor.Red;
Console.SetCursorPosition(26, h - 9);
Console.Write("●:炸弹,倒退5格");
Console.ForegroundColor = ConsoleColor.Yellow;
Console.SetCursorPosition(2, h - 8);
Console.Write("¤:时光隧道,随机倒退,暂停,换位置");
Console.ForegroundColor = ConsoleColor.Cyan;
Console.SetCursorPosition(2, h - 7);
Console.Write("★:玩家");
Console.ForegroundColor = ConsoleColor.Magenta;
Console.SetCursorPosition(12, h - 7);
Console.Write("▲:电脑");
Console.ForegroundColor = ConsoleColor.DarkGreen;
Console.SetCursorPosition(22, h - 7);
Console.Write("◎:玩家电脑重合");
Console.ForegroundColor = ConsoleColor.White;
Console.SetCursorPosition(2, h - 5);
Console.Write("按任意键扔骰子");
}
#endregion
#region 7.绘制玩家
static void DrawPlayer(Player player,Player computer,Map map)
{
//重合时
if (player.nowIndex == computer.nowIndex)
{
//得到重合位置
Grid grid = map.grids[player.nowIndex];
Console.SetCursorPosition(grid.pos.x, grid.pos.y);
Console.ForegroundColor = ConsoleColor.DarkGreen;
Console.WriteLine("◎");
}
//不重合的时候
else
{
player.Draw(map);
computer.Draw(map);
}
}
#endregion
#region 8.扔骰子 函数
//擦除提示的函数
static void ClearInfo(int h)
{
Console.SetCursorPosition(2, h - 5);
Console.Write(" ");
Console.SetCursorPosition(2, h - 4);
Console.Write(" ");
Console.SetCursorPosition(2, h - 3);
Console.Write(" ");
Console.SetCursorPosition(2, h - 2);
Console.Write(" ");
}
///
/// 扔骰子函数
///
/// 窗口的宽
/// 窗口的高
/// 扔骰子的对象
/// 地图信息
/// 默认返回false 代表没有结束
static bool RandomMove(int w, int h, ref Player p, ref Player otherP, Map map)
{
//擦除之前显示的提示信息
ClearInfo(h);
//根据扔骰子的玩家类型 决定信息的颜色
Console.ForegroundColor = p.type == E_PlayerType.Player ? ConsoleColor.Cyan : ConsoleColor.Magenta;
//扔骰子之前 判断 玩家是否处于暂停状态
if (p.isPause)
{
Console.SetCursorPosition(2, h - 5);
Console.Write("处于暂停,{0}需要暂停一回合", p.type == E_PlayerType.Player ? "你" : "电脑");
Console.SetCursorPosition(2, h - 4);
Console.Write("请按任意键,让{0}开始扔骰子", p.type != E_PlayerType.Player ? "你" : "电脑");
//停止暂停
p.isPause = false;
return false;
}
//扔骰子目的 是改变 玩家或者电脑的位置 计算位置变化
//扔骰子 随机一个1到6的数 加上去
Random r = new Random();
int randomNum = r.Next(1, 7);
p.nowIndex += randomNum;
//打印扔的点数
Console.SetCursorPosition(2, h - 5);
Console.Write("{0}扔出的点数为:{1}", p.type == E_PlayerType.Player ? "你" : "电脑",randomNum);
//首先判断是否到终点了
if (p.nowIndex >= map.grids.Length - 1)
{
p.nowIndex = map.grids.Length - 1;
Console.SetCursorPosition(2, h - 4);
if (p.type == E_PlayerType.Player)
{
Console.Write("恭喜你, 你率先到达了终点!");
}
else
{
Console.Write("很遗憾,电脑先到达了终点。");
}
Console.SetCursorPosition(2, h - 3);
Console.Write("请按任意键结束游戏。");
return true;
}
else
{
//没有到终点 就判断 当前对象 到了一个怎样的格子
Grid grid = map.grids[p.nowIndex];
switch (grid.type)
{
case E_Grid_Type.Normal:
//普通格子不用处理
Console.SetCursorPosition(2, h - 4);
Console.Write("{0}到达了一个安全位置", p.type == E_PlayerType.Player ? "你" : "电脑");
Console.SetCursorPosition(2, h - 3);
Console.Write("请按任意键,让{0}开始扔骰子", p.type != E_PlayerType.Player ? "你" : "电脑");
break;
case E_Grid_Type.Boom:
//炸弹退格
p.nowIndex -= 5;
//不能比起点小
if(p.nowIndex < 0)
{
p.nowIndex = 0;
}
Console.SetCursorPosition(2, h - 4);
Console.Write("{0}踩到了炸弹,退回5格", p.type == E_PlayerType.Player ? "你" : "电脑");
Console.SetCursorPosition(2, h - 3);
Console.Write("请按任意键,让{0}开始扔骰子", p.type != E_PlayerType.Player ? "你" : "电脑");
break;
case E_Grid_Type.Pause:
//暂停一回合
//目前 只有加一个对象的暂停标识 才能知道 下一回合它是不是能扔骰子
//下回合要暂停
p.isPause = true;
Console.SetCursorPosition(2, h - 4);
Console.Write("{0}踩到暂停键,暂停一回合", p.type == E_PlayerType.Player ? "你" : "电脑");
Console.SetCursorPosition(2, h - 3);
Console.Write("请按任意键,让{0}开始扔骰子", p.type != E_PlayerType.Player ? "你" : "电脑");
break;
case E_Grid_Type.Tunnel:
Console.SetCursorPosition(2, h - 4);
Console.Write("{0}踩到了时空隧道", p.type == E_PlayerType.Player ? "你" : "电脑");
//随机
randomNum = r.Next(1, 91);
//触发倒退
if (randomNum <= 30)
{
p.nowIndex -= 5;
if(p.nowIndex < 0)
{
p.nowIndex = 0;
}
Console.SetCursorPosition(2, h - 3);
Console.Write("触发倒退5格");
}
//触发暂停
else if(randomNum <= 60)
{
p.isPause = true;
Console.SetCursorPosition(2, h - 3);
Console.Write("触发暂停一回合");
}
else
{
int temp = p.nowIndex;
p.nowIndex = otherP.nowIndex;
otherP.nowIndex = temp;
Console.SetCursorPosition(2, h - 3);
Console.Write("触发交换位置");
}
Console.SetCursorPosition(2, h - 2 );
Console.Write("请按任意键,让{0}开始扔骰子", p.type != E_PlayerType.Player ? "你" : "电脑");
break;
default:
break;
}
}
//默认没有结束
return false;
}
#endregion
}
#region 2.场景选择相关
///
/// 游戏场景枚举类型
///
enum E_SceneType
{
///
/// 开始场景
///
Begin,
///
/// 游戏场景
///
Game,
///
/// 结束场景
///
End,
}
#endregion
#region 5.格子结构体和格子枚举
///
/// 格子类型 枚举
///
enum E_Grid_Type
{
///
/// 普通格子
///
Normal,
///
/// 炸弹
///
Boom,
///
/// 暂停
///
Pause,
///
/// 时空隧道 随机倒退 暂停 换位置
///
Tunnel,
}
///
/// 位置信息结构体 包含xy位置
///
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(int x, int y, E_Grid_Type type)
{
pos.x = x;
pos.y = y;
this.type = type;
}
public void Draw()
{
Console.SetCursorPosition(pos.x, pos.y);
switch (type)
{
//普通格子 怎么画
case E_Grid_Type.Normal:
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("□");
break;
//炸弹 怎么画
case E_Grid_Type.Boom:
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("●");
break;
//暂停 怎么画
case E_Grid_Type.Pause:
Console.ForegroundColor = ConsoleColor.Blue;
Console.WriteLine("||");
break;
//时空隧道 怎么画
case E_Grid_Type.Tunnel:
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("¤");
break;
default:
break;
}
}
}
#endregion
#region 6.地图结构体
struct Map
{
public Grid[] grids;
//初始化中 初始了 各个格子的类型 和 位置
public Map(int x,int y, int num)
{
grids = new Grid[num];
//用于位置改变计数的变量
//表示X变化的次数
int indexX = 0;
//表示Y变化的次数
int indexY = 0;
//X的步长
int stepNum = 2;
Random r = new Random();
int randomNum;
for (int i = 0; i < num; i++)
{
//应该初始化 格子类型
randomNum = r.Next(0, 101);
//设置类型 普通格子
//有85%几率 是普通格子 (首尾两个格子 必须为普通格子)
if(randomNum < 85 || randomNum == 0 || randomNum == num - 1)
{
grids[i].type = E_Grid_Type.Normal;
}
//有5%的几率 是炸弹
else if(randomNum >= 85 && randomNum < 90)
{
grids[i].type = E_Grid_Type.Boom;
}
//有5%的几率 是暂停
else if (randomNum >= 90 && randomNum < 95)
{
grids[i].type = E_Grid_Type.Pause;
}
//有5%的几率 是时空隧道
else if (randomNum >= 95 && randomNum < 100)
{
grids[i].type = E_Grid_Type.Tunnel;
}
//位置应该如何设置
grids[i].pos = new Vector2(x, y);
//每次循环都应该按一定规则去变化位置
//加十次
if (indexX == 10)
{
y += 1;
//加一次Y记一次数
++indexY;
if (indexY == 2)
{
//y加2次后,把x加的次数记0
indexX = 0;
indexY = 0;
//反向步长(秒啊!)
stepNum = -stepNum;
}
}
else
{
x += stepNum;
//加一次X记一次数
++indexX;
}
}
}
//画地图的方法
public void Draw()
{
for (int i = 0; i < grids.Length; i++)
{
grids[i].Draw();
}
}
}
#endregion
#region 7.玩家枚举和玩家结构体
///
/// 玩家类型枚举
///
enum E_PlayerType
{
///
/// 玩家
///
Player,
///
/// 电脑
///
Computer,
}
struct Player
{
//玩家类型
public E_PlayerType type;
//当前所在地图哪一个索引的格子
public int nowIndex;
//是否暂停的标识
public bool isPause;
public Player(int index, E_PlayerType type)
{
nowIndex = index;
this.type = type;
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.WriteLine("★");
break;
case E_PlayerType.Computer:
Console.ForegroundColor = ConsoleColor.Magenta;
Console.WriteLine("▲");
break;
default:
break;
}
}
}
#endregion
}
C#基础实践项目(老师实现)--飞行棋
1.枚举的使用不够稳健
2.自己实现的和老师实现的出入还是挺大的
1.多学习流程图
2.多运用断点调试
3.程序员的目标是:偷懒