《c#貪吃蛇源碼》由會員分享,可在線閱讀,更多相關(guān)《c#貪吃蛇源碼(11頁珍藏版)》請在裝配圖網(wǎng)上搜索。
1、usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Text;
usingSystem.Windows.Forms;
namespaceSnake
{
publicpartialclassForm1:Form
{
publicForm1()
{
InitializeComponent();
}
IntPtrptr;
Snakesnake;
觀察哨
boolObs
2、ervation=true;////開始游戲
privatevoidbutton1_Click(objectsender,EventArgse)
{
Start();
}
//暫停游戲
privatevoidbutton2_Click(objectsender,EventArgse)
{
Suspension();
}
//wasd方向控制
protectedoverrideboolProcessDialogChar(charcharCode)
{
switch(charCode)
{case'w':
if(snake.Direction!=1)snake.Dir
3、ection=3;
break;
case's':
if(snake.Direction!=3)snake.Direction=1;
break;
case'a':
if(snake.Direction!=0)snake.Direction=2;
break;
case'd':
if(snake.Direction!=2)snake.Direction=0;
break;
}returnfalse;
//時鐘控制移動
privatevoidtimer1_Tick(objectsender,EventArgse)
{
//產(chǎn)生食物
snake.MakeFood()
4、;
//吃食物
snake.EatFood();
snake.rule();
//加分
if(snake.boolfood==false)
{
this.textFen.Text=(int.Parse(this.textFen.Text)+1).ToString();
}
//移動
if(snake.State==true)
snake.Move();
}
else
{
snake.Over();
this.timer1.Stop();
intfen=int.Parse(this.textFen.Text)-3;
this.labelDeFen.Text=f
5、en.ToString();
this.labelDengJi.Text=this.textJi.Text;
this.groupBox1.Visible=true;
this.button1.Enabled=true;
this.button2.Enabled=false;
boBoxDengJi.Enabled=true;
}
}
//udlr方向控制
protectedoverrideboolProcessDialogKey(KeyskeyData){
switch(keyData){
caseKeys.Down:
if(snake.Direction!=3)sn
6、ake.Direction=1;
break;
caseKeys.Up:
if(snake.Direction!=1)snake.Direction=3;
break;
caseKeys.Left:
if(snake.Direction!=0)snake.Direction=2;
break;
caseKeys.Right:
if(snake.Direction!=2)snake.Direction=0;
break;
}
returnfalse;
}
//開始游戲
voidStart()
{
this.groupBox1.Visible=false;
if
7、(Observation==false)
{
Speed(boBoxDengJi.Text);
timer1.Start();
this.button1.Enabled=false;
this.button2.Enabled=true;
boBoxDengJi.Enabled=false;
}else
Speed(boBoxDengJi.Text);
ptr=this.pictureBox1.Handle;
snake=newSnake(ptr);
snake.Draw();
timer1.Start();
this.button1.Enabled=false;
t
8、his.button2.Enabled=true;
boBoxDengJi.Enabled=false;
}
}
//暫停游戲
voidSuspension()
{
this.timer1.Stop();
this.button1.Enabled=true;
boBoxDengJi.Enabled=true;
Observation=false;
}
//設(shè)定速度
voidSpeed(stringchr)
{
switch(chr)
{
case"1":this.timer1.Interval=500;break;
case"2":this.timer1.I
9、nterval=400;break;
case"3":this.timer1.Interval=300;break;
case"4":this.timer1.Interval=250;break;
case"5":this.timer1.Interval=200;break;
case"6":this.timer1.Interval=150;break;
case"7":this.timer1.Interval=100;break;
case"8":this.timer1.Interval=75;break;
case"9":this.timer1.Interval=65;bre
10、ak;
case"10":this.timer1.Interval=50;break;
}
}
}
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Text;
namespaceSnake
{
publicclassSnake
{
publicListlist=newList();
IntPtrptr;
//食物變量
publicboolboolfood=false;
Randomran=newRandom();
Squarefood=null;
intdirection=0;//0:
右1:下2:左3:上
publicintDirection
{
get{returndirection;}
set{direction=value;}
}
publicboolstate=true;//判斷游戲是否結(jié)束
publicboolState
{
get{returnstate;}
set{state=value;}
}
publicSnake(IntPtrptr)
{this.ptr=ptr;