博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
github下载下来的C#控制台小游戏[含源码]
阅读量:4913 次
发布时间:2019-06-11

本文共 7516 字,大约阅读时间需要 25 分钟。

早就听说了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                  ||                                     ||                                     |||         *                         *|||                                   *|||                                   *|||                                   *||                                     ||                                     ||                                     ||                                     ||                                     ||_____________________________________|_*/

 

新手和学习者值得看的代码!

转载于:https://www.cnblogs.com/flyant/p/4289559.html

你可能感兴趣的文章
Git 删除服务器的远程分支
查看>>
python中sys.argv使用
查看>>
OpenStack-Keystone(2)
查看>>
《黑马程序员》C语言中的基本数据类型 (C语言)
查看>>
Android中的Telephony学习笔记(2)
查看>>
一步一步学Spring.NET——1、Spring.NET环境准备
查看>>
栈应用 - 后缀表达式的计算
查看>>
关于std:auto_ptr std:shared_ptr std:unique_ptr
查看>>
python字典多重
查看>>
一个最简单的HTML文件
查看>>
1022. D进制的A+B (20)
查看>>
jquery控制按钮的禁用与启用
查看>>
VS2010,VS2012,VS2013中,无法嵌入互操作类型“……”,请改用适用的接口的解决方法...
查看>>
Pascal's Triangle
查看>>
Spark-Dataframe操作
查看>>
CS0234: 命名空间“System.Web.Mvc”中不存在类型或命名空间名称“Ajax”(是否缺少程序集引用?)...
查看>>
rpg游戏技能模型
查看>>
bzoj 4321: queue2
查看>>
Swift使用Alamofire实现网络请求
查看>>
springmvc+shiro认证框架配置
查看>>