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

📄 man.java

📁 J2ME进阶学习代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import java.util.*;
import javax.microedition.lcdui.game.GameCanvas;
public class Man extends MIDlet
{
	private Game game;
	public Display display;

	public Man()
	{
		game = new Game(this);
		display = Display.getDisplay(this);
	}

	public void startApp()
	{
		game.startGame();
	}

	public void pauseApp()
	{
		//
	}

	public void destroyApp(boolean unconditional)
	{
		//
	}

	public void closeman()
	{
	  destroyApp(false);
	  notifyDestroyed();
	}

}

class Game
{
	public MainMenu mainmenu;
	public ManDown mandown;
	private Man G_man;
	private Image imgEmpty;
	public int man_mov_x = 3;
	public int man_mov_y = 3;
	public int obj_mov = 4;

	public Game(Man man)
	{
		this.G_man = man;
		
		this.mainmenu = new MainMenu(this);
	}

	public void startGame()
	{
		G_man.display.setCurrent(this.mainmenu);
	}

	public void startmandown()
	{
		this.mandown = new ManDown(this,G_man);
		setgame(man_mov_x,man_mov_y,obj_mov);
		G_man.display.setCurrent(this.mandown);
	}

	public void exitGame()
	{
		G_man.destroyApp(false);
		G_man.notifyDestroyed();
	}


	public void setgame(int manmovx, int manmovy, int objmov)
	{
                this.mandown.MAN_MOV_X = manmovx;
         	this.mandown.MAN_MOV_Y = manmovy;
		this.mandown.OBJ_MOV =  objmov;
	}

	public void MydrawRegion(Graphics graphics,Image imgSrc,
				int SrcX,int SrcY,
				int TargetX, int TargetY, 
				int TargetWidth,int TargetHeight)
	{

		graphics.setClip(TargetX, TargetY, TargetWidth, TargetHeight);
		graphics.drawImage(imgSrc, TargetX-SrcX, TargetY-SrcY, Graphics.LEFT | Graphics.TOP);

	}

}


class MainMenu extends Canvas
{
	private int SCREEN_WIDTH = this.getWidth();
	private int SCREEN_HEIGHT = this.getHeight();

	public int menuindex = 0;
	private int setindex= 0;
	private Game game;
	private String menustr[] = new String[4];
	private String setstr[] = new String[4];

	public MainMenu(Game game)
	{
		this.game = game;
		try
		{
			setFullScreenMode(true);
			menustr[0] = "开始";
			menustr[1] = "设置";
			menustr[2] = "退出";
			menustr[3] = "关于";
			setstr[0] = "简单";
			setstr[1] = "普通";
			setstr[2] = "困难";
			setstr[3] = "返回";
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
	}

	protected void keyPressed(int key)
	{
		if(menuindex < 5)
		{
			switch (getGameAction(key))
			{
				case Canvas.UP:
					menuindex --;
					if(menuindex < 0) menuindex = 3;
					break;
				case Canvas.DOWN:
					menuindex ++;
					if(menuindex > 3) menuindex = 0;
					break;
				case Canvas.FIRE:
					switch(menuindex)
					{
					case 0:
						// 开始游戏
                				this.game.startmandown();
		               			break;
					case 1:
						// 设置难度
						menuindex = 5;
						break;
					case 2:
						// 退出
						this.game.exitGame();
						break;
					case 3:
						// 关于
						menuindex = 4;
						break;
					case 4:
						//从关于中退出
						menuindex = 3;
						break;
					}
				break;
			}
		}	
		else
		{
			switch (getGameAction(key))
			{
				case Canvas.UP:
					setindex --;
					if(setindex < 0) setindex = 3;
		            		break;
        			case Canvas.DOWN:
					 setindex ++;
					if(setindex > 3) setindex = 0;
					break;
				case Canvas.FIRE:
					switch(setindex)
					{
						case 0:
							// 简单
							this.game.man_mov_x = 3;
							this.game.man_mov_y = 3;
							this.game.obj_mov = 2;
							this.game.startmandown();
                					break;
						case 1:
							// 一般
							this.game.man_mov_x = 3;
							this.game.man_mov_y = 3;
							this.game.obj_mov = 4;
							this.game.startmandown();
			                		break;
						case 2:
							// 困难
							this.game.man_mov_x = 4;
							this.game.man_mov_y = 4;
							this.game.obj_mov = 5;
							this.game.startmandown();
			                		break;
						case 3:
							// 返回
							setindex = 0;
							menuindex = 1;
							break;

					}
				break;
			}
		}

		this.repaint();
	}


    protected void sizeChanged(int w, int h)
    {
        SCREEN_WIDTH = w ;
        SCREEN_HEIGHT = h ;        
    }

    public void paint(Graphics graphics)
    {

		if(menuindex < 4)
		{
	        	graphics.setColor(96,57,19);
        		graphics.fillRect(0,0,SCREEN_WIDTH,SCREEN_HEIGHT);
        		graphics.clipRect(0,0,SCREEN_WIDTH,SCREEN_HEIGHT);
			for(int i =0;i < 4;i++)
			{
				if(i == menuindex)
				{
					graphics.setColor(218,218,218);
					graphics.drawString(menustr[i],this.getWidth()/2,this.getHeight()/6*(1+i),Graphics.HCENTER|Graphics.TOP);
				}
				else
				{
					graphics.setColor(198,156,109);
					graphics.drawString(menustr[i],this.getWidth()/2,this.getHeight()/6*(1+i),Graphics.HCENTER|Graphics.TOP);
				}
			}
		}
		else if(menuindex == 4)
		{
        		graphics.setColor(96,57,19);
        		graphics.fillRect(0,0,SCREEN_WIDTH,SCREEN_HEIGHT);
        		graphics.clipRect(0,0,SCREEN_WIDTH,SCREEN_HEIGHT);
			graphics.setColor(198,156,109);
			graphics.drawString("雨石工作室",this.getWidth()/2,this.getHeight()/5*1,Graphics.HCENTER|Graphics.TOP);
			graphics.drawString("设计开发",this.getWidth()/2,this.getHeight()/5*2,Graphics.HCENTER|Graphics.TOP);
		}
		else if(menuindex == 5)
		{
        		graphics.setColor(96,57,19);
        		//graphics.fillRect(0,0,this.getWidth(),this.getHeight());
	        	//graphics.clipRect(0,0,this.getWidth(),this.getHeight());
        		graphics.fillRect(0,0,SCREEN_WIDTH,SCREEN_HEIGHT);
	        	graphics.clipRect(0,0,SCREEN_WIDTH,SCREEN_HEIGHT);
			for(int i =0;i < 4;i++)
			{
				if(i == setindex)
				{
					graphics.setColor(218,218,218);
					graphics.drawString(setstr[i],this.getWidth()/2,this.getHeight()/6*(1+i),Graphics.HCENTER|Graphics.TOP);
				}
				else
				{
					graphics.setColor(198,156,109);
					graphics.drawString(setstr[i],this.getWidth()/2,this.getHeight()/6*(1+i),Graphics.HCENTER|Graphics.TOP);
				}
			}
		}	

	}
}


class ManDown extends GameCanvas implements Runnable
{

