Fc坦克大战java_FC版BattleCity(坦克大战)的Java实现!!!!!(二) | 学步园

今天的成果 :

完成主程序框架搭建,完成主游戏类的框架搭建,完成数据操作线程的框架。

目前实现了开始选择菜单:1P、2P、编辑地图

选择关口frame,读取map文件,目前map文件的格式为txt格式,26×26的地图,另外有一行是关口的坦克信息。

map后期需要压缩,改变存储格式为二进制,压缩大小,统一用一个map文件来存储,自定义地图的时候关口显示好像是第一关,应该需要一个临时的地图存储数据结构…………这个以后再完善。

进入画出黑色战场,根据敌人坦克数量显示标记,显示游戏玩家的生命,战场中显示当前的level。

计算好了战场的大小和每块地面的大小。

明天:明天把图片做好,把map文件加载并根据map文件显示战场。截战场地面地图,一共是6种吧??老家gif、地面gif,爆炸gif(很多帧),炮弹gif,无敌状态gif,各种坦克gif,宝gif,gameover&pause的gif,应该就这么多了。

如果可能的话把编辑战场地图做完。

声音仍然是个大问题,不知道如何截取,应该是mid的,mid的音频占的空间小,速度快,不过也可以用录音机等录成wav格式。麻烦,这个最后弄。

目前有点不妥的地方:

程序一直在rapaint,对CPU占用有点大,不管是否需要repaint都一直调用,并且repaint是重画了整个画布。

应该可以优化成需要时在repaint,只是repaint需要重画的地方,先把需要repaint的地方clear,然后画上新的图片。然后转移显示指针。

程序结构有点不好…………等着重构吧!先实现了再说!

teeth_smile.gif

目前的源码和图片:

o_pic.JPG

MainFrame:

None.gifimportjava.awt.Color;

None.gifimportjava.awt.Graphics;

None.gifimportjava.awt.Graphics2D;

None.gifimportjava.awt.Image;

None.gifimportjava.awt.Toolkit;

None.gifimportjava.awt.event.KeyEvent;

None.gifimportjava.awt.event.KeyListener;

None.gifimportjava.awt.event.WindowAdapter;

None.gifimportjava.awt.event.WindowEvent;

None.gifimportjava.awt.image.BufferStrategy;

None.gifimportjava.io.BufferedReader;

None.gifimportjava.io.File;

None.gifimportjava.io.FileNotFoundException;

None.gifimportjava.io.FileReader;

None.gifimportjava.io.IOException;

None.gifimportjava.io.StringReader;

None.gif

None.gifimportjavax.swing.ImageIcon;

None.gifimportjavax.swing.JFrame;

None.gif

ExpandedBlockStart.gif

ContractedBlock.gif/** *//**6a9c071a08f1dae2d3e1c512000eef41.png *@authorWS

6a9c071a08f1dae2d3e1c512000eef41.png * @created at 2006-5-3 21:03:20

ExpandedBlockEnd.gif*/None.gif

ExpandedBlockStart.gif

