日期:2014-05-20 浏览次数:20806 次
package Mouse;
import java.io.IOException;
import java.util.Date;
import java.util.Random;
import java.util.Timer;
import java.util.Vector;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.LayerManager;
import javax.microedition.lcdui.game.Sprite;
import com.sun.midp.ssl.PublicKey;
public class MainGameCanvas extends GameCanvas implements Runnable, CommandListener {
private Graphics g;
//地鼠洞
private Image hold_img;
//老鼠
private Image mouse_img;
//锤子
private Image hammer_img;
//锤子
private Sprite hammer;
//老鼠
private Sprite mouse;
//向量存储
private Vector vc;
final int x = getWidth() / 2;
final int y = getHeight() / 2;
private Timer timer;
private MouseMoveTask mtask;
Score score;
private int delaytime=0;
private Command pauseCommand,continueCommand;
//构造方法,参数的作用是表示是否一致传统键盘事件(包括keyPressed()、
//keyReleased()和keyRepeated()),true—是(不调用),false—否(调用)
protected MainGameCanvas(boolean suppressKeyEvents) {
super(suppressKeyEvents);
g = getGraphics();
vc = new Vector();
pauseCommand=new Command("暂停", Command.CANCEL, 1);
continueCommand=new Command("继续", Command.OK, 2);
//实例化image
try {
hold_img = Image.createImage("/images/hole.png");
mouse_img = Image.createImage("/images/mouse.png");
hammer_img = Image.createImage("/images/hammer.png");
} catch (IOException e) {
e.printStackTrace();
}
//hammer
hammer = new Sprite(hammer_img, 42, 40);
hammer.setVisible(false);
//画洞
drawHold();
//在四个地方mouse,设置初始显示,添加进vc
mouse = new Sprite(mouse_img, 32, 34);
mouse.setPosition(x - 15, y - 70);
mouse.setFrame(2);
mouse.paint(g);
vc.addElement(mouse);
mouse = new Sprite(mouse_img, 32, 34);
mouse.setPosition(x + 37, y - 20);
mouse.setFrame(2);
mouse.paint(g);
vc.addElement(mouse);
mouse = new Sprite(mouse_img, 32, 34);
mouse.setPosition(x - 15, y + 30);
mouse.setFrame(2);
mouse.paint(g);
vc.addElement(mouse);
mouse = new Sprite(mouse_img, 32, 34);
mouse.setPosition(x - 65, y - 20);
mouse.setFrame(2);
mouse.paint(g);
vc.addElement(mouse);
score=new Score();
//启动timertask
timer=new Timer();
mtask=new MouseMoveTask(vc,score);
timer.schedule(mtask, delaytime, 1000);
this.addCommand(pauseCommand);
this.addCommand(continueCommand);
this.setCommandListener(this);