3个RPG练习,最后一个是卡马克卷轴

25 篇文章 0 订阅
23 篇文章 0 订阅

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.LayerManager;
import javax.microedition.lcdui.game.Sprite;
import javax.microedition.lcdui.game.TiledLayer;


public class AppCanvas extends GameCanvas implements Runnable{

	private Graphics g;
	private Image roleImg,groundImg;
	private TiledLayer barrierLayer,groundLayer;
	private Sprite hero;
	private LayerManager layerManager;
	private int WORLD_WIDTH,WORLD_HEIGHT;//地图宽高
	private int[] view = new int[4];//可视窗口大小
	private static final int X = 0;
	private static final int Y = 1;
	private static final int WIDTH = 2;
	private static final int HEIGHT = 3;
	private static int STEP = 12;
	
	private int direction = -1;
	private int CELL_WIDTH=24,CELL_HEIGHT=35;
	protected AppCanvas() {
		super(true);
		// TODO Auto-generated constructor stub
		setFullScreenMode(true);
		g=this.getGraphics();
		roleImg=Tools.getImage("/hero.png");
		groundImg=Tools.getImage("/map.png");
		layerManager=new LayerManager();
		hero = new Sprite(roleImg, 24, 29);
		barrierLayer = new TiledLayer(12, 12, groundImg, CELL_WIDTH, CELL_HEIGHT);
		groundLayer = new TiledLayer(12, 12, groundImg, CELL_WIDTH, CELL_HEIGHT);
		hero.defineCollisionRectangle(4,4,hero.getWidth()-8,hero.getHeight()-8);
		hero.setPosition(CELL_WIDTH*2, CELL_HEIGHT*6);
		
		Tools.fillCells(groundLayer, Consts.groundLayer_cells);
		Tools.fillCells(barrierLayer, Consts.barrierLayer_cells);
		
		//获得地图大小
		WORLD_WIDTH=groundLayer.getWidth();
		WORLD_HEIGHT=groundLayer.getHeight();
		
		//添加到图层
		layerManager.append(hero);
		layerManager.append(barrierLayer);
		layerManager.append(groundLayer);
		view[X] = 0;
		view[Y] = 0;
		view[WIDTH] = 240;//getWidth();//240
		view[HEIGHT] = 320;//getHeight();//320
		layerManager.setViewWindow(view[X], view[Y], view[WIDTH], view[HEIGHT]);
		
		new Thread(this).start();
	}

