📄 gameplay.java
字号:
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.LayerManager;
import javax.microedition.rms.RecordStoreNotOpenException;
public class GamePlay {
public static final int WIDTH = 6; // 图片数据的数组宽度
public static final int HEIGHT = 4; // 图片数据的数组高度
Image zhujue, mapImage1, mapImage2;
Map bj;
LayerManager lm;
GameMain gm;
role r;
Font f1, f2;
public int num; // 记录分数
public final byte CLASS_ONE = 1;
public final byte CLASS_TWO = 2;
public final byte CLASS_THREE = 3;
public byte number = CLASS_ONE;
SaveGame sg;
boolean isRead = false;
public boolean onlyOne = false;
public int numTemp = 1;
public GamePlay(GameMain gameMain) {
this.gm = gameMain;
try {
zhujue = Image.createImage("/images/icone.png");
mapImage1 = Image.createImage("/images/map1.png");
mapImage2 = Image.createImage("/images/map3.png");
} catch (IOException e) {
e.printStackTrace();
}
bj = new Map(this);
r = new role(this, zhujue, 16, 16);
sg = new SaveGame(this);
sg.createRMS();
f1 = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_LARGE);
f2 = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_MEDIUM);
onlyOne = true;
}
public void keyPressed(int action) {
r.keyPressed(action);
}
public void render() {
bj.readMap();
switch (number) {
case CLASS_ONE:
// System.out.println(onlyOne);
if (this.r.zeroArry == bj.multiplication) {
gm.gameGraphics.setColor(0, 255, 255);
gm.gameGraphics.drawString("G O O D", 100, 130, 0);
number = CLASS_TWO;
this.r.zeroArry = 0;
onlyOne = true;
}
break;
case CLASS_TWO:
if (this.r.zeroArry == bj.multiplication) {
gm.gameGraphics.setColor(0, 255, 255);
gm.gameGraphics.drawString("G O O D", 100, 130, 0);
number = CLASS_THREE;
this.r.zeroArry = 0;
onlyOne = true;
}
break;
case CLASS_THREE:
if (this.r.zeroArry == bj.multiplication) {
gm.gameGraphics.setColor(0, 255, 255);
gm.gameGraphics.drawString("G O O D", 100, 130, 0);
number = CLASS_THREE;
this.r.zeroArry = 0;
onlyOne = true;
}
break;
}
if (Menu.temp) {
read();
Menu.temp = false;
}
r.render();
}
// private void setNextLevel(byte level){
// number = level;
// switch(number){
// case CLASS_ONE:
// break;
//
// case CLASS_TWO:
// break;
//
// case CLASS_THREE:
// break;
// }
// }
public void update(long time_elapsed) {
// 游戏背景图
gm.gameGraphics.setColor(0, 0, 0);
gm.gameGraphics.fillRect(0, 0, this.gm.getWidth(), this.gm.getHeight());
if (Menu.temp) {
read();
numTemp = 0;
}
switch (number) {
case CLASS_ONE:
gm.gameGraphics.drawImage(mapImage1, 0, 0, 0);
gm.gameGraphics.setFont(Font.getDefaultFont()); // 字体返回
gm.gameGraphics.setColor(0, 0, 0);
gm.gameGraphics.drawString("第一关", 0, 20, 0);
if (onlyOne) {
bj.init(CLASS_ONE, 0, 1);
onlyOne = false;
}
break;
case CLASS_TWO:
gm.gameGraphics.drawImage(mapImage2, 0, 0, 0);
gm.gameGraphics.setFont(Font.getDefaultFont()); // 字体返回
gm.gameGraphics.setColor(0, 0, 0);
gm.gameGraphics.drawString("第二关", 0, 20, 0);
if (onlyOne) {
bj.init(CLASS_TWO, 0, 3);
// read();
onlyOne = false;
}
break;
case CLASS_THREE:
gm.gameGraphics.drawImage(mapImage2, 0, 0, 0);
gm.gameGraphics.setFont(Font.getDefaultFont()); // 字体返回
gm.gameGraphics.setColor(0, 0, 0);
gm.gameGraphics.drawString("第三关", 0, 20, 0);
if (onlyOne) {
bj.init(CLASS_THREE, 3, 2);
// read();
onlyOne = false;
}
break;
}
drawBackGround();
r.update(time_elapsed);
}
public void keyReleased(int action) {
}
public void savanum(DataOutputStream dos) { // 保存数据
r.save(dos);
bj.SaveArry(dos);
isRead = true;
}
public void seadnum(DataInputStream dis, int numTemp) { // 读取数据
r.read(dis ,numTemp);
bj.ReadArry(dis);
}
public void read() { // 数据库不为空,读起数据
if (Menu.temp) {
try {
if (sg.rs.getNumRecords() == 0)
return;
else {
sg.readData();
}
} catch (RecordStoreNotOpenException e) {
e.printStackTrace();
}
}
}
public void drawBackGround() {
// 游戏分数
gm.gameGraphics.setColor(0, 255, 0);
Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_UNDERLINED,
Font.SIZE_LARGE);
gm.gameGraphics.setFont(f2);
gm.gameGraphics.drawString("分数:" + num + "", 170, 32, 0);
// 游戏标题
gm.gameGraphics.setColor(255, 0, 0);
gm.gameGraphics.setFont(f1);
gm.gameGraphics.drawString("对 对 碰", 85, 2, 0);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -