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 = ;
static int secondPlayerPadSize = ;
static int ballPositionX = ;
static int ballPositionY = ;
static bool ballDirectionUp = true; // Determines if the ball direction is up
static bool ballDirectionRight = false;
static int firstPlayerPosition = ;
static int secondPlayerPosition = ;
static int firstPlayerResult = ;
static int secondPlayerResult = ;
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(, y, '|');
PrintAtPosition(, 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 - , y, '|');
PrintAtPosition(Console.WindowWidth - , y, '|');
}
} static void SetInitialPositions()
{
firstPlayerPosition = Console.WindowHeight / - firstPlayerPadSize / ;
secondPlayerPosition = Console.WindowHeight / - secondPlayerPadSize / ;
SetBallAtTheMiddleOfTheGameField();
} static void SetBallAtTheMiddleOfTheGameField()
{
ballPositionX = Console.WindowWidth / ;
ballPositionY = Console.WindowHeight / ;
} static void DrawBall()
{
PrintAtPosition(ballPositionX, ballPositionY, '@');
} static void PrintResult()
{
Console.SetCursorPosition(Console.WindowWidth / - , );
Console.Write("{0}-{1}", firstPlayerResult, secondPlayerResult);
} static void MoveFirstPlayerDown()
{
if (firstPlayerPosition < Console.WindowHeight - firstPlayerPadSize)
{
firstPlayerPosition++;
}
} static void MoveFirstPlayerUp()
{
if (firstPlayerPosition > )
{
firstPlayerPosition--;
}
} static void MoveSecondPlayerDown()
{
if (secondPlayerPosition < Console.WindowHeight - secondPlayerPadSize)
{
secondPlayerPosition++;
}
} static void MoveSecondPlayerUp()
{
if (secondPlayerPosition > )
{
secondPlayerPosition--;
}
} static void SecondPlayerAIMove()
{
int randomNumber = randomGenerator.Next(, );
//if (randomNumber == 0)
//{
// MoveSecondPlayerUp();
//}
//if (randomNumber == 1)
//{
// MoveSecondPlayerDown();
//}
if (randomNumber <= )
{
if (ballDirectionUp == true)
{
MoveSecondPlayerUp();
}
else
{
MoveSecondPlayerDown();
}
}
} private static void MoveBall()
{
if (ballPositionY == )
{
ballDirectionUp = false;
}
if (ballPositionY == Console.WindowHeight - )
{
ballDirectionUp = true;
}
if (ballPositionX == Console.WindowWidth - )
{
SetBallAtTheMiddleOfTheGameField();
ballDirectionRight = false;
ballDirectionUp = true;
firstPlayerResult++;
Console.SetCursorPosition(Console.WindowWidth / , Console.WindowHeight / );
Console.WriteLine("First player wins!");
Console.ReadKey();
}
if (ballPositionX == )
{
SetBallAtTheMiddleOfTheGameField();
ballDirectionRight = true;
ballDirectionUp = true;
secondPlayerResult++;
Console.SetCursorPosition(Console.WindowWidth / , Console.WindowHeight / );
Console.WriteLine("Second player wins!");
Console.ReadKey();
} if (ballPositionX < )
{
if (ballPositionY >= firstPlayerPosition
&& ballPositionY < firstPlayerPosition + firstPlayerPadSize)
{
ballDirectionRight = true;
}
} if (ballPositionX >= Console.WindowWidth - - )
{
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();
}
}
}
}
/*
|____________________________________ |
| 1-0 |
| |
| |
|| * *|
|| *|
|| *|
|| *|
| |
| |
| |
| |
| |
|_____________________________________|_
*/
新手和学习者值得看的代码!点击下载
github下载下来的C#控制台小游戏[含源码]的更多相关文章
- WinFom中经典小游戏(含源码)
最近整理了若干经典的小游戏,无聊时可以打发时间.程序本身不大,练手非常不错,主要是GDI编程,主界面地址如下图所示 源码下载方式 1,关注微信公众号:小特工作室(也可直接扫描签名处二维码) 2,发送: ...
- Chrome自带恐龙小游戏的源码研究(七)
在上一篇<Chrome自带恐龙小游戏的源码研究(六)>中研究了恐龙的跳跃过程,这一篇研究恐龙与障碍物之间的碰撞检测. 碰撞盒子 游戏中采用的是矩形(非旋转矩形)碰撞.这类碰撞优点是计算比较 ...
- Chrome自带恐龙小游戏的源码研究(完)
在上一篇<Chrome自带恐龙小游戏的源码研究(七)>中研究了恐龙与障碍物的碰撞检测,这一篇主要研究组成游戏的其它要素. 游戏分数记录 如图所示,分数及最高分记录显示在游戏界面的右上角,每 ...
- Chrome自带恐龙小游戏的源码研究(六)
在上一篇<Chrome自带恐龙小游戏的源码研究(五)>中实现了眨眼睛的恐龙,这一篇主要研究恐龙的跳跃. 恐龙的跳跃 游戏通过敲击键盘的Spacebar或者Up来实现恐龙的跳跃.先用一张图来 ...
- Chrome自带恐龙小游戏的源码研究(五)
在上一篇<Chrome自带恐龙小游戏的源码研究(四)>中实现了障碍物的绘制及移动,从这一篇开始主要研究恐龙的绘制及一系列键盘动作的实现. 会眨眼睛的恐龙 在游戏开始前的待机界面,如果仔细观 ...
- Chrome自带恐龙小游戏的源码研究(四)
在上一篇<Chrome自带恐龙小游戏的源码研究(三)>中实现了让游戏昼夜交替,这一篇主要研究如何绘制障碍物. 障碍物有两种:仙人掌和翼龙.仙人掌有大小两种类型,可以同时并列多个:翼龙按高. ...
- Chrome自带恐龙小游戏的源码研究(三)
在上一篇<Chrome自带恐龙小游戏的源码研究(二)>中实现了云朵的绘制和移动,这一篇主要研究如何让游戏实现昼夜交替. 昼夜交替的效果主要是通过样式来完成,但改变样式的时机则由脚本控制. ...
- Chrome自带恐龙小游戏的源码研究(二)
在上一篇<Chrome自带恐龙小游戏的源码研究(一)>中实现了地面的绘制和运动,这一篇主要研究云朵的绘制. 云朵的绘制通过Cloud构造函数完成.Cloud实现代码如下: Cloud.co ...
- Chrome自带恐龙小游戏的源码研究(一)
目录 Chrome自带恐龙小游戏的源码研究(一)——绘制地面 Chrome自带恐龙小游戏的源码研究(二)——绘制云朵 Chrome自带恐龙小游戏的源码研究(三)——昼夜交替 Chrome自带恐龙小游戏 ...
随机推荐
- StringUtil内部方法差异
StringUtil 的 isBlank.isEmply.isNotEmpty.isNotBlank 区别 String.trim()方法: trim()是去掉首尾空格 append(Stri ...
- Visual Studio安装空白 和 VS Code打开失败解决方案
微软博文:https://docs.microsoft.com/zh-cn/visualstudio/install/troubleshooting-installation-issues?view= ...
- Android碎纸机效果
1.总体思想 活用padding和margin 2.实现过程 public class PopupShredderView extends FrameLayout{ public PopupShred ...
- win系统下启动linux上的kafka集群及使用
一.首先在win系统下C:\Windows\System32\drivers\etc文件夹中hosts文件加入例如以下内容: 10.61.6.167 slaves1 10.61.6.168 slave ...
- Library Project里面使用Case语句判断R.id值报错。case expressions must be constant expressions
原文地址:http://blog.csdn.net/wchinaw/article/details/7325641 在一般的Android项目里R里面的资源声明看起来是这样的: public stat ...
- WebService注解汇总
Web Service 元数据注释(JSR 181) @WebService 1.serviceName: 对外发布的服务名,指定 Web Service 的服务名称:wsdl:service.缺省值 ...
- solr 7.2.1 单机及伪集群启动
1.solr的下载: 下载地址:solr官网:http://lucene.apache.org/solr进入官网点击download或者点击链接https://lucene.apache.org/so ...
- Hadoop Hive概念学习系列之hive里的索引(十三)
Hive支持索引,但是Hive的索引与关系型数据库中的索引并不相同,比如,Hive不支持主键或者外键. Hive索引可以建立在表中的某些列上,以提升一些操作的效率,例如减少MapReduce任务中需要 ...
- 并不对劲的bzoj4825:loj2018:p3721:[HNOI2017]单旋
题目大意 spaly是一种数据结构,它是只有单旋的splay 有一个初始为空的spaly,\(m\)(\(m\leq10^5\))次操作,每个操作是以下5种中的一种: 1.向spaly中插入一个数(过 ...
- bzoj 2169 连边 —— DP+容斥
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2169 就和这篇博客说的一样:https://blog.csdn.net/WerKeyTom_ ...