贪吃蛇小项目
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace 贪吃蛇
|
||||
{
|
||||
class Food : GameObject
|
||||
{
|
||||
public Food(Snake snake)
|
||||
{
|
||||
RandomPos(snake);
|
||||
}
|
||||
public override void Draw()
|
||||
{
|
||||
Console.ForegroundColor = ConsoleColor.Cyan;
|
||||
Console.SetCursorPosition(pos.x, pos.y);
|
||||
Console.Write("◎");
|
||||
}
|
||||
public void RandomPos(Snake snake)
|
||||
{
|
||||
Random r = new Random();
|
||||
int x = r.Next(1, Game.w / 2 - 1) * 2; //1-38的随机数
|
||||
int y = r.Next(2, Game.h - 3); //1-18的随机数
|
||||
pos = new Position(x, y);
|
||||
//判断生成的位置是否和蛇身重合
|
||||
if (snake.CheckSamePos(pos))
|
||||
{
|
||||
RandomPos(snake);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace 贪吃蛇
|
||||
{
|
||||
enum E_BodyType
|
||||
{
|
||||
Head, //蛇头
|
||||
Body, //蛇身
|
||||
}
|
||||
class SnakeBody : GameObject
|
||||
{
|
||||
private E_BodyType bodyType; //身体类型
|
||||
public SnakeBody(int x, int y, E_BodyType bodyType)
|
||||
{
|
||||
pos = new Position(x, y);
|
||||
this.bodyType = bodyType;
|
||||
}
|
||||
public override void Draw()
|
||||
{
|
||||
Console.SetCursorPosition(pos.x, pos.y);
|
||||
Console.ForegroundColor = bodyType == E_BodyType.Head ? ConsoleColor.Yellow : ConsoleColor.Green;
|
||||
Console.Write(bodyType == E_BodyType.Head ? "●" : "○");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace 贪吃蛇
|
||||
{
|
||||
class Wall : GameObject
|
||||
{
|
||||
public Wall(int x, int y)
|
||||
{
|
||||
pos = new Position(x, y);
|
||||
}
|
||||
public override void Draw()
|
||||
{
|
||||
Console.ForegroundColor = ConsoleColor.Red;
|
||||
Console.SetCursorPosition(pos.x, pos.y);
|
||||
Console.Write("■");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user