正文

SJLR2006-12-15 11:38:00

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

分享到:

import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.*;
import java.util.*;

//Runtime.getRuntime().freeMemory()察看内存
/**
方法内的变量只能在方法内使用
*/
public class SJLR extends MIDlet{
 private DrawMenu drawmenu;//菜单类
 private SJLRMenu sjlrmenu;
 

 public void startApp(){
 //drawmenu=new DrawMenu(Display.getDisplay(this));
 sjlrmenu=new SJLRMenu(Display.getDisplay(this));

         }
 public void pauseApp(){}

  public void destroyApp(boolean e){
  notifyDestroyed();
      }

 

/**
赏金猎人主菜单
*/
class SJLRMenu extends GameCanvas implements Runnable{
 private Display display;
 private Sprite sjlrSprite;//主菜单图片
 private Sprite itemSprite[]=new Sprite[6];
 private int nowPosition=1;
 private Study study;
 private int month,week,day,hour;//三个时间,线程里面添加,最后一个是小时
 private long money=5000;//钱钱钱

 public SJLRMenu(Display d){
 super(true);
 display=d;
 display.setCurrent(this);
 start();
      }
/**
交回DISPLAY,菜单转换
*/
 private void addDisplay(Display d){
 display=d;
 display.setCurrent(this);
       }


/**
初始化
*/

 private void start(){
 try{
 sjlrSprite=new Sprite(Image.createImage("/sjlrSprite.png"),128,128);
 itemSprite[0]=new Sprite(Image.createImage("/item.png"),30,16);
 itemSprite[1]=new Sprite(Image.createImage("/item.png"),30,16);
 itemSprite[2]=new Sprite(Image.createImage("/item.png"),30,16);
 itemSprite[3]=new Sprite(Image.createImage("/item.png"),30,16);
 itemSprite[4]=new Sprite(Image.createImage("/item.png"),30,16);
 itemSprite[5]=new Sprite(Image.createImage("/item.png"),30,16);
 Graphics g=getGraphics();
 draw(g);
    }
 catch(Exception e){}

 Thread t=new Thread(this);
 t.start();
       }
/**
run线程启动
*/

 public void run(){

 while(true){
 Graphics g=getGraphics();
 
 update(g);
 drawsixRect(g);
 flushGraphics();
 
 try{Thread.sleep(1000);}
 catch(Exception e){}
 runtime();
     }
 
    }

/**
时间进行,不断1秒24分之一日
*/
 private void runtime(){
 hour++;
 if(hour==25)
 {
 hour=0;
 day++;
 }
 if(day==31)//30为一月
 {
 day=0;
 week++;
 }
 if(week==7)
 {
 week=0;
 month++;
 }
         }

 

/**
检测键盘
*/
//1为最上位置
 private void update(Graphics g){
 int keyState=getKeyStates();
 if((keyState&FIRE_PRESSED)!=0){
 System.out.println(nowPosition);

 switch(nowPosition)
 {
 case 6:break;

 case 5:break;
 
 case 4:break;
 
 case 3:break;
 
 case 2:study=new Study(display,this);study=null;break;
 
 case 1:break;
 
 default:break;
 }
          }

 if((keyState&UP_PRESSED)!=0){
 fillsixRect(g,nowPosition);//填充方框
 nowPosition-=1;
 if(nowPosition==0){
 nowPosition=6;
     }
        }

 if((keyState&DOWN_PRESSED)!=0){
 fillsixRect(g,nowPosition);//填充方框
 nowPosition+=1;
 if(nowPosition==7){
 nowPosition=1;
     }
          }

 if((keyState&LEFT_PRESSED)!=0){
          }

 if((keyState&RIGHT_PRESSED)!=0){
           }

 
          }
/**
基本背景和方格
*/
 
