使用MIDP2.0开发游戏(2)使用Sprite

文章来源:J2ME开发网

Sprite,精灵,顾名思义,专用来代表游戏中的动画角色,比如飞机,坦克等等。在MIDP1.0中,我们必须自己写专门的类来实现Sprite,幸运的是,MIDP2.0为Sprite提供了强力支持,可以创建静态,动态,不透明和透明的Sprite,下面我们准备在上次的GameCanvas基础上添加一个Sprite并让它动起来。

Sprite的主要构造方法有:

Sprite(Image):构造一个单幅图案的Sprite;

Sprite(Image, int width, int height):构造一个动画Sprite,图片将按照指定大小被分为N个Frame,通过setFrame(int index)就可以让Sprite动起来。我们用了一个有透明背景的png图片创建坦克的Sprite:

注意这个图是放大的JPG格式,你需要用Photoshop之类的软件处理成有透明背景的png格式,大小为64x16

我们在Eclipse中建立如下工程和目录:

以下是画出Sprite的TankGameCanvas.java

package tank.midp.core;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;

public class TankGameCanvas extends GameCanvas implements Runnable {
    // 控制方向:
    private static int INDEX_OF_UP = 0;
    private static int INDEX_OF_DOWN = 1;
    private static int INDEX_OF_LEFT = 3;
    private static int INDEX_OF_RIGHT = 2;

    private boolean isPlay; // Game Loop runs when isPlay is true
    private long delay; // To give thread consistency
    private int currentX, currentY; // To hold current position of the 'X'
    private int width; // To hold screen width
    private int height; // To hold screen height

    private Sprite spriteTank; // our sprite!

    // Constructor and initialization
    public TankGameCanvas() {
        super(true);
        width = getWidth();
        height = getHeight();
        currentX = width / 2;
        currentY = height / 2;
        delay = 20;
        // init sprite:
        try {
            Image image = Image.createImage("/res/img/player1.png"); // 注意路径
            spriteTank = new Sprite(image, 16, 16); // 大小是16x16
        }
        catch(Exception e) { e.printStackTrace(); }
    }

    // Automatically start thread for game loop
    public void start() {
        isPlay = true;
        new Thread(this).start();
    }

    public void stop() { isPlay = false; }

    // Main Game Loop
    public void run() {
        Graphics g = getGraphics();
        while (isPlay) {
            input();
            drawScreen(g);
            try {
                Thread.sleep(delay);
            }
            catch (InterruptedException ie) {}
        }
    }

    // Method to Handle User Inputs
    private void input() {
        int keyStates = getKeyStates();
        // Left
        if ((keyStates & LEFT_PRESSED) != 0) {
            currentX = Math.max(0, currentX - 1);
            spriteTank.setFrame(INDEX_OF_LEFT);
        }
        // Right
        if ((keyStates & RIGHT_PRESSED) !=0 ) {
            if ( currentX + 5 < width)
                currentX = Math.min(width, currentX + 1);
            spriteTank.setFrame(INDEX_OF_RIGHT);
        }
        // Up
        if ((keyStates & UP_PRESSED) != 0) {
            currentY = Math.max(0, currentY - 1);
            spriteTank.setFrame(INDEX_OF_UP);
        }
        // Down
        if ((keyStates & DOWN_PRESSED) !=0) {
            if ( currentY + 10 < height)
                currentY = Math.min(height, currentY + 1);
            spriteTank.setFrame(INDEX_OF_DOWN);
        }
    }
    // Method to Display Graphics
    private void drawScreen(Graphics g) {
        g.setColor(0); // black
        g.fillRect(0, 0, getWidth(), getHeight());

        // 画一个Sprite非常简单:
        spriteTank.setPosition(currentX, currentY);
        spriteTank.paint(g);

        flushGraphics();
    }
}

运行后的画面如下,可以用上下左右控制坦克:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值