初学乍练J2me游戏开发 (二)

上一篇中提到了GameCanvas 对象了,现在我们就来创建一个基于它的扩展类,起个名字叫TankGameKanvas,看代码前先介绍几个对象,
第一:Sprite对象,这个对象主要用来表现我们操纵的对象,比如本例中的坦克,Sprite有很多的方法和属性,我们这里用到的只有简单的几个
更详细的可以参考sun的midp2.0文档。

Sprite(Image image, int frameWidth, int frameHeight)
          Creates a new animated Sprite using frames contained in the provided Image.
            使用给定的图像的不同的帧来创建一个活动的家伙。
setFrame(int sequenceIndex)
          Selects the current frame in the frame sequence.

我们用的图片(放大后)如下
200493215040734.jpg(64pxX16px)      这样我们的Sprite对象就象一个白纸上挖了个16x16的孔,通过移动到原图的不同位置来显示坦克的
不同方向。关于原图文件有比较严格的要求,需要时png图像格式的,其他的都不可以。

现在看看代码如何实现吧。

//TankGameKanvas.java

None.gifimport javax.microedition.lcdui.*;
None.gifimport javax.microedition.lcdui.game.
*;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
public class TankGameCanvas extends GameCanvas implements Runnable dot.gif{
InBlock.gif    
// 控制方向:
InBlock.gif
    private static int INDEX_OF_UP = 0;
InBlock.gif    
private static int INDEX_OF_DOWN = 1;
InBlock.gif    
private static int INDEX_OF_LEFT = 3;
InBlock.gif    
private static int INDEX_OF_RIGHT = 2;
InBlock.gif
InBlock.gif    
private boolean isPlay;                             
InBlock.gif    
private long delay;                                 
InBlock.gif    
private int currentX, currentY;                     
InBlock.gif    
private int width;                                 
InBlock.gif    
private int height;                                 
InBlock.gif
InBlock.gif    
private Sprite spriteTank;                         
InBlock.gif    
InBlock.gif    
//构造函数
ExpandedSubBlockStart.gifContractedSubBlock.gif
    public TankGameCanvas() dot.gif{
InBlock.gif        super(
true);
InBlock.gif        
InBlock.gif        width 
= getWidth();        //获取屏幕宽度
InBlock.gif
        height = getHeight();        //获取屏幕高度
InBlock.gif
        currentX = width / 2;        //当前X坐标 居中
InBlock.gif
        currentY = height / 2;        //当前Y坐标 居中
InBlock.gif
        delay = 20;
InBlock.gif
InBlock.gif       
//看看我们的Sprite吧
ExpandedSubBlockStart.gifContractedSubBlock.gif
        try dot.gif{
InBlock.gif            Image image 
= Image.createImage("/res/img/player1.png"); // 注意路径,和文件格式
InBlock.gif
            spriteTank = new Sprite(image, 1616); // 大小是16x16
ExpandedSubBlockEnd.gif
        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
catch(Exception e) dot.gif{ e.printStackTrace(); }
ExpandedSubBlockEnd.gif    }

InBlock.gif   
//当开始的时候显示参数为真,并且再次启动线程,--这样就循环起来了。
ExpandedSubBlockStart.gifContractedSubBlock.gif
    public void start() dot.gif{
InBlock.gif        isPlay 
= true;
InBlock.gif        
new Thread(this).start();
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public void stop() dot.gif{ isPlay = false; }
InBlock.gif
InBlock.gif    
// -重要的是这里设置了线程,设置了启动间隔时间20毫秒
ExpandedSubBlockStart.gifContractedSubBlock.gif
    public void run() dot.gif{
InBlock.gif        Graphics g 
= getGraphics();
ExpandedSubBlockStart.gifContractedSubBlock.gif        
while (isPlay) dot.gif{
InBlock.gif            input();
InBlock.gif            drawScreen(g);
ExpandedSubBlockStart.gifContractedSubBlock.gif            
try dot.gif{
InBlock.gif                Thread.sleep(delay);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockStart.gifContractedSubBlock.gif            
catch (InterruptedException ie) dot.gif{}
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
// 对用户输入的捕获和处理
ExpandedSubBlockStart.gifContractedSubBlock.gif
    private void input() dot.gif{
InBlock.gif        
int keyStates = getKeyStates();
InBlock.gif        
// Left
ExpandedSubBlockStart.gifContractedSubBlock.gif
        if ((keyStates & LEFT_PRESSED) != 0dot.gif{
InBlock.gif            currentX 
= Math.max(0, currentX - 1);
InBlock.gif            spriteTank.setFrame(INDEX_OF_LEFT);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
// Right
ExpandedSubBlockStart.gifContractedSubBlock.gif
        if ((keyStates & RIGHT_PRESSED) !=0 ) dot.gif{
InBlock.gif            
if ( currentX + 5 < width)
InBlock.gif                currentX 
= Math.min(width, currentX + 1);
InBlock.gif            spriteTank.setFrame(INDEX_OF_RIGHT);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
// Up
ExpandedSubBlockStart.gifContractedSubBlock.gif
        if ((keyStates & UP_PRESSED) != 0dot.gif{
InBlock.gif            currentY 
= Math.max(0, currentY - 1);
InBlock.gif            spriteTank.setFrame(INDEX_OF_UP);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
// Down
ExpandedSubBlockStart.gifContractedSubBlock.gif
        if ((keyStates & DOWN_PRESSED) !=0dot.gif{
InBlock.gif            
if ( currentY + 10 < height)
InBlock.gif                currentY 
= Math.min(height, currentY + 1);
InBlock.gif            spriteTank.setFrame(INDEX_OF_DOWN);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif    
// 显示
ExpandedSubBlockStart.gifContractedSubBlock.gif
    private void drawScreen(Graphics g) dot.gif{
InBlock.gif        g.setColor(
0); // black
InBlock.gif
        g.fillRect(00, getWidth(), getHeight());
InBlock.gif
InBlock.gif        spriteTank.setPosition(currentX, currentY);
InBlock.gif        spriteTank.paint(g);
InBlock.gif        flushGraphics();
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif



编译一下,你的小坦克应该可以来回乱跑了。;)

转载于:https://www.cnblogs.com/shichao/archive/2004/11/04/60374.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值