贪吃蛇小项目

This commit is contained in:
2025-09-21 20:11:21 +08:00
parent 0a1446e7b3
commit 116b65164b
18 changed files with 695 additions and 25 deletions
+36
View File
@@ -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);
}
}
}
}