贪吃蛇代码
using System;
using System.Collections.Generic; using System.Text;
using System.Drawing;
using System.Collections; using System.Windows.Forms;
namespace Snake_2011_12_25_ {
#region Snake 蛇身
///
/// Snake 的摘要说明。
///
public class Snake
{
#region 字段
private static int iScore;
private System.Windows.Forms.Control dcControl;
private static int iMoveDirection = 0x1000; // 蛇的运动方向 , 初始化为 right - 0x1000
private int iCount; // 骨节的总数
private int iRadius; // 骨节的半径
private static int iCurrentHeadX; // 当前蛇头的中心坐标 X
private static int iCurrentHeadY; // 当前蛇头的中心坐标 Y
private static int iCurrentTrailX; // 当前蛇尾的中心坐标 X
private static int iCurrentTrailY; // 当前蛇尾的中心坐标 Y
private static int iNextHeadX; // 下一时刻蛇头的中心坐标 X
private static int iNextHeadY; // 下一时刻蛇头的中心坐标 Y
private static int iPreTrailX; // 前一时刻蛇尾的中心坐标 X
private static int iPreTrailY; // 前一时刻蛇尾的中心坐标 Y
private static ArrayList alSnake; // 存放整条蛇
private bool bDisposing = true;
private bool bIsEatself = false; // 是否吃自己
private bool bIsOutOfRange = false; // 是否超出允许活动的范围
# endregion
#region 属性
public static int IScore
{
get { return Snake.iScore; }
set { Snake.iScore = value; }
}
public Control DcControl
{
set { dcControl = value; }
get { return dcControl;}
}
public int MoveDirection
{
set { iMoveDirection = value; }
get { return iMoveDirection; }
}
public int Count
{
set { iCount = value; }
get { return iCount; }
}
public int Radius
{
set { iRadius = value; }
get { return iRadius; }
}
public int CurrentHeadX
{
set { iCurrentHeadX = value; }
get { return iCurrentHeadX; }
}
public int CurrentHeadY
{
set { iCurrentHeadY = value; }
get { return iCurrentHeadY; }
}
public int CurrentTrailX
{
set { iCurrentTrailX = value; }
get { return iCurrentTrailX; }
}
public int CurrentTrailY
{
set { iCurrentTrailY = value; }
get { return iCurrentTrailY; }
}
public int NextHeadX
{
set { iNextHeadX = value; }
get { return iNextHeadX; }
}
public int NextHeadY
{
set { iNextHeadY = value; }
get { return iNextHeadY; }
}
public int PreTrailX
{
set { iPreTrailX = value; }
get { return iPreTrailX; }
}
public int PreTrailY
{
set { iPreTrailY = value; }
get { return iPreTrailY; }
}
public bool IsEatself
{
set { bIsEatself = value; }
get { return bIsEatself; }
}
public bool IsOutOfRange
{
set { bIsOutOfRange = value; }
get { return bIsOutOfRange;}
}
#endregion
public Snake() : this(null , 20 , 5,IScore)
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public Snake(Control control , int iCount , int iRadius, int iScore)
{
IScore = iScore;
DcControl = control;
Count = iCount;
Radius = iRadius;
CurrentHeadX = CurrentTrailX = PreTrailX = 5;
CurrentHeadY = CurrentTrailY = PreTrailY = 30;
Initialize();
}
~Snake()
{
Dispose(false);
}
// 初始化蛇
private void Initialize()
{
alSnake = new ArrayList();
for (int i = 0; i < Count; i++)
{
alSnake.Insert(0, new SnakeNode(DcControl, CurrentHeadX, CurrentHeadY, Radius));
CurrentHeadX += 2 * Radius;
}
NextHeadX = CurrentHeadX + 2 * Radius;
NextHeadY = CurrentHeadY;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
public void Dispose( bool bDisposing )
{
if (bDisposing)
{
// 调用 Dispose 处理受控资源中的字段
MoveDirection = 0x1000;
CurrentHeadX = CurrentHeadY = NextHeadX = NextHeadY = 5;
alSnake.Clear();
}
// 清除非受控资源
}
// 移动到下一位置
public void MoveNext()
{
//检查能否前进
Check();
// 清除尾(将蛇尾用背景色填充)
((SnakeNode)alSnake[Count - 1]).Clear();
// 画头
((SnakeNode)alSnake[0]).Draw();
// 去尾(将蛇尾从 ArrayList 中删除)
RemoveTrail();
// 加头
AddHead();
}
// 加头
public void AddHead()
{
Count++;
alSnake.Insert(0, new SnakeNode(DcControl, NextHeadX, NextHeadY, Radius));
CurrentHeadX = NextHeadX;
CurrentHeadY = NextHeadY;
}
// 加尾
public void AddTrail()
{
alSnake.Add(new SnakeNode(DcControl , PreTrailX , PreTrailY , Radius));
Count++;
((SnakeNode)alSnake[Count - 1]).Draw();
}
// 去尾
public void RemoveTrail()
{
if (alSnake.Count>1)
{
PreTrailX = ((SnakeNode)alSnake[Count - 1]).CenterX;
PreTrailY = ((SnakeNode)alSnake[Count - 1]).CenterY;
alSnake.RemoveAt(alSnake.Count - 1);
Count--;
CurrentTrailX = ((SnakeNode)alSnake[Count - 1]).CenterX;
CurrentTrailY = ((SnakeNode)alSnake[Count - 1]).CenterY;
}
}
// 画整条蛇
public void Draw()
{
for (int i=0; i < Count;i++) {
((SnakeNode)alSnake[i]).Draw();
}
}
// 清除整条蛇
public void Clear()
{
for (int i=0; i DcControl.Width || NextHeadY < 0 ||
NextHeadY > DcControl.Height;
if (IsOutOfRange)
{
return IsOutOfRange;
}
return IsOutOfRange;
}
// 预先算出下个位置坐标
private void GetNextHeadXY()
{
switch (MoveDirection)
{
case 0x0001:
NextHeadX = CurrentHeadX;
NextHeadY = CurrentHeadY - 2 * Radius;
break;
case 0x0010:
NextHeadX = CurrentHeadX;
NextHeadY = CurrentHeadY + 2 * Radius;
break;
case 0x0100:
NextHeadX = CurrentHeadX - 2 * Radius;
NextHeadY = CurrentHeadY;
break;
case 0x1000:
NextHeadX = CurrentHeadX + 2 * Radius;
NextHeadY = CurrentHeadY;
break;
default:
break;
}
}
}
#endregion
}
--------------------------------以上是Snake.cs文件---------------------------------------
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Collections; using System.Windows.Forms;
namespace Snake_2011_12_25_ {
#region SnakeNode 蛇的骨节
///
/// Snake Note
/// 蛇的骨节
///
public class SnakeNode
{
private Control dcControl; // 用于画图的控件
private int iCenterX; // 中心坐标 X
private int iCenterY; // 中心坐标 Y
private int iRadius; // 半径
private Color colorNode; // 颜色
public Control DcControl
{
set { dcControl = value; }
get { return dcControl; }
}
public int CenterX
{
set { iCenterX = value; }
get { return iCenterX; }
}
public int CenterY
{
set { iCenterY = value; }
get { return iCenterY; }
}
public int Radius
{
set { iRadius = value; }
get { return iRadius; }
}
public Color ColorNode
{
set { colorNode = value; }
get { return colorNode; }
}
private bool bDisposing = true;
public SnakeNode()
: this(null, 0, 0, 5)
{
}
public SnakeNode(Control control, int iX, int iY, int iR)
{
DcControl = control;
CenterX = iX;
CenterY = iY;
Radius = iR;
}
~SnakeNode()
{
Dispose(false);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
public void Dispose(bool bDisposing)
{
if (bDisposing)
{
// 调用 Dispose 处理受控资源中的字段
CenterX = CenterY = 0;
Radius = 5;
}
// 清除非受控资源
}
// 画自身
public void Draw()
{
Draw(Color.Blue);
}
public void Draw(Color color)
{
// 以指定颜色画圆
ColorNode = color;
DrawCircle(ColorNode);
}
// 清除
public void Clear()
{
// 以控件的背景色画圆
DrawCircle(DcControl.BackColor);
}
// 以骨节的中心画圆
public void DrawCircle(Color color)
{
using (Graphics dc = DcControl.CreateGraphics())
{
// 创建实心的笔刷
SolidBrush sbBrush = new SolidBrush(color);
// 创建圆的区间范围
float x = CenterX - Radius;
float y = CenterY - Radius;
float width = 2 * Radius;
float height = 2 * Radius;
// 创建开始和扫过的弧度
float fStartAngle = 0.0F;
float fSweepAngle = 360.0F;
// 画圆
dc.FillPie(sbBrush, x, y, width, height, fStartAngle, fSweepAngle);
}
}
}
#endregion
}
-----------------------以上是SnameNode.cs文件-----------------------------
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
namespace Snake_2011_12_25_
{
#region SnakeFood 蛇的食物
///
/// SnakeFood 的摘要说明。
///
public class SnakeFood
{
private Control dcControl;
private int iMaxCount; // 最多能剩下的食物总数
private int iCurrentCount; // 当前剩下的食物总数
private int iRadius; // 骨节的半径
private static int iX;
private static int iY;
public int IY
{
get { return SnakeFood.iY; }
set { SnakeFood.iY = value; }
}
public int IX
{
get { return SnakeFood.iX; }
set { SnakeFood.iX = value; }
}
private Color[] acolor = new Color[] { Color.Red, Color.Green, Color.Yellow }; // 新点的颜色取值范围
private static ArrayList alSnakeFood; // 蛇的食物
private bool bDisposing = true;
public Control DcControl
{
set { dcControl = value; }
get { return dcControl; }
}
public int MaxCount
{
set { iMaxCount = value; }
get { return iMaxCount; }
}
public int CurrentCount
{
set { iCurrentCount = value; }
get { return iCurrentCount; }
}
public int Radius
{
set { iRadius = value; }
get { return iRadius; }
}
public SnakeNode this[int index]
{
get
{
if (index < 0 || index >= CurrentCount)
{
throw new IndexOutOfRangeException();
}
return (SnakeNode)alSnakeFood[index];
}
}
public SnakeFood()
: this(null, 5, 5)
{
}
public SnakeFood(Control control, int iMaxCount, int iRadius)
{
DcControl = control;
MaxCount = iMaxCount;
CurrentCount = 0;
Radius = iRadius;
alSnakeFood = new ArrayList();
}
~SnakeFood()
{
Dispose(false);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
public void Dispose(bool bDisposing)
{
if (bDisposing)
{
// 调用 Dispose 处理受控资源中的字段
CurrentCount = 0;
alSnakeFood.Clear();
}
// 清除非受控资源
}
// 添加食物
public void AddFood()
{
Random random = new Random();
int iStep = Radius + Radius;
IX = Radius + iStep * random.Next(0, DcControl.Width / iStep);
IY = Radius + iStep * random.Next(0, DcControl.Height / iStep);
SnakeNode sn = new SnakeNode(DcControl, IX, IY, iRadius);
Random randomIndex = new Random();
Color color = acolor[randomIndex.Next(0, acolor.Length)];
color = Color.Green;
sn.Draw(color);
alSnakeFood.Add(sn);
// 当前剩下的食物总数加1
CurrentCount++;
}
// 删除被吃掉的食物
public void RemoveFood(int iIndex)
{
if (CurrentCount > 0)
{
alSnakeFood.RemoveAt(iIndex);
// 当前剩下的食物总数减1
CurrentCount--;
}
}
// 画所有食物
public void Draw()
{
foreach (SnakeNode sn in alSnakeFood)
{
sn.Draw();
}
}
// 清除所有食物
public void Clear()
{
foreach (SnakeNode sn in alSnakeFood)
{
sn.Clear();
}
}
}
#endregion
}
--------------------------以上是SnameFood.cs文件------------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Timers;
namespace Snake_2011_12_25_
{
public partial class Form1 : Form
{
int CurrentScore = 0;
int TopScore = 100;
Snake a_snake;
SnakeFood a_food;
int spead = 200;
Control Control;
int Radius = 15;
int Count = 1;
string KeyDate;
int MaxCount = 5;
int CurrentCount = 0;
bool BL= true;
public System.Timers.Timer timerBlock;
public Form1()
{
InitializeComponent();
this.tb1.Text = CurrentScore.ToString();
this.tb2.Text =TopScore.ToString();
this.tb1.Enabled = false;
this.tb2.Enabled = false;
this.textBox1.Enabled = false;
this.button1.Enabled = false;
Control = this.pictureBox1;
if (TopScore < CurrentScore)
{
TopScore = CurrentScore;
this.tb2.Text = TopScore.ToString();
}
}
private void OnBlockTimedEvent(object source, ElapsedEventArgs e)
{
try
{
a_snake.MoveNext();
a_snake.Draw();
this.KeyDown += new
System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown);
a_snake.ResetMoveDirection(KeyDate);
if ((Math.Abs((a_food.IX - a_snake.CurrentHeadX)) < 20) &&
(Math.Abs((a_snake.CurrentHeadY - a_food.IY)) < 20))
{
CurrentScore += 10;
a_food.Clear();
CurrentScore += 10;
a_food.AddFood();
a_snake.AddHead();
a_snake.Draw();
}
}
catch {
}
}
private void btStart_Click(object sender, EventArgs e)
{
try
{
this.btStart.Enabled = false;
this.button1.Enabled = true;
a_snake = new Snake(Control, Count, Radius, CurrentScore);
a_food = new SnakeFood(Control, MaxCount, 15);
a_food.AddFood();
CurrentCount++;
a_snake.Draw();
MessageBox.Show("上:W 左: A 下: S 右: D","操作提示");
timerBlock = new System.Timers.Timer(spead);
timerBlock.Elapsed += new
System.Timers.ElapsedEventHandler(OnBlockTimedEvent);
timerBlock.AutoReset = true;
timer1.Interval = 1; //时间间隔为0.1秒
timerBlock.Start();
}
catch {
}
}
private void button1_Click(object sender, EventArgs e)
{
this.timer1.Enabled = false;
this.btStart.Enabled = false;
this.tb1.Text = CurrentScore.ToString();
if (TopScore < CurrentScore)
{
TopScore = CurrentScore;
this.tb2.Text = TopScore.ToString();
}
MessageBox.Show("你的得分:" + CurrentScore,"得分提示");
timer1.Enabled = false;
a_snake = new Snake(Control, Count, Radius, CurrentScore);
a_food = new SnakeFood(Control, MaxCount, 15);
a_food.AddFood();
CurrentCount++;
a_snake.Draw();
pictureBox1.Refresh();
a_food.AddFood();
a_snake.MoveNext();
}
public void Form1_KeyDown(object sender, KeyEventArgs e)
{
if ((e.KeyCode == Keys.W || e.KeyCode == Keys.Up))
{
KeyDate = "Up";
}
if ((e.KeyCode == Keys.D || e.KeyCode == Keys.Right))
{
KeyDate = "Right";
}
if ((e.KeyCode == Keys.S || e.KeyCode == Keys.Down) )
{
KeyDate = "Down";
}
if ((e.KeyCode == Keys.A || e.KeyCode == Keys.Left))
{
KeyDate = "Left";
}
//设置KeyDown事件,用按件控制方向
}
private void btClose_Click(object sender, EventArgs e)
{
this.Close();
}
private void 初级ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.spead = 400;
}
private void 中级ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.spead = 250;
}
private void 高级ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.spead = 100;
}
}
public class food//食物类
{
private Point f_point;
public Point F_point//食物的坐标
{
get
{
return f_point;
}
set
{
f_point = value;
}
}
public void drawfood(Graphics g)//画食物
{
SolidBrush b = new SolidBrush(Color.Blue);
Rectangle rtg = new Rectangle(f_point.X, f_point.Y, 10, 10);
g.FillRectangle(b, rtg);
//实心的蓝色方块
}
public Point getpoint()//获得食物坐标[随机数point]
{
int i = 10;
Random rdm = new Random(System.DateTime.Now.Millisecond + i);
i = rdm.Next(0, 27);
int j = rdm.Next(0, 27);
Point newp = new Point(i * 10, j * 10);
return newp;
}
}
}
---------------------------------以上是form1.cs文件------------------------------------