项目坦克大战设计分析报告.docx

上传人:b****7 文档编号:10682379 上传时间:2023-02-22 格式:DOCX 页数:25 大小:97.75KB
下载 相关 举报
项目坦克大战设计分析报告.docx_第1页
第1页 / 共25页
项目坦克大战设计分析报告.docx_第2页
第2页 / 共25页
项目坦克大战设计分析报告.docx_第3页
第3页 / 共25页
项目坦克大战设计分析报告.docx_第4页
第4页 / 共25页
项目坦克大战设计分析报告.docx_第5页
第5页 / 共25页
点击查看更多>>
下载资源
资源描述

项目坦克大战设计分析报告.docx

《项目坦克大战设计分析报告.docx》由会员分享,可在线阅读,更多相关《项目坦克大战设计分析报告.docx(25页珍藏版)》请在冰豆网上搜索。

项目坦克大战设计分析报告.docx

项目坦克大战设计分析报告

《JAVA程序开发课程设计》项目设计

项目名称:

TankWar软件

专业:

软件工程班级:

13软工1班姓名:

毛晨光学号:

一、需求分析:

基本功能:

1.玩家控制的坦克能够四处移动并且打击敌方坦克;

2.敌方坦克能够随机四处移动并且打击玩家控制的坦克;

3.玩家控制的坦克拥有血量,而敌方坦克没有;

4.坦克受到攻击时血条会缩短;

5.敌方坦克被消灭完之后,提示游戏胜利;

6.用户方坦克被消灭后提示游戏结束;

特色功能:

1.坦克具有图片,不单单只是个圈圈。

2.增加了血包功能,地图上会随机出现一个血包,我方坦克开过会增加血量。

二、系统设计:

1.TankMap类:

实现游戏界面地图的初始化。

2.PainTread类:

绘制和重绘功能。

3.DirectionHandler:

监听用户的键盘输入。

4.Tank类:

实现坦克的初始化,绘制,移动,发射等功能。

5.EnemyTank:

实现敌方坦克的初始化,绘制,移动,发射等功能。

6.Shell类:

实现炮弹的初始化,绘制,移动,攻击功能。

7.Explor类:

实现爆炸的初始化。

绘制功能,爆炸效果由绘制半径从小到大再到小的圆实现。

8.Direction类:

包含枚举。

9.Blood类:

用于实现血包的功能。

三、功能实现。

一.绘制地图功能:

publicclassTankMapextendsFrame{

//定义地图的尺寸。

publicstaticfinalintMAPWIDTH=800;

publicstaticfinalintMAPHEIGHT=600;

//我方坦克

Tankt=null;

//定义随机出现的血包

Randomr=newRandom();

ImagebufferImage=null;

//地方坦克集合

->

enemys=newArrayList();

//爆炸集合

public.static.jav

//敌方坦克数量默认10个

publicintenemyCount=5;

//主方法

publicstaticvoidmain(String[]args){

TankMaptv=newTankMap();

tv.init();

}

publicvoiddrawImage(){

}

//地图初始化方法

publicvoidinit(){

//初始化地图

this.setSize(MAPWIDTH,MAPHEIGHT);

this.setTitle("TankWar");

this.setVisible(true);

//添加键盘监听器

this.addKeyListener(newDirectionHandler());

//添加穿口关闭监听器

this.addWindowListener(newWindowAdapter(){

publicvoidwindowClosing(WindowEventarg0){

System.exit(0);

}

});

//初始化我方坦克

t=newTank(this);

//初始化敌方坦克

for(inti=0;i

{

enemys.add(newEnemyTank(40+30*i,80,Color.YELLOW));

}

//启动绘制线程

newThread(newPaintThread()).start();

}

//@Override

publicvoidpaint(Graphicsg)

{

//画地图

Colorc=g.getColor();

g.setColor(Color.GREEN);

g.fillRect(0,0,MAPWIDTH,MAPHEIGHT);

g.setColor(Color.RED);

g.drawString("当前炮弹数目:

"+shells.size(),20,40);

g.drawString("生命值:

",20,60);

g.fillRect(65,55,t.getLife(),5);

g.setColor(c);

//画坦克

t.draw(g);

if(r.nextInt(10)==9&&bloods.size()==0)

bloods.add(newBlood());

if(r.nextInt(60)==7&&bloods.size()==1)

bloods.remove(0);

for(inti=0;i

EnemyTanket=enemys.get(i);

et.draw(g);

}

//画爆炸

for(inti=0;i

Explore=explors.get(i);

e.draw(g);

}

//绘制血包

for(inti=0;i

Bloodb=bloods.get(i);

b.bloodb(t);

b.draw(g);

}

//画炮弹

for(inti=0;i

Shells=shells.get(i);

if(s.islive&&s.isgood){

s.hitTanks(enemys);

}elseif(s.islive&&!

s.isgood){

s.hitTank(t);

}

s.draw(g);

}

}

//双缓冲,防止游戏地图“频闪”

publicvoidupdate(Graphicsg){

paint(g);

if(bufferImage==null){

bufferImage=this.createImage(MAPWIDTH,MAPHEIGHT);

}

Graphicsgbuffer=bufferImage.getGraphics();

Colorc=gbuffer.getColor();

gbuffer.setColor(Color.GREEN);

gbuffer.fillRect(0,0,MAPWIDTH,MAPHEIGHT);

gbuffer.setColor(Color.RED);

gbuffer.drawString("当前炮弹数目:

"+shells.size(),20,40);

gbuffer.drawString("生命值:

",20,60);

gbuffer.fillRect(65,55,t.getLife(),5);

gbuffer.setColor(c);

paint(gbuffer);

g.drawImage(bufferImage,0,0,null);

}

//内部类,主要通过调用repaint方法,实现绘图功能

classPaintThreadimplementsRunnable{

@Override

publicvoidrun(){

while(true){

repaint();

try{

Thread.sleep(260);

}catch(InterruptedExceptione){

e.printStackTrace();

}

}

}

}

//内部类,键盘监听器

classDirectionHandlerextendsKeyAdapter{

@Override

publicvoidkeyPressed(KeyEventarg0){

t.checkDirection(arg0);

}

}

二.绘制坦克功能:

publicclassTank{

//坦克的尺寸和移动速度

publicstaticfinalintWIDTH=50,HEIGHT=50,xspeed=5,yspeed=5;

//坦克的初始位置

publicintx=400,y=300;

//坦克的初始化方向,STOP即停止,Direction是自定义的枚举常量,见枚举源代码

publicDirectiondirection=Direction.STOP;

//地图

TankMaptm;

//坦克是否存活

booleanisLive=true;

//坦克属于哪一方,true为用户方,false为敌方

booleanisgood=true;

//坦克初始化生命值

intlife=100;

//定义坦克的初始化方向,以便插图坦克的图片

publicintzhuanxiang=1;

//获取和设置存活状况

publicbooleanisLive(){

returnisLive;

}

publicvoidsetLive(booleanisLive){

this.isLive=isLive;

}

//获取和设置生命值

publicintgetLife(){

returnlife;

}

publicvoidsetLife(intlife){

this.life=life;

}

//构造方法

publicTank(){};

publicTank(TankMapt){

tm=t;

}

//绘制坦克

//根据坦克的移动方向,插入不同的坦克的图片

publicvoiddraw(Graphicsg){

if(zhuanxiang==1)

{

ImageIconicon=newImageIcon("Etank_shang.gif");

ImagetankImage=icon.getImage();

g.drawImage(tankImage,x,y,null);

}

elseif(zhuanxiang==-2)

{

ImageIconicon=newImageIcon("Etank_you.gif");

ImagetankImage=icon.getImage();

g.drawImage(tankImage,x,y,null);

}

elseif(zhuanxiang==2)

{

ImageIconicon=newImageIcon("Etank_zuo.gif");

ImagetankImage=icon.getImage();

g.drawImage(tankImage,x,y,null);

}

else

{

ImageIconicon=newImageIcon("Etank_xia.gif");

ImagetankImage=icon.getImage();

g.drawImage(tankImage,x,y,null);

}

/*Colorc=g.getColor();

g.setColor(Color.RED);

g.fillOval(x,y,WIDTH,HEIGHT);

g.setColor(c);*/

move(direction);

}

//根据方向,移动坦克,不允许坦克移出地图尺寸

publicvoidmove(Directiond){

if(d==Direction.STOP){

}elseif(d==Direction.UP){

zhuanxiang=1;

y-=yspeed;

if(y<=20)

y=20;

}elseif(d==Direction.RIGHT){

zhuanxiang=-2;

x+=xspeed;

if(x>=800-WIDTH)

x=800-WIDTH;

}elseif(d==Direction.DOWN){

zhuanxiang=-1;

y+=yspeed;

if(y>=600-HEIGHT)

y=600-HEIGHT;

}elseif(d==Direction.LEFT){

zhuanxiang=2;

x-=xspeed;

if(x<=0)

x=0;

}

}

//确定用户单机的方向键,修正坦克即将移动的方向

publicvoidcheckDirection(KeyEventk){

if(k.getKeyCode()==KeyEvent.VK_UP){

direction=Direction.UP;

}elseif(k.getKeyCode()==KeyEvent.VK_RIGHT){

direction=Direction.RIGHT;

}elseif(k.getKeyCode()==KeyEvent.VK_DOWN){

direction=Direction.DOWN;

}elseif(k.getKeyCode()==KeyEvent.VK_LEFT){

direction=Direction.LEFT;

}

if(k.getKeyCode()==KeyEvent.VK_F){

fire();

}

}

//发射炮弹的方法

publicvoidfire(){

}

//返回坦克当前位置矩形,便于判断是否与敌方坦克或炮弹重叠

publicRectanglegetRec(){

returnnewRectangle(this.x,this.y,this.WIDTH,this.HEIGHT);

}

}

三.敌方坦克的绘制方法

publicclassEnemyTankextendsTank{

//初始化敌方坦克的属性

publicstaticfinalintWIDTH=50,HEIGHT=50,xspeed=5,yspeed=5;

publicintx=400,y=300;

publicDirectiondirection=Direction.DOWN;

publicbooleanislive=true;

Colorcolor=Color.YELLOW;

TankMaptm;

//随机数产生器,供敌方坦克随机产生前进方向使用

Randomr=newRandom();

publicintzhuanxiang;

//方向更改限制,避免敌方坦克每移动一次就更改方向

intrandomCount=r.nextInt(10)+5;

//构造方法

publicEnemyTank(){}

publicEnemyTank(intwx,intwy,Colorc){

x=wx;

y=wy;

color=c;

}

@Override

publicvoiddraw(Graphicsg){

//根据敌方坦克的移动方向插入不同的坦克图片

if(zhuanxiang==1)

{

ImageIconicon=newImageIcon("Etank_shang.gif");

ImagetankImage=icon.getImage();

g.drawImage(tankImage,x,y,null);

}

elseif(zhuanxiang==-2)

{

ImageIconicon=newImageIcon("Etank_you.gif");

ImagetankImage=icon.getImage();

g.drawImage(tankImage,x,y,null);

}

elseif(zhuanxiang==2)

{

ImageIconicon=newImageIcon("Etank_zuo.gif");

ImagetankImage=icon.getImage();

g.drawImage(tankImage,x,y,null);

}

else

{

ImageIconicon=newImageIcon("Etank_xia.gif");

ImagetankImage=icon.getImage();

g.drawImage(tankImage,x,y,null);

}

move();

}

publicvoidmove(){

//当方向更改限制为0时,产生一个随机方向

if(randomCount==0){

Directiondirs[]=Direction.values();

direction=dirs[r.nextInt(dirs.length-1)];

randomCount=r.nextInt(10)+5;

}

//方向更改限制减1

randomCount--;

//随机发射炮弹,当产生的随机数大于35时,发射炮弹

if(r.nextInt(40)>36){

fire();

}

//确定敌方的坦克的移动

if(direction==Direction.UP){

y-=yspeed;

zhuanxiang=1;

if(y<=HEIGHT)

y=HEIGHT;

}elseif(direction==Direction.RIGHT){

x+=xspeed;

zhuanxiang=-2;

if(x>=800-WIDTH)

x=800-WIDTH;

}elseif(direction==Direction.DOWN){

y+=yspeed;

zhuanxiang=-1;

if(y>=600-HEIGHT)

y=600-HEIGHT;

}elseif(direction==Direction.LEFT){

x-=xspeed;

zhuanxiang=2;

if(x<=0)

x=0;

}

}

@Override

publicvoidfire(){

Shell(this.x+WIDTH/2,this.y+HEIGHT/2,this.direction,tm,Color.BLUE,false));

}

publicRectanglegetRec(){

returnnewRectangle(this.x,this.y,this.WIDTH,this.HEIGHT);

}

}

四.炮弹绘制方法

publicclassShell{

//初始化炮弹的基本属性

publicfinalintWIDTH=5,HEIGHT=5,xspeed=14,yspeed=14;

publicintx,y;

Directiondir=Direction.STOP;

publicbooleanislive=true;

TankMaptm=null;

Colorcolor=Color.RED;

booleanisgood=true;

booleanisLive=true;

//构造方法

publicShell(){}

publicShell(intxd,intyd,Directiond,TankMapt){

x=xd;

y=yd;

dir=d;

tm=t;

}

publicShell(intxd,intyd,Directiond,TankMapt,Colorc,booleang){

x=xd;

y=yd;

dir=d;

tm=t;

color=c;

isgood=g;

}

//绘制炮弹

publicvoiddraw(Graphicsg){

if(islive){

Colorc=g.getColor();

g.setColor(color);

g.fillOval(x,y,WIDTH,HEIGHT);

g.setColor(c);

move(dir);

}else{

}

}

//炮弹的移动,如果炮弹移动出了地图尺寸,则设定炮弹死亡

publicvoidmove(Directiond){

if(d==Direction.UP){

y-=yspeed;

if(y<=0)islive=false;

}elseif(d==Direction.RIGHT){

x+=xspeed;

if(x>=800)islive=false;

}elseif(d==Direction.DOWN){

y+=yspeed;

if(y>=600)islive=false;

}elseif(d==Direction.LEFT){

x-=xspeed;

if(x<=0)islive=false;

}

}

publicRectanglegetRec(){

returnnewRectangle(this.x,this.y,this.WIDTH,this.HEIGHT);

}

//打击坦克方法

publicbooleanhitTank(Tankt){

if(this.isLive&&t.isLive&&this.getRec().intersects(t.getRec())){

//用户坦克生命值减20

t.setLife(t.getLife()-20);

//如果用户坦克生命值《=0,游戏结束

if(t.getLife()<=0){

t.setLive(false);

t.direction=Direction.STOP;

JOptionPane.showMessageDialog(tm,"GameOver!

");

System.exit(0);

}

//炮弹死亡

this.isLive=false;

//产生爆炸

Explore=newExplor(x-3,y-3,this.tm);

returntrue;

}

returnfalse;

}

//用户坦克打击敌方坦克的方法

publicboo

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 工程科技 > 能源化工

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1