最近开始学j2me,做了个小游戏,很粗糙,不过付出了2天的努力,赞一个~~~
代码如下
Displayable类:
import
javax.microedition.lcdui.
*
;
import javax.microedition.lcdui.game.Sprite;
import java.io. * ;
public class Display1 extends Canvas ... {
Sprite worker,box,win,goal,map;
static int speed=8;//精灵速度
public Display1()...{
try
...{
worker=new Sprite(Image.createImage("/worker.png"),24,30);
box=new Sprite(Image.createImage("/box.png"));
map=new Sprite(Image.createImage("/ditu.gif"));
goal=new Sprite(Image.createImage("/goal.png"));
win=new Sprite(Image.createImage("/win.png"));
win.setVisible(false);
worker.setPosition(80,80);
goal.setPosition(160,160);
box.setPosition(104,104);
}
catch(IOException ex)
...{
}
}
public void paint(Graphics g)...{
g.fillRect(0, 0, 500, 500);
map.paint(g);
worker.paint(g);
goal.paint(g);
box.paint(g);
win.paint(g);
}
public void keyRepeated(int key)...{
keyPressed(key);
}
public void keyPressed(int key)...{
//up -1 down -2 left -3 right -4
//精灵走路~
int x=0,y=0;
if(key==-1)
...{
y=-speed;
if(worker.getFrame()==0)
worker.setFrame(1);
else if(worker.getFrame()==1)
worker.setFrame(2);
else
worker.setFrame(0);
}
else if(key==-4)
...{
x=speed;
if(worker.getFrame()==3)
worker.setFrame(4);
else if(worker.getFrame()==4)
worker.setFrame(5);
else
worker.setFrame(3);
}
else if(key==-2)
...{
y=speed;
if(worker.getFrame()==6)
worker.setFrame(7);
else if(worker.getFrame()==7)
worker.setFrame(8);
else
worker.setFrame(6);
}
else if(key==-3)
...{
x=-speed;
if(worker.getFrame()==9)
worker.setFrame(10);
else if(worker.getFrame()==10)
worker.setFrame(11);
else
worker.setFrame(9);
}
worker.setPosition(worker.getX()+x, worker.getY()+y);
this.collides(x,y);
this.repaint();
}
public void collides(int x,int y)...{
//处理碰撞的函数
if(worker.collidesWith(goal, false))
...{
worker.setPosition(worker.getX()-x, worker.getY()-y);
}
if(worker.collidesWith(box, false))...{
box.setPosition(box.getX()+x, box.getY()+y);
}
if(worker.collidesWith(map, true))
...{
worker.setPosition(worker.getX()-x, worker.getY()-y);
}
if(goal.getX()==box.getX()&&goal.getY()==box.getY())
...{
win.setVisible(true);
}
if(box.collidesWith(map, true))
...{
box.setPosition(box.getX()-x, box.getY()-y);
worker.setPosition(worker.getX()-x, worker.getY()-y);
}
}
}
import javax.microedition.lcdui.game.Sprite;
import java.io. * ;
public class Display1 extends Canvas ... {
Sprite worker,box,win,goal,map;
static int speed=8;//精灵速度
public Display1()...{
try
...{
worker=new Sprite(Image.createImage("/worker.png"),24,30);
box=new Sprite(Image.createImage("/box.png"));
map=new Sprite(Image.createImage("/ditu.gif"));
goal=new Sprite(Image.createImage("/goal.png"));
win=new Sprite(Image.createImage("/win.png"));
win.setVisible(false);
worker.setPosition(80,80);
goal.setPosition(160,160);
box.setPosition(104,104);
}
catch(IOException ex)
...{
}
}
public void paint(Graphics g)...{
g.fillRect(0, 0, 500, 500);
map.paint(g);
worker.paint(g);
goal.paint(g);
box.paint(g);
win.paint(g);
}
public void keyRepeated(int key)...{
keyPressed(key);
}
public void keyPressed(int key)...{
//up -1 down -2 left -3 right -4
//精灵走路~
int x=0,y=0;
if(key==-1)
...{
y=-speed;
if(worker.getFrame()==0)
worker.setFrame(1);
else if(worker.getFrame()==1)
worker.setFrame(2);
else
worker.setFrame(0);
}
else if(key==-4)
...{
x=speed;
if(worker.getFrame()==3)
worker.setFrame(4);
else if(worker.getFrame()==4)
worker.setFrame(5);
else
worker.setFrame(3);
}
else if(key==-2)
...{
y=speed;
if(worker.getFrame()==6)
worker.setFrame(7);
else if(worker.getFrame()==7)
worker.setFrame(8);
else
worker.setFrame(6);
}
else if(key==-3)
...{
x=-speed;
if(worker.getFrame()==9)
worker.setFrame(10);
else if(worker.getFrame()==10)
worker.setFrame(11);
else
worker.setFrame(9);
}
worker.setPosition(worker.getX()+x, worker.getY()+y);
this.collides(x,y);
this.repaint();
}
public void collides(int x,int y)...{
//处理碰撞的函数
if(worker.collidesWith(goal, false))
...{
worker.setPosition(worker.getX()-x, worker.getY()-y);
}
if(worker.collidesWith(box, false))...{
box.setPosition(box.getX()+x, box.getY()+y);
}
if(worker.collidesWith(map, true))
...{
worker.setPosition(worker.getX()-x, worker.getY()-y);
}
if(goal.getX()==box.getX()&&goal.getY()==box.getY())
...{
win.setVisible(true);
}
if(box.collidesWith(map, true))
...{
box.setPosition(box.getX()-x, box.getY()-y);
worker.setPosition(worker.getX()-x, worker.getY()-y);
}
}
}
midlet类:
import
javax.microedition.midlet.
*
;
import javax.microedition.lcdui. * ;
public class MainMIDlet extends MIDlet implements CommandListener ... {
private Display display;
private Form form;
private Command okCommand=new Command("OK",Command.OK,1);
private Command exitCommand=new Command("EXIT",Command.EXIT,1);
public MainMIDlet() ...{
display=Display.getDisplay(this);
}
protected void startApp() throws MIDletStateChangeException ...{
form=new Form("推箱子");
form.addCommand(okCommand);
form.addCommand(exitCommand);
form.setCommandListener(this);
display.setCurrent(form);
}
public void commandAction(Command c,Displayable d)...{
if(c==okCommand)
...{
display.setCurrent(new Display1());
}
else if(c==exitCommand)
...{
destroyApp(false);
notifyDestroyed();
}
}
protected void pauseApp() ...{
}
public void destroyApp(boolean unconditional) ...{
}
}
import javax.microedition.lcdui. * ;
public class MainMIDlet extends MIDlet implements CommandListener ... {
private Display display;
private Form form;
private Command okCommand=new Command("OK",Command.OK,1);
private Command exitCommand=new Command("EXIT",Command.EXIT,1);
public MainMIDlet() ...{
display=Display.getDisplay(this);
}
protected void startApp() throws MIDletStateChangeException ...{
form=new Form("推箱子");
form.addCommand(okCommand);
form.addCommand(exitCommand);
form.setCommandListener(this);
display.setCurrent(form);
}
public void commandAction(Command c,Displayable d)...{
if(c==okCommand)
...{
display.setCurrent(new Display1());
}
else if(c==exitCommand)
...{
destroyApp(false);
notifyDestroyed();
}
}
protected void pauseApp() ...{
}
public void destroyApp(boolean unconditional) ...{
}
}
以上代码在WTK2.2上调试通过。