贪吃蛇 3.0 之 捕蛇者
大概是看错题目,只看见了加入自己的小想法,就很开心地想写一只捕蛇者。写到一半发现是要用unity,但觉得已经开始了 还是做完吧,在过程中还是遇到了很多问题,推翻了原本一半代码,写了这群有点智障的捕蛇者。
设定是:捕蛇者能抓蛇,但是需从蛇尾开始,一节节吃掉,蛇同样能吃掉捕蛇者,吃掉效果同普通食物,场景中可同时存在多个捕蛇者。
遇到的问题
- 关于捕蛇者 的设定是不只 一只,当蛇长到一定长度时就会生成捕蛇者,考虑了数组,动态数组,后觉得用HashSet 来储存捕蛇者应该会比较合适。
private HashSet<snakehunter> h = new HashSet<snakehunter>(); Add(); Remove();
2.本来用foreach遍历Set 里的捕蛇者,来确定哪个捕蛇者被吃掉,需要被移除,后来发现foreach 是只读的,不可在foreach里删除和添加。所以改成了用for循环。
for(i=0;i<h.Count;i++)
{
if (h.ElementAt(i).getx() == x && h.ElementAt(i).gety() == y)
{
h.Remove(h.ElementAt(i));
break;
}
}
3.觉得操作不是很顺手,就把原本的跟移动方向相反移动也会死的设定 改成了 如果输入跟当前移动方向相反 则忽略此次输入。
4.最麻烦的大概是捕蛇者的移动了。 最初的设定是 四个方向随机选择一个方向移动,后来发现这样子 捕蛇者就变成了一个移动的食物,吃到蛇尾的概率太小了。 后来改成了根据蛇尾的坐标来确定移动方向,追着蛇尾跑。但是这样子又出现了一个很尴尬的事情,捕蛇者很快就追到蛇尾,但是追到蛇尾后就会一直黏在蛇尾后,因为捕蛇者和蛇速度相同,蛇长不长,捕蛇者也吃不掉蛇。有考虑过改变捕蛇者的速度,但捕蛇者如果比蛇快,那蛇一旦被追上,很快就会死掉。 最后加入一个随机函数,捕蛇者有一定几率改变方向,不会一直粘着蛇尾。
if (rnd.Next() % 5 == 0)
{
do
{
dir = rnd.Next() % 5;
} while (map[h.getx() + MoveTo[dir], h.gety() + MoveTo[dir + 5]] != 0);
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
using System.Threading;
namespace Snake
{
class snake
{
private int x;
private int y;
public snake(int a,int b)
{
x = a;
y = b;
}
public int getx()
{
return x;
}
public int gety()
{
return y;
}
}
class snakehunter
{
private int x;
private int y;
public snakehunter(int a, int b)
{
x = a;
y = b;
}
public snakehunter() { }
public int getx()
{
return x;
}
public int gety()
{
return y;
}
public void move(int dirx,int diry)
{
x += dirx;
y += diry;
}
}
class Map
{
const int WALL = -1;
const int SNAKE = 1;
const int FOOD = 3;
const int LASTFOOD = 4;
const int HUNTER = 5;
const int MapHeight = 20;
const int MapWidth = 40;
private int [,] map = new int [50,50];
private int HeadX = 5;
private int HeadY = 6;
private int SnakeLength = 3;
private int dir = 4;
private int LastFoodX = -1;
private int LastFoodY = -1;
private bool end = false ;
private int[] MoveTo = new int[10] { 0,-1,0,1,0,0,0,-1,0,1 };
private Queue <snake > s = new Queue<snake>();
private HashSet<snakehunter> h = new HashSet<snakehunter>();
public Map()
{
int i, j;
snake a = new snake(HeadX, HeadY-2);
snake b = new snake(HeadX, HeadY-1);
snake c = new snake(HeadX, HeadY);
s.Enqueue(a);
s.Enqueue(b);
s.Enqueue(c);
for (i = 0; i <= MapHeight; i++)
for (j = 0; j <= MapWidth; j++)
{
if (i == 0 || i == MapHeight || j == 0 || j == MapWidth)
map[i,j] = WALL ;
}
foreach (snake item in s)
{
map[item.getx(), item.gety()] = SNAKE;
}
}
public void AddFood()
{
int x, y;
Random rnd = new Random();
do
{
x =rnd.Next(1,MapHeight);
y =rnd.Next (1,MapWidth);
} while (map[x, y] != 0);
map[x,y] = FOOD;
Console.SetCursorPosition(y, x);
Console.Write("$");
}
public void AddSnakeHunter()
{
int x, y;
Random rnd = new Random();
do
{
x = rnd.Next(1, MapHeight);
y = rnd.Next(1, MapWidth);
} while (map[x, y] != 0);
map[x, y] = HUNTER;
snakehunter newhunter = new snakehunter(x, y);
h.Add(newhunter);
Console.SetCursorPosition(y,x);
Console.Write("@");
}
public void HandleInput()
{
ConsoleKeyInfo op = new ConsoleKeyInfo();
if (Console.KeyAvailable)
{
op = Console.ReadKey(true);
if ((op.KeyChar == 'w' || op.KeyChar == 'W') && dir != 3)
dir = 1;
else if ((op.KeyChar == 'a' || op.KeyChar == 'A') && dir != 4)
dir = 2;
else if ((op.KeyChar == 's' || op.KeyChar == 'S') && dir != 1)
dir = 3;
else if ((op.KeyChar == 'd' || op.KeyChar == 'D') && dir != 2)
dir = 4;
}
}
public void HunterMove(snakehunter h)
{
int i=0,dir=0;
Random rnd = new Random();
snake tail = s.Peek();
map[h.getx(), h.gety()] = 0;
Console.SetCursorPosition(h.gety(), h.getx());
Console.Write(" ");
if (h.getx() - tail.getx() > 0 && map[h.getx() - 1, h.gety()] == 0)
dir = 1;
else if (h.gety() - tail.gety() > 0 && map[h.getx(), h.gety()-1] == 0)
dir = 2;
else if (h.getx() - tail.getx() < 0 && map[h.getx() + 1, h.gety()] == 0)
dir = 3;
else if (h.gety() - tail.gety() < 0 && map[h.getx() , h.gety() + 1] == 0)
dir = 4;
if (rnd.Next() % 5 == 0)
{
do
{
dir = rnd.Next() % 5;
} while (map[h.getx() + MoveTo[dir], h.gety() + MoveTo[dir + 5]] != 0);
}
else
{
for (i = 1; i < 5; i++)
{
if (h.getx() + MoveTo[i] == tail.getx() && h.gety() + MoveTo[i + 5] == tail.gety())
{
dir = i;
SnakeLength--;
Console.SetCursorPosition(13, MapHeight + 1);
Console.Write("{0}", SnakeLength);
s.Dequeue();
break;
}
}
}
h.move(MoveTo[dir], MoveTo[dir + 5]);
map[h.getx(), h.gety()] = HUNTER;
Console.SetCursorPosition(h.gety(), h.getx());
Console.Write("@");
}
public void EatHunter(int x,int y) //集合已修改 ,可能无法执行枚举操作;
{
int i;
for(i=0;i<h.Count;i++)
{
if (h.ElementAt(i).getx() == x && h.ElementAt(i).gety() == y)
{
h.Remove(h.ElementAt(i));
break;
}
}
/* snakehunter c = new snakehunter();
foreach (snakehunter item in h)
{
if(item.getx()==x && item.gety()==y)
{
c = item;
break;
}
}
h.Remove(c);*/
}
public void Move()
{
if (map[HeadX+MoveTo[dir] ,HeadY + MoveTo[dir+5]]!= WALL && map[HeadX + MoveTo[dir], HeadY + MoveTo[dir + 5]] != SNAKE )
{
Console.SetCursorPosition(HeadY, HeadX);
Console.Write("o");
if (LastFoodX > 0 && LastFoodY>0)
{
Console.SetCursorPosition(LastFoodY, LastFoodX);
Console.Write("0");
LastFoodX = -1;
LastFoodY = -1;
}
HeadX += MoveTo[dir];
HeadY += MoveTo[dir + 5];
snake newHead = new snake(HeadX, HeadY);
s.Enqueue(newHead);
if((map[HeadX, HeadY] == FOOD ) || (map[HeadX ,HeadY]==HUNTER))
{
if (map[HeadX, HeadY] == FOOD)
AddFood();
else EatHunter(HeadX ,HeadY);
map[HeadX, HeadY] = LASTFOOD;
LastFoodX = HeadX;
LastFoodY = HeadY;
SnakeLength++;
Console.SetCursorPosition(13, MapHeight+1);
Console.Write("{0}", SnakeLength);
if (SnakeLength % 2 == 0)
{
AddSnakeHunter();
}
}
else
{
map[HeadX, HeadY] = SNAKE;
}
Console.SetCursorPosition(HeadY, HeadX);
Console.Write("O");
snake tail = s.Peek();
if (map[tail.getx(), tail.gety()] == LASTFOOD )
{
map[tail.getx(), tail.gety()] = SNAKE;
Console.SetCursorPosition(tail.gety(), tail.getx());
Console.Write("o");
}
else
{
s.Dequeue();
Console.SetCursorPosition(tail.gety(), tail.getx());
map[tail.getx(), tail.gety()] = 0;
Console.Write(" ");
}
foreach (snakehunter item in h)
{
HunterMove(item);
}
}
else
{
end = true;
Console.SetCursorPosition((MapWidth-9)/2, MapHeight/2-1);
Console.Write("Game Over");
Console.SetCursorPosition( 8, MapHeight / 2);
Console.Write("Press any key to restart...");
}
}
public void show()
{
Console.SetCursorPosition(0, 0);
int i, j;
for (i = 0; i <= MapHeight; i++)
{
for (j = 0; j <= MapWidth; j++)
{
if (map[i, j] == WALL)
Console.Write("#");
else if (map[i, j] == SNAKE)
Console.Write("o");
else Console.Write(" ");
}
Console.WriteLine();
}
Console.SetCursorPosition(HeadY, HeadX);
Console.Write("O");
AddFood();
Console.SetCursorPosition(0, MapHeight+1);
Console.WriteLine("SnakeLength :{0}",SnakeLength);
}
public bool isEnd()
{
return end;
}
}
class Program
{
static void Main(string[] args)
{
while(true )
{
Map m = new Map();
m.show();
while (!m.isEnd())
{
m.HandleInput();
m.Move();
Thread.Sleep(250);
}
Console.ReadKey(true);
}
}
}
}