import java.io.IOException;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
public class MyCanvas extends Canvas implements Runnable{
//动画图片
Image img = null;
Image hero = null;
//动画序列
public int standFrame[] = new int[] { 7 };
public int upFrame[] = new int[] { 0, 1, 2, 1 };
public int downFrame[] = new int[] { 6, 7, 8, 7 };
public int leftFrame[] = new int[] { 9, 10, 11, 10 };
public int rightFrame[] = new int[] { 3, 4, 5, 4 };
// 当前的动画索引
int curIndex=0;
// 当前的按键
int curKey;
int stepLength = 6;
// 当前动作
int curAction[];
int x=0;
int y=0;
private int[][] mapData = new int[20][20];
int maxWidth;
int maxHeight;
Image buffImage;
Graphics buffGraphics;
public MyCanvas() {
this.setFullScreenMode(true);
try {
img = Image.createImage("/map/map0.png");
hero = Image.createImage("/hero/hero.png");
//创建双缓冲
buffImage=Image.createImage(getWidth(), getHeight());
buffGraphics=buffImage.getGraphics();
} catch (IOException e) {
e.printStackTrace();
}
maxWidth=mapData[0].length*24;
maxHeight=mapData.length*24;
//设定地图数据,对应片中的索引,地图索引从1开始,0表示没有
mapData = new int[][] {
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 1, 18, 18, 18, 18, 18, 18, 18, 18, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 2, 18, 18, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 2, 18, 18, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1, 6, 1, 2, 18, 18, 1, 6, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 18, 18, 1, 1, 1, 1, 1, 6, 1, 1, 1, 1 },
{ 1, 1, 1, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 1, 1, 1, 1,
1, 1, 1 },
{ 1, 1, 1, 1, 1, 18, 18, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 1, 6, 1, 1, 18, 18, 20, 1, 1, 1, 1, 6, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1, 18, 18, 18, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1, 1, 18, 18, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 20, 1, 18, 18, 1, 20, 1, 1, 1, 1, 20, 1, 1, 1, 1,
1 },
{ 1, 1, 1, 1, 1, 1, 1, 18, 18, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 18, 18, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 10, 10, 10, 10, 10, 10, 10, 18, 18, 1, 1, 1, 10, 10, 10, 10,
10, 10, 10, 10 },
{ 1, 1, 1, 1, 1, 1, 1, 18, 18, 18, 18, 1, 1, 1, 1, 1, 1, 1, 1,
1 },
{ 1, 1, 1, 1, 1, 1, 1, 18, 18, 18, 18, 1, 1, 1, 1, 1, 1, 1, 1,
1 },
{ 18, 18, 18, 1, 1, 1, 18, 18, 18, 18, 18, 18, 18, 18, 1, 1, 1,
1, 1, 1 },
{ 1, 1, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 1, 1, 1, 1,
1, 1, 1 } };
curAction = standFrame;
Thread t=new Thread(this);
t.start();
}
protected void paint(Graphics g) {
g.setColor(0xffffff);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
this.drawMap(g);
this.drawHero(g);
}
//人物与地图的碰撞判断
int block[]={2,6,8,10,20};
public boolean block(int x,int y){
int cx=0,cy=0;
//当前坐标转化为在地图上的索引
switch(curKey){
case -1:
cx=x/24;
cy=y/24;
break;
case -2:
cx=(x+hero.getWidth()/3-10)/24;
cy=(y+hero.getHeight()/4-10)/24;
break;
case -3:
cx=(x+hero.getWidth()/3-10)/24;
cy=(y+hero.getHeight()/4-10)/24;
break;
case -4:
cx=(x+hero.getWidth()/3-10)/24;
cy=(y+hero.getHeight()/4-10)/24;
break;
}
for(int i=0;i<block.length;i++)
if(mapData[cy][cx]==block[i])
return true;
return false;
}
// 绘制地图
// 计算屏幕位置
// Image buffImage;
// Graphics buffGraphics;
int sx;
int sy;
public void drawMap(Graphics g) {
sx=x-getWidth()/2<0?0:x-getWidth()/2;
sy=y-getHeight()/2<0?0:y-getHeight()/2;
sx=x-getWidth()/2>this.maxWidth-getWidth()-1?maxWidth-getWidth()-1:sx;
sy=y-getHeight()/2>this.maxHeight-getHeight()-1?maxHeight-getHeight()-1:sy;
//绘制地图的开始索引
int startIndexX=sx/24;
int startIndexY=sy/24;
//偏移量
int offsetX=sx%24;
int offsetY=sy%24;
//图片的当前索引
int xPos=0;
int yPos=0;
for(int i=startIndexX;i<startIndexX+getWidth()/24+1;i++)
{
for(int j=startIndexY;j<startIndexY+getHeight()/24+1;j++){
this.drawClipImg(xPos*24-offsetX, yPos*24-offsetY, 24, 24,
(mapData[j][i]-1)*24, 0, img, buffGraphics);
yPos++;
}
//y轴方向归零
xPos++;
yPos=0;
}
g.drawImage(buffImage, 0, 0, 20);
}
//画HERO
public void drawHero(Graphics g){
//确定当前的索引
curIndex=++curIndex>=curAction.length?0:curIndex;
//x-sx为当前坐标相对的屏幕位置
this.drawClipImg(x-sx, y-sy-5, hero.getWidth()/3, hero.getHeight()/4,
curAction[curIndex]%3*24, curAction[curIndex]/3*24, hero, g);
this.repaint();
}
//切割显示图片
public void drawClipImg(int xScreenPos, int yScreenPos, int imgW, int imgH,
int xImgPos, int yImgPos, Image img, Graphics g) {
if(img==null)
return;
g.setClip(xScreenPos, yScreenPos, imgW, imgH);
g.drawImage(img, xScreenPos - xImgPos, yScreenPos - yImgPos,
Graphics.TOP | Graphics.LEFT);
g.setClip(0, 0, this.getWidth(), this.getHeight());
}
public void keyPressed(int key) {
this.curKey=key;
switch(curKey){
case -1:
this.curAction=upFrame;
break;
case -2:
this.curAction=this.downFrame;
break;
case -3:
this.curAction=this.leftFrame;
break;
case -4:
this.curAction=this.rightFrame;
break;
}
}
public void keyReleased(int key) {
curKey=500;
//设定站立动画
this.curAction=standFrame;
}
public void move() {
int bx=x,by=y;
switch(curKey){
case -1:
by=by-this.stepLength<hero.getHeight()/4?0:by-this.stepLength;
break;
case -2:
by=by+this.stepLength+hero.getHeight()/4>maxHeight?maxHeight-hero.getHeight()/4:by+this.stepLength;
break;
case -3:
bx=bx-this.stepLength<0?0:bx-this.stepLength;
break;
case -4:
bx=bx+this.stepLength+hero.getWidth()/3>maxWidth?maxWidth-hero.getWidth()/3:bx+this.stepLength;
break;
}
if(block(bx,by))
return;
x=bx;
y=by;
}
public void run() {
while(true){
this.repaint();
this.move();
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
RoleMap.rar_地图
共17个文件
png:6个
class:4个
java:2个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 200 浏览量
2022-09-14
17:52:16
上传
评论
收藏 93KB RAR 举报
温馨提示
人物漫游,地图卷轴,地图的切割和显示,人物的碰撞
资源推荐
资源详情
资源评论
收起资源包目录
RoleMap.rar (17个子文件)
RoleMap
.eclipseme 242B
bin
map
map0.png 14KB
MyCanvas.class 7KB
RoleMap.class 949B
hero
hero.png 13KB
verified
classes
map
map0.png 14KB
MyCanvas.class 8KB
RoleMap.class 949B
hero
hero.png 13KB
libs
deployed
res
map
map0.png 14KB
hero
hero.png 13KB
.settings
org.eclipse.jdt.core.prefs 208B
src
RoleMap.java 660B
MyCanvas.java 7KB
.project 539B
.classpath 242B
RoleMap.jad 221B
共 17 条
- 1
资源评论
钱亚锋
- 粉丝: 90
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 2023-04-06-项目笔记 - 第一百五十四阶段 - 4.4.2.152全局变量的作用域-152 -2024.06.04
- 松哥解协议松哥解协议松哥解协议松哥解协议松哥解协议
- 618节日618节日618节日
- tensorflow-gpu-2.9.1-cp37-cp37m-win-amd64.whl
- tensorflow-gpu-2.9.0-cp37-cp37m-win-amd64.whl
- tensorflow-gpu-2.9.0-cp39-cp39-win-amd64.whl
- lcd daimalcd daima
- 电影领域-推荐算法-个性化内容-观影决策-电影推荐小程序.zip
- 电气控制PLC考试题库
- 如何使用MATLAB简介
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功