J2ME 2D小游戏入门之旅Word格式文档下载.docx
- 文档编号:16314353
- 上传时间:2022-11-22
- 格式:DOCX
- 页数:30
- 大小:56.12KB
J2ME 2D小游戏入门之旅Word格式文档下载.docx
《J2ME 2D小游戏入门之旅Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《J2ME 2D小游戏入门之旅Word格式文档下载.docx(30页珍藏版)》请在冰豆网上搜索。
二、完善周边工具类(图象、GameObject、Font)
三、控制飞机的移动
四、加入子弹群,实现碰撞运算
五、实现爆炸效果、并加入道具导弹
六、不足多多,你认为呢?
七、源码
我们的游戏需要一个通用的游戏框架,也方便以后的开发,但实现一个引擎是复杂的。
作为初学者如果要你考虑太多的问题,恐怕会让你偏离主线,这里只给出canvas的代码,不理解可以参看本站的另外一篇系列文章《使用MIDP2.0开发游戏》。
publicclassMyGameCanvasextendsGameCanvas
implementsRunnable,CommandListener{
privatestaticMyGameCanvasinstance;
Graphicsg;
booleanrunning;
Threadt;
Commandstartcmd,exitcmd,restartcmd;
intkeystate;
booleankeyevent;
booleankey_up,key_down,key_left,key_right,key_fire;
privatebooleanallowinput;
publicintscreenwidth;
publicintscreenheight;
booleangameover;
//defineyourvariablehere
//defineyourvariableend
protectedMyGameCanvas(){
super(true);
g=getGraphics();
running=false;
t=null;
addCommand(startcmd=newCommand("
start"
Command.OK,1));
addCommand(exitcmd=newCommand("
exit"
Command.EXIT,1));
setCommandListener(this);
screenwidth=getWidth();
screenheight=getHeight();
//putyourinitoncecodehere
//putyourinitoncecodeend
}
synchronizedpublicstaticMyGameCanvasgetInstance(){
if(instance==null){
instance=newMyGameCanvas();
System.out.println("
newMyGameCanvas"
);
returninstance;
publicvoidrun(){
MyGameCanvasrunstart"
longst=0,et=0,diff=0;
intrate=50;
//16-17framepersecond
while(running){
st=System.currentTimeMillis();
gameinput();
gameMain();
et=System.currentTimeMillis();
diff=et-st;
if(diff<
rate){
//System.out.println("
Sleep"
+(rate-diff));
try{
Thread.sleep(rate-diff);
catch(InterruptedExceptionex){}
}else{
rush,andtheframeusingtime:
"
+diff);
MyGameCanvasrunend"
publicvoidstart(){
if(!
running){
running=true;
t=newThread(this);
t.start();
privatevoidgameMain(){
g.setColor(0,0,0);
//clearscreen
g.fillRect(0,0,getWidth(),getHeight());
flushGraphics();
privatevoidgameInit(){
gameover=false;
allowinput=true;
key_up=key_down=key_left=key_right=key_fire=false;
publicvoidstop(){
if(running){
running=false;
publicvoidcommandAction(Commandc,Displayabled){
Stringcmdstr=c.getLabel();
if(cmdstr.equals("
)){
gameInit();
start();
removeCommand(startcmd);
addCommand(restartcmd=newCommand("
restart"
}elseif(cmdstr.equals("
stop();
while(t.isAlive());
Navigate.midlet.destroyApp(false);
Navigate.midlet.notifyDestroyed();
privatevoidgameinput(){
if(allowinput){
keystate=getKeyStates();
keyevent=false;
if((keystate&
UP_PRESSED)!
=0){//up
key_up=true;
keyevent=true;
//dealyourunstopjobcodehere
uppress"
//dealyourunstopjobcodeend
}elseif((keystate&
UP_PRESSED)==0){//releasekey
if(key_up==true){
key_up=false;
//dealyouronepress-onejobcodehere
uprelease"
//dealyouronepress-onejobcodeend
DOWN_PRESSED)!
=0){//down
key_down=true;
downpress"
DOWN_PRESSED)==0){//releasekey
if(key_down==true){
key_down=false;
downrelease"
LEFT_PRESSED)!
=0){//left
key_left=true;
leftpress"
LEFT_PRESSED)==0){//releasekey
if(key_left==true){
key_left=false;
leftrelease"
RIGHT_PRESSED)!
=0){//right
key_right=true;
//System.out.println("
rightpress"
RIGHT_PRESSED)==0){//releasekey
if(key_right==true){
key_right=false;
rightrelease"
FIRE_PRESSED)!
=0){//fire
key_fire=true;
firepress"
FIRE_PRESSED)==0){//releasekey
if(key_fire==true){
key_fire=false;
firerelease"
keyevent){
//nokeyeventhere
NOKEYpress"
//nokeyeventend
publicstaticvoidcleanJob(){
instance=null;
}
使用singlon实现,因为每个gamecanvas都需要很多的内存空间。
另外对我们来说,只要改写gameInit(),gameMain(),一次性初始化的代码写在构造函数中。
2D小游戏入门之旅
(二)完善周边工具类
28|作者:
155]
虽然我们有了midp2.0的支持,但是有时还是需要一些辅助工具,方便我们使用。
这怕是在进行真正的游戏设计之前最有趣的了。
1,首先是一个ImageTools工具类,提供一个方法帮助调用Image
publicclassImageTools{
protectedImageTools(){
publicstaticImagegetImage(Stringstr){
Imageimg=null;
img=Image.createImage(str);
catch(Exceptionex){
System.out.println(ex);
finally{
returnimg;
2.GameObject,提供一个通用的游戏对象。
有了Sprite类,为什么还要GameObject呢?
其实我们一般是将Sprite,看作成一个高级的Image,往往一个Sprite要被多个游戏对象调用,GameObject其实就是Sprite的状态类。
GameObject提供简单的生命周期概念,动画更新速度;
publicclassGameObject{
publicSpritesprite;
//内置的Sprite
publicbooleanalive;
//存活标记
privateintlifecount=0;
//生命周期计数器
publicintlifetime=0;
//生命周期,以桢为单位
publicintspeed=0;
//动画桢更新速度,(0至无穷,0代表每一桢跟新一个画面)
privateintanimcount=0;
///动画桢更新计数器
publicGameObject(Imageimg,intwidth,intheight){
sprite=newSprite(img,width,height);
reset();
publicvoidmove(intdx,intdy){//相对移动
sprite.move(dx,dy);
publicvoidmoveto(intx,inty){//绝对移动
sprite.setPosition(x,y);
publicvoidupdate(){//更新状态,动画桢更新,生命周期更新
alive)
return;
if(++animcount>
speed){
animcount=0;
sprite.nextFrame();
if(lifetime!
=0&
&
++lifecount>
lifetime)
alive=false;
publicvoidpaint(Graphicsg){//Paint
sprite.paint(g);
publicvoidreset(){//复位
alive=true;
lifecount=0;
sprite.setFrame(0);
3.封装字体类,你需要漂亮的字体吗?
我们经常需要用图片来输出文字,一个方便的字体类是必须的。
我们希望仅仅提供一个图片,一个图片所描述的字符的数组,来初始化一个字体类。
字体类提供一个类似Textout的方法,方便得在一个位置输出信息。
先封装一个简单的版本,只支持英文和数字,并且输出不能自动换行。
可能你有一个简单的思路,就是简单的保存字符数组,当打印的时候遍历数组,来查找每个字符在sprite的frameseq中的index,但当我们打印一个字符串的时候就会发现,太多的遍历操作耽误了宝贵时间,这里我们使用一个小技巧用容量换取速度,我们知道Character.hashCode()可以返回字符的ascii编码,常用字符将返回1-127;
利用这一点,我们开辟一个128的数组charhash,将输入的字符c所在图片index存入charhash[c.hashCode()]中。
以后也用这种映射方法来读取字符。
charhash的元素初值为-1,以后只要数值大于0就是有效值。
publicclassFont{
Spritesprite;
//Sprite
intwidth,height;
//每个char的尺寸
int[]charhash;
//储存1-127个常见字符在sprite的frameseq中的位置
publicFont(Graphicsg,Imageimg,intwidth,
intheight,char[]chars){
this.g=g;
this.width=width;
this.height=height;
charhash=newint[128];
for(inti=0;
i<
charhash.length;
i++){
charhash=-1;
//没有代表此字符的图片
Characterc;
chars.length;
c=newCharacter(chars);
charhash[c.hashCode()]=i;
publicvoiddrawChar(charch,intx,inty){
Characterc=newCharacter(ch);
inthashcode=c.hashCode();
if(hashcode>
=0){
sprite.setFrame(charhash[hashcode]);
publicvoiddrawString(Stringstr,intx,inty){
intlength=str.length();
length;
drawChar(str.charAt(i),x+width*i,y);
这样只要有一个实例font,就可以调用font.drawString(“hello”,0,0);
在0,0位置输出漂亮的图片字符串。
怎么样还挺好使的吧:
)
2D小游戏入门之旅(三)控制飞机移动
32|作者:
168]
实在是等不及了吧?
加把劲,让我们加入游戏的主角飞机吧。
你可以将这个游戏定位在动作游戏,一个动作游戏需要不断的从用户端获得输入,基本上不间断,所以plane需要不断的改变位置。
那么这个基于桢的游戏应该定在多少桢合适呢?
这需要在手机中测试,在模
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- J2ME 2D小游戏入门之旅 小游戏 入门