C#贪吃蛇
文章目录
-
-
- Program.cs
- Text1
-
- Game.cs
- ISceneUpdate.cs
- Text2
-
- BeginScene.cs
- BegionOrEndScene.cs
- EndScene.cs
- GameScene.cs
- Text3
-
- GameObject.cs
- IDraw.cs
- Position.cs
- Text4
-
- Food.cs
- SnakeBody.cs
- Wall.cs
- Text5
-
- Map.cs
- Text6
-
- Snake.cs
-
Program.cs
using 贪吃蛇.Text1;
Game game = new Game();
game.start();
Text1
Game.cs
using 贪吃蛇.Text2;
namespace 贪吃蛇.Text1
{
enum E_SceneType
{
Begin,
Game,
End,
}
internal class Game
{
public const int w = 80;
public const int h = 20;
public static ISceneUpdate nowScene;
public Game()
{
Console.CursorVisible = false;
Console.SetWindowSize(w, h);
Console.SetBufferSize(w, h);
ChangeScene(E_SceneType.Begin);
}
public void start()
{
while (true)
{
if (nowScene != null)
{
nowScene.Update();
}
}
}
public static void ChangeScene(E_SceneType type)
{
Console.Clear();
switch (type)
{
case E_SceneType.Begin:
nowScene = new BeginScene();
break;
case E_SceneType.Game:
nowScene = new GameScene();
break;
case E_SceneType.End:
nowScene = new EndScene();
break;
}
}
}
}
ISceneUpdate.cs
namespace 贪吃蛇.Text1
{
/// <summary>
/// 场景更新接口
/// </summary>
internal interface ISceneUpdate
{
void Update();
}
}
Text2
BeginScene.cs
using 贪吃蛇.Text1;
namespace 贪吃蛇.Text2
{
internal class BeginScene : BegionOrEndScene
{
public BeginScene()
{
strTitle = "贪吃蛇";
strOne = "开始游戏";
}
public override void EnterJDoSomthing()
{
if (nowSelIndex == 0)
{
Game.ChangeScene(E_SceneType.Game);
}
else
{
Environment.Exit(0);
}
}
}
}
BegionOrEndScene.cs
using 贪吃蛇.Text1;
namespace 贪吃蛇.Text2
{
abstract internal class BegionOrEndScene : ISceneUpdate
{
protected int nowSelIndex = 0;
protected string strTitle;
protected string strOne;
public abstract void EnterJDoSomthing();
public void Update()
{
Console.ForegroundColor = ConsoleColor.White;
Console.SetCursorPosition(Game.w / 2 - strTitle.Length, 5);
Console.Write(strTitle);
Console.SetCursorPosition(Game.