private int snakeCount//蛇的长度
{
get { return snakeCount; }
set { snakeCount = value; }
}
public ArrayList blockList//存放块
{
get { return blockList; }
set { blockList = value; }
}
public Point headPoint //蛇头的位置
{
get { return headPoint; }
set { headPoint = value; }
}
private int direction = 1;//0上1右2下3左
public int Direction
{
get { return direction; }
set { direction = value; }
}
public void snake(Point vertex, int count)//vertex场地的起始点 //这里说错误 1 “snake”: 成员名称不能与它们的封闭类型相同
{
ArrayList blocklist = new ArrayList(count);
Point p = new Point(vertex.X + 25, vertex.Y + 25);
for (int i = 0; i < count; i++)
{
Block b = new Block();
b.Origin = p;
b.Number = i + 1;
blocklist.Add(b);//初始化蛇身
if (i == count - 1)
{
headPoint = p;//初始化蛇头的位置
}
p.X += 5;
}
snakeCount = count;//初始化蛇长度
}
public static void Growth()
{
Block b = new Block();
snakeCount = snakeCount + 1;//蛇新长度 //这几个提示错误 10 非静态的字段、方法或属性。。。。。要求对象引用
b.Number = snakeCount; //新块的序号
switch (Direction)
{//上
case 0:
Point p = new Point(headPoint.X, headPoint.Y);
b.Origin = p;
break;
//右
case 1:
p = new Point(headPoint.X + 5, headPoint.Y);
b.Origin = p;
break;
//下
case 2:
p = new Point(headPoint.X, headPoint.Y + 5);
b.Origin = p;
break;
//左
case 3:
p = new Point(headPoint.X - 5, headPoint.Y);
b.Origin = p;
break;
}
blockList.Add(b);
headPoint = b.Origin; //这两句总提示错误 10 非静态的字段、方法或属性“tanchishe.snake.blockList.get”要求对象引用
}
public void Display(Graphics g)
{//去掉蛇尾
Block b1 = new Block();
b1 = (Block)blockList[0];
//取出blocklist的第一个元素给b
blockList.Remove(b1);
b1.UnDisplay(g);
//蛇的每一块序号减一
foreach (Block bb in blockList)
{
bb.Number--;
}
//添加蛇头
Block b = new Block();
switch (Direction)
{
//上
case 0:
Point p = new Point(headPoint.X, headPoint.Y);
b.Origin = p;
break;
//右
case 1:
p = new Point(headPoint.X + 5, headPoint.Y);
b.Origin = p;
break;
//下
case 2:
p = new Point(headPoint.X, headPoint.Y + 5);
b.Origin = p;
break;
//左
case 3:
p = new Point(headPoint.X - 5, headPoint.Y);
b.Origin = p;
break;
}
b.Number = snakeCount;
blockList.Add(b);
headPoint = b.Origin;
foreach (Block bb in blockList)
{
bb.Display(g);
}
}
//改变方向
public void TurnDirection(int newDirec)
{
switch (Direction)
{
//原方向上
case 0:
if (newDirec == 3)//新方向为左
Direction = 3;//新方向改为左
else if (newDirec == 1)//新方向为右
Direction = 1;//将运行方向改为右
break;
//原方向右
case 1:
if (newDirec == 0)//新方向为上
Direction = 0;//新方向改为上
else if (newDirec == 2)//新方向为下
Direction = 2;//将运行方向改为左下
break;
//原方向下
case 2:
if (newDirec == 3)//新方向为左
Direction = 3;//新方向改为左
else if (newDirec == 1)//新方向为右
Direction = 1;//将运行方向改为右
break;
//原方向左
case 3:
if (newDirec == 0)//新方向为上
Direction = 0;//新方向改为上
else if (newDirec == 2)//新方向为下
Direction = 2;//将运行方向改为下
break;
}
}
//消除蛇
public void Undisplay(Graphics g)
{
foreach (Block b in blockList)
{
b.UnDisplay(g);
blockList.Remove(b);
}
}
//蛇碰到自身
public bool HitSelf()
{
foreach (Block b in blockList)
{
if (b.Number != snakeCount)
{
if (b.Origin.Equals(headPoint))
{
return true;
}
}
}
return false;
}
public void Reset(Point vertex, int count)
{
blockList = new ArrayList(count);
Point p = new Point(vertex.X + 25, vertex.Y + 25);
for (int i = 0; i < count; i++)
{
Block b = new Block();
b.Number = i + 1;
blockList.Add(b);//初始化蛇身
if (i == count - 1)
{
headPoint = p;//初始化蛇头的位置
}
p.X += 5;
snakeCount = count;//初始化蛇长度
}
}
}
}
class Floor
{
//场地起始点
public Point startpoint
{
get{return startpoint;}
set{value=startpoint;}
}
private static int danwei = 5;
private int chang = 60 * danwei;
private int kuan = 30 * danwei;
private Point dian;
private snake s;
public snake S
{
get { return s; }
}
private int defen=0;
public int Defen
{
get { return defen; }
set { defen = value; }
}