Chinaunix首页 | 论坛 | 博客
  • 博客访问: 7278977
  • 博文数量: 512
  • 博客积分: 12019
  • 博客等级: 上将
  • 技术积分: 6857
  • 用 户 组: 普通用户
  • 注册时间: 2005-08-01 16:46
文章分类

全部博文(512)

文章存档

2024年(2)

2022年(2)

2021年(6)

2020年(59)

2019年(4)

2018年(10)

2017年(5)

2016年(2)

2015年(4)

2014年(4)

2013年(16)

2012年(47)

2011年(65)

2010年(46)

2009年(34)

2008年(52)

2007年(52)

2006年(80)

2005年(22)

分类:

2010-09-29 11:12:38

以下代码是了解j2me游戏的代码,根据教科书的的例子修改的,实现了 地图显示,屏幕滚屏,人物走动等基本功能,仅供学习用,若在项目中用还要优化。附件为对应代码。使用时,先建立一个工程,然后直接解开src.rar即可。
文件:src.rar
大小:9KB
下载:下载



 

package com.test;
import java.io.IOException;
import javax.microedition.lcdui.game.TiledLayer;
import javax.microedition.lcdui.game.LayerManager;
import javax.microedition.lcdui.game.Sprite;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.game.GameCanvas;
/**
 * @author Administrator
 */

public class MainApp extends MIDlet
{
    private Display display = null;
    public MainApp()
    {
      display = Display.getDisplay(this);
    }
    public void startApp()
    {
        Game mc = new Game();
        display.setCurrent(mc);

       }

    public void pauseApp()
    {
    }

    public void destroyApp(boolean unconditional)
    {
    }
}

class Game extends GameCanvas implements Runnable
{
    Graphics g;
    Sprite player;
    Image img;
     private Image playerImage = null;
    TiledLayer t1_map;
    LayerManager lm;
    int map_width;
    int map_height;
    int screen_width;
    int screen_height;
    int viewX;
    int viewY;

    int oldPosX ;
    int oldPosY ;

    public Game(){
        super(true);
        setFullScreenMode(true);
        screen_width = getWidth();
        screen_height = getHeight();
        initMap();
        new Thread(this).start();
    }

  //初始化地图

  public void initMap()
  {
        try{
        img =Image.createImage("/res/map.png");
        playerImage = Image.createImage("/res/hero.png");
        }catch (IOException e) {
        e.printStackTrace();
        }
        map_width = backMap[0].length*16;
        map_height = backMap.length*16;
        lm = new LayerManager();
        t1_map = new TiledLayer(backMap[0].length, backMap.length, img, 16, 16);

        player = new Sprite(playerImage, playerImage.getWidth()/4 , playerImage.getHeight()/4 );
        
        player.setPosition(screen_width/2 - player.getWidth()/2, screen_height/2 - player.getHeight()/2);
        for(int row =0;row <backMap.length; row++)
            for(int col = 0; col<backMap[row].length;col++)
            {
                t1_map .setCell(col,row,backMap[row][col]);
            }

      //两层,注意次序

      lm.append(player);
      lm.append(t1_map);
  }

  public void render(Graphics g)
  {
      g.setColor(255, 255, 255);
      g.fillRect(0, 0, screen_width, screen_height);
      lm.paint(g,0,0);
  }

//得到下一帧数。注意人物图片是要符合。参看hero.png格式

private int get_nextframe(int keyStatus)
{
    int num = 0;
    num = player.getFrame();
    if ((keyStatus & UP_PRESSED) != 0)
    {
        if (num >= 12 && num <= 15)
        {
            num++;
            if (num > 15)
                num = 12;
        } else
             num = 12;
     } else if ((keyStatus & DOWN_PRESSED) != 0)
     {
           if (num >= 0 && num <= 3)
           {
                num++;
                if (num > 3)
                    num = 0;
            } else
                num = 0;

    } else if ((keyStatus & LEFT_PRESSED) != 0)
    {
           if (num >= 4 && num <= 7)
           {
                num++;
                if (num > 7)
                    num = 4;
            } else
                num = 4;
    } else if ((keyStatus & RIGHT_PRESSED) != 0)
    {
        if (num >= 8 && num <= 11)
        {
            num++;
            if (num > 11)
                num = 8;
        } else
            num = 8;
    }

   return num;
}

  public void input()
  {
    int num = 0;
    int posX = player.getX();
    int posY = player.getY();
    int playerWidth = player.getWidth();
    int playerHeight = player.getHeight();

    int keyStates = getKeyStates();
    if ((keyStates&LEFT_PRESSED)!= 0)
    {
        posX -= 4;
        if((posX+playerWidth/2<=map_width-screen_width/2)&&(posX+playerWidth/2>=screen_width/2))
            viewX -=4;
    }
    else if ((keyStates&RIGHT_PRESSED)!= 0)
    {
        posX +=4;
        if((posX+playerWidth/2<=map_width-screen_width/2)&&(posX+playerWidth/2>=screen_width/2))
            viewX+=4;

    }
    else if ((keyStates&UP_PRESSED)!= 0)
    {
        posY -=4;
        if((posY+playerHeight/2<=map_height-screen_height/2)&&(posY+playerHeight/2>=screen_height/2))
            viewY-=4;
    }

    else if ((keyStates&DOWN_PRESSED)!= 0)
    {
        posY += 4;
        if((posY+playerHeight/2<=map_height-screen_height/2)&&(posY+playerHeight/2>=screen_height/2))
            viewY+=4;
    }

    if (posX+playerWidth>=map_width )
        posX = oldPosX;
    if (posY+playerHeight>=map_height )
        posY= oldPosY;


    if(viewX <=0)
        viewX =0;
    if(viewY <=0)
        viewY =0;
    if (viewX>=map_width-screen_width )
       viewX = map_width-screen_width;
    if (viewY>=map_height - screen_height )
        viewY = map_height - screen_height;
    if(posX <=0) posX =0;
    if(posY <=0) posY =0;

    num = get_nextframe(keyStates);
    player.setFrame(num);
    player.setPosition(posX, posY);
    oldPosY= posY;
    oldPosX= posX;
  }

  public void logic()
  {
      lm.setViewWindow(viewX,viewY,screen_width,screen_height);
  }

  public void run()
  {
      Graphics g = getGraphics();
      while(true)
      {
          long startTime = System.currentTimeMillis();
          input();
          logic();
          render(g);
          flushGraphics();
          long timeTaken = System.currentTimeMillis()- startTime;
          if (timeTaken <100)
          {
                try{
                    Thread.sleep(100- timeTaken);
                }catch (Exception e){}
           }
      }
  }
//地图数据

int[][] backMap = {
{ 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4 },
{ 4, 0, 0, 3, 2, 2, 4, 0, 0, 3, 2, 2, 3, 3, 3, 4, 4, 4, 0, 0, 0, 4 },
{ 4, 1, 1, 2, 2, 2, 4, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 1, 1, 0, 4 },
{ 4, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 3, 4, 4, 4, 0, 0, 0, 4 },
{ 4, 1, 1, 2, 2, 2, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 0, 0, 0, 4 },
{ 4, 0,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