第一个j2me游戏--推箱子

最近开始学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(
00500500);
        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) {
 }

}

 

以上代码在WTK2.2上调试通过。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值