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

📄 penguinscreen.java

📁 自己开发的打企鹅小游戏
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
    }

   /**
     * 保存本地高分记录到手机
     */
    public void saveRecord() {
        ByteArrayOutputStream byteOutput = new ByteArrayOutputStream();
        DataOutputStream output = new DataOutputStream(byteOutput);

            // 保存设置
        try {
            output.writeInt(optionMenu[0]);
            output.writeInt(optionMenu[1]);
        }
        catch (Exception e) {
            System.out.println("save error " + e.getMessage());
        }

        // record list,保存姓名长度和姓名以及高分
        for (int i = 0; i < recordCnt; i++) {
            try {
                for (int j = 0; j < 6; j++) {
                    output.writeByte(name[i][j]);
                }
                output.writeInt(newsreel[i]);
                output.writeInt(namelen[i]);
            }
            catch (Exception E) {
                System.out.println(E.getMessage());
            }
        }

        // save to record,做成rs格式保存在手机上,并命名为penguin
        try {
            RecordStore rs = RecordStore.openRecordStore("penguin", true);
            if (rs.getNumRecords() < 1) {
                rs.addRecord(byteOutput.toByteArray(), 0, byteOutput.size());
            }
            else {
                rs.setRecord(1, byteOutput.toByteArray(), 0, byteOutput.size());
            }
            rs.closeRecordStore();
        }
        catch (Exception E) {
            System.out.println(E.getMessage());
        }

    }

       /**
     * 初始化Demo所要用到的变量
     Author:   erduo
     Function:  InitDemoRun
     Date:      2004-7-7
     初始化动画演示的变量,每次进入demo前都要执行
     */
    public void InitDemoRun() {
        nowAction = 0;				//将动作指真置为初始位置.
        curScore = 0;
        highScore = newsreel[0];
		//每次模拟用户按ok操作的间隔时间
        demoAction[0] = 500;
        demoAction[1] = 17;
        demoAction[2] = 20;
        demoAction[3] = 12000;
        demoAction[4] = 4000;
        bShowOK = false;
    }
 /**
     * 运行Demo
     根据gameState状态不同进行不同的操作,这里的代码和keyPressed函数里面的代码是一样的
     */
    public void DoDemoAction() {
        switch (nowAction) {
            case 0:
                Drop = new Animate(20, 23, 22, 57, 44);
                notHit = true;
                UpdateGameState(Ready);
                nowAction = 1;
                break;
            case 1:
                if (Drop.CurY >= HITTOP -20 + rand(40)) nowAction = 2;
                break;
            case 2:
                Strike = new Animate(20, 64, 58, 100, 146);
                UpdateGameState(Hit);
                nowAction = 3;
                break;
            case 3:
                UpdateGameState(Wait);
                nowAction = 4;
                break;
            case 4:
                nowAction = 0;
                break;
        }
        return;
    }

    /**
     * 初始化游戏环境图片
     * @param void
     */
    public void initMap(Image map) {
        try {
            mapImage = map;
        }
        catch (Exception e) {
            System.out.println("initMap:" + e);

        }
    }

    /**
     * 开始
     */
    synchronized void start() {
        gameThread = new Thread(this);
        lastUpdateTime = System.currentTimeMillis();
        lastGcTime = lastUpdateTime;
        gameThread.start();
    }

    /**
     * 停止
     */
    synchronized void stop() {
        gameThread = null;
    }

    /**
     * 周期函数
     */
    public void run() {
        Thread currentThread = Thread.currentThread();
        while (currentThread == gameThread) {

            long startTime = System.currentTimeMillis();
            update(startTime - lastUpdateTime);
            if (startTime - lastGcTime > 1000) {
                System.gc();
                //  	System.out.println("test");
                lastGcTime = startTime;
            }
            lastUpdateTime = startTime;
            currentThread = Thread.currentThread();
        }
    }
   /**
     * 根据企鹅的横向速度和纵向速度来决定应该画的企鹅图片,从静态数组中取出
     */
    public int getMapArrow() {
        int re_arrow = -1;
        if ( (speedY >= -5) && (speedY <= 5) && (speedX > 0)) re_arrow = 0;
        if (speedY > 5) re_arrow = 1;
        if (speedY > 15) re_arrow = 2;
        if (speedY > 30) re_arrow = 3;
        if ( (speedY > 70) && (speedX < 50)) re_arrow = 4;

        if (speedY < -5) re_arrow = 5;
        if (speedY < -15) re_arrow = 6;
        if (speedY < -30) re_arrow = 7;
        if ( (speedY < -50) && (10 * speedY <= -6 * speedX)) re_arrow = 8;
        if ( (speedY < -50) && (10 * speedY <= -9 * speedX)) re_arrow = 9;

        return re_arrow;
    }


    /**
     * 初始化游戏环境,暂时为空
     */
    public void initGame(Graphics g) {
    }

    /**
     * 更新函数,物件状态更新,图片更新等
     * @param aTimeTick 距离上次更新的时间差
     */
    private synchronized void update(long aTimeTick) {
        //  	System.out.println("Free Memory:"+Runtime.getRuntime().freeMemory()+"bStateMapEntry"+bStateMapEntry);
        switch (state) {
            case COPYRIGHT:
            //播放Logo音乐,
                if (PlayOnce) {
                    RePlaymidi(pOnce);
                    PlayOnce = false;
                }
                //Logo界面停留1秒后进入菜单界面
                updateTime += aTimeTick;
                if (updateTime > 1000) {
                    updateTime = 0;
                    if (!isTimeOut(30)) {
                        UpdateState(MENU);
                    }
                    else
                        gameMIDlet.quitApp();
                }
                break;
                //制作菜单
            case MENU:
             //因为从游戏中和菜单都可以进入本地排名,此处用PreState标记进入前的状态,以便退出本地高分后返回该状态
                PreState = MENU;
                ClickTime += aTimeTick;
                //是否可以运行Demo,如果闲置菜单超过预定时间
                if (ClickTime > MENUIDLE && bDemo) {
                    ClickTime = 0;
                    curScore = 0;
                    InitDemoRun();
                    UpdateGameState(Wait);
                    UpdateState(DEMORUN);
                }
                break;
            case INIT:
            //初始化游戏状态
                UpdateState(RUN);
                gameState = Wait;
                PreState = RUN;
                bGameStateMapEntry = true;
                break;
            case INITINPUT:
            //输入高分时的初始化
                namelen[order] = 0;
                UpdateState(INPUT);
                currentLetter = 0;
                break;
            case DEMOLOCAL:
            //演示本地排名
                ShowOK += aTimeTick;
                if (ShowOK > SHOWOK) {
                    ShowOK = 0;
                    if (bShowOK) {
                        bShowOK = false;
                    }
                    else {
                        bShowOK = true;

                    }
                }
                updateTime += aTimeTick;
                if (updateTime > 4000) {
                    updateTime = 0;
                    InitDemoRun();
                    UpdateGameState(Wait);
                    UpdateState(DEMORUN);
                }
                break;
            case DEMORUN:
//演示游戏运行过程
                ShowOK += aTimeTick;
                if (ShowOK > SHOWOK) {
                    ShowOK = 0;
                    if (bShowOK) {
                        bShowOK = false;
                    }
                    else {
                        bShowOK = true;

                    }
                }

                //根据demoAction中设定的时间模拟一次操作
                long saveTime = demoTime / demoAction[nowAction];
                demoTime += aTimeTick;
                if (demoTime / demoAction[nowAction] != saveTime) {
                    DoDemoAction();
                }
				//切换到高分演示状态
                if (demoTime > 22000) {
                    demoTime = 0;
                    if (!isTimeOut(30)) {
                        UpdateState(DEMOLOCAL);
                    }
                    else {
                        gameMIDlet.quitApp();
                    }
                }
            case RUN:
                switch (gameState) {
                    case Wait:
                     //20秒用在输入时的时间限制
                        RemainSecond = 20;
                        curFrame = 0;
                        Score = 0;
                        ActualScore = 0;
                        phyX = 0;
                        break;
                    case Hit:
                        if (notHit) {		//控制每次下落只能打一次
                            Strike.countTime += aTimeTick;
                            if (Strike.countTime >= Strike.RefreshTime) {
                            	//邦邦鼠的挥棒动画
                                if (Strike.mapArrow < mapStrike.length - 1) {
                                    Strike.mapArrow++;
                                    Strike.countTime = 0;
                                    Strike.bDraw = true;
                                }
                                else {
                                    //打击动画完毕,开始判断角度
                                    if ( (Drop.CurY > HITTOP) &&
                                        (Drop.CurY < HITBOTTOM)) {
                                        Flying = new Animate(20, 26, 26,
                                            Drop.CurX, Drop.CurY);
                                         //爆炸效果的动画初始化
                                        Explode = new Animate(20, 39, 40, 0, 0);
                                  speedY = maxSpeed *
                                            (HITBOTTOM - Drop.CurY) /
                                            (HITBOTTOM - HITTOP);
                                        speedX = maxSpeed * (Drop.CurY - HITTOP) /
                                            (HITBOTTOM - HITTOP)+50;

                                        Flying.mapArrow = getMapArrow();

                                         //根据随机数计算垫子
                                        for (int i = 0; i < ElasticPos.length;
                                             i++) {

                                            if (i == 0) {
                                                ElasticPos[i] = rand(50) + 200;
//                                                ElasticPos[i] = rand(10) + 0;
                                            }
                                            else {
                                                ElasticPos[i] = ElasticPos[i -
                                                    1] + rand(50) + 200;
//                                                ElasticPos[i] = ElasticPos[i -1] + rand(10) + 10;

                                            }

                                        }
                                        RePlaymidi(pKnock);
                                        UpdateGameState(Init_fly);
                                        
                                    }
                                    else {
                                        notHit = false;
                                    }
                                }
                            }
                        }
                        //注意这里没有break.也就是说在挥棒的过程中,ready状态也是在运行的
                    case Ready:		//企鹅跳下状态
                        Drop.countTime += aTimeTick;
                         //如果没有落地,游戏还没有结束
                        if (Drop.CurY <= 180) {
                        	 //根据公式来计算要画的Drop图片
                            if (Drop.CurX < 87) {		//企鹅下落轨迹是2个线路,这力是上升的
                                if (Drop.countTime >Drop.RefreshTime-10) {
                                    Drop.CurX++;
                                    Drop.bDraw = true;
                                    //轨迹公式
                                    Drop.CurY = 220 / 171 - 3 * Drop.CurX / 5 +77;
                                    Drop.countTime = 0;

⌨️ 快捷键说明

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