C贪吃蛇代码.docx
- 文档编号:5244809
- 上传时间:2022-12-14
- 格式:DOCX
- 页数:17
- 大小:17.38KB
C贪吃蛇代码.docx
《C贪吃蛇代码.docx》由会员分享,可在线阅读,更多相关《C贪吃蛇代码.docx(17页珍藏版)》请在冰豆网上搜索。
C贪吃蛇代码
C贪吃蛇代码
//Snake.csBegin
//
usingSystem;
usingSystem.Collections;
usingSystem.Drawing;
usingSystem.Windows.Forms;
usingSystem.Timers;
namespaceGreedySnake
{
#regionSnake蛇身
///
///Snake的摘要说明。
///
publicclassSnake
{
privateControldcControl;
privatestaticintiMoveDirection=0x1000;//蛇的运动方向,初始化为right-0x1000
privateintiCount;//骨节的总数
privateintiRadius;//骨节的半径
privatestaticintiCurrentHeadX;//当前蛇头的中心坐标X
privatestaticintiCurrentHeadY;//当前蛇头的中心坐标Y
privatestaticintiCurrentTrailX;//当前蛇尾的中心坐标X
privatestaticintiCurrentTrailY;//当前蛇尾的中心坐标Y
privatestaticintiNextHeadX;//下一时刻蛇头的中心坐标X
privatestaticintiNextHeadY;//下一时刻蛇头的中心坐标Y
privatestaticintiPreTrailX;//前一时刻蛇尾的中心坐标X
privatestaticintiPreTrailY;//前一时刻蛇尾的中心坐标Y
privatestaticArrayListalSnake;//存放整条蛇
privateboolbDisposing=true;
privateboolbIsEatself=false;//是否吃自己
privateboolbIsOutOfRange=false;//是否超出允许活动的范围
publicControlDcControl
{
set{dcControl=value;}
get{returndcControl;}
}
publicintMoveDirection
{
set{iMoveDirection=value;}
get{returniMoveDirection;}
}
publicintCount
{
set{iCount=value;}
get{returniCount;}
}
publicintRadius
{
set{iRadius=value;}
get{returniRadius;}
}
publicintCurrentHeadX
{
set{iCurrentHeadX=value;}
get{returniCurrentHeadX;}
}
publicintCurrentHeadY
{
set{iCurrentHeadY=value;}
get{returniCurrentHeadY;}
}
publicintCurrentTrailX
{
set{iCurrentTrailX=value;}
get{returniCurrentTrailX;}
}
publicintCurrentTrailY
{
set{iCurrentTrailY=value;}
get{returniCurrentTrailY;}
}
publicintNextHeadX
{
set{iNextHeadX=value;}
get{returniNextHeadX;}
}
publicintNextHeadY
{
set{iNextHeadY=value;}
get{returniNextHeadY;}
}
publicintPreTrailX
{
set{iPreTrailX=value;}
get{returniPreTrailX;}
}
publicintPreTrailY
{
set{iPreTrailY=value;}
get{returniPreTrailY;}
}
publicboolIsEatself
{
set{bIsEatself=value;}
get{returnbIsEatself;}
}
publicboolIsOutOfRange
{
set{bIsOutOfRange=value;}
get{returnbIsOutOfRange;}
}
publicSnake():
this(null,20,5)
{
//
//TODO:
在此处添加构造函数逻辑
//
}
publicSnake(Controlcontrol,intiCount,intiRadius)
{
DcControl=control;
Count=iCount;
Radius=iRadius;
CurrentHeadX=CurrentTrailX=PreTrailX=5;
CurrentHeadY=CurrentTrailY=PreTrailY=5;
Initialize();
}
~Snake()
{
Dispose(false);
}
//初始化蛇
privatevoidInitialize()
{
alSnake=newArrayList();
for(inti=0;i{
alSnake.Insert(0,newSnakeNode(DcControl,CurrentHeadX,CurrentHeadY,Radius));
CurrentHeadX+=2*Radius;
}
CurrentHeadX-=2*Radius;
NextHeadX=CurrentHeadX+2*Radius;
NextHeadY=CurrentHeadY;
}
publicvoidDispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
publicvoidDispose(boolbDisposing)
{
if(bDisposing)
{
//调用Dispose处理受控资源中的字段
MoveDirection=0x1000;
CurrentHeadX=CurrentHeadY=NextHeadX=NextHeadY=5;
alSnake.Clear();
}
//清除非受控资源
}
//加头
publicvoidAddHead()
{
alSnake.Insert(0,newSnakeNode(DcControl,NextHeadX,NextHeadY,Radius));
CurrentHeadX=NextHeadX;
CurrentHeadY=NextHeadY;
Count++;
}
//加尾
publicvoidAddTrail()
{
alSnake.Add(newSnakeNode(DcControl,PreTrailX,PreTrailY,Radius));
Count++;
((SnakeNode)alSnake[Count-1]).Draw();
}
//去尾
publicvoidRemoveTrail()
{
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;
}
}
//移动到下一位置
publicvoidMoveNext()
{
//加头
AddHead();
//画头
((SnakeNode)alSnake[0]).Draw();
//清除尾(将蛇尾用背景色填充)
((SnakeNode)alSnake[Count-1]).Clear();
//去尾(将蛇尾从ArrayList中删除)
RemoveTrail();
}
//画整条蛇
publicvoidDraw()
{
for(inti=0;i{
((SnakeNode)alSnake[i]).Draw();
}
}
//清除整条蛇
publicvoidClear()
{
for(inti=0;i{
((SnakeNode)alSnake[i]).Clear();
}
}
//重设运动方向
publicvoidResetMoveDirection(stringstrKeyData)
{
//获取键盘输入
intiKeyDirection;
switch(strKeyData)
{
case"W":
case"Up":
iKeyDirection=0x0001;
break;
case"S":
case"Down":
iKeyDirection=0x0010;
break;
case"A":
case"Left":
iKeyDirection=0x0100;
break;
case"D":
case"Right":
iKeyDirection=0x1000;
break;
default:
iKeyDirection=0x0010;
break;
}
//重设蛇的运动方向(综合按键方向和当前蛇的运动方向考虑)
intiDirection=iKeyDirection|MoveDirection;
if(iDirection==0x0011||iDirection==0x1100)
MoveDirection=MoveDirection;//运动方向保持不变
else
MoveDirection=iKeyDirection;//运动方向等于按键方向
}
//是否超出范围
publicvoidCheck()
{
GetNextHeadXY();
//检查是否吃自己
foreach(SnakeNodesninalSnake)
{
if(sn.CenterX==NextHeadX&&sn.CenterY==NextHeadY)
{
IsEatself=true;
break;
}
}
//检查是否超出允许活动的范围
IsOutOfRange=NextHeadX<0||NextHeadX>DcControl.Width||NextHeadY<0||NextHeadY>DcControl.Height;
}
//预先算出下个位置坐标
privatevoidGetNextHeadXY()
{
switch(MoveDirection)
{
case0x0001:
NextHeadX=CurrentHeadX;
NextHeadY=CurrentHeadY-2*Radius;
break;
case0x0010:
NextHeadX=CurrentHeadX;
NextHeadY=CurrentHeadY+2*Radius;
break;
case0x0100:
NextHeadX=CurrentHeadX-2*Radius;
NextHeadY=CurrentHeadY;
break;
case0x1000:
NextHeadX=CurrentHeadX+2*Radius;
NextHeadY=CurrentHeadY;
break;
default:
break;
}
}
}
#endregion
#regionSnakeNode蛇的骨节
///
///SnakeNote
///蛇的骨节
///
publicclassSnakeNode
{
privateControldcControl;//用于画图的控件
privateintiCenterX;//中心坐标X
privateintiCenterY;//中心坐标Y
privateintiRadius;//半径
privateColorcolorNode;//颜色
publicControlDcControl
{
set{dcControl=value;}
get{returndcControl;}
}
publicintCenterX
{
set{iCenterX=value;}
get{returniCenterX;}
}
publicintCenterY
{
set{iCenterY=value;}
get{returniCenterY;}
}
publicintRadius
{
set{iRadius=value;}
get{returniRadius;}
}
publicColorColorNode
{
set{colorNode=value;}
get{returncolorNode;}
}
privateboolbDisposing=true;
publicSnakeNode():
this(null,0,0,5)
{
}
publicSnakeNode(Controlcontrol,intiX,intiY,intiR)
{
DcControl=control;
CenterX=iX;
CenterY=iY;
Radius=iR;
}
~SnakeNode()
{
Dispose(false);
}
publicvoidDispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
publicvoidDispose(boolbDisposing)
{
if(bDisposing)
{
//调用Dispose处理受控资源中的字段
CenterX=CenterY=0;
Radius=5;
}
//清除非受控资源
}
//画自身
publicvoidDraw()
{
Draw(Color.Blue);
}
publicvoidDraw(Colorcolor)
{
//以指定颜色画圆
ColorNode=color;
DrawCircle(ColorNode);
}
//清除
publicvoidClear()
{
//以控件的背景色画圆
DrawCircle(DcControl.BackColor);
}
//以骨节的中心画圆
publicvoidDrawCircle(Colorcolor)
{
using(Graphicsdc=DcControl.CreateGraphics())
{
//创建实心的笔刷
SolidBrushsbBrush=newSolidBrush(color);
//创建圆的区间范围
floatx=CenterX-Radius;
floaty=CenterY-Radius;
floatwidth=2*Radius;
floatheight=2*Radius;
//创建开始和扫过的弧度
floatfStartAngle=0.0F;
floatfSweepAngle=360.0F;
//画圆
dc.FillPie(sbBrush,x,y,width,height,fStartAngle,fSweepAngle);
}
}
}
#endregion
#regionSnakeFood蛇的食物
///
///SnakeFood的摘要说明。
///
publicclassSnakeFood
{
privateControldcControl;
privateintiMaxCount;//最多能剩下的食物总数
privateintiCurrentCount;//当前剩下的食物总数
privateintiRadius;//骨节的半径
privateColor[]acolor=newColor[]{Color.Red,Color.Green,Color.Yellow};//新点的颜色取值范围
privatestaticArrayListalSnakeFood;//蛇的食物
privateboolbDisposing=true;
publicControlDcControl
{
set{dcControl=value;}
get{returndcControl;}
}
publicintMaxCount
{
set{iMaxCount=value;}
get{returniMaxCount;}
}
publicintCurrentCount
{
set{iCurrentCount=value;}
get{returniCurrentCount;}
}
publicintRadius
{
set{iRadius=value;}
get{returniRadius;}
}
publicSnakeNodethis[intindex]
{
get
{
if(index<0||index>=CurrentCount)
{
thrownewIndexOutOfRangeException();
}
return(SnakeNode)alSnakeFood[index];
}
}
publicSnakeFood():
this(null,5,5)
{
}
publicSnakeFood(Controlcontrol,intiMaxCount,intiRadius)
{
DcControl=control;
MaxCount=iMaxCount;
CurrentCount=0;
Radius=iRadius;
alSnakeFood=newArrayList();
}
~SnakeFood()
{
Dispose(false);
}
publicvoidDispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
publicvoidDispose(boolbDisposing)
{
if(bDisposing)
{
//调用Dispose处理受控资源中的字段
CurrentCount=0;
alSnakeFood.Clear();
}
//清除非受控资源
}
//添加食物
publicvoidAddFood()
{
Randomrandom=newRandom();
intiStep=Radius+Radius;
intiX=Radius+iStep*random.Next(0,DcControl.Width/iStep);
intiY=Radius+iStep*random.Next(0,DcControl.Height/iStep);
SnakeNodesn=newSnakeNode(DcControl,iX,iY,iRadius);
RandomrandomIndex=newRandom();
Colorcolor=acolor[randomIndex.Next(0,acolor.Length)];
color=Color.Green;
sn.Draw(color);
alSnakeFood.Add(sn);
//当前剩下的食物总数加1
CurrentCount++;
}
//删除被吃掉的食物
publicvoidRemoveFood(intiIndex)
{
if(CurrentCount>0)
{
alSnakeFood.RemoveAt(iIndex);
//当前剩下的食物总数减1
CurrentCount--;
}
}
//画所有食物
publicvoidDraw()
{
foreach(SnakeNodesninalSnakeFood)
{
sn.Draw();
}
}
//清除所有食物
publicvoidClear()
{
foreach(SnakeNodesninalSnakeFood)
{
sn.Clear();
}
}
}
#endregion
}
//
//Snake.csEnd
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 贪吃 代码