📄 game.java
字号:
import java.util.Random;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Gauge;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Ticker;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class game extends MIDlet implements Runnable, CommandListener {
canvas mc = null;
private Display dis;
private Form frm = null;
private Gauge gauge = null;
private Command cmdExit = new Command("Exit", Command.EXIT, 1);
public game() {
dis = null;
frm = new Form("");
mc = new canvas();
gauge = new Gauge("Loading.....", false, 50, 0);
}
protected void startApp() throws MIDletStateChangeException {
dis = Display.getDisplay(this);
dis.setCurrent(frm);
frm.append(gauge);
frm.addCommand(cmdExit);
Thread t = new Thread(this);
t.start();
mc.setTicker(new Ticker("数字游戏 按中得一分 按错扣一分"));
}
public void commandAction(Command c, Displayable d) {
if (c == cmdExit) {
try {
destroyApp(false);
} catch (MIDletStateChangeException e) {
e.printStackTrace();
}
this.notifyDestroyed();
}
}
public void run() {
int max = gauge.getMaxValue();
int i = gauge.getValue();
while (i < max) {
gauge.setValue(++i);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (i == max) {
dis.setCurrent(mc);
}
}
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
}
protected void pauseApp() {
}
}
class canvas extends Canvas implements Runnable, CommandListener {
private char randomnumber, num1;
private Random r1;
String num;
private boolean GameState;
private boolean win, over;
private Command cmdNew, cmdPause;
Thread th = null;
private int n = 5;
public canvas() {
win = false;
over = false;
GameState = false;
r1 = new Random();
cmdNew = new Command("Start", Command.SCREEN, 1);
cmdPause = new Command("Pause", Command.BACK, 1);
this.addCommand(cmdNew);
this.addCommand(cmdPause);
this.setCommandListener(this);
}
public void gamerun() {
if (randomnumber == num1) {
n++;
if (n == 15) {
win = true;
}
} else {
n--;
if (n <= 0) {
over = true;
}
}
}
public void commandAction(Command c, Displayable d) {
if (c == cmdNew) {
win = false;
over = false;
if (th == null) {
GameState = true;
th = new Thread(this);
th.start();
}
}
else if (c == cmdPause) {
GameState = false;
th = null;
}
}
public void paint(Graphics g) {
g.setColor(255, 255, 255);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
Font font = Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_BOLD,
Font.SIZE_LARGE);
g.setFont(font);
g.setColor(r1.nextInt(255), r1.nextInt(255), r1.nextInt(255));
if (win) {
g.drawString("You Win", this.getWidth() / 2, this.getHeight() / 2, 0);
GameState = false;
th = null;
n = 5;
} else if (over) {
g.drawString("Game Over", this.getWidth() / 2,
this.getHeight() / 2, 0);
GameState = false;
th = null;
n = 5;
} else{
String str=Integer.toString(n);
this.setTitle("The points:"+str);
g.drawChar(randomnumber, r1.nextInt(this.getWidth() - 10), r1
.nextInt(this.getHeight() - 10), 0);
}
}
protected void keyPressed(int keyCode) {
num = Integer.toString(keyCode - 48);
num1 = num.charAt(0);
gamerun();
}
public void run() {
while (GameState) {
int number = r1.nextInt(10) + 48;
randomnumber = (char) number;
repaint();
try {
Thread.sleep(550);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -