using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;
namespace 贪吃蛇
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
public void food()
{
int x1,y1,a;
Random r=new Random();
do
{
x1=r.Next(0,20);
y1=r.Next(0,20);
a=r.Next(4);
}while(xy[x1,y1]!=0);
xy[x1,y1]=2;
this.banana.Location = new System.Drawing.Point(x1*15, y1*15);
this.watermelon.Location = new System.Drawing.Point(x1*15, y1*15);
this.apple.Location = new System.Drawing.Point(x1*15, y1*15);
this.radish.Location = new System.Drawing.Point(x1*15, y1*15);
switch(a)
{
case 0:{banana.Visible=true;watermelon.Visible=apple.Visible=radish.Visible=false;break;}
case 1:{watermelon.Visible=true;banana.Visible=apple.Visible=radish.Visible=false;break;}
case 2:{apple.Visible=true;watermelon.Visible=banana.Visible=radish.Visible=false;break;}
case 3:{radish.Visible=true;watermelon.Visible=apple.Visible=banana.Visible=false;break;}
}
}
public void yidong1()
{
Graphics g=CreateGraphics();
switch(Fanxian)
{
case "up":
{
sanke_fx[head.x,head.y]=Fanxian;
if(head.y==0)
head.y=19;
else
head.y=head.y-1;
if(head.y==-1||xy[head.x,head.y]==1)
{
timer.Stop();
menuItem4.Enabled=false;
MessageBox.Show("GAME OVER,Length="+length.ToString()+".","Game Ovre!");
return;
}
else if(xy[head.x,head.y]==2)
{
xy[head.x,head.y]=1;
if(timer.Interval!=5)
timer.Interval-=5;
length+=1;
food();
g.FillRectangle(new SolidBrush(Color.Yellow),head.x*15,head.y*15,14,15);
g.DrawRectangle(new Pen(Color.Black),head.x*15,head.y*15,14,15);
return;
}
xy[head.x,head.y]=1;
g.FillRectangle(new SolidBrush(Color.Yellow),head.x*15,head.y*15,14,15);
g.DrawRectangle(new Pen(Color.Black),head.x*15,head.y*15,14,15);
break;
}
case "down":
{
sanke_fx[head.x,head.y]=Fanxian;
if(head.y==19)
head.y=0;
else
head.y=head.y+1;
if(head.y==20||xy[head.x,head.y]==1)
{
timer.Stop();
menuItem4.Enabled=false;
MessageBox.Show("GAME OVER,Length="+length.ToString()+".","Game Ovre!");
return;
}
else if(xy[head.x,head.y]==2)
{
xy[head.x,head.y]=1;
if(timer.Interval!=5)
timer.Interval-=5;
length+=1;
food();
g.FillRectangle(new SolidBrush(Color.Yellow),head.x*15,head.y*15,14,15);
g.DrawRectangle(new Pen(Color.Black),head.x*15,head.y*15,14,15);
return;
}
xy[head.x,head.y]=1;
g.FillRectangle(new SolidBrush(Color.Yellow),head.x*15,head.y*15,14,15);
g.DrawRectangle(new Pen(Color.Black),head.x*15,head.y*15,14,15);
break;
}
case "left":
{
sanke_fx[head.x,head.y]=Fanxian;
if(head.x==0)
head.x=19;
else
head.x=head.x-1;
if(head.x==-1||xy[head.x,head.y]==1)
{
timer.Stop();
menuItem4.Enabled=false;
MessageBox.Show("GAME OVER,Length="+length.ToString()+".","Game Ovre!");
return;
}
else if(xy[head.x,head.y]==2)
{
xy[head.x,head.y]=1;
if(timer.Interval!=5)
timer.Interval-=5;
length+=1;
food();
g.FillRectangle(new SolidBrush(Color.Yellow),head.x*15,head.y*15,15,14);
g.DrawRectangle(new Pen(Color.Black),head.x*15,head.y*15,15,14);
return;
}
xy[head.x,head.y]=1;
g.FillRectangle(new SolidBrush(Color.Yellow),head.x*15,head.y*15,15,14);
g.DrawRectangle(new Pen(Color.Black),head.x*15,head.y*15,15,14);
break;
}
case "right":
{
sanke_fx[head.x,head.y]=Fanxian;
if(head.x==19)
head.x=0;
else
head.x=head.x+1;
if(head.x==20||xy[head.x,head.y]==1)
{
timer.Stop();
menuItem4.Enabled=false;
MessageBox.Show("GAME OVER,Length="+length.ToString()+".","Game Ovre!");
return;
}
else if(xy[head.x,head.y]==2)
{
xy[head.x,head.y]=1;
if(timer.Interval!=5)
timer.Interval-=5;
length+=1;
food();
g.FillRectangle(new SolidBrush(Color.Yellow),head.x*15,head.y*15,15,14);
g.DrawRectangle(new Pen(Color.Black),head.x*15,head.y*15,15,14);
return;
}
xy[head.x,head.y]=1;
g.FillRectangle(new SolidBrush(Color.Yellow),head.x*15,head.y*15,15,14);
g.DrawRectangle(new Pen(Color.Black),head.x*15,head.y*15,15,14);
break;
}
}
switch(sanke_fx[end.x,end.y])
{
case "up":
{
xy[end.x,end.y]=0;
g.FillRectangle(new SolidBrush(Color.White),end.x*15,end.y*15+1,16,15);
sanke_fx[end.x,end.y]="";
if(end.y==0)
end.y=19;
else
end.y=end.y-1;
break;
}
case "down":
{
xy[end.x,end.y]=0;
g.FillRectangle(new SolidBrush(Color.White),end.x*15,end.y*15,16,15);
sanke_fx[end.x,end.y]="";
if(end.y==19)
end.y=0;
else
end.y=end.y+1;
break;
}
case "left":
{
xy[end.x,end.y]=0;
g.FillRectangle(new SolidBrush(Color.White),end.x*15+1,end.y*15,15,16);
sanke_fx[end.x,end.y]="";
if(end.x==0)end.x=19;
else end.x=end.x-1;break;
}
case "right":
{
xy[end.x,end.y]=0;
g.FillRectangle(new SolidBrush(Color.White),end.x*15,end.y*15,15,16);
sanke_fx[end.x,end.y]="";
if(end.x==19)
end.x=0;
else
end.x=end.x+1;
break;
}
}
}
[此贴子已经被作者于2006-4-23 16:34:29编辑过]





2006-4-15 13:38 

