?? gamecanvas.java
字號:
/* * GameCanvas.java * * Created on 2003年11月18日, 上午9:19 */package handenglish;import javax.microedition.lcdui.*;import java.io.*;/** * * @author com * @version */public class GameCanvas extends Canvas implements Runnable,KeyRepeator{ public final int WIDTH=128; public final int HEIGHT=128; public final int WHITE=0xffffff; public final int BLACK=00000000; public final int YELLOW = 0xffff00; public final int SLEEPTIME=300;//200 public final int CHANGETIME=80;//80 public final int STEP=5; public final int BALLOONNUM=8; public final int RIGHTTIME=3; private Words words; private Balloons balloons; private Thread moveThread; private Image ground,back,firer; private boolean isMove,isFire,isGame; private int locx,locy; private char key; private int moveTime,createTime,blastNum,k; private int[] m; private KeyRepeat m_KeyRepeat; private Midlet midlet; private boolean helpView; //private Sound sound1,sound2,sound3; /** * paint */ public GameCanvas(Midlet midlet,int nowUnitType,int nowUnitId){ setFullScreenMode( true ); try{ ground=Image.createImage("/handenglish/images/ground.png"); back=Image.createImage("/handenglish/images/back.png"); firer=Image.createImage("/handenglish/images/fire.png"); }catch( Exception e ){} words=new Words(midlet,nowUnitType,nowUnitId); balloons=new Balloons(this); m=new int[BALLOONNUM]; init(); // sound1=new Sound(initSound("hurt.ott",17),1); // sound2=new Sound(initSound("win.ott",21),1); // sound3=new Sound(initSound("fail.ott",18),1); moveThread=new Thread(this); m_KeyRepeat = new KeyRepeat(this); m_KeyRepeat.start(); this.midlet=midlet; } private byte[] initSound(String s, int i) { byte abyte0[] = new byte[i]; try { InputStream inputstream = this.getClass().getResourceAsStream(s); DataInputStream db=new DataInputStream(inputstream); db.read(abyte0); System.out.println(abyte0); db.close(); } catch(Exception e){e.printStackTrace();} return abyte0; } private void init(){ isGame=true; isMove=true; isFire=false; helpView=false; words.rightNum=0; blastNum=0; moveTime=0; createTime=0; locx=55; locy=55; for(int i=0;i<BALLOONNUM;i++) m[i]=0; } private void nextGame(){ init(); } /** *是否爆炸過 */ public boolean isBlast(int i){ if(m[i]>3) return true; else return false; } public void startThread(){ moveThread.start(); } public void stopThread(){ if(moveThread!=null) moveThread=null; m_KeyRepeat.cancel(); } public void paint(Graphics g) { if(words.getSize()>0){ if(isGame){ g.setClip(0,0,WIDTH,HEIGHT); g.setColor(WHITE); g.fillRect(0,0,WIDTH,HEIGHT); g.setClip(0,0,WIDTH,HEIGHT); g.drawImage(back,3,3,g.TOP|g.LEFT); balloons.draw(g,m); g.setClip(3,3,122,17); g.setColor(0); g.fillRect(3,3,122,17); g.setClip(0,0,WIDTH,HEIGHT); g.drawImage(ground,0,0,g.TOP|g.LEFT); if(isMove){ words.draw(g); } else if(isFire){ if(key!=0){ m[k]=blastNum; blastNum++; if(blastNum>=5){ blastNum=0; moveTime=0; createTime=0; isMove=true; isFire=false; } } else{ moveTime=0; createTime=0; isMove=true; isFire=false; } } g.setClip(0,0,WIDTH,HEIGHT); g.drawImage(firer,locx,locy,g.TOP|g.LEFT); g.setColor(WHITE); } else{ isMove=false; g.setClip(0,0,WIDTH,HEIGHT); g.setColor(BLACK); g.fillRect(0,0,WIDTH,HEIGHT); g.setClip(0,0,WIDTH,HEIGHT); g.setFont(Font.getFont(Font.FACE_SYSTEM,Font.STYLE_PLAIN,Font.SIZE_SMALL)); g.setColor(YELLOW); g.drawString("你答對了"+words.rightNum+"題",10,20,g.TOP|g.LEFT); g.drawString("按左軟鍵重新開始",10,50,g.TOP|g.LEFT); g.drawString("按右軟鍵返回菜單",10,80,g.TOP|g.LEFT); g.setColor(WHITE); g.drawString("返回",100,110,Graphics.LEFT|Graphics.TOP); g.drawString("重新",2,110,Graphics.LEFT|Graphics.TOP); /*if(words.isfinish){ g.drawString("重新",100,110,Graphics.LEFT|Graphics.TOP); } else g.drawString("繼續(xù)",100,110,Graphics.LEFT|Graphics.TOP);*/ //stopMoveThread(); } if(helpView){ g.setColor(255,255,255); g.fillRoundRect(2,15,124,85,2,2); g.setColor(51,0,0); g.drawRect(3,16,121,82); g.drawString("上下左右鍵移動瞄準(zhǔn)器",5,20,g.LEFT|g.TOP); g.drawString("提機鍵開槍",5,40,g.LEFT|g.TOP); g.drawString("右軟鍵返回菜單",5,60,g.LEFT|g.TOP); g.drawString("任意鍵返回游戲",5,80,g.LEFT|g.TOP); } } else{ g.setClip(0,0,WIDTH,HEIGHT); g.setColor(BLACK); g.fillRect(0,0,WIDTH,HEIGHT); g.setClip(0,0,WIDTH,HEIGHT); g.setFont(Font.getFont(Font.FACE_SYSTEM,Font.STYLE_PLAIN,Font.SIZE_SMALL)); g.setColor(YELLOW); g.drawString("當(dāng)前詞集暫無單詞...",64,64,g.HCENTER|g.TOP); g.drawString("返回",100,110,Graphics.LEFT|Graphics.TOP); } } private void fire(){ if(isMove){ key=balloons.isHit(locx,locy); k=balloons.hitNum; if(key!=0&&words.judge(key)){//擊中并且答案虐正確 words.rightNum++; // sound2.play(1); } else if(key!=0){ // sound3.play(1); } else{ // sound1.play(1); } isMove=false; isFire=true; } } /** *氣球移動 */ public void move(){ for(int i=0;i<BALLOONNUM;i++){ if(!balloons.move(i)){ if(createTime%RIGHTTIME==1){ balloons.balloonLetter[i]=words.justLetter; } else{ m[i]=0; balloons.balloonLetter[i]=balloons.getLetter(); } createTime++; } } } public void run(){ Thread current = Thread.currentThread(); while(moveThread == current){ System.gc(); try{ moveThread.sleep(SLEEPTIME); if(!helpView){ if(isMove){ move(); if(moveTime%CHANGETIME==0){ isGame=words.changeWord(); createTime=0; } moveTime++; } } repaint(); serviceRepaints(); }catch(Exception e){ // e.printStackTrace(); } } } /** * Called when a key is pressed. */ protected void keyPressed(int keyCode){ int gameAction=getGameAction(keyCode); if(words.getSize()==0){ if(keyCode==-7){ stopThread(); midlet.nowDisplay(this,midlet.WIN_SELUNIT); } } else{ if(isMove){ if(helpView){ helpView=false; repaint(); } else{ if(keyCode==-7){ stopThread(); midlet.nowDisplay(this,midlet.WIN_SELUNIT); } else if(gameAction==FIRE){ //sound1.play(1); fire(); repaint(); } else if(keyCode==KEY_NUM0){ helpView=true; repaint(); } else if ( m_KeyRepeat != null ) m_KeyRepeat.startRepeat(gameAction); } } else if(!isGame){ if(keyCode==-7){ stopThread(); midlet.nowDisplay(this,midlet.WIN_SELUNIT); } else if(keyCode==-1){ words.isfinish=false; nextGame(); repaint(); } } } } public void movePressed(int gameAction){ switch(gameAction){ case UP: if(locy-STEP>=20) locy=locy-STEP; break; case DOWN: if(locy+STEP<=100) locy=locy+STEP; break; case LEFT: if(locx-STEP>=0) locx=locx-STEP; break; case RIGHT: if(locx+STEP<=WIDTH) locx=locx+STEP; break; } repaint(); } /** * Called when a key is released. */ protected void keyReleased(int keyCode) { m_KeyRepeat.stopRepeat( getGameAction( keyCode ) ); } }
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -