用.NET编写一个贪吃蛇游戏
时间: 2023-05-20 18:05:03 浏览: 46
好的,我可以为您提供一个简单的贪吃蛇游戏的.NET实现。以下是代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace SnakeGame
{
class Program
{
static void Main(string[] args)
{
Console.SetWindowSize(80, 25); // 设置控制台窗口大小
Console.SetBufferSize(80, 25); // 设置控制台缓冲区大小
Walls walls = new Walls(80, 25); // 创建游戏边界
walls.Draw(); // 绘制游戏边界
Point p = new Point(4, 5, '*'); // 创建初始蛇头
Snake snake = new Snake(p, 4, Direction.RIGHT); // 创建蛇
snake.Draw(); // 绘制蛇
FoodCreator foodCreator = new FoodCreator(80, 25, '$'); // 创建食物生成器
Point food = foodCreator.CreateFood(); // 生成食物
food.Draw(); // 绘制食物
while (true)
{
if (walls.IsHit(snake) || snake.IsHitTail()) // 判断游戏是否结束
{
Console.Clear();
Console.WriteLine("游戏结束!");
Console.ReadLine();
break;
}
if (snake.Eat(food)) // 判断蛇是否吃到食物
{
food = foodCreator.CreateFood(); // 生成新的食物
food.Draw(); // 绘制新的食物
}
else
{
snake.Move(); // 蛇移动
}
Thread.Sleep(100); // 控制游戏速度
if (Console.KeyAvailable) // 处理键盘输入
{
ConsoleKeyInfo key = Console.ReadKey();
snake.HandleKey(key.Key);
}
}
}
}
}
// 定义方向枚举
enum Direction
{
LEFT,
RIGHT,
UP,
DOWN
}
// 定义点类
class Point
{
public int x;
public int y;
public char sym;
public Point(int _x, int _y, char _sym)
{
x = _x;
y = _y;
sym = _sym;
}
public Point(Point p)
{
x = p.x;
y = p.y;
sym = p.sym;
}
public void Move(int offset, Direction direction)
{
if (direction == Direction.RIGHT)
x += offset;
else if (direction == Direction.LEFT)
x -= offset;
else if (direction == Direction.UP)
y -= offset;
else if (direction == Direction.DOWN)
y += offset;
}
public bool IsHit(Point p)
{
return p.x == x && p.y == y;
}
public void Draw()
{
Console.SetCursorPosition(x, y);
Console.Write(sym);
}
public void Clear()
{
sym = ' ';
Draw();
}
}
// 定义蛇类
class Snake
{
List<Point> body;
Direction direction;
int step;
public Snake(Point tail, int length, Direction _direction)
{
direction = _direction;
step = 0;
body = new List<Point>();
for (int i = 0; i < length; i++)
{
Point p = new Point(tail);
p.Move(i, direction);
body.Add(p);
}
}
public bool Eat(Point food)
{
Point head = GetNextPoint();
if (head.IsHit(food))
{
food.sym = head.sym;
body.Add(food);
return true;
}
else
{
return false;
}
}
public Point GetNextPoint()
{
Point head = body.Last();
Point nextPoint = new Point(head);
nextPoint.Move(1, direction);
return nextPoint;
}
public void Move()
{
Point tail = body.First();
body.Remove(tail);
Point head = GetNextPoint();
body.Add(head);
tail.Clear();
head.Draw();
step++;
}
public void HandleKey(ConsoleKey key)
{
if (key == ConsoleKey.LeftArrow)
direction = Direction.LEFT;
else if (key == ConsoleKey.RightArrow)
direction = Direction.RIGHT;
else if (key == ConsoleKey.UpArrow)
direction = Direction.UP;
else if (key == ConsoleKey.DownArrow)
direction = Direction.DOWN;
}
public bool IsHitTail()
{
var head = body.Last();
for (int i = 0; i < body.Count - 2; i++)
{
if (head.IsHit(body[i]))
return true;
}
return false;
}
public void Draw()
{
foreach (Point p in body)
{
p.Draw();
}
}
}
// 定义食物生成器类
class FoodCreator
{
int mapWidth;
int mapHeight;
char sym;
Random random = new Random();
public FoodCreator(int mapWidth, int mapHeight, char sym)
{
this.mapWidth = mapWidth;
this.mapHeight = mapHeight;
this.sym = sym;
}
public Point CreateFood()
{
int x = random.Next(2, mapWidth - 2);
int y = random.Next(2, mapHeight - 2);
return new Point(x, y, sym);
}
}
// 定义游戏边界类
class Walls
{
List<Point> wallList;
public Walls(int mapWidth, int mapHeight)
{
wallList = new List<Point>();
// 上下边界
for (int x = 0; x < mapWidth; x++)
{
Point p1 = new Point(x, 0, '#');
Point p2 = new Point(x, mapHeight - 1, '#');
wallList.Add(p1);
wallList.Add(p2);
}
// 左右边界
for (int y = 0; y < mapHeight; y++)
{
Point p1 = new Point(0, y, '#');
Point p2 = new Point(mapWidth - 1, y, '#');
wallList.Add(p1);
wallList.Add(p2);
}
}
public bool IsHit(Point p)
{
foreach (var wall in wallList)
{
if (wall.IsHit(p))
{
return true;
}
}
return false;
}
public void Draw()
{
foreach (var wall in wallList)
{
wall.Draw();
}
}
}