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

📄 racerlayermanager.java

📁 Programming java 2 microedition symbian os一书中作者编写的一个赛车游戏的半成品
💻 JAVA
字号:
/*
 * Copyright 2003, 2004 Symbian Ltd.
 * For License terms see http://www.symbian.com/developer/techlib/codelicense.html
 */

package demoracer;

/**
 * <p>Title: DemoRacer</p>
 * <p>Description: Game API Demo for Symbian MIDP 2.0 Book.</p>
 * @author Alan Newman - alan@sensibledevelopment.net
 * @version 1.0
 */


/*
 * This manages the animation process.
 * GameCanvas (RacerCanvas) creates this class and passes it a Grpahics context via its draw() method.
 * This class creates all the Sprites by passing in PNG files. These Sprites are then added to the layerr manager's index.
 * The Layers are added to the Manager in the order in which they should be dosplayed, with the layer at the "back"
 * of the display being added first.
 */
import javax.microedition.lcdui.game.*;
import javax.microedition.lcdui.*;
import java.io.IOException;

public class RacerLayerManager extends LayerManager {
    
    private Background backGround;
    private Car car;
    private Puddle puddle;
    private StartFinish startFinish;
    private int xWindow;
    private int yWindow;
    private RacerGameCanvas gameCanvas;
    private final String LAP_COMPLETE="Lap Complete";
    private final String PAUSED="PAUSED";
    private int yOffset=0;
    private int xOffset=0;
    private Font font = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_LARGE);
    
    public RacerLayerManager(RacerGameCanvas gameCanvas) throws IOException {
        // get the GameCanvas and set it to full screen mode
        this.gameCanvas=gameCanvas;
        gameCanvas.setFullScreenMode(true);
        // create the sprites and then add them to the layer manager
        backGround=createBackground();
        startFinish=createStartFinishSprite();
        puddle=createPuddleSprite();
        car=createCarSprite();
        this.append(car);
        this.append(puddle);
        this.append(startFinish);
        this.append(backGround);
    }
    
    // move the sprite objects on to the next frame.
    public void tick() {
        backGround.tick();
        car.tick();
        puddle.tick();
        startFinish.tick();
    }
    
    public Puddle getPuddle(){return puddle;}
    public StartFinish getStartFinish(){return startFinish;}
    
    // this draws all the Sprites to the display
    public void draw(Graphics g) {
        // appears to be a bug in implementation so have to query the FullCanvas size after repaint.
        g.setClip(0,0,gameCanvas.getWidth(),gameCanvas.getHeight());
        paint(g, xOffset, yOffset);
        drawMessage(g);
    }
    
    private void drawMessage(Graphics g) {
        int x;
        int y;
        g.setFont(font);
        // draw a "lap complete" message on screen according to the toggle.
        if(startFinish.getLapComplete()) {
            g.setColor(200,0,0);
            x=gameCanvas.getWidth()/2;
            y=gameCanvas.getHeight()/2;
            g.setClip(0,y, gameCanvas.getWidth(), font.getHeight());
            g.drawString(LAP_COMPLETE,x,y,Graphics.TOP|Graphics.HCENTER);
        }
        
        if(!gameCanvas.isRunning()) {
            g.setColor(200,0,0);
            x=gameCanvas.getWidth()/2;
            y = gameCanvas.getHeight() / 2;
            g.drawString(PAUSED, x, y, Graphics.TOP | Graphics.HCENTER);
        }
        
        // draw the "Exit" button on the screen
        x=0;
        g.setColor(0,0,0);
        y=gameCanvas.getHeight()-font.getHeight();
        g.setClip(0,y, gameCanvas.getWidth(), font.getHeight());
        g.drawString("Exit",2,y,Graphics.TOP|Graphics.LEFT);
        y=20;
        g.setClip(0,y, gameCanvas.getWidth(), font.getHeight());
    }
    
    private Background createBackground() throws IOException {
        Image image = null;
        image = Image.createImage("/background.png");
        return new Background(Background.WIDTH, Background.HEIGHT, image, Background.TILE_WIDTH, Background.TILE_HEIGHT);
    }
    
    private Car createCarSprite() throws IOException {
        Image image = Image.createImage("/car.png");
        int width = image.getWidth() / 2;
        int height = image.getHeight() / 2;
        int x=gameCanvas.getWidth()/5;
        int y=backGround.getCellHeight()*4-(int)(height*2);
        return new Car(image, width, height, x, y, this);
    }
    
    public StartFinish createStartFinishSprite() throws IOException {
        Image image = Image.createImage("/startfinish.png");
        int width = image.getWidth() / StartFinish.FRAME_COLS;
        int height = image.getHeight() / StartFinish.FRAME_WIDTH;
        int x = backGround.getCellWidth() * 4;
        int y = backGround.getCellHeight() * 3;
        return new StartFinish(image, width, height, x, y);
    }
    
    public Puddle createPuddleSprite() throws IOException {
        Image image = Image.createImage("/puddle.png");
        int width = image.getWidth() / Puddle.FRAME_COLS;
        int height = image.getHeight() / Puddle.FRAME_WIDTH;
        int x = backGround.getCellWidth() * 3;
        int y = backGround.getCellHeight() * 3;
        return new Puddle(image, width, height, x, y);
    }
    
}

⌨️ 快捷键说明

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