static char[,] FrogUpdate(Container container, char[,] mappos, Frog frog) { int y = frog.y; int x = frog.x; frog.jumpduring = DateTime.Now;//跳跃时间计算 if (frog.jump == true) { if (frog.Be_Brick(frog.x, frog.y - 1, container) || frog.y - 1 <= 1)//在空中撞到砖块或者边界,马上下降 { frog.jump = false; } else { if (frog.y >= frog.starty - 4) //如果在跳跃高度内,往上跳 { double c = TimeGo(frog.jumpduring, frog.jumpstart); //与起跳时间的秒数差 frog.y -= (int)Math.Round(-0.444 * c * c + 2.666 * c + 1); Clear(mappos); mappos[frog.y, frog.x] = 'm'; } else if (frog.y < frog.starty - 3)//超越跳跃最高点了 { frog.jump = false; } } }//跳跃 if (frog.Be_Reporter(frog.x, frog.y, container) || frog.Be_Water(frog.x, frog.y + 1, container)) { frog.hp = 0; }//青蛙碰怪或者碰到水 if (!frog.Be_Brick(frog.x, frog.y + 1, container) && frog.jump == false) { frog.y++; Clear(mappos); mappos[frog.y, frog.x] = 'm'; }//青蛙脚下没有砖块 if (mappos[frog.y + 1, frog.x] == '@') { mappos[frog.y + 1, frog.x] = ' '; container.re.Remove((frog.y + 1) * 100 + frog.x); }//青蛙脚下是怪 if (container.bu.Count > 0) { List <Bullet> disappear = new List <Bullet>(); BulletClear(mappos); foreach (var bullet in container.bu) { if (bullet.x - bullet.startx > 10 || bullet.Be_Brick(bullet.x + 1, bullet.y, container) || bullet.Be_Reporter(bullet.x, bullet.y, container)) { disappear.Add(bullet); } else { bullet.x++; mappos[bullet.y, bullet.x] = '>'; } } foreach (var bullet in disappear) { container.bu.Remove(bullet); } } //子弹判定(如果子弹超过射程或者子弹碰墙或者子弹碰到怪便消失) return(mappos); } //青蛙的更新
static void Main(string[] args) { Console.CursorVisible = false; Console.WindowHeight = 30; Console.WindowWidth = 100; string namespaceName = Assembly.GetExecutingAssembly().GetName().Name.ToString(); Assembly assembly = Assembly.GetExecutingAssembly(); SoundPlayer sp = new SoundPlayer(assembly.GetManifestResourceStream(namespaceName + ".Resources" + ".180.wav")); sp.PlayLooping(); Frog frog = new Frog(19, 0); frog.resurrection = 3; int levelnum = 1; bool win = false; while (frog.resurrection > 0 && win == false) //只要还有复活次数,就重新读取地图 { frog.y = 19; //重新定义坐标 frog.x = 0; MapCreate map = new MapCreate(); //创建新地图 Container container = new Container(); //创建新存储类 char[,] mappos = null; //初始化临时地图 container = map.ReadChar(map.PosRead(map.Mapchoose(levelnum))); //存储类储存数据 mappos = map.PosRead(map.Mapchoose(levelnum)); //临时地图存储坐标 int test = 0; while (true) { Console.SetCursorPosition(0, 0); mappos[frog.y, frog.x] = 'm'; map.PrintDraw(mappos); Console.WriteLine(test); test++; //刷新帧数测试 if (Console.KeyAvailable) //有键值输入 { mappos = FrogMove(container, mappos, frog); //操作输入 } if (test % 5 == 0) { FrogUpdate(container, mappos, frog);//青蛙更新 } if (test % 10 == 0) { ReporterUpdate(container, mappos); //怪物更新 } if (frog.hp == 0) //死亡跳出 { frog.arm = false; frog.resurrection--; //如果失败,则消耗复活次数 frog.hp = 92; //回血 break; } if (frog.x + 1 == 99)//碰到城门或者右边界,进入下一关 { levelnum++; break; } if (frog.Be_Gate(frog.x, frog.y, container)) { win = true; break; } for (int n = 0; n < frog.resurrection; n++) { mappos[1, 12 + n] = 'M'; } } } if (frog.resurrection == 0) { sp.Stop(); sp = new SoundPlayer(assembly.GetManifestResourceStream(namespaceName + ".Resources" + ".1756.wav")); sp.Play(); MapCreate map = new MapCreate(); //创建新地图 char[,] mappos = map.PosRead(map.Mapchoose(7)); //临时地图存储坐标 Console.SetCursorPosition(0, 0); map.PrintDraw(mappos); } else if (win == true) { sp.Stop(); sp = new SoundPlayer(assembly.GetManifestResourceStream(namespaceName + ".Resources" + ".1761.wav")); sp.Play(); MapCreate map = new MapCreate(); //创建新地图 char[,] mappos = map.PosRead(map.Mapchoose(6)); //临时地图存储坐标 Console.SetCursorPosition(0, 0); map.PrintDraw(mappos); } Console.ReadLine(); }
static char[,] FrogMove(Container container, char[,] mappos, Frog frog)//判断青蛙运动 { ConsoleKeyInfo keyInfo = Console.ReadKey(true); int y = frog.y; int x = frog.x; if (keyInfo.Key == ConsoleKey.RightArrow || keyInfo.Key == ConsoleKey.LeftArrow)//如果往左或者往右运动,就计算按键时差 { float oversecond = 0, startsecond = 0; startsecond = DateTime.Now.Millisecond; if (System.Math.Abs(startsecond - oversecond) < 100) { return(mappos); } else { if (!frog.Be_Brick(frog.x, frog.y + 1, container) && frog.airmove <= 0) { return(mappos); } else { frog.airmove--; oversecond = startsecond; if (keyInfo.Key == ConsoleKey.RightArrow) { if (frog.Be_Brick(frog.x + 1, frog.y, container)) { return(mappos); } //往左撞墙 else { frog.x++; Clear(mappos); mappos[frog.y, frog.x] = 'm'; } } else if (keyInfo.Key == ConsoleKey.LeftArrow) { if (frog.Be_Brick(frog.x - 1, frog.y, container)) { return(mappos); } //往右撞墙 else if (frog.x - 1 < 0) { frog.x++; return(mappos); } else { frog.x--; Clear(mappos); mappos[frog.y, frog.x] = 'm'; } } } } } if (keyInfo.Key == ConsoleKey.UpArrow && frog.Be_Brick(frog.x, frog.y + 1, container))//如果按键跳跃就计算是否站在地面上,是则跳跃,否则不跳跃 { frog.jump = true; frog.starty = frog.y; frog.airmove = 5; frog.jumpstart = DateTime.Now; } if (frog.Be_Glasses(frog.x, frog.y, container))//如果碰到眼镜,则捡到武器 { frog.arm = true; } if (keyInfo.Key == ConsoleKey.Spacebar && frog.arm == true)//如果持有武器且按键,则射击 { frog.attack = true; Bullet bullet = new Bullet(); container.bu.Add(bullet); bullet.x = frog.x; bullet.y = frog.y; bullet.startx = frog.x; } if (frog.Be_Timecoin(frog.x, frog.y, container))//碰到硬币 { container.ti.Remove((y + 1) * 100 + x); frog.timecoin++; } if (frog.Be_Brick(frog.x, frog.y + 1, container)) { frog.airmove = 5; } return(mappos); }