游戏入门之一 雷电 精灵模型
????? 设计游戏我个人认为一个好的角色操作将事半工倍。所以我现在把雷电的所有角色抽象成一个Fairy。里面有实现绘制的方法以及移动,碰撞算法。
????? 在这里我强调下,我的碰撞算法是简单的实现。就是一个物体为参照物体。在10像素范围内x,y轴如果发现另外一个物体侵入则判断为true,发之为false
java 代码?
- ???
- package?org.wuhua.game.model;??
- ??
- import?javax.microedition.lcdui.Graphics;??
- import?javax.microedition.lcdui.Image;??
- ??
- import?org.wuhua.game.util.Log;??
- ???
- /**?
- ?*?类名:Sprite.java?
?? - ?*?编写日期:?2006-11-29?
? - ?*?程序功能描述:建立精灵物体模型?
? - ?*?Demo:?
? - ?*?Bug:?
? - ?*??
- ?*?程序变更日期?:
?? - ?*?变更作者?:
?? - ?*?变更说明?:
? - ?*??
- ?*?@author?wuhua?
?? - ?*/??
- public?class?Fairy?{??
- ????static?Log?log?=?Log.getLog("Fairy");??
- ????/**?
- ?????*?position?of?Fairy?in?x?offset??
- ?????*/??
- ????int?x;?//?=?0;??
- ??
- ????/**?
- ?????*?position?of?Fairy?in?y?offset??
- ?????*/??
- ????int?y;?//?=?0;??
- ??
- ????/**?
- ?????*?width?of?layer??
- ?????*/??
- ????int?width;?//?=?0;??
- ??
- ????/**?
- ?????*?height?of?layer?
- ?????*/??
- ????int?height;?//?=?0;??
- ??
- ????/**??
- ?????*?If?the?Layer?is?visible?it?will?be?drawn?when?
paint? - ?????*?is?called.?
- ?????*/??
- ????boolean?visible?=?true;??
- ??????
- ????/**?
- ?????*?图片资源?
- ?????*???
- ?????*/??
- ??????
- ????Image?fairy;???
- ??????
- ????public?Fairy(Image?fairy,int?x,?int?y){??
- ????????this.fairy?=?fairy;??
- ????????this.x?=?x;??
- ????????this.y?=?y;??
- ????}??
- ??????
- ????public?void?setPosition(int?x,?int?y)?{??
- ????????this.x?=?x;??
- ????????this.y?=?y;??
- ????}??
- ??????
- ????public?void?move(int?dx,?int?dy)?{????
- ???????
- ????????x?+=?dx;??
- ????????y?+=?dy;??
- ????}??
- ??????
- ????public?void?setVisible(boolean?visible)?{??
- ????????this.visible?=?visible;??
- ????}??
- ??
- ?????
- ????public?final?boolean?isVisible()?{??
- ????????return?visible;??
- ????}??
- ??
- ????public?final?int?getHeight()?{??
- ????????return?height;??
- ????}??
- ??
- ????public?final?int?getWidth()?{??
- ????????return?width;??
- ????}??
- ??
- ????public?final?int?getX()?{??
- ????????return?x;??
- ????}??
- ??
- ????public?final?int?getY()?{??
- ????????return?y;??
- ????}??
- ??????
- ????public?void?paint(Graphics?g){??
- ????????if?(g?==?null)?{??
- ????????????throw?new?NullPointerException("Graphics?不存在");??
- ????????}??
- ????????if(this.visible){??
- ????????????//log.debug("x="?+?x?+?"?y="?+?y);???
- ????????????g.drawImage(fairy,?x,?y,??Graphics.TOP?|?Graphics.HCENTER);??
- ????????}??
- ????}??
- ??
- ????/**?
- ?????*?进行简单的碰撞算法,?希望高手可以给个建议。?
- ?????*?@param?f?
- ?????*?@return?
- ?????*/??
- ????public?final?boolean?collidesWith(Fairy?f){??
- ??????????
- ???????
- ????????if((f.getX()?>=?this.getX()?-?20?&&?f.getX()?<=?this.getX()?+?20)??
- ????????????????&&??(f.getY()?>=?this.getY()?-?10??&&?f.getY()?<=?this.getY()+10?)){??
- ????????????//log.debug("this.getY="?+?this.getY());??
- ????????????//log.debug("f.getY="?+?f.getY());??
- ???????????????
- ???????????????
- ????????????return?true;??
- ????????}??
- ??????????????
- ????????return?false;??
- ????}??
- ???
- ??
- }??