坦克大战的游戏代码.docx
《坦克大战的游戏代码.docx》由会员分享,可在线阅读,更多相关《坦克大战的游戏代码.docx(8页珍藏版)》请在冰豆网上搜索。
![坦克大战的游戏代码.docx](https://file1.bdocx.com/fileroot1/2023-2/3/e4722591-c6d8-4b92-be9f-69fdd786358b/e4722591-c6d8-4b92-be9f-69fdd786358b1.gif)
坦克大战的游戏代码
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,1);//产生0—9的数
this.top=r.Next(0,1);//产生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//坦克的类型属性
{
get
{
returntype;
}
set
{
if(top>=1&&top<=5)
{
type=value;
}
}
}
publicintLeft//Left属性
{
get
{
returnleft;
}
set
{
if(left>=0&&left<=9)
{
left=value;
//if(left==0||left==9)newDirect();
}
}
}
publicintDirect//Direct属性(坦克方向)
{
get
{
returndirect;
}
set
{
direct=value;
}
}
publicvoidnewDirect()//改变方向
{
Randomr=newRandom();
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)//坦克爆炸动画
{
//得到绘制这个坦克图形的在游戏面板中的矩形区域
RectangledestRect=newRectangle(this.left*width,this.top*height,width,height);
RectanglesrcRect=newRectangle(0,0,width,height);
ImagetankImage=Image.FromFile("BMP/explode1.bmp");
g.DrawImage(tankImage,destRect,srcRect,GraphicsUnit.Pixel);
tankImage=Image.FromFile("BMP/explode1.bmp");
g.DrawImage(tankImage,destRect,srcRect,GraphicsUnit.Pixel);
tankImage=Image.FromFile("BMP/explode2.bmp");
g.DrawImage(tankImage,destRect,srcRect,GraphicsUnit.Pixel);
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;
//}
}
}