ContractedBlock.gifpublicclassMainFrameextendsJFrameimplementsKeyListener...{

6a9c071a08f1dae2d3e1c512000eef41.png

ExpandedSubBlockStart.gif

ContractedSubBlock.gif/**//*开始从下网上番的大图片*/6a9c071a08f1dae2d3e1c512000eef41.pngprivateImage startImage;

6a9c071a08f1dae2d3e1c512000eef41.png

ExpandedSubBlockStart.gif

ContractedSubBlock.gif/**//*游戏坦克状态1*/6a9c071a08f1dae2d3e1c512000eef41.pngprivateImage tankImage1;

6a9c071a08f1dae2d3e1c512000eef41.png

ExpandedSubBlockStart.gif

ContractedSubBlock.gif/**//*游戏坦克状态2*/6a9c071a08f1dae2d3e1c512000eef41.pngprivateImage tankImage2;

6a9c071a08f1dae2d3e1c512000eef41.png

ExpandedSubBlockStart.gif

ContractedSubBlock.gif/**//*0-1*/6a9c071a08f1dae2d3e1c512000eef41.pngpublicinttankStatus=0;

6a9c071a08f1dae2d3e1c512000eef41.png

ExpandedSubBlockStart.gif

ContractedSubBlock.gif/**//*无敌状态的白框1*/6a9c071a08f1dae2d3e1c512000eef41.pngprivateImage super1Image;

6a9c071a08f1dae2d3e1c512000eef41.png

ExpandedSubBlockStart.gif

ContractedSubBlock.gif/**//*无敌状态的白框2*/6a9c071a08f1dae2d3e1c512000eef41.pngprivateImage super2Image;

6a9c071a08f1dae2d3e1c512000eef41.png

6a9c071a08f1dae2d3e1c512000eef41.pngprivateImage stageImage;

6a9c071a08f1dae2d3e1c512000eef41.png

6a9c071a08f1dae2d3e1c512000eef41.pngprivateImage tankLetImage;

6a9c071a08f1dae2d3e1c512000eef41.png

6a9c071a08f1dae2d3e1c512000eef41.pngprivateImage p1LifeImage;

6a9c071a08f1dae2d3e1c512000eef41.png

6a9c071a08f1dae2d3e1c512000eef41.pngprivateImage p2LifeImage;

6a9c071a08f1dae2d3e1c512000eef41.png

6a9c071a08f1dae2d3e1c512000eef41.pngprivateImage flagImage;

6a9c071a08f1dae2d3e1c512000eef41.png

6a9c071a08f1dae2d3e1c512000eef41.pngprivateImage num[]=newImage[10];

6a9c071a08f1dae2d3e1c512000eef41.png

6a9c071a08f1dae2d3e1c512000eef41.pngprivateinttankX=140;

6a9c071a08f1dae2d3e1c512000eef41.png

6a9c071a08f1dae2d3e1c512000eef41.pngprivateinttankY=265;

6a9c071a08f1dae2d3e1c512000eef41.png

ExpandedSubBlockStart.gif

ContractedSubBlock.gif/**//*Frames状态机 0-开始 1-选择 2-幕 3-开始显示level 4-游戏中 5-Win 6-地图编辑 ……*/6a9c071a08f1dae2d3e1c512000eef41.pngpublicintframes=0;

6a9c071a08f1dae2d3e1c512000eef41.png

6a9c071a08f1dae2d3e1c512000eef41.pngpublicbooleangameRunning=true;

6a9c071a08f1dae2d3e1c512000eef41.png

6a9c071a08f1dae2d3e1c512000eef41.pngpublicintstartY=466;

6a9c071a08f1dae2d3e1c512000eef41.png

ExpandedSubBlockStart.gif

ContractedSubBlock.gif/**//*0-单人游戏 1-双人游戏 3-地图编辑*/6a9c071a08f1dae2d3e1c512000eef41.pngpublicintselectGame=0;

6a9c071a08f1dae2d3e1c512000eef41.png

6a9c071a08f1dae2d3e1c512000eef41.pngprivateintp1Life=2;

6a9c071a08f1dae2d3e1c512000eef41.png

6a9c071a08f1dae2d3e1c512000eef41.pngprivateintp2Life=2;

6a9c071a08f1dae2d3e1c512000eef41.png

ExpandedSubBlockStart.gif

ContractedSubBlock.gif/**//*1-n*/6a9c071a08f1dae2d3e1c512000eef41.pngpublicintlevel=1;

6a9c071a08f1dae2d3e1c512000eef41.png

ExpandedSubBlockStart.gif

ContractedSubBlock.gif/**//*刷新频率 20毫秒*/6a9c071a08f1dae2d3e1c512000eef41.pngprivatelongfresh=20;

6a9c071a08f1dae2d3e1c512000eef41.png

6a9c071a08f1dae2d3e1c512000eef41.pngpublicintmask=0;

6a9c071a08f1dae2d3e1c512000eef41.png

6a9c071a08f1dae2d3e1c512000eef41.pngpublicintleftTank=20;

6a9c071a08f1dae2d3e1c512000eef41.png

6a9c071a08f1dae2d3e1c512000eef41.pngpubliccharmap[][]=newchar[27][26];

6a9c071a08f1dae2d3e1c512000eef41.png

ExpandedSubBlockStart.gif

ContractedSubBlock.gifpublicMainFrame(String title)...{

6a9c071a08f1dae2d3e1c512000eef41.pngsuper(title);

6a9c071a08f1dae2d3e1c512000eef41.png        loadImages();

6a9c071a08f1dae2d3e1c512000eef41.png        initFrame();

6a9c071a08f1dae2d3e1c512000eef41.png        Thread engine=newOperatorThread(this);

6a9c071a08f1dae2d3e1c512000eef41.png        engine.start();

ExpandedSubBlockStart.gif

ContractedSubBlock.gifthis.addWindowListener(newWindowAdapter()...{

ExpandedSubBlockStart.gif

ContractedSubBlock.gifpublicvoidwindowClosing(WindowEvent e)...{

6a9c071a08f1dae2d3e1c512000eef41.png                System.exit(0);

ExpandedSubBlockEnd.gif            }ExpandedSubBlockEnd.gif        });

ExpandedSubBlockEnd.gif    }6a9c071a08f1dae2d3e1c512000eef41.png

ExpandedSubBlockStart.gif

ContractedSubBlock.gifprivatevoidinitFrame()...{

6a9c071a08f1dae2d3e1c512000eef41.png        setSize(520,486);

6a9c071a08f1dae2d3e1c512000eef41.png        setVisible(true);

6a9c071a08f1dae2d3e1c512000eef41.png        setBackground(Color.BLACK);

6a9c071a08f1dae2d3e1c512000eef41.png        createBufferStrategy(2);

6a9c071a08f1dae2d3e1c512000eef41.png        addKeyListener(this);

ExpandedSubBlockEnd.gif    }6a9c071a08f1dae2d3e1c512000eef41.png

ExpandedSubBlockStart.gif

ContractedSubBlock.gifprivatevoidloadImages()...{

6a9c071a08f1dae2d3e1c512000eef41.png        startImage=loadImage("D:/eclipse/workspace/Tank/bin/start.gif");

6a9c071a08f1dae2d3e1c512000eef41.png        tankImage1=loadImage("D:/eclipse/workspace/Tank/bin/tank1.gif");

6a9c071a08f1dae2d3e1c512000eef41.png        tankImage2=loadImage("D:/eclipse/workspace/Tank/bin/tank2.gif");

6a9c071a08f1dae2d3e1c512000eef41.png        stageImage=loadImage("D:/eclipse/workspace/Tank/bin/stage.gif");

6a9c071a08f1dae2d3e1c512000eef41.png        tankLetImage=loadImage("D:/eclipse/workspace/Tank/bin/tankLet.gif");

6a9c071a08f1dae2d3e1c512000eef41.png        p1LifeImage=loadImage("D:/eclipse/workspace/Tank/bin/1plife.gif");

6a9c071a08f1dae2d3e1c512000eef41.png        p2LifeImage=loadImage("D:/eclipse/workspace/Tank/bin/2plife.gif");

6a9c071a08f1dae2d3e1c512000eef41.png        flagImage=loadImage("D:/eclipse/workspace/Tank/bin/flag.gif");

ExpandedSubBlockStart.gif

ContractedSubBlock.giffor(inti=0; i<10; i++)...{

6a9c071a08f1dae2d3e1c512000eef41.png            num[i]=loadImage("D:/eclipse/workspace/Tank/bin/"+i+".gif");

ExpandedSubBlockEnd.gif        }ExpandedSubBlockStart.gif

ContractedSubBlock.gifif(startImage==null)...{

6a9c071a08f1dae2d3e1c512000eef41.png            System.out.println("Null");

ExpandedSubBlockEnd.gif        }ExpandedSubBlockEnd.gif    }6a9c071a08f1dae2d3e1c512000eef41.png

ExpandedSubBlockStart.gif

ContractedSubBlock.gifprivateImage loadImage(String fileName)...{

6a9c071a08f1dae2d3e1c512000eef41.pngreturnnewImageIcon(fileName).getImage();

ExpandedSubBlockEnd.gif    }6a9c071a08f1dae2d3e1c512000eef41.png

ExpandedSubBlockStart.gif

ContractedSubBlock.gifpublicGraphics2D getDoubleGraphics()...{

6a9c071a08f1dae2d3e1c512000eef41.png        BufferStrategy strategy=this.getBufferStrategy();

6a9c071a08f1dae2d3e1c512000eef41.pngreturn(Graphics2D) strategy.getDrawGraphics();

ExpandedSubBlockEnd.gif    }6a9c071a08f1dae2d3e1c512000eef41.png

ExpandedSubBlockStart.gif

ContractedSubBlock.gifprivatevoidupdate()...{

6a9c071a08f1dae2d3e1c512000eef41.png        BufferStrategy strategy=this.getBufferStrategy();

ExpandedSubBlockStart.gif

ContractedSubBlock.gifif(!strategy.contentsLost())...{

6a9c071a08f1dae2d3e1c512000eef41.png            strategy.show();

ExpandedSubBlockEnd.gif        }6a9c071a08f1dae2d3e1c512000eef41.png        Toolkit.getDefaultToolkit().sync();

ExpandedSubBlockEnd.gif    }6a9c071a08f1dae2d3e1c512000eef41.png

6a9c071a08f1dae2d3e1c512000eef41.png//0-开始 1-选择 2-开始显示level 3-游戏中 4-Win 5-地图编辑ExpandedSubBlockStart.gif

ContractedSubBlock.gifpublicvoidStart()throwsException...{

6a9c071a08f1dae2d3e1c512000eef41.pnglongstartTime;

6a9c071a08f1dae2d3e1c512000eef41.pnglongtmp=0;

ExpandedSubBlockStart.gif

ContractedSubBlock.gifwhile(gameRunning)...{

6a9c071a08f1dae2d3e1c512000eef41.png            startTime=System.currentTimeMillis();

6a9c071a08f1dae2d3e1c512000eef41.png            repaint();

6a9c071a08f1dae2d3e1c512000eef41.png            tmp=System.currentTimeMillis()-startTime;

6a9c071a08f1dae2d3e1c512000eef41.png//System.out.println(fresh - tmp);6a9c071a08f1dae2d3e1c512000eef41.pngThread.sleep((fresh-tmp)<0?0: fresh-tmp);

ExpandedSubBlockEnd.gif        }ExpandedSubBlockEnd.gif    }6a9c071a08f1dae2d3e1c512000eef41.png

6a9c071a08f1dae2d3e1c512000eef41.pngprivatevoiddrawTank(Graphics2D g2,intdirect,intposX,intposY,

678231.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值