	public void run() {
		// TODO Auto-generated method stub
		while (true){
			long start=System.currentTimeMillis();
			input();
			draw(g);
			long end=System.currentTimeMillis();
			if(end-start<100){
				try {
					Thread.sleep(100-(end-start));
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		
	}
	public void draw(Graphics g){
		layerManager.paint(g, 0, 0);
		this.flushGraphics();
	}
	public void input() {
		int keyState=getKeyStates();
		if ((keyState & DOWN_PRESSED) != 0) {
			if (direction != DOWN_PRESSED) {
				direction = DOWN_PRESSED;
				hero.setFrameSequence(new int[] { 6, 7, 8 });
			} else {
				hero.nextFrame();
			}
			if (hero.getY() + hero.getHeight() < WORLD_HEIGHT)
				hero.move(0, STEP);
			else
				hero.setPosition(hero.getX(), WORLD_HEIGHT - hero.getHeight());
		} else if ((keyState & UP_PRESSED) != 0) {
			if (direction != UP_PRESSED) {
				direction = UP_PRESSED;
				hero.setFrameSequence(new int[] { 0, 1, 2 });
			} else {
				hero.nextFrame();
			}
			if (hero.getY() - STEP >= 0)
				hero.move(0, -STEP);
			else
				hero.setPosition(hero.getX(), 0);
		} else if ((keyState & LEFT_PRESSED) != 0) {
			if (direction != LEFT_PRESSED) {
				direction = LEFT_PRESSED;
				hero.setFrameSequence(new int[] { 9, 10, 11 });
			} else {
				hero.nextFrame();
			}
			if (hero.getX() - STEP >= 0)
				hero.move(-STEP, 0);
			else
				hero.setPosition(0, hero.getY());
		} else if ((keyState & RIGHT_PRESSED) != 0) {
			if (direction != RIGHT_PRESSED) {
				direction = RIGHT_PRESSED;
				hero.setFrameSequence(new int[] { 3, 4, 5 });
			} else {
				hero.nextFrame();
			}
			if (hero.getX() + hero.getWidth() < WORLD_WIDTH)
				hero.move(STEP, 0);
			else
				hero.setPosition(WORLD_WIDTH - hero.getWidth(), hero.getY());
		}
		checkCollision(direction);
		// Next scroll the view if needed
		//地图滚动
		if (hero.getX() < view[X] +view[WIDTH]/2){//左,+ hero.getWidth()
			int dx = view[X] +view[WIDTH]/2 - hero.getX() ;
			if (view[X] - dx >= 0){
				view[X] -= dx;
			}
		}else if(hero.getX() > view[X]  + view[WIDTH]/2){//右- hero.getWidth()
			int dx = hero.getX()- view[X]  - view[WIDTH]/2;
			if(view[X] + view[WIDTH] <= WORLD_WIDTH){
				view[X] += dx;
			}
		}

		if (hero.getY() < view[Y] + view[HEIGHT]/2){ // scoll up,+hero.getHeight()
			int dy = view[Y] + view[HEIGHT]/2 - hero.getY();
			if (view[Y] - dy >= 0){
				view[Y] -= dy;
			}
		}else if(hero.getY()> view[Y] + view[HEIGHT]/2){ // scroll down
			int dy =hero.getY() - view[Y] - view[HEIGHT]/2;
			if(view[Y] + view[HEIGHT] <= WORLD_HEIGHT){
				view[Y] += dy;
			}
		}

		layerManager.setViewWindow(view[X],view[Y],view[WIDTH],view[HEIGHT]);
	}

	private void checkCollision(int direction) {
		if (hero.collidesWith(barrierLayer, false)) {
			switch (direction) {
			case LEFT_PRESSED:
				hero.move(STEP, 0);
				break;
			case RIGHT_PRESSED:
				hero.move(-STEP, 0);
				break;
			case UP_PRESSED:
				hero.move(0, STEP);
				break;
			case DOWN_PRESSED:
				hero.move(0, -STEP);
				break;
			}
			positionAtWorld();
			//障碍物的值
			byte arrayValue=Consts.barrierLayer_cells[indexY*barrierLayer.getColumns()+indexX];
			System.out.println("arrayValue="+arrayValue);
			switch (arrayValue) {
			case 5:
			case 14:
			case 15:
			case 16:
			case 17:
			case 19:
				//根据障碍物的值一一处理事件
				break;
			}
		}
	}
	//障碍物在地图上的列,行坐标索引(根据人物当前位置和方向推算)
	private int indexX,indexY;//角色碰撞的障碍物在地图上的列,行坐标索引
	private void positionAtWorld() {
		int x=hero.getX()/CELL_WIDTH;//24,这样不精确,不能整除的话可能要+1
		int y=hero.getY()/CELL_HEIGHT;//35
		System.out.println("x========"+x);
		System.out.println("y========"+y);
		switch (direction) {
		case UP_PRESSED:// 上
			indexX = x;
			indexY = y - 1;
			break;
		case DOWN_PRESSED:// 下
			indexX = x;
			indexY = y + 1;
			break;
		case LEFT_PRESSED:// 左
			indexX = x - 1;
			indexY = y;
			break;
		case RIGHT_PRESSED:// 右
			indexX = x + 1;
			indexY = y;
			break;
		}
	}
	
}



import java.io.IOException;

import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.TiledLayer;


public class Tools {

	public static Image getImage(String name){
		Image image=null;
		try {
			image=Image.createImage(name);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return image;
	}
	
	public static final boolean isCollideWith(int ax,int ay,int aw,int ah,int bx,int by,int bw,int bh){
		if(ay-by>bh||ax-bx>bw||by-ay>ah||bx-ax>aw){
			return false;
		}
		return true;
	}
	
	public static void fillCells(TiledLayer tl,byte[][] map){
		//二维数组
		for(int i=0;i<tl.getColumns();i++)
			for(int j=0;j<tl.getRows();j++)
				tl.setCell(i, j, map[j][i]);
	}
	
	public static void fillCells(TiledLayer tl,byte[] map){
		//一维数组
		for(int i=0;i<map.length;i++){
			int col=i%tl.getColumns();
			int row=(i-col)/tl.getColumns();
			tl.setCell(col,row,map[i]);
		}
	}
	
}




public class Consts {

	//键值 Nokia,SE机型
	  public final static int KEY_UP = -1;
	  public final static int KEY_DOWN = -2;
	  public final static int KEY_LEFT = -3;
	  public final static int KEY_RIGHT = -4;
	  public final static int KEY_LS = -6;
	  public final static int KEY_RS = -7;
	  public final static int KEY_OK = -5;
	  public final static int KEY_BACK = -11;
	  
	  public final static byte KEY_0 = 48;
	  public final static byte KEY_1 = 49;
	  public final static byte KEY_2 = 50;
	  public final static byte KEY_3 = 51;
	  public final static byte KEY_4 = 52;
	  public final static byte KEY_5 = 53;
	  public final static byte KEY_6 = 54;
	  public final static byte KEY_7 = 55;
	  public final static byte KEY_8 = 56;
	  public final static byte KEY_9 = 57;
	  public final static byte KEY_STAR = 42;
	  public final static byte KEY_SHARP = 35;
	  
	  
	  public static byte[] groundLayer_cells = new byte[] {   
			  1, 1, 2, 3, 3, 3, 4, 3, 3, 3, 3, 3,
			  3, 3, 6, 3, 3, 7, 8,3, 3, 3, 3, 3,
			  3, 3, 6, 3, 3, 3, 3, 8,3, 3, 3, 3,
			  3, 3, 6, 3, 3, 3, 3, 7, 8, 9,3, 3,
			  3, 3, 6, 3, 3, 3, 3, 3, 7, 3,3, 3,
			  3, 3, 6, 3, 3, 3, 3, 3, 3, 3,3, 3,
			  11,11,10,11,11,11,11,11,11,11,3, 3,
			  3, 3, 6, 3, 3, 18, 3, 7, 3, 3,3, 3,
			  3, 3, 12, 1, 1, 1, 1, 13, 3, 3,3, 3,
			  3, 3, 3, 3, 3, 3, 3, 18, 3, 3,3, 3,
			  3, 3, 3, 3, 3, 3, 3, 18, 3, 3,3, 3,
			  3, 3, 3, 3, 3, 3, 3, 18, 3, 3,3, 3
			  };
	  public static byte[] barrierLayer_cells = new byte[] {
			   0, 0, 0, 14, 0, 0, 8, 5, 5, 5,5, 5,
			   0, 15, 0, 0, 0, 0, 0, 8, 5, 5,5, 5,
			   0, 0, 0, 15, 0, 0, 14, 8, 9, 5,5, 5,
			   0, 19, 0, 0, 15, 0, 14, 0, 8, 9,5, 5,
			   0, 0, 0, 20, 0, 14, 0, 0, 8, 9,5, 5,
			   0, 0, 0, 16, 14, 16, 0, 0, 0, 0,5, 5,
			   0, 0, 0, 0, 0, 0, 0, 0, 0, 0,5, 5,
			   15, 0, 0, 16, 0, 0, 0, 0, 0, 0,5, 5,
			   19, 15, 0, 0, 0, 0, 0, 0, 17, 0,5, 5,
			   0, 15, 16, 0, 0, 15, 0, 0, 0, 0,5, 5,
			   0, 15, 16, 0, 0, 15, 0, 0, 0, 0,0, 0,
			   0, 15, 16, 0, 0, 15, 0, 0, 0, 0,0, 0
			   };
	  
}







  • 大小: 36.3 KB
  • 大小: 51.7 KB
  • 大小: 11.2 KB
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值