早就听说了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 | | | | | || * *| || *| || *| || *| | | | | | | | | | | |_____________________________________|_ */
新手和学习者值得看的代码!点击下载
时间: 2024-09-30 05:57:51