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

📄 wormpit.java

📁 用JBuilder2005作的java的手机小游戏
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            }
        } else {
            if (audioPlayer != null) {
            try {
                Manager.playTone(69, 50, 100);   // Play audio
            } catch (MediaException me) { }
            }
        }

        g.setColor(WormPit.ERASE_COLOUR);
        g.fillRect((width - (SCORE_CHAR_WIDTH * 3))-START_POS,
               height-START_POS,
               (SCORE_CHAR_WIDTH * 3),
               SCORE_CHAR_HEIGHT);
        g.setColor(WormPit.DRAW_COLOUR);

        // Display new score
        g.drawString("" + score,
                 width - (SCORE_CHAR_WIDTH * 3) - START_POS,
                 height - START_POS, g.TOP|g.LEFT);

        myFood.regenerate();
        int x = myFood.getX();
        int y = myFood.getY();
        while (myWorm.contains(x, y)) {
        // generate again if food placed under worm..
            myFood.regenerate();
            x = myFood.getX();  y = myFood.getY();
        }
        }

        myFood.paint(g);
    } catch (WormException se) {
        gameOver = true;
    }
    }

    /**
     * Paint the worm pit and its components
     * @param g graphics object to be rendered
     */
    public void paint(Graphics g) {
    if (forceRedraw) {
        // Redraw the entire screen
        forceRedraw = false;

        // Clear background
        g.setColor(WormPit.ERASE_COLOUR);
        g.fillRect(0, 0, getWidth(),
              getHeight());

        // Draw pit border
        g.setColor(WormPit.DRAW_COLOUR);
        g.drawRect(1, 1, (width - START_POS), (height - START_POS));

        // Display current level
        g.drawString("L: " + level, START_POS, height, g.TOP|g.LEFT);
        g.drawString("" + score,
             (width - (SCORE_CHAR_WIDTH * 3)),
             height, g.TOP|g.LEFT);

        // Display current score
        g.drawString("S: ",
             (width - (SCORE_CHAR_WIDTH * 4)),
             height, g.TOP|g.RIGHT);
        g.drawString("" + score,
             (width - (SCORE_CHAR_WIDTH * 3)),
             height, g.TOP|g.LEFT);

        // Display highest score for this level
        g.drawString("H: ",
             (width - (SCORE_CHAR_WIDTH * 4)),
             (height + SCORE_CHAR_HEIGHT),
             g.TOP|g.RIGHT);
        g.drawString("" + WormScore.getHighScore(level),
             (width - (SCORE_CHAR_WIDTH * 3)),
             (height + SCORE_CHAR_HEIGHT),
             g.TOP|g.LEFT);

        // Draw worm & food
        g.translate(START_POS, START_POS);
        g.setClip(0, 0, CellWidth*CELL_SIZE, CellHeight*CELL_SIZE);
        myWorm.paint(g);
        myFood.paint(g);
    } else {
        // Draw worm & food
        g.translate(START_POS, START_POS);
    }

    if (gamePaused) {
        Font pauseFont = g.getFont();
        int fontH = pauseFont.getHeight();
        int fontW = pauseFont.stringWidth("Paused");
        g.setColor(WormPit.ERASE_COLOUR);
        g.fillRect((width-fontW)/2 - 1, (height-fontH)/2,
               fontW + 2, fontH);
        g.setColor(WormPit.TEXT_COLOUR);
        g.setFont(pauseFont);
        g.drawString("Paused", (width-fontW)/2, (height-fontH)/2,
             g.TOP|g.LEFT);
    } else if (gameOver) {
        Font overFont = g.getFont();
        int fontH = overFont.getHeight();
        int fontW = overFont.stringWidth("Game Over");
        g.setColor(WormPit.ERASE_COLOUR);
        g.fillRect((width-fontW)/2 - 1, (height-fontH)/2,
               fontW + 2, fontH);
        g.setColor(WormPit.TEXT_COLOUR);
        g.setFont(overFont);
        g.drawString("Game Over", (width-fontW)/2, (height-fontH)/2,
             g.TOP|g.LEFT);
    } else {
        paintPitContents(g);
    }
    g.translate(-START_POS, -START_POS);
    }

    /**
     * Notification handler when canvas hidden.
     * Forces a redraw when canvas is shown again.
     * Signals that the game is paused while the canvas is obscured.
     */
    protected void hideNotify() {
    super.hideNotify();
    forceRedraw = true;
    if (!gameOver) {
        gamePaused = true;
    }
    }

    /**
     * The main execution loop.
     */
    public void run() {
    while (!gameDestroyed) {
        try {
        synchronized (myWorm) {
            if (gameOver) {
            if (WormScore.getHighScore(level) < score) {
                /* Display score screen */
                WormScore.setHighScore(level, score, "me");
            }
            if ((audioPlayer != null) &&
                (audioPlayer.getState() == Player.STARTED)) {
                try {
                audioPlayer.stop();
                Manager.playTone(60, 400, 100);
                } catch (Exception ex) { }
            }
            repaint();
            // serviceRepaints(); // Draw immediately
            myWorm.wait();    // Wait until user presses 'restart'
            } else if (gamePaused) {
            repaint();
            // serviceRepaints(); // Draw immediately
            myWorm.wait();    // Wait until user presses 'restart'
            } else {
            myWorm.moveOnUpdate();
            repaint();
            // serviceRepaints(); // Draw immediately
            myWorm.wait(DEFAULT_WAIT-(level*40));
            }
        }
        } catch (java.lang.InterruptedException ie) {
        }
    }
    }

    /**
     * Destroy the entire game. Call this prior to destroying the MIDlet.
     */
    public void destroyGame() {
    synchronized (myWorm) {
        gameDestroyed = true;
        //myWorm.notifyAll();
        myWorm.notifyAll();
    }
    }

    /**
     * Destroy the audio player. Call this destroy the audio player.
     */
    public void destroyAudioPlayer() {
    synchronized (myWorm) {
        if (audioPlayer != null) {
        audioPlayer.close();
        audioPlayer = null;
        }
        if (tonePlayer != null) {
        tonePlayer.close();
        tonePlayer = null;
        }
        myWorm.notifyAll();
    }
    }

    /**
     * Create an audio player.
     */
    public void createAudioPlayer() {
    byte[] bkmelody = {
            ToneControl.VERSION, 1,
            ToneControl.SET_VOLUME, 40,
            76, 8,
            81, 24,
            76, 8,
            88, 24,
            81, 8,
            85, 16,
            83, 8,
            85, 8,
            81, 16,
            76, 16,
            81, 16,
            90, 16,
            88, 16,
            85, 8,
            86, 8,
            88, 48,
            ToneControl.SILENCE, 8,
            76, 8,
            81, 16,
            90, 16,
            88, 16,
            85, 8,
            86, 8,
            88, 16,
            83, 8,
            85, 8,
            81, 16,
            76, 16,
            81, 16,
            85, 8,
            86, 8,
            83, 24,
            81, 8,
            81, 32
    };

    byte[] tseq = {
            ToneControl.VERSION, 1,
            64, 4,
            65, 4,
            66, 4
    };

    if (audioPlayer != null) {
        audioPlayer.close();
        audioPlayer = null;
        try {
        Thread.sleep(200);
        } catch (Exception ex) { }
    }

    try {
        ToneControl tControl;

        tonePlayer = Manager.createPlayer(Manager.TONE_DEVICE_LOCATOR);
        tonePlayer.realize();
        tControl = (ToneControl)tonePlayer.getControl("ToneControl");
        tControl.setSequence(tseq);

        audioPlayer = Manager.createPlayer(Manager.TONE_DEVICE_LOCATOR);
        audioPlayer.setLoopCount(-1);
        audioPlayer.realize();
        tControl = (ToneControl)audioPlayer.getControl("ToneControl");
        tControl.setSequence(bkmelody);
        audioPlayer.start();


    } catch (Exception ex) {
        ex.printStackTrace();
    }
    }

}

⌨️ 快捷键说明

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