gamedemo.java

来自「This is a resource based on j2me embedde」· Java 代码 · 共 935 行 · 第 1/3 页

JAVA
935
字号
/* * @(#)GameDemo.java	1.6 06/10/10 * * Copyright  1990-2008 Sun Microsystems, Inc. All Rights Reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER *  * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License version * 2 only, as published by the Free Software Foundation.  *  * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License version 2 for more details (a copy is * included at /legal/license.txt).  *  * You should have received a copy of the GNU General Public License * version 2 along with this work; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA  *  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa * Clara, CA 95054 or visit www.sun.com if you need additional * information or have any questions.  */package basis.demos;import java.awt.*;import java.awt.event.*;import java.io.*;import java.util.*;import basis.Builder;public class GameDemo extends Demo implements KeyListener, FocusListener {    private static final int OUTSIDE = 0;    private static final int INSIDE = 1;    private static final int ACTIVE = 2;    private static final int FIXED = 3;    private static final int SPARE = 4;    private static final int BLANK = 5;    private static final Color COLOR_OUTSIDE = Builder.SUN_YELLOW;    private static final Color COLOR_INSIDE = Builder.SUN_BLUE;    private static final Color COLOR_ACTIVE = Builder.SUN_RED;    private static final Color COLOR_SPARE = Builder.SUN_LIGHTBLUE;    private static final Color COLOR_BLANK = Color.white;    private static final int DEFAULT_DELAY = 101;    private static final int DELAY_INCREMENT = 10;    private static final int DEFAULT_LIVES = 3;    private static final Color[] winnerColors = {        Builder.SUN_RED,        Builder.SUN_BLUE    };    private int demoWidth;    private int demoHeight;    private int rows = 30;    private int cols = 30;    private int[][] cells = new int[rows][cols];    private int cellWidth;    private int cellHeight;    private int target;    private int level = 1;    private int total;    private int delay = DEFAULT_DELAY;    private int lives = DEFAULT_LIVES;    private boolean initialized;    private boolean stopped; // player stopped by user    private boolean paused;  // game paused by user    private boolean aborted; // game aborted by user    private boolean halted;  // game halted by system    private boolean crashed;     private boolean gameover;    private boolean showHighScores;    private boolean showHelpStrings;    private Animator animator = new Animator();    private Player player;    private ArrayList enemies;    private ArrayList highScores = new ArrayList();    private ArrayList helpStrings = new ArrayList();    private Image pausedImage;    private Image abortedImage;    private Image gameoverImage;    private boolean newGame;    private boolean winner;    private int percentageArea;    private int percentageTime;    private long startTime;    private long pausedTime;    private long haltedTime;    private boolean warmed;    public GameDemo() {        // start early to "warm up"        animator.start();        pausedImage = ImageDemo.loadImage(this, "images/paused.gif");        abortedImage = ImageDemo.loadImage(this, "images/aborted.gif");        gameoverImage = ImageDemo.loadImage(this, "images/gameover.gif");        addKeyListener(this);        addFocusListener(this);        addMouseListener(new MouseAdapter() {            public void mousePressed(MouseEvent e) {                requestFocus();            }        });        helpStrings.add("n/1-9   New game");        helpStrings.add("arrows      Move");        helpStrings.add("i/j/k/l     Move");        helpStrings.add("space       Stop");        helpStrings.add("p          Pause");        helpStrings.add("a/ESC      Abort");        helpStrings.add("s/-       Slower");        helpStrings.add("d/=      Default");        helpStrings.add("f/+       Faster");        helpStrings.add("z     Highscores");        helpStrings.add("h           Help");        loadHighScores();    }    public void paint(Graphics g) {        requestFocus();        if (winner) {            return;        }        Dimension d = getSize();        demoWidth = d.width;        demoHeight = d.height;        cellWidth = demoWidth / cols;        cellHeight = demoHeight / (rows + 1);        if (!initialized) {            play(false);            halted = true;            showHelpStrings = true;            initialized = true;        }        if (showHelpStrings) {            drawStrings(g, "Jonix", helpStrings);            return;        }        if (showHighScores) {            loadHighScores();            drawStrings(g, "High Scores", highScores, 10);            return;        }        for (int row = 0; row < rows; row++) {            for (int col = 0; col < cols; col++) {                if (cells[row][col] == OUTSIDE) {                    g.setColor(COLOR_OUTSIDE);                }                if (cells[row][col] == INSIDE) {                    g.setColor(COLOR_INSIDE);                }                if (cells[row][col] == ACTIVE) {                    g.setColor(COLOR_ACTIVE);                }                if (cells[row][col] == SPARE) {                    g.setColor(COLOR_SPARE);                }                if (cells[row][col] == BLANK) {                    g.setColor(COLOR_BLANK);                }                g.fillRect(col * cellWidth, row * cellHeight, cellWidth, cellHeight);                if (cells[row][col] == SPARE) {                    g.setColor(COLOR_BLANK);                    g.drawRect(col * cellWidth, row * cellHeight, cellWidth - 1, cellHeight - 1);                }            }        }        player.draw(g);        for (int i = 0; i < enemies.size(); i++) {            Enemy enemy = (Enemy) enemies.get(i);            enemy.draw(g);        }        if (paused || aborted || gameover) {            Image image = pausedImage;            if (aborted) {                image = abortedImage;            }            if (gameover) {                image = gameoverImage;            }            ((Graphics2D) g).setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));            g.drawImage(image, demoWidth / 10, 4 * demoHeight / 9, 8 * demoWidth / 10, demoHeight / 9, this);        }        drawPercentageTime();    }    private void drawStrings(Graphics g, String heading, ArrayList strings) {        drawStrings(g, heading, strings, strings.size());    }    private void drawStrings(Graphics g, String heading, ArrayList strings, int number) {        g.setColor(COLOR_OUTSIDE);        g.fillRect(0, 0, demoWidth, demoHeight);        String longest = "";        for (int i = 0; i < strings.size(); i++) {            String s = (String) strings.get(i);            if (s.length() > longest.length()) {                longest = s;            }        }        int fontSize = demoHeight / (number + 2) - 2;        g.setColor(Builder.SUN_RED);        int headingFontSize = 2 * fontSize;        int fw = 0;        while (true) {            Font font = new Font("Monospaced", Font.BOLD, headingFontSize);            g.setFont(font);            FontMetrics fm = g.getFontMetrics(font);            fw = fm.stringWidth(heading);            if (fw < demoWidth) {                break;            }            headingFontSize -= 4;        }        g.drawString(heading, (demoWidth - fw) / 2, headingFontSize);        g.setColor(Builder.SUN_BLUE);        Font font = new Font("Monospaced", Font.PLAIN, fontSize);        g.setFont(font);        FontMetrics fm = g.getFontMetrics(font);        fw = fm.stringWidth(longest);        for (int i = 0; i < strings.size() && i < number; i++) {            String s = (String) strings.get(i);            g.drawString(s, (demoWidth - fw) / 2, (i + 3) * fontSize);        }    }    private void play(boolean preserve) {        if (preserve) {            for (int row = 0; row < rows; row++) {                for (int col = 0; col < cols; col++) {                    if (cells[row][col] == ACTIVE) {                        cells[row][col] = INSIDE;                    }                }            }        } else {            target = 0;            for (int row = 0; row < rows; row++) {                for (int col = 0; col < cols; col++) {                    if (row < rows / 10 ||                         row > 9 * rows / 10 ||                        row < 3 ||                        row >= rows - 3 ||                        col < cols / 10 ||                        col > 9 * cols / 10 ||                        col < 3 ||                        col >= cols - 3) {                        cells[row][col] = OUTSIDE;                    } else {                        cells[row][col] = INSIDE;                        target++;                    }                }            }            int col = cols / 2 - DEFAULT_LIVES / 2;            for (int c = 0; c < DEFAULT_LIVES; c++) {                if (c < lives) {                    cells[0][col + c] = SPARE;                } else {                    cells[0][col + c] = BLANK;                }            }        }        player = new Player();        ArrayList newEnemies = new ArrayList();        if (preserve) {            for (int i = 0; i < enemies.size(); i++) {                Enemy enemy = (Enemy) enemies.get(i);                if (enemy.inside) {                    newEnemies.add(enemy);                }            }        }        for (int i = 0; i < level; i++) {            newEnemies.add(new Enemy(false));            if (!preserve) {                newEnemies.add(new Enemy(true));            }        }        enemies = newEnemies;        percentageArea = percentageArea();        setStatus("Level " + level + " Score " + (total + percentageArea));        startTime = System.currentTimeMillis();        percentageTime = 100;        crashed = false;        aborted = false;        gameover = false;    }    private void crash() {        halted = true;        lives--;        percentageArea = percentageArea();        setStatus("Level " + level + " Score " + (total + percentageArea));        if (lives > 0) {            repaint();            sleep(3000);            // In case user aborted during delay...            if (!aborted) {                play(true);                halted = false;                repaint();            }            return;        }        total += percentageArea;        if (total > 0) {            String scoreString = "" + total;            while (scoreString.length() < 3) {                scoreString = " " + scoreString;            }            boolean newHighScore = false;            boolean inserted = false;

⌨️ 快捷键说明

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