正文

[原创]J2ME 移动的格子2006-08-28 10:02:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/sword2008/17949.html

分享到:

import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.*;

//made by sword2008·程序之巢
//游戏第一课  画格子
public class Gofirst1 extends MIDlet
{


 public Gofirst1(){
 
           }
 public void startApp(){
  Display.getDisplay(this).setCurrent(new First1());
         }
 public void pauseApp(){}
 public void destroyApp(boolean unkown){}


 

 


}

class First1 extends Canvas
{  private int color;
   private int forOneTime=0;
 private Graphics g;
 
 private int startX=getWidth()/4-40;//起点坐标和终点坐标非常重要,格子剩下的是分成等分
 private int startY=getHeight()/4-40;
 private int nowX=startX,nowY=startY;

 

 

 public void paint(Graphics g){


if(forOneTime==0){
  color=g.getColor();
 g.setColor(255,255,255);
 g.fillRect(0,0,getWidth(),getHeight());
 g.setColor(255,0,0);
 g.drawRect(startX,startY,200,200);
 drawline(g);
    }
else if(forOneTime==1){//fire
g.setColor(0,255,0);
g.fillRect(nowX,nowY,20,20);
long memory = (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory());
System.gc();
System.out.println("当前占用内存:"+memory);}//fillRect()(起点坐标,终点坐标,横坐标,竖坐标)
else if(forOneTime==2){//down
 nowY+=20;//竖坐标加20,以下依此类推
ifmaxmingolong();
g.fillRect(nowX,nowY,20,20);
   }
else if(forOneTime==3){//up
        nowY-=20;
ifmaxmingolong();
g.fillRect(nowX,nowY,20,20);
   }
else if(forOneTime==4){//left
 nowX-=20;
ifmaxmingolong();
g.fillRect(nowX,nowY,20,20);
   }
else if(forOneTime==5){//right
 nowX+=20;
ifmaxmingolong();
g.fillRect(nowX,nowY,20,20);
   }
 
         }

   private void  drawline(Graphics g)//画格子
 {    for(int i=1;i<11;i++){//横线 x坐标不边,y分别分成10等分,从上画下,
 g.drawLine(startX,startY+200/10*i,startX+200,startY+200/10*i);
     //竖线
 g.drawLine(startX+200/10*i,startY,startX+200/10*i,startY+200);
 
      }
 }
//填充

 private void fillline(){}

//调整越界坐标
private void ifmaxmingolong()
{
if(nowX<startX){nowX+=20;}
if(nowY<startY){nowY+=20;}
if(nowX>startX+180){nowX-=20;}
if(nowY>startY+180){nowY-=20;}
}

//key
   public void keyPressed(int Keycode){//k小写
 int action=this.getGameAction(Keycode);
 switch(action){
 case Canvas.FIRE:forOneTime=1;repaint();break;
 case Canvas.DOWN:forOneTime=2;repaint();break;
 case Canvas.UP:  forOneTime=3;repaint();break;
 case Canvas.LEFT:forOneTime=4;repaint();break;
 case Canvas.RIGHT:forOneTime=5;repaint();break;
 default:break;
       }
         }

 

}

 

阅读(3128) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册