📄 pcanvas.java
字号:
import java.io.InputStream;
import java.util.Random;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.media.Manager;
import javax.microedition.media.Player;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
/**
*
* <P>标题:UI类 </P>
* <P>描述:逻辑控制,渲染,输入处理等.</P>
* <P>作者:supren lee</P>
* <P>时间:V1.0 2006.9.18</P>
*
*/
public class PCanvas extends Canvas implements Runnable {
static final short WIDTH = 176;
static final short HEIGHT = 208;
private byte level;// 难度
PDoc doc;
static final byte G_SPLASH = 0;
static final byte G_LOGO = 1;
static final byte G_TITLE = 2;
static final byte G_MENU = 3;
static final byte G_SELECT= 4;
static final byte G_HELP = 5;
static final byte G_SET = 6;
static final byte G_TOP = 7;
static final byte G_ABOUT = 8;
static final byte G_START = 9;
static final byte G_PLAY = 10;
static final byte G_END = 11;
static final byte G_TYPE = 12;//类型选择
static final byte G_LOST =13;
static final byte G_WIN = 14;
static final byte G_OVER = 15;
static final byte G_PAUSE = 16;
private byte G_STATE;
//#ifdef _BT
static final byte G_NET =17;
static final byte G_SERVER = 18;
static final byte G_CLIENT = 19;
private Server server=null;
private Client client=null;
private byte isServer=0;
private String data="00";
//#endif
private boolean ispause=false;//是否暂停
static final String[] help={
"游戏简介: ",
"明是个喜欢游戏的孩",
"子,受到了冰果游戏",
"乐园的邀请,让他有",
"机会去冰果游戏乐园",
"参观,明为了表示自",
"己了解并喜欢冰果游",
"戏,参加了一个有趣",
"的游戏,在参观前,",
"明将会得到一大批散",
"乱的冰果游戏的宣传",
"画,明需要把这些宣",
"传画根据自己的了解",
"拼完整,而还必须选",
"择这个游戏是属于哪",
"一个游戏类型,表明",
"对游戏的了解。 ",
" ",
"规则说明: ",
"本游戏共分为两种难",
"度,简单难度的是16",
"块,而困难难度的是",
"25块。玩家需要利用",
"移动每块图片,最后",
"使这个图片成为一张",
"完整的图片。如果在",
"规定的时间内完成就",
"算胜利。 ",
" ",
"关于时间: ",
"随着关数增加,时间",
"减少。每关失败后还",
"有两次机会重新打乱",
" ",
"关于保险: ",
"保险只能保存一个,",
"能延长1/6的时间,保",
"险是自动触发的。 ",
" ",
"关于提示: ",
"简单难度提示可以无",
"限使用,困难难度提",
"示有次数限制。 ",
//#ifdef _BT
" ",
"关于蓝牙: ",
"在蓝牙模式,只有16",
"块图块。图块数显示",
"在分数的左边 ",
//#endif
" ",
"关于分数: ",
"分数显示在游戏界面",
"的上方的右边。 ",
" ",
"操作方法: ",
"左:左方向键或4键 ",
"右:右方向键或6键 ",
"上:上方向键或2键 ",
"下:下方向键或8键 ",
"选择:中间键或5键 ",
"提示: # 键 "
};
static final String[] dev={
"Programmer:",
"Supern Lee",
"",
"Engineer:",
"Hongtu Lin",
"",
"Designer:",
"Yumin Gao",
"",
"Tester:",
"Jin Zhong",
"Zhuo Li",
"",
};
Thread t;
private long starttime;
private long endtime;
private static long roundtime = 100;
private long limitime;
static final long onetime[]={600 * 1000,600 * 1000,600 * 1000,600 * 1000,600 * 1000,
480 * 1000,480 * 1000,480 * 1000,480 * 1000,480 * 1000,
360 * 1000,360 * 1000,360 * 1000,360 * 1000,360 * 1000,
300 * 1000,300 * 1000,300 * 1000,300 * 1000,300 * 1000};//每关的时间
private long pretime;
private int count = 0;
private int loadcount = 0;
private boolean pick=false;
Image imgmonternet, imgbbox, imgcom2us, slogan,title,w_water,w_smile,w_eye;
Image menu,menu_text;
Image flash[]=new Image[3];
Image plat, toolpic, time, alarm,last,about,font,num,posts,ui;
Image pointpic[]=new Image[6];
Image pic;
private Player sound;
private boolean soundon=true;
private short point = 0;
private short prepoint=-1;
private boolean canmove = false;
private String[] name = new String[3];
private long[] score = new long[4];
private int no;// 名次
private StringBuffer myname;//名字
static final char[] text = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
'w', 'x', 'y', 'z'};//字符
private short times=2;//打乱次数
private byte prompt=-1;// 提示,-1不提示,>0提示
private byte prompts;
static final byte[] sprompt={3,3,3,3,3,
2,2,2,2,2,
1,1,1,1,1,
0,0,0,0,0};//提示次数
private short[] order={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19};//图片顺序
static final String[] gametype={"动作类","射击类","益智类","体育类","角色扮演","养成类","策略类","棋牌类","竞速类","格斗类"};
static final byte[] typenum={0,1,0,2,0,0,2,3,2,4,5,2,1,1,0,6,2,7,9,2};
private byte[] typeindex;
private int correct;
private boolean view=false;
private short stage;//关数
private boolean tool;//保险
private int[] index=new int[6];//分数索引
Random rd=new Random();//随机数.
// -------------------------------------构造函数--------------------------------
public PCanvas() {
// super(false);
this.setFullScreenMode(true);
loadLogo();
G_STATE = G_SPLASH;
t = new Thread(this);
t.start();
}
// -------------------------------------初始化--------------------------------
private void init(){
//初始化图片顺序
for(int i=0;i<order.length-1;i++){
int n=Math.abs(rd.nextInt()%(order.length-1));
short temp=order[i];
order[i]=order[n];
order[n]=temp;
}
//初始化分数索引
for(int i=0;i<6;i++){
index[i]=0;
}
//初始化指针
point=0;
//初始化保险
tool=false;
//初始化关数
stage=0;
//初始化关数
score[3]=0;
//#ifdef _BT
if(isServer==1){
server=null;
isServer=0;
}else if(isServer==2){
client=null;
isServer=0;
}
//#endif
}
// -------------------------------------主循环--------------------------------
public void run() {
while (true) {
starttime = System.currentTimeMillis();
// System.gc();
switch (G_STATE) {
case G_SPLASH:
count++;
if (count >= 20) {
G_STATE = G_LOGO;
count = 0;
}
break;
case G_LOGO:
count++;
if (count >= 7) {
loadres();// 载入资源
}
break;
case G_TITLE:
if(soundindex!=0&&isp)
playsound(0,true);
break;
case G_MENU:
if(soundindex!=0&&isp)
playsound(0,true);
loadcount++;
break;
case G_SELECT:
case G_HELP:
case G_SET:
case G_TOP:
case G_ABOUT:
if(soundindex!=0&&!ispause&&isp)
playsound(0,true);
break;
//#ifdef _BT
case G_NET:
loadcount++;
break;
case G_SERVER:
loadcount++;
break;
case G_CLIENT:
loadcount++;
break;
//#endif
case G_START:
stopsound();
count++;
if (count >= 20) {
if(!pick){
doc = new PDoc(pic, level);
doc.change();
//#ifdef _BT
if(isServer==1){
server.send(doc.ok());
}else if(isServer==2){
client.send(doc.ok());
}
//#endif
limitime=onetime[stage];
prompts=sprompt[stage];
prompt=-1;
count=0;
G_STATE = G_PLAY;
}else{
pick=false;
if(times==2){
score[3]+=(limitime/1000)*15;
}else{
score[3]+=(limitime/1000)*10;
}
if(stage<=5){
score[3]+=500;
}else if(stage>5&&stage<=10){
score[3]+=600;
}else if(stage>10&&stage<=15){
score[3]+=800;
}else if(stage>15){
score[3]+=1000;
}
times=2;
point=0;
count=0;
gotype();
playsound(2,false);
G_STATE= G_TYPE;
releaseimage();
}
}
break;
case G_PLAY:
if(soundindex!=0&&isp)
playsound(0,true);
count++;
if(count>7)
count=0;
limitime -= pretime;
if (doc.check() && limitime >= 0) {
//#ifdef _BT
if(isServer==1){
server.send("-1");
}else if(isServer==2){
client.send("-1");
}
//#endif
pick=true;
G_STATE=G_START;
}
if (limitime < 0) {
if(tool){
limitime=onetime[stage]/6;//保险增加6分之1的时间
tool=false;
}else{
if(times>0){
times--;
limitime=onetime[stage];
doc.change();
}else{
releaseimage();
count=0;
playsound(3,false);
//#ifdef _BT
if(isServer==1){
server=null;
isServer=0;
}else if(isServer==2){
client=null;
isServer=0;
}
//#endif
G_STATE = G_LOST;
}
}
}
break;
case G_TYPE:
break;
case G_END:
stopsound();
loadcount++;
break;
case G_WIN:
break;
case G_OVER:
count++;
break;
case G_LOST:
count++;
break;
case G_PAUSE:
stopsound();
loadcount++;
break;
}
// System.gc();
endtime = System.currentTimeMillis();
try {
if ((endtime - starttime) < roundtime) {
pretime = roundtime;
Thread.sleep(roundtime - (endtime - starttime));
} else {
pretime = endtime - starttime;
}
} catch (Exception e) {
}
repaint();
}
}
// -------------------------------------主渲染--------------------------------
public void paint(Graphics g) {
switch (G_STATE) {
case G_SPLASH:
clearscreen(g, 0xFFFFFF);
g.drawImage(imgmonternet, WIDTH / 2, HEIGHT / 2 - 20,
Graphics.HCENTER | Graphics.VCENTER);
if (count > 0)
g.setClip(WIDTH / 2 - 60, HEIGHT / 2 + 50 - 10,
(count / 2) * 20, 20);
g.drawImage(slogan, WIDTH / 2, HEIGHT / 2 + 50, Graphics.HCENTER| Graphics.VCENTER);
break;
case G_LOGO:
clearscreen(g, 0xFFFFFF);
g.drawImage(imgbbox, 0, 0, Graphics.TOP | Graphics.LEFT);
g.drawImage(imgcom2us, WIDTH / 2, HEIGHT / 2, Graphics.HCENTER| Graphics.VCENTER);
drawSmile(g);
break;
case G_TITLE:
clearscreen(g, 0xFFFFFF);
//大机型
g.drawImage(title,WIDTH/2,HEIGHT/2,Graphics.HCENTER|Graphics.VCENTER);
break;
case G_MENU:
clearscreen(g, 0xFFFFFF);
//大机型
g.drawImage(menu,WIDTH/2,HEIGHT/2,Graphics.HCENTER|Graphics.VCENTER);
switch(count){
case 0:
g.setClip(98,35,42,16);
g.drawImage(menu_text,98,35-count*16,Graphics.TOP|Graphics.LEFT);
g.setClip(0,0,WIDTH,HEIGHT);
if(loadcount<8){
g.drawImage(flash[Math.abs(rd.nextInt())%3],84+loadcount*7,35+Math.abs(rd.nextInt())%8+2,Graphics.LEFT|Graphics.TOP);
}else{
for(int i=0;i<5;i++){
g.drawImage(flash[Math.abs(rd.nextInt())%3],145-Math.abs(rd.nextInt())%15,35+Math.abs(rd.nextInt())%15-5,Graphics.LEFT|Graphics.TOP);
}
}
break;
case 1:
g.setClip(98,62,42,16);
g.drawImage(menu_text,98,62-count*16,Graphics.TOP|Graphics.LEFT);
g.setClip(0,0,WIDTH,HEIGHT);
if(loadcount<8){
g.drawImage(flash[Math.abs(rd.nextInt())%3],84+loadcount*7,62+Math.abs(rd.nextInt())%8+2,Graphics.LEFT|Graphics.TOP);
}else{
for(int i=0;i<5;i++){
g.drawImage(flash[Math.abs(rd.nextInt())%3],145-Math.abs(rd.nextInt())%15,62+Math.abs(rd.nextInt())%15-5,Graphics.LEFT|Graphics.TOP);
}
}
break;
case 2:
g.setClip(98,91,42,16);
g.drawImage(menu_text,98,91-count*16,Graphics.TOP|Graphics.LEFT);
g.setClip(0,0,WIDTH,HEIGHT);
if(loadcount<8){
g.drawImage(flash[Math.abs(rd.nextInt())%3],84+loadcount*7,91+Math.abs(rd.nextInt())%8+2,Graphics.LEFT|Graphics.TOP);
}else{
for(int i=0;i<5;i++){
g.drawImage(flash[Math.abs(rd.nextInt())%3],145-Math.abs(rd.nextInt())%15,91+Math.abs(rd.nextInt())%15-5,Graphics.LEFT|Graphics.TOP);
}
}
break;
case 3:
g.setClip(98,119,42,16);
g.drawImage(menu_text,98,119-count*16,Graphics.TOP|Graphics.LEFT);
g.setClip(0,0,WIDTH,HEIGHT);
if(loadcount<8){
g.drawImage(flash[Math.abs(rd.nextInt())%3],84+loadcount*7,119+Math.abs(rd.nextInt())%8+2,Graphics.LEFT|Graphics.TOP);
}else{
for(int i=0;i<5;i++){
g.drawImage(flash[Math.abs(rd.nextInt())%3],145-Math.abs(rd.nextInt())%15,119+Math.abs(rd.nextInt())%15-5,Graphics.LEFT|Graphics.TOP);
}
}
break;
case 4:
g.setClip(98,147,42,16);
g.drawImage(menu_text,98,147-count*16,Graphics.TOP|Graphics.LEFT);
g.setClip(0,0,WIDTH,HEIGHT);
if(loadcount<8){
g.drawImage(flash[Math.abs(rd.nextInt())%3],84+loadcount*7,147+Math.abs(rd.nextInt())%8+2,Graphics.LEFT|Graphics.TOP);
}else{
for(int i=0;i<5;i++){
g.drawImage(flash[Math.abs(rd.nextInt())%3],145-Math.abs(rd.nextInt())%15,147+Math.abs(rd.nextInt())%15-5,Graphics.LEFT|Graphics.TOP);
}
}
break;
case 5:
g.setClip(98,174,42,16);
g.drawImage(menu_text,98,174-count*16,Graphics.TOP|Graphics.LEFT);
g.setClip(0,0,WIDTH,HEIGHT);
if(loadcount<8){
g.drawImage(flash[Math.abs(rd.nextInt())%3],84+loadcount*7,174+Math.abs(rd.nextInt())%8+2,Graphics.LEFT|Graphics.TOP);
}else{
for(int i=0;i<5;i++){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -