下面是游戏的源代码。有两个文件:main.cs 和 snake.cs
//
// main.cs begin
//
using system;
using system.drawing;
using system.collections;
using system.componentmodel;
using system.windows.forms;
using system.data;
using system.threading;
namespace greedysnake
{
///
/// formmain 的摘要说明。
///
public class formmain : system.windows.forms.form
{
///
/// 必需的设计器变量。
///
private system.componentmodel.container components = null;
private system.windows.forms.menuitem menugame;
private system.windows.forms.menuitem menugamebegin;
private system.windows.forms.menuitem menugameend;
private system.windows.forms.mainmenu menumain;
private system.windows.forms.menuitem menugamerebegin;
private system.windows.forms.menuitem menuoption;
private system.windows.forms.menuitem menuoptiontopmost;
private system.windows.forms.menuitem menuoptiongamedifficulty;
private system.windows.forms.menuitem menuoptionsplit;
private system.windows.forms.menuitem menuoptiongamedifficultyeasy;
private system.windows.forms.menuitem menuoptiongamedifficultycommon;
private system.windows.forms.menuitem menuoptiongamedifficultydifficult;
private system.windows.forms.panel panelgameregion;
private system.windows.forms.panel panel1;
private system.windows.forms.label labelgamedifficulty;
private system.windows.forms.label labelgamecurrentdifficulty;
private system.windows.forms.label labelgameintroduce;
private system.windows.forms.label labelup;
private system.windows.forms.label labeldown;
private system.windows.forms.label labelleft;
private system.windows.forms.label labelright;
private system.windows.forms.label labelstatistic;
private system.windows.forms.label labelinitcount;
private system.windows.forms.label labelcurrentcount;
private system.windows.forms.label labelscore;
private system.windows.forms.label labeleatgreencount;
private const int cispeed = 80; // 游戏速度(默认难度:一般)
private int ispeed = cispeed; // 游戏速度
private const int icount = 18; // 蛇长
private const int iradius = 5; // 蛇骨接的半径
private const int istep = 2 * iradius; // 两个相邻的蛇骨中心相差的距离
private const int imaxcount = 5; // 最多能剩下的食物总数
private snake snake;
private snakefood snakefood;
public formmain()
{
//
// windows 窗体设计器支持所必需的
//
initializecomponent();
//
// todo: 在 initializecomponent 调用后添加任何构造函数代码
//
}
#region 清理正在使用的资源
///
/// 清理所有正在使用的资源。
///
protected override void dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.dispose();
}
}
base.dispose( disposing );
}
#endregion
#region windows 窗体设计器生成的代码
///
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
///
private void initializecomponent()
{
this.menumain = new system.windows.forms.mainmenu();
this.menugame = new system.windows.forms.menuitem();
this.menugamebegin = new system.windows.forms.menuitem();
this.menugamerebegin = new system.windows.forms.menuitem();
this.menugameend = new system.windows.forms.menuitem();
this.menuoption = new system.windows.forms.menuitem();
this.menuoptiongamedifficulty = new system.windows.forms.menuitem();
this.menuoptiongamedifficultyeasy = new system.windows.forms.menuitem();
this.menuoptiongamedifficultycommon = new system.windows.forms.menuitem();
this.menuoptiongamedifficultydifficult = new system.windows.forms.menuitem();
this.menuoptionsplit = new system.windows.forms.menuitem();
this.menuoptiontopmost = new system.windows.forms.menuitem();
this.panelgameregion = new system.windows.forms.panel();
this.panel1 = new system.windows.forms.panel();
this.labelscore = new system.windows.forms.label();
this.labeleatgreencount = new system.windows.forms.label();
this.labelcurrentcount = new system.windows.forms.label();
this.labelinitcount = new system.windows.forms.label();
this.labelstatistic = new system.windows.forms.label();
this.labelright = new system.windows.forms.label();
this.labelleft = new system.windows.forms.label();
this.labeldown = new system.windows.forms.label();
this.labelup = new system.windows.forms.label();
this.labelgameintroduce = new system.windows.forms.label();
this.labelgamecurrentdifficulty = new system.windows.forms.label();
this.labelgamedifficulty = new system.windows.forms.label();
this.panel1.suspendlayout();
this.suspendlayout();
//
// menumain
//
this.menumain.menuitems.addrange(new system.windows.forms.menuitem[] {
this.menugame,
this.menuoption});
//
// menugame
//
this.menugame.index = 0;
this.menugame.menuitems.addrange(new system.windows.forms.menuitem[] {
this.menugamebegin,
this.menugamerebegin,
this.menugameend});
this.menugame.text = "游戏(&g)";
//
// menugamebegin
//
this.menugamebegin.index = 0;
this.menugamebegin.text = "开始(&s)";
this.menugamebegin.click += new system.eventhandler(this.menugamebegin_click);
//
// menugamerebegin
//
this.menugamerebegin.enabled = false;
this.menugamerebegin.index = 1;
this.menugamerebegin.text = "重新开始(&r)";
this.menugamerebegin.click += new system.eventhandler(this.menugamerebegin_click);
//
// menugameend
//
this.menugameend.index = 2;
this.menugameend.text = "结束(&x)";
this.menugameend.click += new system.eventhandler(this.menugameend_click);
//
// menuoption
//
this.menuoption.index = 1;
this.menuoption.menuitems.addrange(new system.windows.forms.menuitem[] {
this.menuoptiongamedifficulty,
this.menuoptionsplit,
this.menuoptiontopmost});
this.menuoption.text = "选项(&o)";
//
// menuoptiongamedifficulty
//
this.menuoptiongamedifficulty.index = 0;
this.menuoptiongamedifficulty.menuitems.addrange(new system.windows.forms.menuitem[] {
this.menuoptiongamedifficultyeasy,
this.menuoptiongamedifficultycommon,
this.menuoptiongamedifficultydifficult});
this.menuoptiongamedifficulty.text = "游戏难度";
//
// menuoptiongamedifficultyeasy
//
this.menuoptiongamedifficultyeasy.index = 0;
this.menuoptiongamedifficultyeasy.radiocheck = true;
this.menuoptiongamedifficultyeasy.text = "容易";
this.menuoptiongamedifficultyeasy.click += new system.eventhandler(this.menuoptiongamedifficultyeasy_click);
//
// menuoptiongamedifficultycommon
//
this.menuoptiongamedifficultycommon.checked = true;
this.menuoptiongamedifficultycommon.index = 1;
this.menuoptiongamedifficultycommon.radiocheck = true;
this.menuoptiongamedifficultycommon.text = "一般";
this.menuoptiongamedifficultycommon.click += new system.eventhandler(this.menuoptiongamedifficultycommon_click);
//
// menuoptiongamedifficultydifficult
//
this.menuoptiongamedifficultydifficult.index = 2;
this.menuoptiongamedifficultydifficult.radiocheck = true;
this.menuoptiongamedifficultydifficult.text = "极难";
this.menuoptiongamedifficultydifficult.click += new system.eventhandler(this.menuoptiongamedifficultydifficult_click);
//
// menuoptionsplit
//
this.menuoptionsplit.index = 1;
this.menuoptionsplit.text = "-";
//
// menuoptiontopmost
//
this.menuoptiontopmost.index = 2;
this.menuoptiontopmost.text = "总在最前面";
this.menuoptiontopmost.click += new system.eventhandler(this.menuoptiontopmost_click);
//
// panelgameregion
//
this.panelgameregion.anchor = ((system.windows.forms.anchorstyles)((((system.windows.forms.anchorstyles.top | system.windows.forms.anchorstyles.bottom)
| system.windows.forms.anchorstyles.left)
| system.windows.forms.anchorstyles.right)));
this.panelgameregion.backcolor = system.drawing.color.black;
this.panelgameregion.borderstyle = system.windows.forms.borderstyle.fixed3d;
this.panelgameregion.location = new system.drawing.point(8, 16);
this.panelgameregion.name = "panelgameregion";
this.panelgameregion.size = new system.drawing.size(500, 540);
this.panelgameregion.tabindex = 0;
//
// panel1
//
this.panel1.anchor = ((system.windows.forms.anchorstyles)(((system.windows.forms.anchorstyles.top | system.windows.forms.anchorstyles.bottom)
| system.windows.forms.anchorstyles.right)));
this.panel1.controls.add(this.labelscore);
this.panel1.controls.add(this.labeleatgreencount);
this.panel1.controls.add(this.labelcurrentcount);
this.panel1.controls.add(this.labelinitcount);
this.panel1.controls.add(this.labelstatistic);
this.panel1.controls.add(this.labelright);
this.panel1.controls.add(this.labelleft);
this.panel1.controls.add(this.labeldown);
this.panel1.controls.add(this.labelup);
this.panel1.controls.add(this.labelgameintroduce);
this.panel1.controls.add(this.labelgamecurrentdifficulty);
this.panel1.controls.add(this.labelgamedifficulty);
this.panel1.location = new system.drawing.point(515, 8);
this.panel1.name = "panel1";
this.panel1.size = new system.drawing.size(104, 545);
this.panel1.tabindex = 3;
//
// labelscore
//
this.labelscore.forecolor = system.drawing.color.white;
this.labelscore.location = new system.drawing.point(8, 184);
this.labelscore.name = "labelscore";
this.labelscore.size = new system.drawing.size(88, 16);
this.labelscore.tabindex = 11;
this.labelscore.textalign = system.drawing.contentalignment.middleright;
//
// labeleatgreencount
//
this.labeleatgreencount.forecolor = system.drawing.color.green;
this.labeleatgreencount.location = new system.drawing.point(8, 168);
this.labeleatgreencount.name = "labeleatgreencount";
this.labeleatgreencount.size = new system.drawing.size(88, 16);
this.labeleatgreencount.tabindex = 10;
this.labeleatgreencount.textalign = system.drawing.contentalignment.middleright;
//
// labelcurrentcount
//
this.labelcurrentcount.forecolor = system.drawing.color.white;
this.labelcurrentcount.location = new system.drawing.point(8, 144);
this.labelcurrentcount.name = "labelcurrentcount";
this.labelcurrentcount.size = new system.drawing.size(88, 16);
this.labelcurrentcount.tabindex = 9;
this.labelcurrentcount.textalign = system.drawing.contentalignment.middleright;
//
// labelinitcount
//
this.labelinitcount.forecolor = system.drawing.color.white;
this.labelinitcount.location = new system.drawing.point(8, 119);
this.labelinitcount.name = "labelinitcount";
this.labelinitcount.size = new system.drawing.size(88, 16);
this.labelinitcount.tabindex = 8;
this.labelinitcount.textalign = system.drawing.contentalignment.middleright;
//
// labelstatistic
//
this.labelstatistic.font = new system.drawing.font("隶书", 12f, system.drawing.fontstyle.regular, system.drawing.graphicsunit.point, ((system.byte)(134)));
this.labelstatistic.forecolor = system.drawing.color.white;
this.labelstatistic.location = new system.drawing.point(8, 80);
this.labelstatistic.name = "labelstatistic";
this.labelstatistic.size = new system.drawing.size(88, 32);
this.labelstatistic.tabindex = 7;
this.labelstatistic.text = "游戏统计:";
this.labelstatistic.textalign = system.drawing.contentalignment.middleleft;
//
// labelright
//
this.labelright.forecolor = system.drawing.color.white;
this.labelright.location = new system.drawing.point(8, 408);
this.labelright.name = "labelright";
this.labelright.size = new system.drawing.size(88, 23);
this.labelright.tabindex = 6;
this.labelright.text = "d(right) 右";
this.labelright.textalign = system.drawing.contentalignment.middleright;
//
// labelleft
//
this.labelleft.forecolor = system.drawing.color.white;
this.labelleft.location = new system.drawing.point(8, 376);
this.labelleft.name = "labelleft";
this.labelleft.size = new system.drawing.size(88, 23);
this.labelleft.tabindex = 5;
this.labelleft.text = "a(left) 左";
this.labelleft.textalign = system.drawing.contentalignment.middleright;
//
// labeldown
//
this.labeldown.forecolor = system.drawing.color.white;
this.labeldown.location = new system.drawing.point(8, 344);
this.labeldown.name = "labeldown";
this.labeldown.size = new system.drawing.size(88, 23);
this.labeldown.tabindex = 4;
this.labeldown.text = "s(down) 向下";
this.labeldown.textalign = system.drawing.contentalignment.middleright;
//
// labelup
//
this.labelup.forecolor = system.drawing.color.white;
this.labelup.location = new system.drawing.point(8, 312);
this.labelup.name = "labelup";
this.labelup.size = new system.drawing.size(88, 23);
this.labelup.tabindex = 3;
this.labelup.text = "w(up) 向上";
this.labelup.textalign = system.drawing.contentalignment.middleright;
//
// labelgameintroduce
//
this.labelgameintroduce.font = new system.drawing.font("隶书", 12f, system.drawing.fontstyle.regular, system.drawing.graphicsunit.point, ((system.byte)(134)));
this.labelgameintroduce.forecolor = system.drawing.color.white;
this.labelgameintroduce.location = new system.drawing.point(8, 272);
this.labelgameintroduce.name = "labelgameintroduce";
this.labelgameintroduce.size = new system.drawing.size(88, 32);
this.labelgameintroduce.tabindex = 2;
this.labelgameintroduce.text = "游戏说明:";
this.labelgameintroduce.textalign = system.drawing.contentalignment.middleleft;
//
// labelgamecurrentdifficulty
//
this.labelgamecurrentdifficulty.forecolor = system.drawing.color.white;
this.labelgamecurrentdifficulty.location = new system.drawing.point(8, 48);
this.labelgamecurrentdifficulty.name = "labelgamecurrentdifficulty";
this.labelgamecurrentdifficulty.size = new system.drawing.size(88, 23);
this.labelgamecurrentdifficulty.tabindex = 1;
this.labelgamecurrentdifficulty.text = "一般";
this.labelgamecurrentdifficulty.textalign = system.drawing.contentalignment.middleright;
//
// labelgamedifficulty
//
this.labelgamedifficulty.font = new system.drawing.font("隶书", 12f, system.drawing.fontstyle.regular, system.drawing.graphicsunit.point, ((system.byte)(134)));
this.labelgamedifficulty.forecolor = system.drawing.color.white;
this.labelgamedifficulty.location = new system.drawing.point(8, 8);
this.labelgamedifficulty.name = "labelgamedifficulty";
this.labelgamedifficulty.size = new system.drawing.size(88, 32);
this.labelgamedifficulty.tabindex = 0;
this.labelgamedifficulty.text = "游戏难度:";
this.labelgamedifficulty.textalign = system.drawing.contentalignment.middleleft;
//
// formmain
//
this.autoscalebasesize = new system.drawing.size(6, 14);
this.backcolor = system.drawing.color.black;
this.clientsize = new system.drawing.size(632, 565);
this.controlbox = false;
this.controls.add(this.panel1);
this.controls.add(this.panelgameregion);
this.menu = this.menumain;
this.name = "formmain";
this.startposition = system.windows.forms.formstartposition.centerscreen;
this.text = "贪吃蛇 - by lzumcj";
this.windowstate = system.windows.forms.formwindowstate.maximized;
this.keydown += new system.windows.forms.keyeventhandler(this.formmain_keydown);
this.panel1.resumelayout(false);
this.resumelayout(false);
}
#endregion
///
/// 应用程序的主入口点。
///
[stathread]
static void main()
{
application.run(new formmain());
}
///
/// 选择开始游戏
///
///
///
private void menugamebegin_click(object sender, system.eventargs e)
{
this.menugamebegin.enabled = false;
this.menugamerebegin.enabled = true;
begingame();
}
///
/// 选择重新开始游戏
///
///
///
private void menugamerebegin_click(object sender, system.eventargs e)
{
begingame();
}
///
/// 选择结束游戏
///
///
///
private void menugameend_click(object sender, system.eventargs e)
{
exitgame();
}
///
/// 选择游戏难度
///
///
///
private void menuoptiongamedifficultyeasy_click(object sender, system.eventargs e)
{
selectgamedifficulty(sender , e);
}
private void menuoptiongamedifficultycommon_click(object sender, system.eventargs e)
{
selectgamedifficulty(sender , e);
}
private void menuoptiongamedifficultydifficult_click(object sender, system.eventargs e)
{
selectgamedifficulty(sender , e);
}
///
/// 选择总在最前面
///
///
///
private void menuoptiontopmost_click(object sender, system.eventargs e)
{
menuoptiontopmost.checked = !menuoptiontopmost.checked;
this.topmost = menuoptiontopmost.checked;
}
///
/// 响应键盘输入
///
///
///
private void formmain_keydown(object sender, system.windows.forms.keyeventargs e)
{
//
// 根据键盘输入重新设置蛇的运动方向
//
if (snake != null)
snake.resetmovedirection(e.keydata.tostring());
}
///
/// 开始游戏(设置游戏规则)
///
private void begingame()
{
int igreencount = 0; // 吃的绿色数
// 清除上次剩下的食物
if (snakefood != null)
{
snakefood.clear();
snakefood.dispose();
}
snakefood = new snakefood(this.panelgameregion , imaxcount , iradius);
// 清除上次的蛇
if (snake != null)
{
snake.clear();
snake.dispose();
}
// 初始化新蛇
snake = new snake(this.panelgameregion , icount , iradius);
snake.draw();
int k = 0;
while (!gameover())
{
application.doevents();
snake.movenext();
for (int iindex=0; iindex {
if (snakefood[iindex].centerx == snake.currentheadx && snakefood[iindex].centery == snake.currentheady)
{
if (snakefood[iindex].colornode == color.green)
{
snake.addtrail();
igreencount++;
snakefood.removefood(iindex);
}
}
}
// 统计结果
labelinitcount.text = "初始蛇长:" + convert.tostring(icount);
labelcurrentcount.text = "现在蛇长:" + convert.tostring(snake.count);
labeleatgreencount.text = "吃了:● × " + convert.tostring(igreencount);
labelscore.text = "得分:" + convert.tostring(igreencount * 20);
// 两次移动的时间间隔(即移动速度)
thread.sleep(ispeed);
// 显示随机产生的点 , 且控制当前最多只能有imaxcount
if (k%(ispeed/40*30) == 0 && snakefood.currentcount {
snakefood.addfood();
}
k++;
}
this.menugamerebegin.enabled = true;
dialogresult drresult = messagebox.show("重新开始游戏?" , "提示" , messageboxbuttons.okcancel , messageboxicon.information , messageboxdefaultbutton.button1);
if (drresult == dialogresult.ok)
begingame();
else
exitgame();
}
///
/// 判断游戏是否满足结束条件
///
///
private bool gameover()
{
if (snake != null)
{
snake.check();
return snake.iseatself || snake.isoutofrange;
}
return false;
}
///
/// 退出游戏
///
private void exitgame()
{
if (application.allowquit)
{
application.exitthread();
application.exit();
}
}
///
/// 选择游戏难度(互斥)
///
///
///
private void selectgamedifficulty(object sender , eventargs e)
{
if (sender == menuoptiongamedifficultyeasy)
{
menuoptiongamedifficultyeasy.checked = true;
menuoptiongamedifficultycommon.checked = false;
menuoptiongamedifficultydifficult.checked = false;
labelgamecurrentdifficulty.text = menuoptiongamedifficultyeasy.text;;
ispeed = cispeed * 2;
}
else if (sender == menuoptiongamedifficultycommon)
{
menuoptiongamedifficultyeasy.checked = false;
menuoptiongamedifficultycommon.checked = true;
menuoptiongamedifficultydifficult.checked = false;
labelgamecurrentdifficulty.text = menuoptiongamedifficultycommon.text;
ispeed = cispeed;
}
else if (sender == menuoptiongamedifficultydifficult)
{
menuoptiongamedifficultyeasy.checked = false;
menuoptiongamedifficultycommon.checked = false;
menuoptiongamedifficultydifficult.checked = true;
labelgamecurrentdifficulty.text = menuoptiongamedifficultydifficult.text;
ispeed = cispeed / 2;
}
}
}
}
//
// main.cs end
//
//
// snake.cs begin
//
using system;
using system.collections;
using system.drawing;
using system.windows.forms;
using system.timers;
namespace greedysnake
{
#region snake 蛇身
///
/// snake 的摘要说明。
///
public class snake
{
private 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; // 是否超出允许活动的范围
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;}
}
public snake() : this(null , 20 , 5)
{
//
// todo: 在此处添加构造函数逻辑
//
}
public snake(control control , int icount , int iradius)
{
dccontrol = control;
count = icount;
radius = iradius;
currentheadx = currenttrailx = pretrailx = 5;
currentheady = currenttraily = pretraily = 5;
initialize();
}
~snake()
{
dispose(false);
}
// 初始化蛇
private void initialize()
{
alsnake = new arraylist();
for (int i=0 ; i {
alsnake.insert(0 , new snakenode(dccontrol , currentheadx , currentheady , radius));
currentheadx += 2 * 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 addhead()
{
alsnake.insert(0 , new snakenode(dccontrol , nextheadx , nextheady , radius));
currentheadx = nextheadx;
currentheady = nextheady;
count++;
}
// 加尾
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 movenext()
{
// 加头
addhead();
// 画头
((snakenode)alsnake[0]).draw();
// 清除尾(将蛇尾用背景色填充)
((snakenode)alsnake[count-1]).clear();
// 去尾(将蛇尾从 arraylist 中删除)
removetrail();
}
// 画整条蛇
public void draw()
{
for (int i=0; i {
((snakenode)alsnake[i]).draw();
}
}
// 清除整条蛇
public void clear()
{
for (int i=0; i {
((snakenode)alsnake[i]).clear();
}
}
// 重设运动方向
public void resetmovedirection(string strkeydata)
{
// 获取键盘输入
int ikeydirection;
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;
}
// 重设蛇的运动方向(综合按键方向和当前蛇的运动方向考虑)
int idirection = ikeydirection | movedirection;
if (idirection == 0x0011 || idirection == 0x1100)
movedirection = movedirection; // 运动方向保持不变
else
movedirection = ikeydirection; // 运动方向等于按键方向
}
// 是否超出范围
public void check()
{
getnextheadxy();
// 检查是否吃自己
foreach (snakenode sn in alsnake)
{
if (sn.centerx == nextheadx && sn.centery == nextheady)
{
iseatself = true;
break;
}
}
// 检查是否超出允许活动的范围
isoutofrange = nextheadx<0 || nextheadx>dccontrol.width || nextheady<0 || nextheady>dccontrol.height;
}
// 预先算出下个位置坐标
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
#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
#region snakefood 蛇的食物
///
/// snakefood 的摘要说明。
///
public class snakefood
{
private control dccontrol;
private int imaxcount; // 最多能剩下的食物总数
private int icurrentcount; // 当前剩下的食物总数
private int iradius; // 骨节的半径
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;
int ix = radius + istep * random.next(0 , dccontrol.width/istep);
int 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
}
//
// snake.cs end
//
新闻热点
疑难解答