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

📄 penguinscreen.java

📁 为公司开发的打企鹅小游戏
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
            namelen[0] = 3;

        }

    }

    /**
     * 保存本地高分记录到手机
     */
    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 {
                // output name
                for (int j = 0; j < 6; j++) {
                    output.writeByte(name[i][j]);
                }
                // output record
                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;//将动作指真置为初始位置.
        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, 22, 21, 30, 20);
                notHit = true;
                UpdateGameState(Ready);
                nowAction = 1;
                break;
            case 1:
                if (Drop.CurY >= HITTOP + 1 + rand(40)) nowAction = 2;
                break;
            case 2:
                Strike = new Animate(200, 52, 44, 70, 80);
                UpdateGameState(Hit);
                nowAction = 3;
                break;
            case 3:
                UpdateGameState(Wait);
                nowAction = 4;
                break;
            case 4:
                nowAction = 0;
                break;
        }
        return;
    }

    /**
     * 初始化游戏环境图片
     * @param void
     */
    public void initMap(String map) {
        try {
            mapImage = null;
            //System.gc();
            mapImage = Image.createImage(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();
                lastGcTime = startTime;
            }
            lastUpdateTime = startTime;
            currentThread = Thread.currentThread();
        }
    }

    /**
     * 根据企鹅的高度来决定应该画的企鹅图片,从静态数组中取出
     */
    public int getMapArrow() {
        //System.out.println(speedX+";"+speedY);
        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;
//		if((speedY<-70)&&(speedX<30)) re_arrow=9;

        return re_arrow;
    }

    /**
     * 初始化游戏环境
     * @param gate 关卡数,可以为 1,2,3
     */
    public void initGame(Graphics g) {
    }

    /**
     * 更新函数,物件状态更新,图片更新等
     * @param aTimeTick 距离上次更新的时间差
     */
    private synchronized void update(long aTimeTick) {
        switch (state) {
            case COPYRIGHT:
                //播放Logo音乐,
                if (PlayOnce) {
                    RePlaymidi(pOnce);
                    PlayOnce = false;
                }
                updateTime += aTimeTick;
                //Logo界面停留1秒后进入菜单界面
                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;
                    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 > 20000) {
                    updateTime = 0;
                    InitDemoRun();
                    UpdateGameState(Wait);
                    UpdateState(DEMORUN);
                }
                break;
            case DEMORUN:
                //演示游戏运行过程

                ShowOK += aTimeTick;
                if (ShowOK > SHOWOK) {
                    ShowOK = 0;
                    if (bShowOK) {
                        bShowOK = false;
                    }
                    else {
                        bShowOK = true;

                    }
                }

                //每500个aTimeTick模拟一次操作
                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) {//控制每次下落只能打一次
                            int angle = -1;
                            strikeTime += aTimeTick;
                            if (strikeTime >= Strike.RefreshTime) {
                              //邦邦鼠的挥棒动画
                                if (Strike.mapArrow < mapStrike.length - 1) {
                                    Strike.mapArrow++;
                                    bDrawStrike = true;
                                }
                                else {
                                    //打击动画完毕,开始判断角度
                                    if ( (Drop.CurY > HITTOP) &&
                                        (Drop.CurY < HITBOTTOM)) {
                                        angle = 90 * (HITBOTTOM - Drop.CurY) /
                                            (HITBOTTOM - HITTOP);
                                        if (angle > 20) Drop.mapArrow = 1;
                                        if (angle > 70) Drop.mapArrow = 2;
                                        Flying = new Animate(20, 22, 21,
                                            Drop.CurX, Drop.CurY);
                                        //爆炸效果的动画初始化
                                        Explode = new Animate(20, 27, 27, 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(36) + 144;
                                            }
                                            else {
                                                ElasticPos[i] = ElasticPos[i -
                                                    1] + rand(36) + 144;
                                            }
                                        }
                                        RePlaymidi(pKnock);
                                        UpdateGameState(Init_fly);
                                    }
                                    else {
                                        notHit = false;
                                    }
                                }
                                strikeTime = 0;
                            }
                        }
                    case Ready:
                        updateTime += aTimeTick;
                        //如果没有落地,游戏还没有结束
                        if (Drop.CurY <= 100) {
                            //根据公式来计算要画的Drop图片
                            if (Drop.CurX < 50) {
                                if (updateTime >= Drop.CurX - 10) {
                                    Drop.CurX++;
                                    bDrawDrop = true;
                                    //轨迹公式
                                    updateTime = 0;

⌨️ 快捷键说明

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