	private static int MAN_WIDTH = 16;
	private static int MAN_HEIGHT = 16;

	private static int OBJ_WIDTH = 48;
	private static int OBJ_HEIGHT = 8;

	private static int SIDE_WIDTH = 8;
	private static int SIDE_HEIGHT = 16;

	private static int MAX_LIFE = 15;

	//private int SCREEN_WIDTH = this.getWidth();
	//private int SCREEN_HEIGHT = this.getHeight();
	private int SCREEN_WIDTH;
	private int SCREEN_HEIGHT;

	public int MAN_MOV_X;
	public int MAN_MOV_Y;
	public int OBJ_MOV;

	private Random rnd;
	private Game game;
	private Man man;
	private Image I_man,I_obj1,I_obj2,I_side,I_top;
	private Image image_temp;
	private Thread thread;
	private int man_picIndex = 0;
	public int position_obj_x[] = new int[16];
	public int position_obj_y[] = new int[16];
	public int position_man_x;
	public int position_man_y;
	public int position_side_y = 0;
	public int direction = 0;
	public int directionup = 0;
	public int directionhit = 0;
	public int time_num = 0;
	public int life;
	public int level = 0;
	public int life_change = 0;
	public int level_change = 0;
	private Image I_tmp[] = new Image[5];
	private int Int_tmp[] = new int[16];
	public boolean isgameover = false;
	public boolean running = true;

	public ManDown(Game game, Man man)
	{
		super(true);
		thread = new Thread(this);
		rnd = new Random();
		this.game = game;
		this.man = man;
		setFullScreenMode(true);
		SCREEN_WIDTH = this.getWidth();
		SCREEN_HEIGHT = this.getHeight();
		try
		{
			life = MAX_LIFE;
			I_man  = Image.createImage("/man.png");
 			I_obj1 = Image.createImage("/obj1.png");
			I_side = Image.createImage("/side.png");
			I_top  = Image.createImage("/top.png");
			initobj(16);
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
		thread.start();
	}

	public void run()
	{
		try
		{
			while (running)
			//while (true) 
			{
				man_picIndex++;
				man_picIndex = man_picIndex % 4;
				thread.sleep(100);
				inputs();
				changelevel(SCREEN_HEIGHT/3);
				scrollside(OBJ_MOV,14);
				objup(OBJ_MOV);				
				this.repaint();	
				//flushGraphics();

			}
		}
		catch(InterruptedException ie)
		{
		ie.printStackTrace();
		}
	}

    protected void sizeChanged(int w, int h)
    {
        SCREEN_WIDTH = w ;
        SCREEN_HEIGHT = h ;        
    }


	//产生物体的坐标以及物体
	public void initobj(int obj_num)
	{
		for(int i=0;i<obj_num;i++)
      		{
			if (i==0)
		{
			position_obj_x[0] = (SCREEN_WIDTH - OBJ_WIDTH)/2;
			position_obj_y[0] = SCREEN_HEIGHT;
			position_man_x = (SCREEN_WIDTH - MAN_WIDTH)/2;
			position_man_y = SCREEN_HEIGHT - MAN_HEIGHT;
			Int_tmp[0] = 0;
		}
		else
		{
			position_obj_x[i] = SIDE_WIDTH + (rnd.nextInt() >>> 1)%(SCREEN_WIDTH/12-1)*12;
			if (position_obj_x[i]>SCREEN_WIDTH - OBJ_WIDTH - SIDE_WIDTH)
			{
				position_obj_x[i] = position_obj_x[i] - OBJ_WIDTH - SIDE_WIDTH;
			}
			position_obj_y[i]=position_obj_y[i-1]+30;
			Int_tmp[i] = (rnd.nextInt() >>> 1)%6;
		}
	}	
	}

	public void gameover()
	{
		/*if(isgameover == true)
			running = false;*/

⌨️ 快捷键说明

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