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自带恐龙小游戏 ...
随机推荐
- CentOS 5.11安装配置LAMP服务器(Apache+PHP5+MySQL)
http://www.osyunwei.com/archives/8880.html 准备篇: CentOS 5.x系统安装配置图解教程 http://www.osyunwei.com/archive ...
- SQL 主机
SQL 主机 SQL 主机 如果您想要您的网站存储数据在数据库并从数据库显示数据,您的 Web 服务器必须能使用 SQL 语言访问数据库系统. 如果您的 Web 服务器托管在互联网服务提供商(ISP, ...
- Project Euler:Problem 61 Cyclical figurate numbers
Triangle, square, pentagonal, hexagonal, heptagonal, and octagonal numbers are all figurate (polygon ...
- Hive中行列转换
1.演示多列转为单行 数据文件及内容: student.txt xiaoming|english|92.0 xiaoming|chinese|98.0 xiaoming|math|89.5 huahu ...
- 【C#】无损转换Image为Icon 【C#】组件发布:MessageTip,轻快型消息提示窗 【C#】给无窗口的进程发送消息 【手记】WebBrowser响应页面中的blank开新窗口及window.close关闭本窗体 【手记】调用Process.EnterDebugMode引发异常:并非所有引用的特权或组都分配给呼叫方 【C#】DataRowState演变备忘
[C#]无损转换Image为Icon 如题,市面上常见的方法是: var handle = bmp.GetHicon(); //得到图标句柄 return Icon.FromHandle(handle ...
- VMware 虚拟机添加硬盘以及为新添加的硬盘创建Samba共享 (转)
一.为VMware虚拟机添加硬盘 1. 首先在VMware虚拟机的VM->Setting子菜单中为虚拟机添加一块15G大小的SCSI类型的硬盘(注意:如果原来为IDE硬盘,SCSI类型的硬盘可能 ...
- 理解Paxos Made Practical
Paxos Made Practical 当一个组中一台机器提出一个值时,其它成员机器通过PAXOS算法在这个值上达成一致. Paxos分三个阶段. 第一阶段: 提出者会选出一个提议编号n(n> ...
- Android SDK update被墙
1.输入命令:$ sudo gedit /etc/hosts 2.在打开的 /etc/hosts 在文件的末尾添加下面一句:74.125.237.1 dl-ssl.google.com
- 定时任务 bash 对远程数据库 备份 读写
1g表 每行都有可能被更新,故全表备份 检测备份是否在进行 [root@hadoop1 ~]# netstat --numeric-ports | grep 3306tcp 0 ...
- [noip模拟赛]小U的女装
https://www.zybuluo.com/ysner/note/1329304 题面 有一张\(n\)点\(m\)边的.不一定联通的无向图. 如果选了一条边,就不能选其两个端点. 现在同时选点和 ...