 private void draw(Graphics g){

 sjlrSprite.paint(g);
 for(int i=0;i<6;i++){
 itemSprite[i].setPosition(90,i*20);
 itemSprite[i].paint(g);
       }
//选择菜单数目

 switch(6)
 {
 case 6:g.drawString(" 系统",85,20*5,Graphics.TOP|Graphics.LEFT);

 case 5:g.drawString(" 任务",85,20*4,Graphics.TOP|Graphics.LEFT);
 
 case 4:g.drawString(" 跑路",85,20*3,Graphics.TOP|Graphics.LEFT);
 
 case 3:g.drawString(" 商店",85,20*2,Graphics.TOP|Graphics.LEFT);
 
 case 2:g.drawString(" 训练",85,20*1,Graphics.TOP|Graphics.LEFT);
 
 case 1:g.drawString(" 日程",85,20*0,Graphics.TOP|Graphics.LEFT);
 
 default:break;
 }
 g.setColor(0x00ffffff);
 g.drawString("月",0,30,Graphics.TOP|Graphics.LEFT);
 g.drawString("周",0,50,Graphics.TOP|Graphics.LEFT);
 g.drawString("日",0,70,Graphics.TOP|Graphics.LEFT);
 g.setColor(0x009960ee);
 g.fillRect(0,0,20,15);
 g.setColor(0x00ffffff);
 g.drawString("$$",0,0,Graphics.TOP|Graphics.LEFT);
 
 

             }

/**
当前选择的方框
*/
 private void drawsixRect(Graphics g){
 
 g.setColor(0x00ffffff);
 
 switch(nowPosition)
 {
 case 6:g.drawRect(90,20*5,30,16);break;

 case 5:g.drawRect(90,20*4,30,16);break;
 
 case 4:g.drawRect(90,20*3,30,16);break;
 
 case 3:g.drawRect(90,20*2,30,16);break;
 
 case 2:g.drawRect(90,20*1,30,16);break;
 
 case 1:g.drawRect(90,20*0,30,16);break;
 
 default:break;
 }
 g.setColor(0x00000000);
 g.fillRect(0,20,15,12);
 g.fillRect(0,40,15,12);
 g.fillRect(0,60,15,12);
 g.setColor(0x00ffffff);
 g.drawString(" "+month,0,20,Graphics.TOP|Graphics.LEFT);//线程里面计算时间
 g.drawString(" "+week,0,40,Graphics.TOP|Graphics.LEFT);
 g.drawString(" "+day,0,60,Graphics.TOP|Graphics.LEFT);
 g.setColor(0x009960ee);//青色,和原背景一致
 g.fillRect(10,0,40,15);
 g.setColor(0x00ff77ff);
 g.drawRect(0,0,50,15);
 g.setColor(0x00ffffff);
 g.drawString(" "+money,10,0,Graphics.TOP|Graphics.LEFT);
 
         }

              }

/**
填补上次的边框空白,按键按下才覆盖
*/
  private void fillsixRect(Graphics g,int nowPosition){
 if(nowPosition==7)
 nowPosition=6;
 if(nowPosition==0)
 nowPosition=1;
 g.setColor(0x00000000);
 g.drawRect(90,20*(nowPosition-1),30,16);
 
            }

/**
训练类
*/
class Study extends GameCanvas implements Runnable{
 private Display display;
 private Sprite sprite;
 private SJLRMenu sjlr;//返回主菜单要用
 private Random rand;
 private Sprite backSprite;
 private int enemysenergy,rolesenergy;
 private Sprite roleSprite,rolesspear;//主角,武器
 private boolean isTomain=false;//按键决定返回主菜单
 private boolean isRun=true;
 private Sprite enermy;
 private int rolemoveX=4,rolemoveY=4;//武器移动速度,以后要看敏捷,可以10敏+1
 private int enermymoveX=4,enermymoveY=4;
 Timer timer = new Timer();
 TimerTask task = new MyTask();
 
 
 
 public Study(Display d,SJLRMenu s){
 super(true);
 display=d;
 display.setCurrent(this);
 sjlr=s;
 
 System.out.println("432");
 start();//不是一传就马上进入线程,可能对维护有利,说不定
    }

