java8.docx

上传人:b****2 文档编号:2604916 上传时间:2023-05-04 格式:DOCX 页数:27 大小:27.71KB
下载 相关 举报
java8.docx_第1页
第1页 / 共27页
java8.docx_第2页
第2页 / 共27页
java8.docx_第3页
第3页 / 共27页
java8.docx_第4页
第4页 / 共27页
java8.docx_第5页
第5页 / 共27页
java8.docx_第6页
第6页 / 共27页
java8.docx_第7页
第7页 / 共27页
java8.docx_第8页
第8页 / 共27页
java8.docx_第9页
第9页 / 共27页
java8.docx_第10页
第10页 / 共27页
java8.docx_第11页
第11页 / 共27页
java8.docx_第12页
第12页 / 共27页
java8.docx_第13页
第13页 / 共27页
java8.docx_第14页
第14页 / 共27页
java8.docx_第15页
第15页 / 共27页
java8.docx_第16页
第16页 / 共27页
java8.docx_第17页
第17页 / 共27页
java8.docx_第18页
第18页 / 共27页
java8.docx_第19页
第19页 / 共27页
java8.docx_第20页
第20页 / 共27页
亲,该文档总共27页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

java8.docx

《java8.docx》由会员分享,可在线阅读,更多相关《java8.docx(27页珍藏版)》请在冰点文库上搜索。

java8.docx

java8

实验八实验改进

1.完成打飞机游戏的策划书.

2.根据策划书和实验5,完善本游戏。

1).增加游戏开始画面.

2).使得玩家可以具有4次游戏机会,游戏失败后可以选择继续游戏或者退出。

3).可以设置玩家飞机的生命长度,可以设置一个生命进度条,比如中弹三次之后才会牺牲。

4).使得游戏背景可以移动(可参考MM冒险游戏里的摄像机跟随方法)

5).可加控制玩家和敌机发射子弹的速度和方向。

6).总结出开发过程中对结构上的控制、变量的使用、算法的优化(参考第13章手机游戏的优化)

将对实验内容完善的功能作出一个总结报告。

3.实验代码

BulletSprite

importjavax.microedition.lcdui.Image;

importjavax.microedition.lcdui.game.Sprite;

publicclassBulletSpriteextendsSprite{

privateintm_nSpeedX=0;//x轴方向速度

privateintm_nSpeedY=0;//y轴方向速度

BulletSprite(Imageimage,intframeWidth,intframeHeight){

super(image,frameWidth,frameHeight);//调用sprite类的构造方法

defineReferencePixel(frameWidth/2,frameHeight/2);//设置参考点

setVisible(false);//初始时不可见

}

publicvoidSetSpeed(intnSpeedX,intnSpeedY){

m_nSpeedX=nSpeedX;

m_nSpeedY=nSpeedY;

}

publicvoidLogic(){//逻辑操作,每50mm被上层调用一次

if(isVisible()==false)

return;

intnX=getRefPixelX()+m_nSpeedX;//导弹自动移动

intnY=getRefPixelY()+m_nSpeedY;

setRefPixelPosition(nX,nY);//如果飞机飞出屏幕,不可见m_nScrWidth与m_nScrHeight是maincanvas类的全局静态变量m_nScrWidth与m_nScrHeight分别记录屏幕的宽和高

if(getRefPixelX()<-10||

getRefPixelX()>MainCanvas.m_nScrWidth+10||

getRefPixelY()<-10||

getRefPixelY()>MainCanvas.m_nScrHeight+10)

setVisible(false);

}

}

Enemy

importjava.util.Random;

importjavax.microedition.lcdui.Image;

publicclassEnemyextendsPlaneSprite{

privateRandomm_Random;//产生随机数

Enemy(Imageimg,intnWidth,intnHeight){

super(img,nWidth,nHeight);

m_Random=newRandom();

}

publicvoidLogic(){//逻辑操作

super.Logic();

if(isVisible()==false)

return;

intnX=getRefPixelX()+m_nSpeedX;//自动移动敌机

intnY=getRefPixelY()+m_nSpeedY;

setRefPixelPosition(nX,nY);//敌机飞出。

不可见

if(getRefPixelX()<-10||

getRefPixelX()>MainCanvas.m_nScrWidth+10||

getRefPixelY()<-10||

getRefPixelY()>MainCanvas.m_nScrHeight+10)

setVisible(false);

CreateBullet();//不断产生导弹

}

privatevoidCreateBullet(){

if(m_Bullet1.isVisible()&&m_Bullet2.isVisible()&&m_Bullet3.isVisible())

return;

//三十分之一产生导弹

intRS=Math.abs(m_Random.nextInt()%30);

if(RS!

=0)

return;

intnSpeedX=m_Random.nextInt()%2;

intnSpeedY=Math.abs(m_Random.nextInt()%2)+1;

m_Bullet1.SetSpeed(nSpeedX,nSpeedY);

m_Bullet2.SetSpeed(nSpeedX*2,nSpeedY*2);

m_Bullet3.SetSpeed(nSpeedX*3,nSpeedY*3);

m_Bullet1.setRefPixelPosition(

getRefPixelX(),getRefPixelY());

m_Bullet2.setRefPixelPosition(

getRefPixelX()*2,getRefPixelY());

m_Bullet3.setRefPixelPosition(

getRefPixelX(),getRefPixelY()*2);

m_Bullet1.setVisible(true);

m_Bullet2.setVisible(true);

m_Bullet3.setVisible(true);

}

}

ExplosionSprite

importjavax.microedition.lcdui.Image;

importjavax.microedition.lcdui.game.Sprite;

publicclassExplosionSpriteextendsSprite{

ExplosionSprite(Imageimage,intframeWidth,intframeHeight){

super(image,frameWidth,frameHeight);

defineReferencePixel(frameWidth/2,frameHeight/2);

setVisible(false);

}

publicvoidLogic(){//逻辑操作

if(isVisible()==false)

return;

intnFrame=getFrame();

nFrame++;

if(nFrame>7){

setVisible(false);

return;

}

setFrame(nFrame);

}

}

MainCanvas

importjava.io.IOException;

importjava.io.InputStream;

importjava.util.Random;

importjavax.microedition.lcdui.Command;

importjavax.microedition.lcdui.CommandListener;

importjavax.microedition.lcdui.Displayable;

importjavax.microedition.lcdui.Font;

importjavax.microedition.lcdui.Graphics;

importjavax.microedition.lcdui.Image;

importjavax.microedition.lcdui.game.GameCanvas;

importjavax.microedition.lcdui.game.LayerManager;

importjavax.microedition.lcdui.game.Sprite;

importjavax.microedition.media.Manager;

importjavax.microedition.media.Player;

importjavax.microedition.rms.RecordStore;

publicclassMainCanvasextendsGameCanvasimplementsCommandListener,Runnable{

privatePlaneMidletmidlet;

privateCommandExitCommand;//对应退出事件

privateCommandOKCommand;

privatebooleanm_bGameEnd=false;//记录游戏是否结束

privateintm_nHighScore=0;//记录历史最高分

privateintm_nScore=0;//记录当前积分

privateMyPlanem_MyPlane;//玩家飞机实例

privateEnemym_aEnemy[];//敌机数组

privateExplosionSpritem_aExplosion[];//爆炸精灵数组

privateLayerManagerm_LayerManager;//管理各个图层

privateImagem_BackImage;//背景图片

privateRandomm_Random;//产生随机数

staticintm_nScrWidth;

staticintm_nScrHeight;

publicinttimes=4;

privateintzhongdan=0;

privateMyUIm_UI;

privateSpritem_MySprite;

privatePlayerm_Player;

//privateScenem_Scene;

publicMainCanvas(PlaneMidletmMidlet)

{

super(true);

midlet=mMidlet;

m_UI=newMyUI(getWidth(),getHeight());

Threadthread=newThread(this);

thread.start();

PlayWav();

}

publicvoidStart(){

m_UI=null;

ExitCommand=newCommand("Exit",Command.EXIT,0);

OKCommand=newCommand("OK",Command.OK,1);

addCommand(ExitCommand);

addCommand(OKCommand);

setCommandListener(this);

m_nScrWidth=getWidth();

m_nScrHeight=getHeight();

m_Random=newRandom();

//创建背景和个精灵

m_LayerManager=newLayerManager();

try{

Imageimg=Image.createImage("/Blast.png");

m_aExplosion=newExplosionSprite[5];

for(intn=0;n

{

m_aExplosion[n]=newExplosionSprite(img,21,17);

m_LayerManager.append(m_aExplosion[n]);

}

img=null;

img=Image.createImage("/MyPlane.png");

m_MyPlane=newMyPlane(img,30,20);

intnX=getWidth()/2;

intnY=getHeight()-120;

m_MyPlane.setRefPixelPosition(nX,nY);

m_MyPlane.setVisible(true);

m_LayerManager.append(m_MyPlane);

m_LayerManager.append(m_MyPlane.m_Bullet1);

m_LayerManager.append(m_MyPlane.m_Bullet2);

m_LayerManager.append(m_MyPlane.m_Bullet3);

img=null;

img=Image.createImage("/Enemy.png");

m_aEnemy=newEnemy[8];

for(intn=0;n

{

m_aEnemy[n]=newEnemy(img,15,15);

m_LayerManager.append(m_aEnemy[n]);

m_LayerManager.append(m_aEnemy[n].m_Bullet1);

m_LayerManager.append(m_aEnemy[n].m_Bullet2);

m_LayerManager.append(m_aEnemy[n].m_Bullet3);

}

m_BackImage=Image.createImage("/Back.png");

m_MySprite=newSprite(m_BackImage,200,200);

m_MySprite.setPosition(-10,-40);

m_LayerManager.append(m_MySprite);

SetViewWindow();

}

catch(IOExceptione){}//读取最高分

LoadHighScore();

}

publicvoidrun()//新线程调用方法

{

longlTime1=System.currentTimeMillis();

longlTime2=lTime1;

try

{

while(true)

{

lTime2=System.currentTimeMillis();

if(lTime2-lTime1>50)

{

lTime1=lTime2;

Input();

Logic();

Paint();

}

}

}

catch(Exceptione){}

}

publicvoidInput()

{

intkeyStates=getKeyStates();

if(m_UI!

=null)

{

if(m_UI.Input(keyStates)==true)

{

Start();

}

return;

}

else{

m_MyPlane.Input(keyStates);

SetViewWindow();

}

}

privatevoidSetViewWindow(){

intnX=m_MyPlane.getRefPixelX()-getWidth()/2;

intnY=m_MyPlane.getRefPixelY()-getHeight()/2;

if(nX<0)

nX=0;

elseif(nX>m_MySprite.getWidth()-getWidth())

nX=m_MySprite.getWidth()-getWidth();

if(nY<0)

nY=0;

elseif(nY>m_MySprite.getHeight()-getHeight())

nY=m_MySprite.getHeight()-getHeight();

m_LayerManager.setViewWindow(nX,nY,getWidth(),getHeight());

}

publicvoidLogic()

{

if(m_UI!

=null)

{

return;

}

elseif(times!

=0){

m_MyPlane.Logic();

for(intn=0;n

m_aEnemy[n].Logic();

for(intn=0;n

m_aExplosion[n].Logic();

CreateEnemy();

CheckCollisions();

}

else

midlet.pauseApp();

}

protectedvoidPaint()

{

Graphicsg=getGraphics();

g.setColor(0);

g.fillRect(0,0,getWidth(),getHeight());

if(m_UI!

=null)

{

m_UI.Paint(g);

flushGraphics();

return;

}

else

{

m_LayerManager.paint(g,0,0);

if(m_bGameEnd==true)

{

if(times==0)

g.drawString("请问您要继续吗?

",getWidth()/2,getHeight()/2,Graphics.TOP|Graphics.HCENTER);

else{

FontmFont=g.getFont();

StringBuffertemp=newStringBuffer();

temp.append("游戏结束,本次得分:

");

temp.append(m_nScore);

intnX=getWidth()/2;

intnY=getHeight()/2-mFont.getHeight();

g.drawString(temp.toString(),nX,nY,Graphics.TOP|Graphics.HCENTER);

temp=null;

temp=newStringBuffer();

temp.append("历史最高分:

");

temp.append(m_nHighScore);

nY=getHeight()/2+mFont.getHeight();

g.drawString(temp.toString(),nX,nY,Graphics.TOP|Graphics.HCENTER);

}

}

flushGraphics();

}

}

publicvoidcommandAction(Commandc,Displayables){

//TODOAuto-generatedmethodstub

if(c.getCommandType()==Command.EXIT)

{

midlet.notifyDestroyed();

}

elseif(c.getCommandType()==Command.OK)

{

times=4;

m_MyPlane.setVisible(true);

}

}

publicvoidEndGame()

{

zhongdan++;

if(zhongdan==3)

{

times--;

m_bGameEnd=true;

zhongdan=0;

}

if(times>0)

{

if(m_nScore>m_nHighScore)

{

m_nHighScore=m_nScore;

SaveHighScore(m_nHighScore);

m_nScore=0;

}

}

else

{

m_MyPlane.setVisible(false);

midlet.pauseApp();

}

}

privatevoidSaveHighScore(intnHighScore)

{

try

{

byteb[]=newbyte[4];

inttemp=nHighScore;

for(inti=b.length-1;i>=0;i--)

{

b[i]=newInteger(temp&0xff).byteValue();

temp=temp>>8;

}

RecordStorers=RecordStore.openRecordStore("BattlePlane",true);

if(rs.getNumRecords()>0)

{

intnID=rs.getNextRecordID();

rs.setRecord(nID,b,0,b.length);

}

else

{

rs.addRecord(b,0,b.length);

}

}

catch(Exceptionex){}

}

privateintLoadHighScore()

{

intnHighScore=0;

try

{

RecordStorers=RecordStore.openRecordStore("BattlePlane",false);

if(rs!

=null&&rs.g

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 农林牧渔 > 林学

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2