刚学编程写了这个游戏炼炼,因为图形是用画画的会有闪烁。
不懂链表是什么,所以没用着。
蛇的移动速度会随着蛇的长度增快。自己反应迟钝只得35分。
原来上传附件忘了按 上传 按钮
怪不得上传不了。
[此贴子已经被作者于2006-4-24 17:23:19编辑过]
刚学编程写了这个游戏炼炼,因为图形是用画画的会有闪烁。
不懂链表是什么,所以没用着。
蛇的移动速度会随着蛇的长度增快。自己反应迟钝只得35分。
原来上传附件忘了按 上传 按钮
怪不得上传不了。
[此贴子已经被作者于2006-4-24 17:23:19编辑过]
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编辑过]
public void yidong()
{
Graphics g=CreateGraphics();
switch(Fanxian)
{
case "up":
{
sanke_fx[head.x,head.y]=Fanxian;
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;
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;
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;
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]="";
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]="";
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]="";
end.x=end.x+1;
break;
}
}
}
public void start()
{
Graphics g=CreateGraphics();
g.FillRectangle(new SolidBrush(Color.White),0,0,400,400);
for(int i=0;i<=19;i++)
for(int j=0;j<=19;j++)
{
xy[i,j]=0;
sanke_fx[i,j]="";
}
for(int i=1;i<=4;i++)
{
xy[i,1]=1;
sanke_fx[i,1]="right";
}
head.x=4; head.y=1;
end.x=1; end.y=1;
length=4;
Fanxian="right";
food();
timer.Interval=200;
if(menuItem4.Text=="暂停")
timer.Start();
menuItem4.Enabled=true;
}
public void draw()
{
Graphics g=CreateGraphics();
for(int x=0;x<=19;x++)
for(int y=0;y<=19;y++)
switch(sanke_fx[x,y])
{
case "up":
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":
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":
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":
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;
}
}
[此贴子已经被作者于2006-4-23 16:35:36编辑过]
int length;
int [,]xy=new int[20,20];
string [,]sanke_fx=new string[20,20];
string fanxian;
sankebody head=new sankebody(4,1);
sankebody end=new sankebody(1,1);
private System.Windows.Forms.Timer timer;
private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.MenuItem menuItem2;
private System.Windows.Forms.MenuItem menuItem3;
private System.Windows.Forms.MenuItem menuItem4;
private System.Windows.Forms.MenuItem menuItem5;
private System.Windows.Forms.MenuItem menuItem6;
private System.Windows.Forms.PictureBox radish;
private System.Windows.Forms.PictureBox apple;
private System.Windows.Forms.PictureBox watermelon;
private System.Windows.Forms.PictureBox banana;
private System.Windows.Forms.MenuItem menuItem7;
private System.ComponentModel.IContainer components;
public Form1()
{
for(int i=1;i<=4;i++)
{
xy[i,1]=1;
sanke_fx[i,1]="right";
}
length=4;
Fanxian="right";
InitializeComponent();
food();
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
this.timer = new System.Windows.Forms.Timer(this.components);
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.menuItem2 = new System.Windows.Forms.MenuItem();
this.menuItem3 = new System.Windows.Forms.MenuItem();
this.menuItem4 = new System.Windows.Forms.MenuItem();
this.menuItem6 = new System.Windows.Forms.MenuItem();
this.menuItem5 = new System.Windows.Forms.MenuItem();
this.radish = new System.Windows.Forms.PictureBox();
this.apple = new System.Windows.Forms.PictureBox();
this.watermelon = new System.Windows.Forms.PictureBox();
this.banana = new System.Windows.Forms.PictureBox();
this.menuItem7 = new System.Windows.Forms.MenuItem();
this.SuspendLayout();
//
// timer
//
this.timer.Enabled = true;
this.timer.Interval = 200;
this.timer.Tick += new System.EventHandler(this.time_Tick);
//
// mainMenu1
//
this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem1,
this.menuItem4,
this.menuItem6,
this.menuItem5});
//
// menuItem1
//
this.menuItem1.Index = 0;
this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem2,
this.menuItem3});
this.menuItem1.Text = "文件";
//
// menuItem2
//
this.menuItem2.Index = 0;
this.menuItem2.Text = "开始游戏";
this.menuItem2.Click += new System.EventHandler(this.menuItem2_Click);
//
// menuItem3
//
this.menuItem3.Index = 1;
this.menuItem3.Text = "退出";
this.menuItem3.Click += new System.EventHandler(this.menuItem3_Click);
//
// menuItem4
//
this.menuItem4.Index = 1;
this.menuItem4.Text = "暂停";
this.menuItem4.Click += new System.EventHandler(this.menuItem4_Click);
//
// menuItem6
//
this.menuItem6.Index = 2;
this.menuItem6.Text = "有墙模式";
this.menuItem6.Click += new System.EventHandler(this.menuItem6_Click);
//
// menuItem5
//
this.menuItem5.Index = 3;
this.menuItem5.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem7});
this.menuItem5.Text = "帮助";
//
// radish
//
this.radish.Image = ((System.Drawing.Image)(resources.GetObject("radish.Image")));
this.radish.Location = new System.Drawing.Point(136, 168);
this.radish.Name = "radish";
this.radish.Size = new System.Drawing.Size(15, 15);
this.radish.TabIndex = 0;
this.radish.TabStop = false;
//
// apple
//
this.apple.Image = ((System.Drawing.Image)(resources.GetObject("apple.Image")));
this.apple.Location = new System.Drawing.Point(136, 168);
this.apple.Name = "apple";
this.apple.Size = new System.Drawing.Size(16, 16);
this.apple.TabIndex = 1;
this.apple.TabStop = false;
//
// watermelon
//
this.watermelon.Image = ((System.Drawing.Image)(resources.GetObject("watermelon.Image")));
this.watermelon.Location = new System.Drawing.Point(136, 168);
this.watermelon.Name = "watermelon";
this.watermelon.Size = new System.Drawing.Size(16, 16);
this.watermelon.TabIndex = 2;
this.watermelon.TabStop = false;
//
// banana
//
this.banana.Image = ((System.Drawing.Image)(resources.GetObject("banana.Image")));
this.banana.Location = new System.Drawing.Point(136, 168);
this.banana.Name = "banana";
this.banana.Size = new System.Drawing.Size(16, 16);
this.banana.TabIndex = 3;
this.banana.TabStop = false;
//
// menuItem7
//
this.menuItem7.Index = 0;
this.menuItem7.Text = "关于";
this.menuItem7.Click += new System.EventHandler(this.menuItem7_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
this.ClientSize = new System.Drawing.Size(301, 321);
this.Controls.Add(this.watermelon);
this.Controls.Add(this.apple);
this.Controls.Add(this.radish);
this.Controls.Add(this.banana);
this.MaximizeBox = false;
this.MaximumSize = new System.Drawing.Size(309, 355);
this.Menu = this.mainMenu1;
this.MinimumSize = new System.Drawing.Size(309, 355);
this.Name = "Form1";
this.Text = "贪吃蛇";
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown);
this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseDown);
this.Load += new System.EventHandler(this.Form1_MouseEnter);
this.MouseEnter += new System.EventHandler(this.Form1_MouseEnter);
this.ResumeLayout(false);
}
[此贴子已经被作者于2006-4-23 16:40:04编辑过]
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
bool b;
private void time_Tick(object sender, System.EventArgs e)
{
Font f=new Font("Arial", 13);
Graphics g=CreateGraphics();
g.FillRectangle(new SolidBrush(Color.White),255,15,20,15);
g.DrawString(length.ToString(),f,new SolidBrush(Color.Black), new Point(255, 12));
b=true;
if(b1==true)
yidong();
else
yidong1();
}
public string Fanxian
{
get
{
return fanxian;
}
set
{
fanxian=value;
}
}
private void Form1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
if(b==false||menuItem4.Text!="暂停")
return;
if (e.KeyCode==Keys.Up)
{
if (Fanxian!="down")
Fanxian="up";
}
else if (e.KeyCode==Keys.Down)
{
if (Fanxian!="up")
{
Fanxian="down";
}
}
else if (e.KeyCode==Keys.Left)
{
if (Fanxian!="right")
{
Fanxian= "left";
}
}
else if (e.KeyCode==Keys.Right)
{
if (Fanxian!="left")
{
Fanxian= "right";
}
}
b=false;
}
private void menuItem4_Click(object sender, System.EventArgs e)
{
if(this.menuItem4.Text=="暂停")
{
timer.Stop();
this.menuItem4.Text="继续";
}
else
{
timer.Start();
this.menuItem4.Text="暂停";
}
}
private void menuItem2_Click(object sender, System.EventArgs e)
{
start();
}
private void menuItem3_Click(object sender, System.EventArgs e)
{
Application.Exit();
}
private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
if(b==false||menuItem4.Text!="暂停")
return;
if(e.Button==MouseButtons.Left)
switch(Fanxian)
{
case "up":Fanxian="left";break;
case "down":Fanxian="left";break;
case "left":Fanxian="up";break;
case "right":Fanxian="up";break;
}
else if(e.Button==MouseButtons.Right)
switch(Fanxian)
{
case "down":Fanxian="right";break;
case "up":Fanxian="right";break;
case "right":Fanxian="down";break;
case "left":Fanxian="down";break;
}
b=false;
}
private void Form1_MouseEnter(object sender, System.EventArgs e)
{
draw();
}
bool b1=true;
private void menuItem6_Click(object sender, System.EventArgs e)
{
if(b1==true)
{
b1=!b1;
menuItem6.Text="无墙模式";
}
else
{
b1=!b1;
menuItem6.Text="有墙模式";
}
}
private void menuItem7_Click(object sender, System.EventArgs e)
{
MessageBox.Show("贪吃蛇大家一定玩吧,刚学编程就写几个程序炼炼手脚。\n请大家多多指教。\n 不锈钢\n ht.zz@126.com");
}
}
public class sankebody
{
public int x=0;
public int y=0;
public sankebody(int x,int y)
{
this.x=x;
this.y=y;
}
}
}
[此贴子已经被作者于2006-4-23 16:39:22编辑过]
楼主学C#多长时间了,我刚刚学习,有很多地方不是很清楚。不知道要不要学习一下C++。请楼主赐教