 private void addDisplay(Display d){
 display=d;
 display.setCurrent(this);
       }
 
/**
初始化一些东西
*/
 private void start(){
 try{
 roleSprite=new Sprite(Image.createImage("/girl.png"),20,15);
 rolesspear=new Sprite(Image.createImage("/fire.png"),40,55);
 enermy=new Sprite(Image.createImage("/1.png"),39,50);
 roleSprite.setPosition(0,80);
 rolesspear.setPosition(50,50);
 enermy.setPosition(50,30);
 rand=new Random();
 int nowBack=Math.abs(rand.nextInt())%6;//随机训练背景
 System.out.println("00000");
 switch(nowBack){
 case 0:backSprite=new Sprite(Image.createImage("/defeat0.png"),120,96);System.out.println("00000");break;
 case 1:backSprite=new Sprite(Image.createImage("/defeat1.png"),120,96);System.out.println("00000");break;
 case 2:backSprite=new Sprite(Image.createImage("/defeat2.png"),120,96);System.out.println("00000");break;
 case 3:backSprite=new Sprite(Image.createImage("/defeat3.png"),120,96);System.out.println("00000");break;
 case 4:backSprite=new Sprite(Image.createImage("/defeat4.png"),120,96);System.out.println("00000");break;
 case 5:backSprite=new Sprite(Image.createImage("/defeat5.png"),120,96);System.out.println("00000");break;
 default:System.out.println("00000");break;
         }
     }
 catch(Exception e){e.toString();}

 //Graphics g=getGraphics();
 //draw(g,backSprite);
 
 timer.schedule(task,0,1000);

 
 Thread t=new Thread(this);
 t.start();
       }


/**
画固定背景
*/
 private void draw(Graphics g,Sprite backSprite){
 g.setColor(0x00ffffff);
 backSprite.paint(g);
 g.drawRect(0,0,99,5);//血最长99
 roleSprite.paint(g);
 g.drawRect(20,90,99,5);
 
                  }

/**
线程
*/
 public void run(){
 while(isRun){
 Graphics g=getGraphics();
 enermyMove();//怪随机移动
 update();
 draw(g,backSprite);
 draw(g);
 System.out.println("run");
 try{Thread.sleep(300);}
 catch(Exception e){}
 
 flushGraphics();
 if(isTomain==true)
 addtomain();

      }
 
    }
/**
怪随机移动
*/
 private void enermyMove(){
 rand=new Random();
 int enermyMovePosition=Math.abs(rand.nextInt())%4;
 switch(enermyMovePosition)
 {
 case 0:enermy.move(enermymoveX,0);break;
 case 1:enermy.move(-enermymoveX,0);break;
 case 2:enermy.move(0,enermymoveY);break;
 case 3:enermy.move(0,-enermymoveY);break;
 default:break;
 }
 
 
     }

/**
按键
*/
 private void update(){
 
 int keyState=getKeyStates();
 if((keyState&GAME_A_PRESSED)!=0){
 isTomain=true;
            }
 
 if((keyState&FIRE_PRESSED)!=0){
 if(rolesspear.collidesWith(enermy,true))
 sjlr.money+=50;
 isTomain=true;
 
          }

 if((keyState&UP_PRESSED)!=0){
 rolesspear.move(0,-rolemoveY);
        }

 if((keyState&DOWN_PRESSED)!=0){
 rolesspear.move(0,rolemoveY);
     }

 if((keyState&LEFT_PRESSED)!=0){
 rolesspear.move(-rolemoveX,0);
     }

 if((keyState&RIGHT_PRESSED)!=0){
 rolesspear.move(rolemoveX,0);
     }
 spearisMove();
 
        }
/**
武器不被移动到相关的位置
*/
 private void spearisMove(){
 if(rolesspear.getX()>80)
 rolesspear.setPosition(80,rolesspear.getY());
 if(rolesspear.getX()<10)
 rolesspear.setPosition(10,rolesspear.getY());
 if(rolesspear.getY()>80)
 rolesspear.setPosition(rolesspear.getX(),80);
 if(rolesspear.getY()<30)
 rolesspear.setPosition(rolesspear.getX(),30);
      }


/**
根据按键画图,和动态
*/
 private void draw(Graphics g){
 enermy.paint(g);
 rolesspear.paint(g);
         }


/**
返回主菜单,并且释放所有资源
*/
 private void addtomain(){
 sjlr.addDisplay(display);
 
 display=null;
 sprite=null;
 sjlr=null;
 rand=null;
 backSprite=null;
 System.out.println(" $"+enemysenergy);
 roleSprite=null;rolesspear=null;
 isRun=false;
 

    }
/**
任务类,计算时间,按时杀不了怪就退出,Study.start()方法中调用
*/
 class MyTask extends TimerTask{
 int killTime=60;
 int nowTime=0;
 public MyTask(){}
 
/**
Run线程
*/
 public void run(){
 nowTime++;
 try{
 Thread.sleep(1000);
    }
 catch(Exception e){}
 if(nowTime>killTime)
 System.out.println("task");
 cancel();
    } 
 
 
         }
 

        }


/**
系统菜单
*/
class DrawMenu extends GameCanvas{
private Display display;

public DrawMenu(Display d){

 super(true);
 
 display=d;
 
 display.setCurrent(this);
 Graphics g=getGraphics();
 drawMenu(g,200,20,5);
 flushGraphics();
         }


     }


/**
 ***********菜单制作,涵数*************
*/

private void drawMenu(Graphics graphics,int MenuX,int MenuY,int number){
//要传进来的Graphics,菜单宽,菜单间隔,菜单项目数
/**
*内部值,可以释放,方便修改
*/
  Graphics g;
  int x,y;
  int num;
 
 g=graphics;
 x=MenuX;
 y=MenuY;
 num=number;


 
/**
*开始画咯,这里可以修改颜色,或者你设置自己的字体
*g.setFont(Font.getFont(Font.FACE_SYSTEM,Font.STYLE_PLAIN,Font.SIZE_SMALL));方法都给你了
*起始地址还没写,默认0,0
*/
 
//先清屏
 g.setColor(0xffffff);
 g.fillRect(0,0,128,128);//我手机是128*128大小的

 g.setColor(0xff00ff);
 for(int i=0;i<num;i++){
 g.drawRect(0,0+y*i,x,y);
         }

/**
*传进来的菜单项目,简单点说就是有多少格,五格以上就免了,那样还要写一个更加广泛的涵数,现在没空
*写上每格的字
*/
 g.setColor(0X333333);

 switch(num)
 {
 case 5:g.drawString("2006.12.11",0,y*4,Graphics.TOP|Graphics.LEFT);
 
 case 4:g.drawString("programfan.com",0,y*3,Graphics.TOP|Graphics.LEFT);
 
 case 3:g.drawString("www.sword2008.",0,y*2,Graphics.TOP|Graphics.LEFT);
 
 case 2:g.drawString("qq14923349",0,y*1,Graphics.TOP|Graphics.LEFT);
 
 case 1:g.drawString("本程序由程序之巢",0,y*0,Graphics.TOP|Graphics.LEFT);
 
 default:break;
 }
 
 //flushGraphics();


                }
    }


 

阅读(4247) | 评论(2)


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

评论

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