標籤:
早就聽說了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 || || ||| * *||| *||| *||| *|| || || || || ||_____________________________________|_*/
新手和學習者值得看的代碼!點擊下載
github下載下來的C#控制台小遊戲[含源碼]