⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 drawgame.java

📁 javaME useful code with the J2ME helpful learning cla
💻 JAVA
字号:

import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Ticker;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

public class DrawGame extends Canvas implements CommandListener
{
	private Display display;
	private Form form;
	private ImageLab newIL;
	private MIDlet midlet;
	private int Index=1;
	private int score=0;

	private int MoveBX=-100,MoveBY=-100;
	private int cousorX=0,cousorY=0;
	private int MenuX=70,MenuY=110;

	private int[] MoveEX=new int[3],MoveEY=new int[3];
	private int MovePX,MovePY;

	private int Score=0;
	private int Lives=30;
	
	boolean alive=true,Iback=true;
 
	Random rd = new Random();
	private Command menuBack,menuOk,menuExit;
	int roleX=getWidth()/2;
	int roleY=this.getHeight()-50;
	int roleState=0;
	
	//private int[] epW = new int[4],epH = new int[4];	 //敌机宽高
	Timer timer = new Timer();
	
	public DrawGame(MIDlet m,Display s)
	{

		this.midlet=m;
		this.display=s;
		cousorX=MenuX-1;
		cousorY=MenuY-1;
		menuBack=new Command("返回",Command.BACK,1);
		menuOk=new Command("新游戏",Command.OK,1);
		
		menuExit=new Command("退出",Command.EXIT,1);
		form=new Form("英雄");
		form.addCommand(menuExit);
		form.setCommandListener(this);
		newIL=new ImageLab();
		//newIL.LoadImage();  //初始化游戏图片
		  for(int i=0;i<3;i++){
			  MoveEY[i]=70;
			  }
		  MovePY = 0;

		timer.schedule(new bullet(),1,50);		//子弹线程
	}
	
	public void paint(Graphics g)
	{
		switch (Index)
		{
			case 1:
				paintMain(g);
				break;
			case 2:
				paintGame(g);
				break;
			case 3:
				paintEnd(g);
				break;
		}		
	}


	public void paintMain(Graphics g)
	{
		addCommand(menuExit);
		this.setCommandListener(this);
		g.setColor(255,255,255);
		g.fillRect(0,0,getWidth(),getHeight());
		g.setColor(255,0,0);
		g.drawImage(newIL.gameImage[5],0,0,0);	
		g.drawRect(cousorX, cousorY, 52, 18);//选择框
		for (int i=8;i<10;i++){
			g.drawImage(newIL.gameImage[i],MenuX,MenuY+(i-8)*23,0);
		}		
	}
	

	public void paintGame(Graphics g)
	{	
		addCommand(menuBack);
		addCommand(menuOk);
		removeCommand(menuExit);
		this.setCommandListener(this);
		g.setColor(255,255,255);
		g.fillRect(0,0,getWidth(),getHeight());

		g.drawImage(newIL.gameImage[6],0,-15,0);
		  
	  for(int i=0;i<3;i++){

		  if(MoveEY[i]==70)
			  MoveEX[i]=rd.nextInt()%220;
		  if(MoveEX[i]++<230)
			  MoveEX[i]++;
		  if(MoveEX[i]-->10)
			  MoveEX[i]--;
		  
	   	    g.drawImage(newIL.gameImage[i], MoveEX[i],MoveEY[i]++, 0);
	   	    if(MoveEY[i]>260)
	   	    	MoveEY[i]=70;
	  }
	  if(MovePY==5)
		  MovePX=rd.nextInt()%120+80;
		  g.drawImage(newIL.gameImage[3], MovePX,MovePY++, 0);
	   	    if(MovePY>260)
	   	    	MovePY=-170;
		
		//self
		g.drawImage(newIL.gameImage[4],roleX,roleY,0);

		//Bullet
		g.drawImage(newIL.gameImage[11],MoveBX,MoveBY,0);
		
		//life
		g.setColor(0,255,0);
		g.drawString("生命值:" + Lives,0,275,0);
		
		//score
		g.drawString("分数:" + Score,180,275,0);
		
		Impact(g);

		}

	public void paintEnd(Graphics g) 
	{
		g.setColor(255,255,255);
		g.fillRect(0,0,getWidth(),getHeight());
		g.setColor(247,114,224);
		if(Lives<=0)
			g.drawImage(newIL.gameImage[12],20,40,0);	
		g.drawImage(newIL.gameImage[7],20,40,0);
		Lives=10;
		score=0;
	}
	
	public void keyPressed(int KeyCode){
		
		int gameAction = getGameAction(KeyCode);
	
		if(gameAction==FIRE)
		{
			if(Index==1)
				Index=2;
			
			MoveBX = roleX+15;
			MoveBY = roleY;
			MoveBY -=10;
			repaint();
		}

		if(gameAction==DOWN) {
			if(cousorY<MenuY+3)
				cousorY+=23;
			else
				cousorY=MenuY;
			roleY=roleY+10;
			repaint();
		}
		if (gameAction==UP) {
			roleY=roleY-10;
			repaint();
		}

		if (gameAction==LEFT) 
		{
			if(roleX-10<0)
				roleX=0;
			else
			roleX=roleX-10;
			repaint();
		}
		if (gameAction==RIGHT) 
		{
			if(roleX+10>getWidth())
				roleX=getWidth();
			else
			roleX=roleX+10;
			repaint();
		}
	}
	

	
	class bullet extends TimerTask
	{

		public void run() 
		{
			if(alive)
	        {
    
	    			MoveBY -= 20;
	    			repaint();			
	        }
			
		}
		
	}
	
	public void Impact(Graphics g)
	{
		for(int i =0; i < 2; i++)
		{
			if(roleY == MoveEY[i] + 10 )
			{
				Lives--;
				g.drawImage(newIL.gameImage[10], roleX, roleY, 0);//绘制爆炸图				
			}
		}	
		
		for(int i =0; i < 2; i++)
		{
			if(((MoveBY < MoveEY[i])&&(MoveBY > MoveEY[i]-20)) && MoveBX> MoveEX[i] && MoveBX < MoveEX[i]+20)
			{
				g.drawImage(newIL.gameImage[10], MoveEX[i], MoveEY[i], 0);//绘制爆炸图
				MoveEY[i] = 70;
				Score = Score +20;
			}
		}
		if(((MoveBY < MovePY)&&(MoveBY > MovePY-20))&& ( (MoveBX  > MovePX)&&(MoveBX<MovePX+60)))
		{
			g.drawImage(newIL.gameImage[10], MovePX, MovePY, 0);//绘制爆炸图
			MovePY = -150;
			Score = Score +20;
		}
		if(Score>1000||Lives<=0)
		{
			Index=3;
			
		}
	}	    
	
	public void commandAction(Command c, Displayable d) 
	{
		if(c==menuOk)//触发确认命令
    	{
			Score=0;
			Lives=10;
			Index=2;
			repaint(); //触发新游戏菜单
			System.out.println("aaaon");

    	}
    	
		if (c == menuBack)//触发返回菜单
		{		    

			System.out.println("bbb");
			    Index=1;
			    removeCommand(menuBack);
			    display.setCurrent(this);
			    repaint();
			
		}
		if (c == menuExit)//触发返回菜单
		{		    
			System.out.println("ccc");
			midlet.notifyDestroyed();
			
		}
  
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -