C#坦克游戏详细讲解.docx
《C#坦克游戏详细讲解.docx》由会员分享,可在线阅读,更多相关《C#坦克游戏详细讲解.docx(24页珍藏版)》请在冰豆网上搜索。
C#坦克游戏详细讲解
本游戏主要分4个内容讲解:
1、设计坦克类
2、设计子弹类
3、设计音乐类
4、设计窗体类
通俗易懂,希望对广大的学生们都有帮助
欢迎下载
技术qq1278263100
邮箱*****************
游戏所需图片
游戏运行界面如下图:
1、设计坦克类:
新建一个类文件tank.cs
(代码加注释如下)
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Text;
usingSystem.Drawing;//add
usingSystem.Collections;//add
namespace坦克
{
classTank
privateintwidth;//坦克的宽度
privateintheight;//坦克的高度
privateinttop;//坦克位置的纵坐标
privateintleft;//坦克位置的横坐标
privateinttype;//坦克的类型(2---5敌方,6己方)
privateintdirect;//0--上,1--下,2--左,3--右
publicArrayListbList=newArrayList();//子弹序列
publicTank(inttank_type)//构造函数
Randomr=newRandom();
this.direct=r.Next(0,4);//产生0—3的数
this.width=32;
this.height=32;
this.left=r.Next(0,10);//产生0—9的数
this.top=r.Next(0,10);//产生0—9的数
this.type=tank_type;
}
publicintTop//Top属性
get
returntop;
set
if(top>=0&&top<=9)
top=value;
//if(top==0||top==9)newDirect();
publicintType//坦克的类型属性
returntype;
if(top>=1&&top<=5)
type=value;
publicintLeft//Left属性
returnleft;
if(left>=0&&left<=9)
left=value;
//if(left==0||left==9)newDirect();
publicintDirect//Direct属性(坦克方向)
returndirect;
direct=value;
publicvoidnewDirect()//改变方向
intnew_Direct=r.Next(0,4);//产生0—3的数
while(this.direct==new_Direct)
new_Direct=r.Next(0,4);//产生0—3的数
this.direct=new_Direct;
publicvoidDraw(Graphicsg,inttype)//根据坦克类型选择不同图片
ImagetankImage=Image.FromFile("BMP/ETANK1.BMP");
if(type==2)tankImage=Image.FromFile("BMP/ETANK2.BMP");
if(type==3)tankImage=Image.FromFile("BMP/ETANK3.BMP");
if(type==4)tankImage=Image.FromFile("BMP/ETANK4.BMP");
if(type==5)tankImage=Image.FromFile("BMP/ETANK1.BMP");
if(type==6)tankImage=Image.FromFile("BMP/MYTANK.BMP");
//得到绘制这个坦克图形的在游戏面板中的矩形区域
RectangledestRect=newRectangle(this.left*width,this.top*height,width,height);
RectanglesrcRect=newRectangle(direct*width,0,width,height);
g.DrawImage(tankImage,destRect,srcRect,GraphicsUnit.Pixel);
publicvoidExplore(Graphicsg)//坦克爆炸动画
RectanglesrcRect=newRectangle(0,0,width,height);
ImagetankImage=Image.FromFile("BMP/explode1.bmp");
tankImage=Image.FromFile("BMP/explode1.bmp");
tankImage=Image.FromFile("BMP/explode2.bmp");
PlaySound.Play("Sound/Explode.wav");
publicvoidfire()
bulletb=newbullet(this.type);//根据坦克产生不同子弹
b.Direct=this.Direct;//坦克的朝向
b.Top=this.Top;
b.Left=this.Left;
//b.move();
bList.Add(b);
if(this.type==6)PlaySound.Play("Sound/Shoot.wav");//己方发射出声
publicvoidMoveBullet(refint[,]Map)
for(inti=bList.Count-1;i>=0;i--)//遍历子弹序列
//for(inti=0;i{bullett=((bullet)bList[i]);//移动以前if(t.Left<0||t.Left>9||t.Top<0||t.Top>9)//超出边界{bList.RemoveAt(i);continue;//删除此颗子弹}if(Map[t.Left,t.Top]!=0&&Map[t.Left,t.Top]!=this.type)//已遇到坦克和墙等障碍物{bList.RemoveAt(i);//删除此颗子弹if(t.hitE(Map[t.Left,t.Top]))//击中对方坦克Map[t.Left,t.Top]=-1;//此处坦克被打中continue;}t.move();//移动以后if(t.Left<0||t.Left>9||t.Top<0||t.Top>9)//超出边界{bList.RemoveAt(i);continue;//删除此颗子弹}if(Map[t.Left,t.Top]!=0)//已遇到物体{bList.RemoveAt(i);//删除此颗子弹if(t.hitE(Map[t.Left,t.Top]))//击中对方坦克Map[t.Left,t.Top]=-1;//此处坦克被打中continue;}}}publicvoidDrawBullet(Graphicsg,int[,]Map)//画子弹{MoveBullet(refMap);foreach(bullettinbList)//遍历子弹序列t.Draw(g);}//publicvoidNewPosition()//产生新坐标//{//Randomr=newRandom();//this.left=r.Next(0,10);//产生0—9的数//this.top=r.Next(0,10);//产生0—9的数//}//publicboolComparePosition(Tanks)//比较坦克位置//{//if(this.left==s.left&&this.top==s.top)//returntrue;//else//returnfalse;//}}}2、设计子弹类新建一个类文件:bullet.csusingSystem;usingSystem.Collections.Generic;usingSystem.Text;usingSystem.Drawing;//addusingSystem.Collections;//addnamespace坦克{classbullet{privateinttop;//子弹坐标(Top,Left)privateintleft;privateintdirect;//子弹行进方向privateintwidth=32;privateintheight=32;privatebooltype;//己方子弹true,敌方子弹falsepublicbullet(inttype)//子弹类构造函数{if(type==6)//己方this.type=true;elsethis.type=false;}publicintTop//Top属性{get{returntop;}set{top=value;}}publicintLeft//Left属性{get{returnleft;}set{left=value;}}publicintDirect//Direct属性(子弹行进方向){get{returndirect;}set{direct=value;}}publicvoidmove(){switch(Direct){case0:Top--;break;case1:Top++;break;case2:Left--;break;case3:Left++;break;}}publicvoidDraw(Graphicsg){ImagebulletImage;if(type==true)//己方bulletImage=Image.FromFile("BMP/missile1.bmp");elsebulletImage=Image.FromFile("BMP/missile2.bmp");//得到绘制这个子弹图形的在游戏面板中的矩形区域RectangledestRect=newRectangle(this.left*width,this.top*height,width,height);RectanglesrcRect=newRectangle(0,0,width,height);g.DrawImage(bulletImage,destRect,srcRect,GraphicsUnit.Pixel);}publicboolhitE(inttanktype)//是否击中对方坦克{if(type==false)//敌方子弹if(tanktype>=2&&tanktype<=5)//坦克的类型(2---5敌方,6己方)returnfalse;elsereturntrue;if(type==true)//己方子弹if(tanktype==6)//坦克的类型(2---5敌方,6己方)returnfalse;elsereturntrue;returnfalse;}}}3、设计音乐播放类新建类文件PlaySound.csusingSystem;usingSystem.Runtime.InteropServices;namespace坦¬1克?{//////PlaySound的Ì?摘a要°a说¦Ì明¡Â。¡ê///publicclassPlaySound{privateconstintSND_SYNC=0x0;privateconstintSND_ASYNC=0x1;privateconstintSND_NODEFAULT=0x2;privateconstintSND_LOOP=0x8;privateconstintSND_NOSTOP=0x10;publicstaticvoidPlay(stringfile){intflags=SND_ASYNC|SND_NODEFAULT;sndPlaySound(file,flags);}[DllImport("winmm.dll")]privateexternstaticintsndPlaySound(stringfile,intuFlags);}}4、设计游戏窗体类usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.Collections;//addnamespace坦克{publicpartialclassForm1:Form{publicForm1(){InitializeComponent();}//privateTank[]eTanks=newTank[11];privateinteCount=0;//敌方坦克数量privateinteMaxCount=10;//eMaxCount敌方坦克最大量privatestringpath;//应用程序路径privateTankeTank;privateArrayListeTanks=newArrayList();privateint[,]Map=newint[10,10];//砖块地图publicint[,]TMap=newint[10,10];//含坦克,砖的地图privateintwidth=32;privateTankMyTank=newTank(6);privateintScore=0;//计分privatevoidForm1_Load(objectsender,System.EventArgse){pictureBox1.Width=10*width;pictureBox1.Height=10*width;path=Application.StartupPath;Randomr=newRandom();for(intx=0;x<10;x+=2)for(inty=0;y<10;y+=2){//产生0,1数其中0代表空地,1代表墙砖Map[x,y]=r.Next(0,2);}Map[4,9]=0;MyTank.Top=9;MyTank.Left=4;MyTank.Direct=0;lblX.Text="X坐标:"+MyTank.Left+"Y坐标:"+MyTank.Top;}privatevoidDragWall(Graphicsg)//画游戏地图{ImageWallImage=Image.FromFile("BMP/TQ.BMP");for(intx=0;x<10;x++)for(inty=0;y<10;y++){if(Map[x,y]==1){//得到绘制这个墙砖块的在游戏面板中的矩形区域RectangleRect=newRectangle(x*width,y*width,width,width);g.DrawImage(WallImage,Rect);}}}privatevoidForm1_KeyDown(objectsender,System.Windows.Forms.KeyEventArgse){switch(e.KeyCode){caseKeys.Up://上if(MyTank.Top==0||Map[MyTank.Left,MyTank.Top-1]==1||Meet_Tank(MyTank.Left,MyTank.Top-1))//遇到墙砖或坦克;//不动elseif(MyTank.Direct==0)MyTank.Top--;MyTank.Direct=0;break;caseKeys.Down://下if(MyTank.Top==9||Map[MyTank.Left,MyTank.Top+1]==1||Meet_Tank(MyTank.Left,MyTank.Top+1))//遇到墙砖或坦克;//不动elseif(MyTank.Direct==1)MyTank.Top++;MyTank.Direct=1;break;caseKeys.Left://左if(MyTank.Left==0||Map[MyTank.Left-1,MyTank.Top]==1||Meet_Tank(MyTank.Left-1,MyTank.Top))//遇到墙砖或坦克;//不动elseif(MyTank.Direct==2)MyTank.Left--;MyTank.Direct=2;break;caseKeys.Right://右if(MyTank.Left==9||Map[MyTank.Left+1,MyTank.Top]==1||Meet_Tank(MyTank.Left+1,MyTank.Top))//遇到墙砖或坦克;//不动elseif(MyTank.Direct==3)MyTank.Left++;MyTank.Direct=3;break;caseKeys.Space://空格发射子弹MyTank.fire();break;}pictureBox1.Invalidate();//重画游戏面板区域lblX.Text="X坐标:"+MyTank.Left+"Y坐标:"+MyTank.Top;}privatevoidtimer1_Tick(objectsender,EventArgse){foreach(TanktineTanks){switch(t.Direct)//0--上,1--下,2--左,3--右{case0://向上if(t.Top==0||Map[t.Left,t.Top-1]==1 ||Meet_Tank(t.Left,t.Top-1))//遇到墙砖或坦克t.newDirect();//坦克转向elset.Top--;break;case1: //向下if(t.Top==9||Map[t.Left,t.Top+1]==1||Meet_Tank(t.Left,t.Top+1))//遇到墙砖或坦克t.newDirect();//坦克转向elset.Top++;break;case2: //向左if(t.Left==0||Map[t.Left-1,t.Top]==1||Meet_Tank(t.Left-1,
bullett=((bullet)bList[i]);
//移动以前
if(t.Left<0||t.Left>9||t.Top<0||t.Top>9)
//超出边界
bList.RemoveAt(i);continue;//删除此颗子弹
if(Map[t.Left,t.Top]!
=0&&Map[t.Left,t.Top]!
=this.type)
//已遇到坦克和墙等障碍物
bList.RemoveAt(i);//删除此颗子弹
if(t.hitE(Map[t.Left,t.Top]))//击中对方坦克
Map[t.Left,t.Top]=-1;//此处坦克被打中
continue;
t.move();//移动以后
=0)//已遇到物体
publicvoidDrawBullet(Graphicsg,int[,]Map)//画子弹
MoveBullet(refMap);
foreach(bullettinbList)//遍历子弹序列
t.Draw(g);
//publicvoidNewPosition()//产生新坐标
//{
//Randomr=newRandom();
//this.left=r.Next(0,10);//产生0—9的数
//this.top=r.Next(0,10);//产生0—9的数
//}
//publicboolComparePosition(Tanks)//比较坦克位置
//if(this.left==s.left&&this.top==s.top)
//returntrue;
//else
//returnfalse;
新建一个类文件:
bullet.cs
classbullet
privateinttop;//子弹坐标(Top,Left)
privateintleft;
privateintdirect;//子弹行进方向
privateintwidth=32;
privateintheight=32;
privatebooltype;//己方子弹true,敌方子弹false
publicbullet(inttype)//子弹类构造函数
if(type==6)//己方
this.type=true;
else
this.type=false;
publicintDirect//Direct属性(子弹行进方向)
publicvoidmove()
switch(Direct)
case0:
Top--;break;
case1:
Top++;break;
case2:
Left--;break;
case3:
Left++;break;
publicvoidDraw(Graphicsg)
ImagebulletImage;
if(type==true)//己方
bulletImage=Image.FromFile("BMP/missile1.bmp");
bulletImage=Image.FromFile("BMP/missile2.bmp");
//得到绘制这个子弹图形的在游戏面板中的矩形区域
g.DrawImage(bulletImage,destRect,srcRect,GraphicsUnit.Pixel);
publicboolhitE(inttanktype)//是否击中对方坦克
if(type==false)//敌方子弹
if(tanktype>=2&&tanktype<=5)
//坦克的类型(2---5敌方,6己方)
returnfalse;
returntrue;
if(type==true)//己方子弹
if(tanktype==6)//坦克的类型(2---5敌方,6己方)
3、设计音乐播放类
新建类文件PlaySound.cs
usingSystem.Runtime.InteropServices;
namespace坦¬1克?
///
///PlaySound的Ì?
摘a要°a说¦Ì明¡Â。
¡ê
publicclassPlaySound
privateconstintSND_SYNC=0x0;
privateconstintSND_ASYNC=0x1;
privateconstintSND_NODEFAULT=0x2;
privateconstintSND_LOOP=0x8;
privateconstintSND_NOSTOP=0x10;
publicstaticvoidPlay(stringfile)
intflags=SND_ASYNC|SND_NODEFAULT;
sndPlaySound(file,flags);
[DllImport("winmm.dll")]
privateexternstaticintsndPlaySound(stringfile,intuFlags);
4、设计游戏窗体类
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Windows.Forms;
publicpartialclassForm1:
Form
publicForm1()
InitializeComponent();
//privateTank[]eTanks=newTank[11];
privateinteCount=0;//敌方坦克数量
privateinteMaxCount=10;//eMaxCount敌方坦克最大量
privatestringpath;//应用程序路径
privateTankeTank;
privateArrayListeTanks=newArrayList();
privateint[,]Map=newint[10,10];//砖块地图
publicint[,]TMap=newint[10,10];//含坦克,砖的地图
privateTankMyTank=newTank(6);
privateintScore=0;//计分
privatevoidForm1_Load(objectsender,System.EventArgse)
pictureBox1.Width=10*width;
pictureBox1.Height=10*width;
path=Application.StartupPath;
for(intx=0;x<10;x+=2)
for(inty=0;y<10;y+=2)
//产生0,1数其中0代表空地,1代表墙砖
Map[x,y]=r.Next(0,2);
Map[4,9]=0;
MyTank.Top=9;
MyTank.Left=4;
MyTank.Direct=0;
lblX.Text="X坐标:
"+MyTank.Left+"Y坐标:
"+MyTank.Top;
privatevoidDragWall(Graphicsg)//画游戏地图
ImageWallImage=Image.FromFile("BMP/TQ.BMP");
for(intx=0;x<10;x++)
for(inty=0;y<10;y++)
if(Map[x,y]==1)
//得到绘制这个墙砖块的在游戏面板中的矩形区域
RectangleRect=newRectangle(x*width,y*width,width,width);
g.DrawImage(WallImage,Rect);
privatevoidForm1_KeyDown(objectsender,System.Windows.Forms.KeyEventArgse)
switch(e.KeyCode)
caseKeys.Up:
//上
if(MyTank.Top==0||Map[MyTank.Left,MyTank.Top-1]==1
||Meet_Tank(MyTank.Left,MyTank.Top-1))//遇到墙砖或坦克
;//不动
if(MyTank.Direct==0)MyTank.Top--;
break;
caseKeys.Down:
//下
if(MyTank.Top==9||Map[MyTank.Left,MyTank.Top+1]==1
||Meet_Tank(MyTank.Left,MyTank.Top+1))//遇到墙砖或坦克
if(MyTank.Direct==1)MyTank.Top++;
MyTank.Direct=1;
caseKeys.Left:
//左
if(MyTank.Left==0||Map[MyTank.Left-1,MyTank.Top]==1
||Meet_Tank(MyTank.Left-1,MyTank.Top))//遇到墙砖或坦克
if(MyTank.Direct==2)MyTank.Left--;
MyTank.Direct=2;
caseKeys.Right:
//右
if(MyTank.Left==9||Map[MyTank.Left+1,MyTank.Top]==1
||Meet_Tank(MyTank.Left+1,MyTank.Top))//遇到墙砖或坦克
if(MyTank.Direct==3)MyTank.Left++;
MyTank.Direct=3;
caseKeys.Space:
//空格发射子弹
MyTank.fire();
pictureBox1.Invalidate();//重画游戏面板区域
privatevoidtimer1_Tick(objectsender,EventArgse)
foreach(TanktineTanks)
switch(t.Direct)//0--上,1--下,2--左,3--右
//向上
if(t.Top==0||Map[t.Left,t.Top-1]==1
||Meet_Tank(t.Left,t.Top-1))//遇到墙砖或坦克
t.newDirect();//坦克转向
t.Top--;
//向下
if(t.Top==9||Map[t.Left,t.Top+1]==1
||Meet_Tank(t.Left,t.Top+1))//遇到墙砖或坦克
t.Top++;
//向左
if(t.Left==0||Map[t.Left-1,t.Top]==1
||Meet_Tank(t.Left-1,
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1