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

📄 engine.java

📁 this a game,you can learn the j2me content by this project
💻 JAVA
字号:
/* * Copyright (c) 2002, 2003 Sun Microsystems, Inc. All Rights Reserved. * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are * met: * - Redistribution of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * - Redistribution in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * Neither the name of Sun Microsystems, Inc., 'Java', 'Java'-based * names, or the names of contributors may be used to endorse or promote * products derived from this software without specific prior written * permission. * This software is provided "AS IS," without a warranty of any kind. ALL * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN * MIDROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR * ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR * DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF * SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. * You acknowledge that this software is not designed, licensed or * intended for use in the design, construction, operation or maintenance * of any nuclear facility. $Id: Engine.java,v 1.4 2003/06/03 22:20:35 ro89390 Exp $ */package com.sun.j2me.blueprints.jbricks;import javax.microedition.lcdui.*;/** * This class drives the games (hence the name * engine :). It has all the level data, animates * the objects, keeps track of state, and triggers * screen redraws. */public class Engine implements Runnable {    public static final int PATTERN_WIDTH = 11;    public final static int TITLE = 0;    public final static int PLAY = 1;    public final static int OVER = 2;    public final static int DEMO = 3;    private Ball ball;    private Brick paddle;    private BrickList bricks;    private Screen screen;    private int score;    private int hi_score;    private int level;    private int lives;    private int state;    private int key;    private int calibration;    private boolean paused;    private long last_key_press;    private boolean level_started;    private boolean done;    private static final int LAST_KEY_DELTA = 7000;    private static final int STD = Brick.STANDARD;    private static final int FIX = Brick.FIXED;    private static final int SLI = Brick.SLIDE;    private static final int ZOM = Brick.ZOMBIE;    // Note that the width of each pattern    // has to be equal to the PATTERN_WIDTH    // variable above    private int[] title_pattern = {        STD, STD, STD, STD, STD, STD, STD, STD, STD, STD, STD, STD, STD, STD,         FIX, FIX, FIX, FIX, FIX, STD, STD, STD, STD, STD, STD, STD, STD, STD,         STD, FIX, STD, STD, STD, STD, STD, STD, STD, STD, STD, STD, FIX, STD,         STD, STD, STD, STD, STD, STD, STD, STD, STD, FIX, STD, STD, STD, STD,         STD, STD, FIX, STD, STD, STD, FIX, STD, STD, STD, STD, STD, STD, STD,         FIX, FIX, FIX, STD, STD, STD, STD, STD, STD, STD, STD, STD, STD, STD,         STD, STD, STD, STD,     };    private int[][] pattern_list = {         {            STD, STD, STD, STD, STD, STD, STD, STD, STD, STD, STD, STD, STD,             STD, STD, STD, STD, STD, STD, STD, STD, STD, STD, STD, STD, STD,             STD, STD, STD, STD, STD, STD, STD, STD, STD, STD, STD, STD, STD,             STD, STD, STD, STD, STD, STD, STD, STD, STD, STD, STD, STD, STD,             STD, STD, STD, STD, STD, STD, STD, STD, STD, STD, STD, STD, STD,             STD, STD, STD, STD, STD, STD, STD, STD, STD, STD, STD, STD, STD,             STD, STD, STD, STD, STD, STD, STD, STD, STD, STD,         }, {            STD, STD, STD, STD, STD, STD, STD, STD, STD, STD, STD, ZOM, SLI,             ZOM, ZOM, SLI, ZOM, ZOM, SLI, ZOM, ZOM, SLI, ZOM, ZOM, ZOM, ZOM,             ZOM, ZOM, ZOM, ZOM, ZOM, ZOM, ZOM, SLI, ZOM, ZOM, SLI, ZOM, ZOM,             SLI, ZOM, ZOM, SLI, ZOM, ZOM, ZOM, ZOM, ZOM, ZOM, ZOM, ZOM, ZOM,             ZOM, ZOM, ZOM, ZOM, SLI, ZOM, ZOM, SLI, ZOM, ZOM, SLI, ZOM, ZOM,             SLI, ZOM, ZOM, ZOM, ZOM, ZOM, ZOM, ZOM, ZOM, ZOM, ZOM, ZOM, SLI,             ZOM, ZOM, SLI, ZOM, ZOM, SLI, ZOM, ZOM, SLI, ZOM,         }, {            ZOM, ZOM, ZOM, ZOM, ZOM, ZOM, ZOM, ZOM, ZOM, ZOM, ZOM, ZOM, FIX,             SLI, FIX, FIX, FIX, FIX, FIX, SLI, FIX, ZOM, ZOM, FIX, ZOM, ZOM,             ZOM, ZOM, ZOM, ZOM, ZOM, FIX, ZOM, ZOM, FIX, ZOM, STD, STD, STD,             STD, STD, ZOM, FIX, ZOM, ZOM, FIX, ZOM, ZOM, ZOM, ZOM, ZOM, ZOM,             ZOM, FIX, ZOM, ZOM, FIX, FIX, FIX, FIX, FIX, FIX, FIX, FIX, FIX,             ZOM, ZOM, ZOM, ZOM, ZOM, ZOM, ZOM, ZOM, ZOM, ZOM, ZOM, ZOM, STD,             STD, STD, STD, STD, STD, STD, STD, STD, STD, STD,         }, {            STD, STD, STD, STD, STD, STD, STD, STD, STD, STD, STD, STD, FIX,             FIX, FIX, FIX, FIX, FIX, FIX, FIX, FIX, STD, STD, FIX, STD, STD,             STD, STD, STD, STD, STD, FIX, STD, STD, STD, STD, STD, STD, STD,             STD, STD, STD, STD, STD, STD, STD, STD, STD, STD, STD, STD, STD,             STD, STD, STD, STD, FIX, STD, STD, STD, STD, STD, STD, STD, FIX,             STD, STD, FIX, FIX, FIX, FIX, FIX, FIX, FIX, FIX, FIX, STD, STD,             STD, STD, STD, STD, STD, STD, STD, STD, STD, STD        }    };    public Engine(Screen screen) {        int i, n;        long then;        this.screen = screen;        screen.setEngine(this);        paddle = new Brick(null, Screen.width / 2 - Brick.WIDTH / 2,                            Screen.VDIVIDER - Brick.HEIGHT - 4, -1, 2);        paddle.setColor(ThreeDColor.lightGray);        paddle.width = Screen.width / 8;        paddle.height = Brick.HEIGHT;        ball = new Ball(Screen.width / 2 - Brick.WIDTH / 4,                         paddle.y - 2 * Ball.RADIUS,                         2 * Math.max(1, Screen.width / 80),                         2 * -Math.max(1, Screen.width / 80));        level = 0;        state = TITLE;        done = false;        bricks = new BrickList(title_pattern, PATTERN_WIDTH, -1);        last_key_press = System.currentTimeMillis();        then = System.currentTimeMillis();        n = 0;        for (i = 0; i < 30000; i++) {            n++;        }         if (n > 0) {            calibration = (int) (System.currentTimeMillis() - then);        }         Thread runner = new Thread(this);        runner.start();    }    private void reset() {        level = 0;        lives = 2;        score = 0;        startLevel();    }     private void restartLevel() {        if (state == PLAY) {            paused = true;        }         synchronized (this) {            level_started = true;        }         paddle.moveTo(Screen.width / 2 - Brick.WIDTH / 2,                       Screen.VDIVIDER - Brick.HEIGHT - 4);        ball.moveTo(Screen.width / 2 - Brick.WIDTH / 4,                     paddle.y - 2 * Ball.RADIUS);        ball.setSteps(Math.max(1, Screen.width / 80),                       -Math.max(1, Screen.width / 80));        screen.repaint();        screen.serviceRepaints();    }     private void startLevel() {        bricks = new BrickList(pattern_list[level], PATTERN_WIDTH, level);        restartLevel();    }     private void nextLevel() {        level++;        if (level == pattern_list.length) {            level = 0;        }         startLevel();    }     public void keyPressed(int key_code, int game_action) {        key = game_action;        last_key_press = System.currentTimeMillis();        if (key_code == Canvas.KEY_NUM0 || game_action == Canvas.GAME_A) {            nextLevel();            return;        }         if (key_code == Canvas.KEY_NUM1 || game_action == Canvas.GAME_B) {            screen.setShowFPS(!screen.isShowFPS());            return;        }         if (state == TITLE || state == OVER || state == DEMO) {            state = PLAY;            reset();        } else if (state == PLAY                    && (key == Canvas.LEFT || key == Canvas.RIGHT                        || key == Canvas.FIRE)) {            paused = false;        }     }     public void keyReleased(int key_code, int game_action) {        key = 0;    }     public void getState(EngineState engineState) {        engineState.bricks = bricks;        engineState.ball = ball;        engineState.paddle = paddle;        engineState.state = state;        engineState.score = score;        engineState.hi_score = hi_score;        engineState.lives = lives;    }     public synchronized boolean levelStarted() {        boolean x = level_started;        level_started = false;        return x;    }     public void stop() {        done = true;    }     public void run() {        boolean recent_collision;        long then;        int px, pw, delta;        int paddle_speed = 0;        recent_collision = false;        then = System.currentTimeMillis();        while (!done) {            if ((state == TITLE || state == OVER)                     && System.currentTimeMillis() - last_key_press                        > LAST_KEY_DELTA) {                state = DEMO;                reset();            }             px = paddle.x;            pw = paddle.width;            if (state == DEMO) {                int ball_center = ball.x + Ball.RADIUS;                int paddle_center = px + Brick.WIDTH / 2;                int epsilon = pw / 4;                if (ball_center >= px + epsilon                         && ball_center <= px + pw - epsilon) {                    key = 0;                } else if (ball_center < paddle_center) {                    key = Canvas.LEFT;                } else {                    key = Canvas.RIGHT;                }            }             if ((state == PLAY || state == DEMO) &&!paused) {                if (key == Canvas.LEFT) {                    paddle_speed = Math.min(-1,                                             -Brick.STEP                                             / (state == DEMO ? 3 : 1));                } else if (key == Canvas.RIGHT) {                    paddle_speed = Math.max(1,                                             Brick.STEP                                             / (state == DEMO ? 3 : 1));                } else {                    paddle_speed = 0;                }                if (paddle_speed < 0 && px > 0                         || paddle_speed > 0 && px + pw < Screen.width) {                    paddle.moveBy(paddle_speed, 0);                }                 ball.move();                if (ball.x <= 0 || ball.x >= Screen.width - ball.width - 1) {                    ball.bounceHorizontal();                }                 if (ball.y <= 0) {                    ball.bounceVertical();                }                 if (ball.intersects(paddle)) {                    if (!recent_collision) {                        ball.bounce(paddle);                    }                     recent_collision = true;                } else {                    recent_collision = false;                }                score += bricks.checkForCollision(ball);                if (state == PLAY && score >= hi_score) {                    hi_score = score;                }                 if (bricks.isClean()) {                    nextLevel();                }                 if (ball.y >= paddle.y + paddle.height) {                    if (state == PLAY) {                        lives--;                    }                     if (lives < 0) {                        state = OVER;                        level = 0;                    }                     restartLevel();                }             }             screen.repaint();            delta = (int) (System.currentTimeMillis() - then);            if (delta < 30 && calibration < 100) {                try {                    Thread.sleep(30 - delta);                } catch (InterruptedException e) {}            }             then = System.currentTimeMillis();        }     } }

⌨️ 快捷键说明

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