二、完善周边工具类(图象、gameobject、font)
虽然我们有了midp2.0的支持,但是有时还是需要一些辅助工具,方便我们使用。这怕是在进行真正的游戏设计之前最有趣的了。
1、首先是一个imagetools工具类,提供一个方法帮助调用image
| public class imagetools { protected imagetools() {} public static image getimage(string str){ image img=null; try { img = image.createimage(str); } catch (exception ex) { system.out.println(ex); } finally{ return img; } } } |
| public class gameobject { public sprite sprite;//内置的sprite public boolean alive;//存活标记 private int lifecount=0;//生命周期计数器 public int lifetime=0;//生命周期,以桢为单位 public int speed=0;//动画桢更新速度,(0至无穷,0代表每一桢跟新一个画面) private int animcount=0;// /动画桢更新计数器 public gameobject(image img,int width,int height){ sprite=new sprite(img,width,height); reset(); } public void move(int dx,int dy){//相对移动 sprite.move(dx,dy); } public void moveto(int x,int y){//绝对移动 sprite.setposition(x,y); } public void update(){//更新状态,动画桢更新,生命周期更新 if(!alive) return; if(++animcount>speed){ animcount=0; sprite.nextframe(); if(lifetime!=0 && ++lifecount>lifetime) alive=false; } } public void paint(graphics g){//paint if(!alive) return; sprite.paint(g); } public void reset(){//复位 alive=true; lifecount=0; animcount=0; sprite.setframe(0); } } |
| public class font { sprite sprite; //sprite int width,height; //每个char的尺寸 int[] charhash; //储存1-127个常见字符在sprite的frameseq中的位置 graphics g; public font(graphics g,image img, int width, int height, char[] chars) { this.g=g; sprite=new sprite(img,width,height); this.width=width; this.height=height; charhash=new int[128]; for (int i = 0; i < charhash.length; i++) { charhash[i]=-1;//没有代表此字符的图片 } character c; for (int i = 0; i < chars.length; i++) { c=new character(chars[i]); charhash[c.hashcode()]=i; } } public void drawchar(char ch, int x, int y){ character c=new character(ch); int hashcode=c.hashcode(); sprite.setposition(x,y); if(hashcode>=0){ sprite.setframe(charhash[hashcode]); sprite.paint(g); } } public void drawstring(string str, int x, int y){ int length=str.length(); for (int i = 0; i < length; i++) { drawchar(str.charat(i),x+width*i,y); } } } |
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器