下面是游戲的源代碼。有兩個文件: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
//
|
新聞熱點
疑難解答