早就听说了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#控制台小游戏[含源码]的更多相关文章

  1. WinFom中经典小游戏(含源码)

    最近整理了若干经典的小游戏,无聊时可以打发时间.程序本身不大,练手非常不错,主要是GDI编程,主界面地址如下图所示 源码下载方式 1,关注微信公众号:小特工作室(也可直接扫描签名处二维码) 2,发送: ...

  2. Chrome自带恐龙小游戏的源码研究(七)

    在上一篇<Chrome自带恐龙小游戏的源码研究(六)>中研究了恐龙的跳跃过程,这一篇研究恐龙与障碍物之间的碰撞检测. 碰撞盒子 游戏中采用的是矩形(非旋转矩形)碰撞.这类碰撞优点是计算比较 ...

  3. Chrome自带恐龙小游戏的源码研究(完)

    在上一篇<Chrome自带恐龙小游戏的源码研究(七)>中研究了恐龙与障碍物的碰撞检测,这一篇主要研究组成游戏的其它要素. 游戏分数记录 如图所示,分数及最高分记录显示在游戏界面的右上角,每 ...

  4. Chrome自带恐龙小游戏的源码研究(六)

    在上一篇<Chrome自带恐龙小游戏的源码研究(五)>中实现了眨眼睛的恐龙,这一篇主要研究恐龙的跳跃. 恐龙的跳跃 游戏通过敲击键盘的Spacebar或者Up来实现恐龙的跳跃.先用一张图来 ...

  5. Chrome自带恐龙小游戏的源码研究(五)

    在上一篇<Chrome自带恐龙小游戏的源码研究(四)>中实现了障碍物的绘制及移动,从这一篇开始主要研究恐龙的绘制及一系列键盘动作的实现. 会眨眼睛的恐龙 在游戏开始前的待机界面,如果仔细观 ...

  6. Chrome自带恐龙小游戏的源码研究(四)

    在上一篇<Chrome自带恐龙小游戏的源码研究(三)>中实现了让游戏昼夜交替,这一篇主要研究如何绘制障碍物. 障碍物有两种:仙人掌和翼龙.仙人掌有大小两种类型,可以同时并列多个:翼龙按高. ...

  7. Chrome自带恐龙小游戏的源码研究(三)

    在上一篇<Chrome自带恐龙小游戏的源码研究(二)>中实现了云朵的绘制和移动,这一篇主要研究如何让游戏实现昼夜交替. 昼夜交替的效果主要是通过样式来完成,但改变样式的时机则由脚本控制. ...

  8. Chrome自带恐龙小游戏的源码研究(二)

    在上一篇<Chrome自带恐龙小游戏的源码研究(一)>中实现了地面的绘制和运动,这一篇主要研究云朵的绘制. 云朵的绘制通过Cloud构造函数完成.Cloud实现代码如下: Cloud.co ...

  9. Chrome自带恐龙小游戏的源码研究(一)

    目录 Chrome自带恐龙小游戏的源码研究(一)——绘制地面 Chrome自带恐龙小游戏的源码研究(二)——绘制云朵 Chrome自带恐龙小游戏的源码研究(三)——昼夜交替 Chrome自带恐龙小游戏 ...

随机推荐

  1. 报错** is not accessible due to restriction on required library

    报错: Description Resource Path Location TypeAccess restriction: The type Map<String,Object> is ...

  2. [Angular] Architectures for Huge Angular Based Enterprise

    Using Angular CLI v6, we are able to create library or small application inside a Angular CLI genera ...

  3. RxJava系列之中的一个 初识Rxjava

    1.简单介绍 基础知识 响应式代码的基本组成部分是Observables和Subscribers(事实上Observer才是最小的构建块,但实践中使用最多的是Subscriber.由于Subscrib ...

  4. soapUI系列之—-05 JDBC Request & Xpath Match

    一.配置JDBC Connection String 1. 以Oracle为例,要使用JDBC数据库就要先下一个 oracle JDBC的驱动,下载成功后把它放到soapUI安装目录下的  bin/e ...

  5. Linux_C——动态库,静态库

    /usr/lib /lib:标准系统库文件          库是一组预先编译好的函数的集合,这些函数都是按照可重用的原则编写的.它们通常有一组相互关联的函数组成以          执行某项常见的任 ...

  6. java读取Excel表格中的数据

    1.需求 用java代码读取hello.xls表格中的数据 2.hello.xls表格 3.java代码 package com.test; import java.io.File; import j ...

  7. Windows下安装MySQL5.6绿色版

    建议安装MySQL绿色版的,什么是绿色版的?就是免安装,下载下来的截图是这样的 在该目录下创建一个文件夹/data用于存放数据, 新建一个my.ini文件,my.ini里面最基本的配置如下,my.in ...

  8. MySQL 存储过程传參之in, out, inout 參数使用方法

    存储过程传參:存储过程的括号中.能够声明參数. 语法是 create procedure p([in/out/inout] 參数名  參数类型 ..) in :给參数传入值,定义的參数就得到了值 ou ...

  9. Eclipse添加Qt插件

    此文件仅为步骤操作作一个记录,以便以后方便查阅. 1.操作大体参考这个网站:http://blog.csdn.net/defonds/article/details/5013412 2.我的运行环境: ...

  10. 替换Android自带apk【转】

    本文转载自:http://www.voidcn.com/article/p-gonowdjh-vz.html 安卓自带的app放在/system/app/下,当我们想要替换这些应用时可以参考如下步骤: ...