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


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
};

}


[img]http://dl.iteye.com/upload/attachment/365614/011f4384-2e68-37f9-84ed-26771fb9fc85.png[/img]

[img]http://dl.iteye.com/upload/attachment/365612/69f2b5bd-befd-30e2-9764-cf95e33c1153.png[/img]

[img]http://dl.iteye.com/upload/attachment/365618/daa0bd76-4f23-3187-8f2d-c0303bd68d65.png[/img]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
资源包主要包含以下内容: ASP项目源码:每个资源包中都包含完整的ASP项目源码,这些源码采用了经典的ASP技术开发,结构清晰、注释详细,帮助用户轻松理解整个项目的逻辑和实现方式。通过这些源码,用户可以学习到ASP的基本语法、服务器端脚本编写方法、数据库操作、用户权限管理等关键技术。 数据库设计文件:为了方便用户更好地理解系统的后台逻辑,每个项目中都附带了完整的数据库设计文件。这些文件通常包括数据库结构图、数据表设计文档,以及示例数据SQL脚本。用户可以通过这些文件快速搭建项目所需的数据库环境,并了解各个数据表之间的关系和作用。 详细的开发文档:每个资源包都附有详细的开发文档,文档内容包括项目背景介绍、功能模块说明、系统流程图、用户界面设计以及关键代码解析等。这些文档为用户提供了深入的学习材料,使得即便是从零开始的开发者也能逐步掌握项目开发的全过程。 项目演示与使用指南:为帮助用户更好地理解和使用这些ASP项目,每个资源包中都包含项目的演示文件和使用指南。演示文件通常以视频或图文形式展示项目的主要功能和操作流程,使用指南则详细说明了如何配置开发环境、部署项目以及常见问题的解决方法。 毕业设计参考:对于正在准备毕业设计的学生来说,这些资源包是绝佳的参考材料。每个项目不仅功能完善、结构清晰,还符合常见的毕业设计要求和标准。通过这些项目,学生可以学习到如何从零开始构建一个完整的Web系统,并积累丰富的项目经验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值