欢迎光临
我们一直在努力

github下载下来的C#控制台小游戏[含源码]

早就听说了github是世界最大的源码库,但自己却不是很懂,今天去研究了下,注册了一个帐号,然后在上面搜索了一下C# game,然后发现有许多的游戏.

随意地选择了一个,感觉比较简单,于是就下载了下来。这个解决方案包含了5个项目,每个项目都是一个小的控制台游戏。

我打开运行了了下,有2个项目报错,但是汽车和乒乓可以运行。

看了下代码,感觉还不错,有许多值得学习的地方。

这个代码库是一个美国人提供的,瞬间感觉自己也变得洋气了起来!

每个项目都只有一个文件,真是够简单。

贴出乒乓的代码看看

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; namespace PingPong { class Program { static int firstPlayerPadSize = 10; static int secondPlayerPadSize = 4; static int ballPositionX = 0; static int ballPositionY = 0; static bool ballDirectionUp = true; // Determines if the ball direction is up static bool ballDirectionRight = false; static int firstPlayerPosition = 0; static int secondPlayerPosition = 0; static int firstPlayerResult = 0; static int secondPlayerResult = 0; static Random randomGenerator = new Random(); static void RemoveScrollBars() { console.ForegroundColor = ConsoleColor.Yellow; Console.BufferHeight = Console.WindowHeight; Console.BufferWidth = Console.WindowWidth; } static void DrawFirstPlayer() { for (int y = firstPlayerPosition; y < firstPlayerPosition + firstPlayerPadSize; y++) { PrintAtPosition(0, y, '|'); PrintAtPosition(1, y, '|'); } } static void PrintAtPosition(int x, int y, char symbol) { Console.SetCursorPosition(x, y); Console.Write(symbol); } static void DrawSecondPlayer() { for (int y = secondPlayerPosition; y < secondPlayerPosition + secondPlayerPadSize; y++) { PrintAtPosition(Console.WindowWidth - 1, y, '|'); PrintAtPosition(Console.WindowWidth - 2, y, '|'); } } static void SetInitialPositions() { firstPlayerPosition = Console.WindowHeight / 2 - firstPlayerPadSize / 2; secondPlayerPosition = Console.WindowHeight / 2 - secondPlayerPadSize / 2; SetBallAtTheMiddleOfTheGameField(); } static void SetBallAtTheMiddleOfTheGameField() { ballPositionX = Console.WindowWidth / 2; ballPositionY = Console.WindowHeight / 2; } static void DrawBall() { PrintAtPosition(ballPositionX, ballPositionY, '@'); } static void PrintResult() { Console.SetCursorPosition(Console.WindowWidth / 2 - 1, 0); Console.Write("{0}-{1}", firstPlayerResult, secondPlayerResult); } static void MoveFirstPlayerDown() { if (firstPlayerPosition < Console.WindowHeight - firstPlayerPadSize) { firstPlayerPosition++; } } static void MoveFirstPlayerUp() { if (firstPlayerPosition > 0) { firstPlayerPosition--; } } static void MoveSecondPlayerDown() { if (secondPlayerPosition < Console.WindowHeight - secondPlayerPadSize) { secondPlayerPosition++; } } static void MoveSecondPlayerUp() { if (secondPlayerPosition > 0) { secondPlayerPosition--; } } static void SecondPlayerAIMove() { int randomNumber = randomGenerator.Next(1, 101); //if (randomNumber == 0) //{ // MoveSecondPlayerUp(); //} //if (randomNumber == 1) //{ // MoveSecondPlayerDown(); //} if (randomNumber <= 70) { if (ballDirectionUp == true) { MoveSecondPlayerUp(); } else { MoveSecondPlayerDown(); } } } private static void MoveBall() { if (ballPositionY == 0) { ballDirectionUp = false; } if (ballPositionY == Console.WindowHeight - 1) { ballDirectionUp = true; } if (ballPositionX == Console.WindowWidth - 1) { SetBallAtTheMiddleOfTheGameField(); ballDirectionRight = false; ballDirectionUp = true; firstPlayerResult++; Console.SetCursorPosition(Console.WindowWidth / 2, Console.WindowHeight / 2); Console.WriteLine("First player wins!"); Console.ReadKey(); } if (ballPositionX == 0) { SetBallAtTheMiddleOfTheGameField(); ballDirectionRight = true; ballDirectionUp = true; secondPlayerResult++; Console.SetCursorPosition(Console.WindowWidth / 2, Console.WindowHeight / 2); Console.WriteLine("Second player wins!"); Console.ReadKey(); } if (ballPositionX < 3) { if (ballPositionY >= firstPlayerPosition && ballPositionY < firstPlayerPosition + firstPlayerPadSize) { ballDirectionRight = true; } } if (ballPositionX >= Console.WindowWidth - 3 - 1) { if (ballPositionY >= secondPlayerPosition && ballPositionY < secondPlayerPosition + secondPlayerPadSize) { ballDirectionRight = false; } } if (ballDirectionUp) { ballPositionY--; } else { ballPositionY++; } if (ballDirectionRight) { ballPositionX++; } else { ballPositionX--; } } static void Main(string[] args) { RemoveScrollBars(); SetInitialPositions(); while (true) { if (Console.KeyAvailable) { ConsoleKeyInfo keyInfo = Console.ReadKey(); if (keyInfo.Key == ConsoleKey.UpArrow) { MoveFirstPlayerUp(); } if (keyInfo.Key == ConsoleKey.DownArrow) { MoveFirstPlayerDown(); } } SecondPlayerAIMove(); MoveBall(); Console.Clear(); DrawFirstPlayer(); DrawSecondPlayer(); DrawBall(); PrintResult(); Thread.Sleep(60); } } } } /* |____________________________________ | | 1-0 | | | | | || * *| || *| || *| || *| | | | | | | | | | | |_____________________________________|_ */

 

  • 海报
海报图正在生成中...
赞(0) 打赏
声明:
1、本博客不从事任何主机及服务器租赁业务,不参与任何交易,也绝非中介。博客内容仅记录博主个人感兴趣的服务器测评结果及一些服务器相关的优惠活动,信息均摘自网络或来自服务商主动提供;所以对本博客提及的内容不作直接、间接、法定、约定的保证,博客内容也不具备任何参考价值及引导作用,访问者需自行甄别。
2、访问本博客请务必遵守有关互联网的相关法律、规定与规则;不能利用本博客所提及的内容从事任何违法、违规操作;否则造成的一切后果由访问者自行承担。
3、未成年人及不能独立承担法律责任的个人及群体请勿访问本博客。
4、一旦您访问本博客,即表示您已经知晓并接受了以上声明通告。
文章名称:《github下载下来的C#控制台小游戏[含源码]》
文章链接:https://www.456zj.com/40980.html
本站资源仅供个人学习交流,请于下载后24小时内删除,不允许用于商业用途,否则法律问题自行承担。

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址