📄 snake.cs
字号:
using System;
using System.Drawing;
using System.Collections;
namespace WindowsApplication9
{
public delegate void SnakeDele();
public class Snake
{
public Snake()
{
}
public Snake(Point vertex, int count)
{
Block newB;
Point p = new Point(vertex.X + 25, vertex.Y + 25);
blockList = new ArrayList(count);
for (int i = 0; i < count; i ++)
{
p.X = p.X + 5;
newB = new Block();
newB.Number = i + 1;
newB.Origin = p;
blockList.Add(newB);
if (i == count -1)
{
headPoint = newB.Origin;
}
}
headNumber = count;
}
public event SnakeDele snakeDie;
public void SnakeDie()
{
if (snakeDie != null)
snakeDie();
}
ArrayList blockList = new ArrayList();
private int headNumber;
public int HeadNumber
{
get { return headNumber; }
set { headNumber = value; }
}
private Point headPoint;
public Point getHeadPoint
{
get { return headPoint; }
/*get
{
IEnumerator myEnumerator = blockList.GetEnumerator();
try
{
while ( myEnumerator.MoveNext() )
{
Block b = (Block)myEnumerator.Current;
if (b.Number == headNumber)
{
return b.Origin;
}
}
}
catch(Exception e)
{
System.Console.WriteLine(e.ToString());
}
return new Point(0, 0);
}*/
}
public bool getHitSelf
{
get
{
IEnumerator myEnumerator = blockList.GetEnumerator();
try
{
while ( myEnumerator.MoveNext() )
{
Block b = (Block)myEnumerator.Current;
if (b.Number != headNumber && b.Origin.Equals(headPoint))
{
return true;
}
}
}
catch(Exception e)
{
System.Console.WriteLine(e.ToString());
}
return false;
}
}
private int score;
public int Score
{
get { return score; }
set { score = value; }
}
private int direction = 1;
public int Direction
{
get { return direction; }
set { direction = value; }
}
public void TurnDirection(int pDirection)
{
switch(direction)
{
case 0:
if (pDirection == 0)
direction = 1;
else if (pDirection == 1)
direction = 3;
break;
case 1:
if (pDirection == 0)
direction = 2;
else if (pDirection == 1)
direction = 0;
break;
case 2:
if (pDirection == 0)
direction = 3;
else if (pDirection == 1)
direction = 1;
break;
case 3:
if (pDirection == 0)
direction = 0;
else if (pDirection == 1)
direction = 2;
break;
}
}
//生长
public void Growth()
{
Block newB = new Block();
IEnumerator myEnumerator = blockList.GetEnumerator();
try
{
while ( myEnumerator.MoveNext() )
{
Block b = (Block)myEnumerator.Current;
if (b.Number == headNumber)
{
int x = b.Origin.X;
int y = b.Origin.Y;
switch(direction)
{
case 0:
y = y - 5;
break;
case 1:
x = x + 5;
break;
case 2:
y = y + 5;
break;
case 3:
x = x - 5;
break;
}
Point headP = new Point(x, y);
newB.Origin = headP;
newB.Number = b.Number + 1;
headNumber ++;
headPoint = headP;
blockList.Add(newB);
}
}
}
catch(Exception e)
{
System.Console.WriteLine(e.ToString());
}
}
public void Display(Graphics g)
{
try
{
Block newB = new Block();
IEnumerator myEnumerator = blockList.GetEnumerator();
while ( myEnumerator.MoveNext() )
{
Block b = (Block)myEnumerator.Current;
b.Number--;
if (b.Number < 1)
{
blockList.Remove(b);
b.UnDisplay(g);
continue;
}
if (b.Number == (headNumber - 1))
{
newB = new Block();
int x = b.Origin.X;
int y = b.Origin.Y;
switch(direction)
{
case 0:
y = y - 5;
break;
case 1:
x = x + 5;
break;
case 2:
y = y + 5;
break;
case 3:
x = x - 5;
break;
}
Point headP = new Point(x, y);
newB.Origin = headP;
newB.Number = headNumber;
newB.Display(g);
headPoint = newB.Origin;
}
b.Display(g);
}
blockList.Add(newB);
}
catch (Exception e)
{
System.Console.WriteLine(e.ToString());
}
}
public void UnDisplay(Graphics g)
{
try
{
Block newB = new Block();
IEnumerator myEnumerator = blockList.GetEnumerator();
while ( myEnumerator.MoveNext() )
{
Block b = (Block)myEnumerator.Current;
b.UnDisplay(g);
}
}
catch(Exception e)
{
System.Console.WriteLine(e.ToString());
}
}
public void Reset(Point vertex, int count)
{
Block newB;
Point p = new Point(vertex.X + 25, vertex.Y + 25);
blockList = new ArrayList(count);
for (int i = 0; i < count; i ++)
{
p.X = p.X + 5;
newB = new Block();
newB.Number = i + 1;
newB.Origin = p;
blockList.Add(newB);
if (i == count -1)
{
headPoint = newB.Origin;
}
}
headNumber = count;
direction = 1;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -