今天的成果 :
完成主程序框架搭建,完成主游戏类的框架搭建,完成数据操作线程的框架。
目前实现了开始选择菜单: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,然后画上新的图片。然后转移显示指针。
程序结构有点不好…………等着重构吧!先实现了再说!
目前的源码和图片:
MainFrame:
importjava.awt.Color;
importjava.awt.Graphics;
importjava.awt.Graphics2D;
importjava.awt.Image;
importjava.awt.Toolkit;
importjava.awt.event.KeyEvent;
importjava.awt.event.KeyListener;
importjava.awt.event.WindowAdapter;
importjava.awt.event.WindowEvent;
importjava.awt.image.BufferStrategy;
importjava.io.BufferedReader;
importjava.io.File;
importjava.io.FileNotFoundException;
importjava.io.FileReader;
importjava.io.IOException;
importjava.io.StringReader;
importjavax.swing.ImageIcon;
importjavax.swing.JFrame;
/** *//** *@authorWS
* @created at 2006-5-3 21:03:20
*/
publicclassMainFrameextendsJFrameimplementsKeyListener...{
/**//*开始从下网上番的大图片*/privateImage startImage;
/**//*游戏坦克状态1*/privateImage tankImage1;
/**//*游戏坦克状态2*/privateImage tankImage2;
/**//*0-1*/publicinttankStatus=0;
/**//*无敌状态的白框1*/privateImage super1Image;
/**//*无敌状态的白框2*/privateImage super2Image;
privateImage stageImage;
privateImage tankLetImage;
privateImage p1LifeImage;
privateImage p2LifeImage;
privateImage flagImage;
privateImage num[]=newImage[10];
privateinttankX=140;
privateinttankY=265;
/**//*Frames状态机 0-开始 1-选择 2-幕 3-开始显示level 4-游戏中 5-Win 6-地图编辑 ……*/publicintframes=0;
publicbooleangameRunning=true;
publicintstartY=466;
/**//*0-单人游戏 1-双人游戏 3-地图编辑*/publicintselectGame=0;
privateintp1Life=2;
privateintp2Life=2;
/**//*1-n*/publicintlevel=1;
/**//*刷新频率 20毫秒*/privatelongfresh=20;
publicintmask=0;
publicintleftTank=20;
publiccharmap[][]=newchar[27][26];
publicMainFrame(String title)...{
super(title);
loadImages();
initFrame();
Thread engine=newOperatorThread(this);
engine.start();
this.addWindowListener(newWindowAdapter()...{
publicvoidwindowClosing(WindowEvent e)...{
System.exit(0);
} });
}
privatevoidinitFrame()...{
setSize(520,486);
setVisible(true);
setBackground(Color.BLACK);
createBufferStrategy(2);
addKeyListener(this);
}
privatevoidloadImages()...{
startImage=loadImage("D:/eclipse/workspace/Tank/bin/start.gif");
tankImage1=loadImage("D:/eclipse/workspace/Tank/bin/tank1.gif");
tankImage2=loadImage("D:/eclipse/workspace/Tank/bin/tank2.gif");
stageImage=loadImage("D:/eclipse/workspace/Tank/bin/stage.gif");
tankLetImage=loadImage("D:/eclipse/workspace/Tank/bin/tankLet.gif");
p1LifeImage=loadImage("D:/eclipse/workspace/Tank/bin/1plife.gif");
p2LifeImage=loadImage("D:/eclipse/workspace/Tank/bin/2plife.gif");
flagImage=loadImage("D:/eclipse/workspace/Tank/bin/flag.gif");
for(inti=0; i<10; i++)...{
num[i]=loadImage("D:/eclipse/workspace/Tank/bin/"+i+".gif");
}
if(startImage==null)...{
System.out.println("Null");
} }
privateImage loadImage(String fileName)...{
returnnewImageIcon(fileName).getImage();
}
publicGraphics2D getDoubleGraphics()...{
BufferStrategy strategy=this.getBufferStrategy();
return(Graphics2D) strategy.getDrawGraphics();
}
privatevoidupdate()...{
BufferStrategy strategy=this.getBufferStrategy();
if(!strategy.contentsLost())...{
strategy.show();
} Toolkit.getDefaultToolkit().sync();
}
//0-开始 1-选择 2-开始显示level 3-游戏中 4-Win 5-地图编辑
publicvoidStart()throwsException...{
longstartTime;
longtmp=0;
while(gameRunning)...{
startTime=System.currentTimeMillis();
repaint();
tmp=System.currentTimeMillis()-startTime;
//System.out.println(fresh - tmp);Thread.sleep((fresh-tmp)<0?0: fresh-tmp);
} }
privatevoiddrawTank(Graphics2D g2,intdirect,intposX,intposY,