📄 gamecanvas.java
字号:
// if(enemy[i][2]==1)
// {
// offG.drawImage(imgEnemy[type],tmpx-tmpleft,tmpy-tmptop-40,0);
// }
// else
// {
// offG.drawImage(imgEnemy[type],tmpx-tmpleft,tmpy-tmptop,0);
// }
//
//
// }
// else
// {
if (enemy[i][2] == 0) {
offG.drawImage(imgEnemy, tmpx - tmpleft, tmpy - tmptop
- tmph, 0);
} else {
offG.drawImage(imgEnemy, tmpx - tmpleft, tmpy - tmptop, 0);
}
// if((type==1)&&(enemy[i][2]==1))
// {
// offG.setClip(tmpx+tmpw,tmpy+15,TailGasData[TailGasMove[tailFrame]][2],TailGasData[TailGasMove[tailFrame]][3]);
// offG.drawImage(TailGas,tmpx+tmpw-TailGasData[TailGasMove[tailFrame]][0],tmpy+15-TailGasData[TailGasMove[tailFrame]][1],0);
// tailFrame++;
// if(tailFrame>TailGasMove.length-1){tailFrame=0;}
// /////////throw
// if((!Throw)&&(tailFrame==0)&&(mon.size()<15)&&(tmpx>100)&&(tmpx<170))
// {//make random
// ThrowP[0]=tmpx+5;
// ThrowP[1]=tmpy+5;
// ThrowP[2]=Math.abs(r1.nextInt())%3+4;
// Throw=true;
// }
//
//
// }
// }
// ////// check
if ((type != 1) && (Math.abs(enemy[i][0] - map_CurX - px) < 20)
&& (py + 56 > enemy[i][1] - this.map_CurY)) {
if (!flashPlayer) {
life--;
flashPlayer = true;
}
}
for (int tmp = 0; tmp < 18; tmp++) {
int nowY = py + 56 + tmp;
// System.out.println(" check px"+px+" py"+nowY+"
// dx"+(enemy[i][0]-map_CurX)+" dy"+(enemy[i][1]));
// System.out.println(" check
// type"+type+"deadok"+this.deadOk(px,enemy[i][0]-map_CurX));
if ((dir2 == DIR_DOWN) && (type != 1)
&& (nowY == enemy[i][1] - map_CurY)
&& (this.deadOk(px, enemy[i][0] - map_CurX))) {
if (!this.flashEnemy) {
enemy[i][5]++;
flashEnemy = true;
this.direction = 0;
dir2 = DIR_UP;
this.upFlag = 18;
py = nowY - 56;
}
}
}
}
}
}
public boolean Throw = false;
private int ThrowP[] = { 0, 0, 0 };// x y type
public short imgData[][] = { { 0, 0, 40, 70 },/* 0站立 */
{ 0, 0, 1, 1 },/* 1空 */
{ 40, 0, 40, 70 },/* 2走1 */
{ 80, 0, 40, 70 },/* 3走2 */
{ 120, 0, 40, 70 },/* 4走3 */
{ 160, 1, 40, 69 },/* 5受伤 */};
public byte enemyMotion[] = { 3, 2, 4, 2 };
public void drawmenubk(Graphics offG) {
// {// { try {
// //this.level_Image=null;
// this.imgEnemy=null;
// System.gc();
// menuBK =Image.createImage("/menuBG.png");
// }
// catch (Exception e) {}
//
// }
offG.setClip(0, 0, SCREENWIDTH, SCREENHEIGHT);
if (menuBK == null) {
offG.setColor(0xFFFFFF);
offG.fillRect(0, 0, SCREENWIDTH, SCREENHEIGHT);
} else {
offG.drawImage(menuBK, 0, -20, 0);
}
// if(gameStart.midlet.gc!=null&&gameStart.midlet.gc.gameState==GameCanvas.GAMEOVER)
// {GameCanvas.ISRUN=false;
// gameStart.midlet.gc=null;
//
// }
if (this.gameState == PAUSE) {
// offG.setClip(144,79,88,19);
offG.setColor(0);
offG.fillRect(0, 0, 176, 208);
offG.setColor(0, 255, 128);
offG.drawString("都市历险记", 50, 30, 0);
offG.drawString("返回游戏", 79, 122, 0);
offG.drawString("游戏帮助", 79, 144, 0);
offG.drawString("关 于", 79, 166, 0);
offG.drawString("退出游戏", 79, 188, 0);
}
}
// public void drawhiScore()
// {
// offG.setColor(0xFFFFFF);
// offG.fillRect(0,0,this.getWidth(),this.getHeight());
// offG.setColor(0);
// int score=0;
// try{
// RecordStore rs=RecordStore.openRecordStore("hi",true);
// //System.out.println("record num"+rs.getNumRecords());
// if (rs.getNumRecords()>0)
// {
// byte tmp[]=rs.getRecord(1);
// score+=(int)(tmp[0]<<24) ;
// score+=(int)(tmp[1]<<16) ;
// score+=(int)(tmp[2]<<8) ;
// score+=(int)(tmp[3]) ;
//
// }
// rs.closeRecordStore();
// }catch(Exception e){
// e.printStackTrace();
// }
// offG.drawString( "最 高 分",20, 20,0);
// offG.drawString(" "+score,50,50,0);
// }
public void drawmenu(Graphics offG) {
offG.setClip(46, 118 + 22 * nowFramemenu, 26, 22);
offG.drawImage(icon, 46, 118 + 22 * nowFramemenu, 0);
}
String[] strAbout;
String[] strHelp;
public byte HelpIndex = 0;
public void drawhelp(Graphics offG) {
offG.setColor(0);
offG.fillRect(0, 0, this.getWidth(), this.getHeight());
offG.setColor(0, 255, 128);
// if (strHelp == null) {
if (HelpIndex == 0) {
strHelp = new String[] { "**游戏背景**", "可爱的红发男孩 ", "——小不点,",
"是生活在现代都市", "的一名小学生。在", "每天的生活中。总", "会遇到一些不文明", "的社会现象,乱扔", };
} else if (HelpIndex == 1) {
strHelp = new String[] { "的垃圾、恶犬、高", "空坠物、汽车尾气", "等等。于是小不点",
"就用自己的行动,", "来为城市的文明建", "设添砖加瓦...", "**游戏规则**", "控制小不点尽量躲", };
} else if (HelpIndex == 2) {
strHelp = new String[] { "避垃圾、高空坠物", "及不良份子,也可", "以跳落在不良份子",
"身上以消灭他们,", "在每个关卡中都有", "一个Boss,只有消", "灭了他,小不点才",
"能从屏幕最右边到", };
} else if (HelpIndex == 3) {
strHelp = new String[] { "达下一关。", "", "**按键控制**", "方向键左和数字4:",
"往左跑动", "方向键右和数字6:", "往右跑动", "数字键1 :左上跳跃", };
} else if (HelpIndex == 4) {
strHelp = new String[] { "数字键1 :左上跳跃", "数字键3 :左上跳跃", "数字5键: 拾取垃圾",
"左软键: 呼出菜单", "", "**客户服务**", "电话:010-8589XXXX",
"E-mail:tanXXXXy@163.com", };
}
// }
offG.drawString("帮 助", 20, 20, 0);
for (int i = 0; i < strHelp.length; i++) {
offG.drawString(strHelp[i], 5, 40 + i * 20, 0);
}
}
public int aboutIndex = 0;
public void drawabout(Graphics offG) {
offG.setColor(0);
offG.fillRect(0, 0, this.getWidth(), this.getHeight());
offG.setColor(0, 255, 128);
if (strAbout == null) {
strAbout = new String[] { "上海幽幽XX", "客服联系人", "姓名 谭XX ",
"客服电话 0108589XX", "传真 0108589XXX ", "移动电话 1352055XXX",
"客服联系人EMAIL ", "tantXXXX@163.com", "各地客服电话 0212823XXXX",
"客服手机 13681XXXXX ", "WEB地址", "www.XXXXX.com.cn" };
}
offG.drawString("关 于", 20, 20, 0);
// System.out.println("index"+aboutIndex);
for (int i = aboutIndex; i < strAbout.length; i++) {
if (i > aboutIndex + 5) {
break;
}
offG.drawString(strAbout[i], 5, 60 + (i % 6) * 20, 0);
}
}
/***************************************************************************
* 画所有元素的paint方法
**************************************************************************/
public void paint(Graphics g) {
//System.out.println("gamestate " + gameState);
offG.setClip(0, 0, SCREENWIDTH, SCREENHEIGHT);
switch (gameState) {
case PAUSE:
case MENU:
switch (this.menuState) {
case 0:
try {
d_logo = Image.createImage("/logo1.png");
} catch (Exception e) {
}
offG.drawImage(d_logo, 0, 0, 0);
break;
case 1:
try {
d_logo = null;
System.gc();
d_logo = Image.createImage("/logo2.png");
} catch (Exception e) {
}
offG.drawImage(d_logo, 0, 0, 0);
break;
case 2:
try {
d_logo = null;
System.gc();
} catch (Exception e) {
}
offG.drawImage(menuBK, 0, -20, 0);
break;
case 3:
drawmenubk(offG);
drawmenu(offG);
break;
case 4:
drawhelp(offG);
break;
case 5:
break;
case 6:
drawabout(offG);
}
break;
case LOAD:
offG.setClip(0, 0, SCREENWIDTH, SCREENHEIGHT);
offG.setColor(0);
offG.fillRect(0, 0, SCREENWIDTH, SCREENHEIGHT);
offG.setColor(0, 255, 128);
offG.drawString("第" + this.level_Mode + "关 游戏加载 ", 30, 50, 0);
offG.drawString(proess + "%", 60, 70, 0);
if (proess == 0 && this.level_Image == null) {
this.initLevel(level_Mode);
}
// }
// System.out.println("now Load");
break;
case GAMEING:
// offG.setClip(0,0,SCREENWIDTH,SCREENHEIGHT);
// offG.setColor(200,205,207);
// offG.fillRect(0,0,SCREENWIDTH,SCREENHEIGHT);
drawBack();
drawMap();
try {
drawEnemy();
} catch (Exception e) {
e.printStackTrace();
}
if (lifeState == 1) {
drawPerson(); // 画人物
}
// checkAll();
makeBottle();
// drawThrow();
drawInterface();
// offG.setClip(12*16-map_CurX,16*16-map_CurY, 24, 24);
// dg.drawImage(mon_Img1,12*16-map_CurX,16*16-map_CurY,0,0);
offG.setColor(0xff0000);
offG.setClip(0, 0, 70, 15);
offG.drawString("分数:" + score, 0, 0, 0);
break;
case GAMEOVER:
// offG.setClip(0,0,SCREENWIDTH,SCREENHEIGHT);
// offG.setColor(200,205,207);
// offG.fillRect(0,0,SCREENWIDTH,SCREENHEIGHT);
// System.out.println("now gameover");
// if(level_Mode<=4)
// {drawBack();
// drawMap();
//
// }
offG.setClip(0, 0, SCREENWIDTH, SCREENHEIGHT);
offG.setColor(0);
offG.fillRect(0, 0, SCREENWIDTH, SCREENHEIGHT);
drawPerson(); // 画人物
// ////////save Record
// this.saveRecord();
// offG.setColor(0xffffff);
// offG.fillRect(0, 0, SCREENWIDTH, 80);
// System.out.println("now levetl"+level_Mode);
if (level_Mode > 3) {
offG.fillRect(0, 0, SCREENWIDTH, SCREENHEIGHT);
endCount++;
if (endCount > 20) {
ISRUN = false;
gameStart.midlet.notifyDestroyed();
}
}
offG.setClip(0, 0, SCREENWIDTH, SCREENHEIGHT);
offG.setColor(0, 255, 128);
offG.drawString("游戏结束", 50, 50, 0);
offG.drawString("爱护环境人人有责", 30, 80, 0);
break;
}
if (this.gameState == PAUSE1) {
offG.setColor(0, 255, 128);
offG.drawString("暂停", 88, 100, Graphics.TOP | Graphics.HCENTER);
}
// offG.setClip(0,0,SCREENWIDTH,SCREENHEIGHT);
// offG.setColor(255,0,0);
// offG.drawRect(0,0,SCREENWIDTH, SCREENHEIGHT);
g.drawImage(im, 0, 0, 0);
}
public byte endCount = 0;
//
// /***************************************************************************
// * 画当前屏幕的敌人
// **************************************************************************/
// public void drawMon() {
// offG.setClip(monPx, monPy, 24, 24);
// dg.drawImage(mon_Img, monPx - 24 * monFrame, map_CurY - monLorR * 24,
// 0, 0);
// }
// public void drawCurrentMon() {
// for (int i = 0; i < currentMon.size(); i++) {
// Monster m2 = (Monster) currentMon.elementAt(i);
// if (m2.imflag == 1) {
// mon_Img = mon_Img1;
// } else {
// mon_Img = mon_Img2;
// }
// int monx = m2.mx - map_CurX +monPx;
// int mony= m2.my - map_CurY +monPy;
// offG.setClip(monx,mony, 24, 24);
// dg.drawImage(mon_Img, monx-24*monFrame,mony-24*monLorR,0,0);
// }
// }
public void saveRecord() {
try {
RecordStore.deleteRecordStore("hi");
} catch (Exception e) {
}
try {
RecordStore rs = RecordStore.openRecordStore("hi", true);
byte tmp[] = new byte[4];
tmp[0] = (byte) (score >> 24);
tmp[1] = (byte) (score >> 16);
tmp[2] = (byte) (score >> 8);
tmp[3] = (byte) (score & 255);
rs.addRecord(tmp, 0, tmp.length);
rs.closeRecordStore();
// System.out.println("add ok");
} catch (Exception e) {
e.printStackTrace();
}
}
/***************************************************************************
* 初始化当前屏幕的敌人
**************************************************************************/
// public void initMon() {
// for (int i = 0; i < mon.size(); i++) {
// Monster m1;
// m1 = (Monster) mon.elementAt(i);
// // System.out.println("mx= " + m1.mx + " my= " + m1.my);
// // System.out.println("px= " + (px + map_CurY) + " py= "
// // + (py + map_CurY));
// if (Math.abs(m1.mx - px - map_CurX) < 100
// && Math.abs(m1.my - py - map_CurY) < 208) {
// drawMonFlag = true;
//
// if (m1.imflag == 1) {
// mon_Img = mon_Img1;
// } else {
// mon_Img = mon_Img2;
// }
// //
// int monx = m1.mx - map_CurX + monPx;
// int mony = m1.my - map_CurY + monPy;
//
// if (deadOk((px + 12), (py + 12), monx, mony)) {
// deadFlag = true;
// life--;
// px += 32;
// }
//
// // currentMon.addElement(m1);
// // System.out.println(m1.mx);
// // System.out.println(m1.my);
// // offG.setClip(m1.mx - map_CurX + monPx,
// // m1.my - map_CurY + monPy, 24, 24);
// // dg.drawImage(mon_Img, m1.mx - map_CurX + monPx - 24 *
// // monFrame,
//
// offG.setClip(monx, mony, 24, 24);
// dg.drawImage(mon_Img, monx - 24 * monFrame,
// mony - 24 * monLorR, 0, 0);
// }
// }
// }
/***************************************************************************
* 碰撞检测
**************************************************************************/
public boolean deadOk(int x1, int x2) {
if (Math.abs(x1 - x2) < 40) {
return true;
} else {
return false;
}
}
/***************************************************************************
* 初始化敌人
**************************************************************************/
// public void initMon(int levelFlag) {
// switch (levelFlag) {
// case 1:// 第一关
// InputStream is = this.getClass().getResourceAsStream("/mon1.dat");
// DataInputStream dis = new DataInputStream(is);
// try {
// for (int m = 0; m < 22; m++) {
// for (int j = 0; j < 88; j++) {
// mon_Move[m][j] = dis.readByte();
// if (mon_Move[m][j] == 21) {
// Monster monster1 = new Monster();
// monster1.mx = 24 * j;
// monster1.my = 24 * m - 8;
// monster1.imflag = Math.abs(r1.nextInt() % 2 + 1);
// // System.out.println("mx= "+16*j);
// // System.out.println("my= "+(16*m-8));
// mon.addElement(monster1);
// }
// }
// }
// is.close();
// } catch (Exception e) {
// System.out.println("load mon faile!");
// }
// break;
// case 2:// 第二关
// InputStream is2 = this.getClass().getResourceAsStream("/mon2.dat");
// DataInputStream dis2 = new DataInputStream(is2);
// try {
// for (int m = 0; m < 22; m++) {
// for (int j = 0; j < 88; j++) {
// mon_Move[m][j] = dis2.readByte();
// if (mon_Move[m][j] == 21) {
// Monster monster1 = new Monster();
// monster1.mx = 24 * j;
// monster1.my = 24 * m - 8;
// monster1.imflag = Math.abs(r1.nextInt() % 2 + 1);
// // System.out.println("mx= "+16*j);
// // System.out.println("my= "+(16*m-8));
// mon.addElement(monster1);
// }
// }
// }
// is2.close();
// } catch (Exception e) {
// System.out.println("load mon faile!");
// }
// break;
// }
// }
//
//
/***************************************************************************
* 画人物
**************************************************************************/
public void drawPerson() {
if (flashPlayer) {
this.flashPlayerframe++;
if (flashPlayerframe % 2 == 0) {
return;
}
if (flashPlayerframe > 9) {
flashPlayerframe = 0;
flashPlayer = false;
}
}
switch (index) {
case 0: // 站立
offG.setClip(px, py, imgDataPlayer[playerStop[nowFrame]][2],
imgDataPlayer[playerStop[nowFrame]][3]);
if (!mirro) {
offG.drawImage(person, px
- imgDataPlayer[playerStop[nowFrame]][0], py
- imgDataPlayer[playerStop[nowFrame]][1], 0);
} else {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -