📄 rpgcanvas.java
字号:
import java.util.Random;
import java.util.Vector;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.LayerManager;
public class RpgCanvas extends GameCanvas implements Runnable {
// 定义屏幕宽和屏幕高的变量
private int screenwidth = getWidth();
private int screenheight = getHeight();
/**
* 线程和笔属性
*/
Thread t;
Graphics g;
//声明加载类
Load load;
/**
* 主角属性
*/
// 声明主角类变量
MainRole mrole;
/**
* 图层属性
*/
// 声明图层管理一
LayerManager lmscene1;
// 声明图层管理二
LayerManager lmscene2;
// 设置图层管理一的坐标
int screenx, screeny;
// 检测转换场景的变量,初始为农场,值为2时变为房屋
int scene=1;
// 声明图层背景图宽和高
int imgbackgroundwidth = 512;
int imgbackgroundheight = 512;
// 声明图层最后的坐标
int lastscreenx, lastscreeny;
/**
* 键盘属性
*/
// 键盘输入索引值
int inputmode = 0;
/**
* 状态检测变量的声明
*/
// 声明控制小猫NPC的显示的变量
boolean catchcat = false;
boolean catchcat2 = false;
// 声明控制对话主斛选择的变量
boolean showyesorno = false;
// 声明检测碰撞的变量
boolean iscollide = false;
// 声明检测主角回话的变量
boolean mainroleanswer = false;
/**
* 对话变量
*/
// 声明对话肯定话语变量
String missionmessage;
int selecty = 152;
Random r;
Vector vectormessage;
Vector vectorpicture;
String curmessage;
Image curpicture;
int tempx = 0, tempy = 0;
/**
* 屏幕类构造方法,用于生成笔,线程,提取图片图层类,图层管理类等,
* 启动线程
*/
public RpgCanvas(boolean arg0) {
super(arg0);
g = getGraphics();
t = new Thread(this);
/*
* 提取图片、图层、
*/
load = new Load();
// 生成主角对象,从Load类中取主角图片
mrole = new MainRole(load.imgrole, 16, 24, 50, 50,80,153);
/*
* 生成图层管理对象
*/
lmscene1 = new LayerManager();//生成图层管理一
lmscene2 = new LayerManager();//生成图层管理二
r = new Random();
vectormessage = new Vector();
vectorpicture = new Vector();
curmessage = null;
missionmessage = null;
curpicture = null;
t.start();
}
/**绘画函数
*/
void draw() {
//清屏
g.setColor(255, 255, 255);
g.fillRect(0, 0, getWidth(), getHeight());
if (scene==1)
{
// 农场图层管理的显示窗口设置
lmscene1.setViewWindow(screenx, screeny, screenwidth, screenheight);
// 向图层管理1添加图层的方法
append();
lmscene1.paint(g, 0, 0);
}
else if (scene==2)
{
mrole.setPosition(mrole.roomx, mrole.roomy);
// 房屋图层管理的显示窗口设置
lmscene2.setViewWindow(0, 0, screenwidth, screenheight);
append2();
lmscene2.paint(g, 0, 0);
}
// 显示对话
drawmessage();
if (showyesorno == true) {
drawlist();
}
if (missionmessage != null) {
g.setColor(255, 255, 255);
g.drawString(missionmessage, 0, 0, 0);
}
flushGraphics();
}
// 绘制对话函数
public void drawmessage() {
if (curmessage != null) {
//设置对话框的大小和颜色
g.setColor(0, 0, 100);
g.fillRect(0, getHeight() - 40, getWidth(), 40);
// 画回答问题NPC的BIG图
g.drawImage(curpicture, 0, getHeight() - 40, 0);
Font f = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_MEDIUM);
g.setFont(f);
g.setColor(255, 255, 255);
if (curmessage.length() <= getWidth() / 12) {
g.drawString(curmessage, 37, getHeight() - 40, 0);
} else {
int length = curmessage.length();
int len = getWidth() / 12;
int y = getHeight() - 40;
int offset = 0;
while (length > 0) {
g.drawSubstring(curmessage, offset, len, 37, y, 0);
y += 12;
offset += getWidth() / 12;
length -= getWidth() / 12;
if (length > getWidth() / 12) {
len = getWidth() / 12;
} else {
len = length;
}
}
}
// 画取消对话框提示
Font ff = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN,Font.SIZE_SMALL);
g.setFont(ff);
g.setColor(255, 255, 255);
g.drawString("Push FIRE", 130, getHeight() - 10, 0);
}
}
/**画对话列表
*
*
*/
public void drawlist() {
// 设置并画对话框的大小和颜色
g.setColor(0, 0, 100);
g.fillRect(0, getHeight() - 40, getWidth(), 40);
g.setColor(255, 255, 128);
// 画主角选择答案的字符串
g.drawString("好的", 10, getHeight() - 30, 0);
g.drawString("不了", 10, getHeight() - 20, 0);
g.setColor(255, 255, 0);
g.fillArc(5, selecty, 5, 5, 0, 360);
// 画取消对话框提示
Font ff = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN,Font.SIZE_SMALL);
g.setFont(ff);
g.setColor(255, 255, 255);
g.drawString("Push FIRE", 130, getHeight() - 10, 0);
}
public void nextmessage() {
if (vectormessage.size() > 0 && vectorpicture.size() > 0)
{
curpicture = (Image) vectorpicture.elementAt(0);
curmessage = (String) vectormessage.elementAt(0);
}
if (Math.abs(tempx - mrole.x) > 4 || Math.abs(tempy - mrole.y) > 4) {
curmessage = null;
vectormessage.removeAllElements();
vectorpicture.removeAllElements();
showyesorno = false;
iscollide = false;
}
}
/**得到随机数方法
*
*/
int getrand(int m) {
int n = r.nextInt();
if (n < 0)
n *= -1;
n %= m;
return n;
}
/** 键盘输入函数
*/
void input() {
int key = getKeyStates();
/*
* 在农场上控制屏幕和主角的按键输入
*/
if (inputmode == 0) {
if ((key & FIRE_PRESSED) != 0) {
if (catchcat == true) {
load.property[0].setVisible(false);
catchcat2 = true;
} else {
if (iscollide == true) {
showyesorno = true;
inputmode = 1;
}
}
}
// 向上按键
if ((key & UP_PRESSED) != 0) {
// 主角向上移动
mrole.setFrame(mrole.directionup++);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -