⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gamecanvas.java

📁 英语背单词联网版本
💻 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("继续",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("上下左右键移动瞄准器",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("当前词集暂无单词...",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 ) );    }    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -