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自带恐龙小游戏 ...
 
随机推荐
- Qt Quick 图像处理实例之美图秀秀(附源代码下载)
			
在<Qt Quick 之 QML 与 C++ 混合编程具体解释>一文中我们解说了 QML 与 C++ 混合编程的方方面面的内容,这次我们通过一个图像处理应用.再来看一下 QML 与 C++ ...
 - 《The Swift Programming Language》的笔记-第27页
			
页 1 type safelanguage 本页的主要内容是说swift语言是"类型检查"的安全型编程语言.意思是赋值语句的左值和右值的类型要一致,左值声明是string型变量那么 ...
 - (WPF)附加属性
			
<Window x:Class="DeepXAML.MainWindow" xmlns="http://schemas.microsoft.com/winfx/20 ...
 - thinkphp3.2.3 数据库写入add 方法的一些问题。
			
最近在做项目中遇到的一个数据操作add()方法,在不开启debug的模式下会漏掉一些字段没写入数据库. 当时并不知道是这个原因,明明在开发的时候都是没问题的,怎么突然出现这个问题,找了好久都没有头绪, ...
 - 3.2.1 配置构建Angular应用——简单的笔记存储应用——展示功能
			
本节我们会通过构建一个简单的笔记存储应用(可以载入并修改一组简单的笔记)来学习如何应用Angular的特性.这个应用用到的特性有: 在JSON文件中存储笔记 展示.创建.修改和删除笔记 在笔记中使用M ...
 - MVC+ZTree实现对树的CURD及拖拽操作
			
上一讲中,我们学习了如何使用zTree对一棵大树(大数据量的树型结构的数据表,呵呵,名称有点绕,但说的是事实)进行异步加载,今天这讲,我们来说说,如何去操作这棵大树,无非就是添加子节点,删除节点,编辑 ...
 - 大话设计模式C++实现-第14章-观察者模式
			
一.UML图 关键词:Subject维护一个Observer列表.Subject运行Notify()时就运行列表中的每一个Observer的Update(). 二.概念 观察者模式:定义了一种一对多的 ...
 - [学习笔记]渗透测试metasploit
			
1.渗透成功后,在meterpreter命令行,需要使用如下命令切换当前目录.更多信息,可以参考: meterpreter > pwd C:\ meterpreter > cd /&quo ...
 - easy_install uncompyle6 egg
			
localhost:~ # easy_install uncompyle6-3.0.1-py3.6.eggProcessing uncompyle6-3.0.1-py3.6.eggCopying un ...
 - HTML标签防XSS攻击过滤模块--待优化
			
HTML标签防XSS攻击过滤模块 http://cnodejs.org/topic/5058962f8ea56b5e7806b2a3