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

📄 gamemenu.java

📁 最近弄的一个跨栏小游戏
💻 JAVA
字号:
import java.io.IOException;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;


public class GameMenu {

	public GameMenu(GameScreen gs){
		this.gs = gs;
	}
	
	public void init(){
		x = 100;
		y = 95;
		selectIndex = 0;
		isSelect = false;
		selectPlayer = false;
		try {
			if(bg_img==null)
			    bg_img = Image.createImage("/mainbg.png");
			if(menu_img==null)
				menu_img = Image.createImage("/menu.png");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void paint(Graphics g){
		g.setClip(0, 0, GameScreen.screenW, GameScreen.screenH);
		g.drawImage(bg_img, 0, 0, 20);
		for(int i=0; i<3; i++){
			int border = 0;
			if(i==1)
				border = -20;
			g.setClip(x+border, y+23*i, 63, 15);
			g.drawImage(menu_img, x+border, y+8*i-(selectIndex-1)*15, 20);
		}
		// 头选单
		if(selectIndex==0){
			g.setClip(x, y, 63, 15);
			g.drawImage(menu_img, x, y-75, 20);
		}
		// 尾选单
		if(selectIndex==num-1){
			g.setClip(x, y+46, 63, 15);
			g.drawImage(menu_img, x, y+46, 20);
		}
	}
	
	public void run(){
		doLogic();
	}
	
	public void keyPress(int keyCode){
		if(gs.getGameAction(keyCode)==Canvas.UP || keyCode==Canvas.KEY_NUM2){
			selectIndex--;
		}
		if(gs.getGameAction(keyCode)==Canvas.DOWN || keyCode==Canvas.KEY_NUM8){
			selectIndex++;
		}
		if(gs.getGameAction(keyCode)==Canvas.FIRE || keyCode==Canvas.KEY_NUM5)
			isSelect = true;
	}
	
	public void doLogic(){
		if(selectIndex>=num)
			selectIndex = 0;
		if(selectIndex<0)
			selectIndex = num-1;
        if(isSelect){
		    switch(selectIndex){
		    case 0:
		    	gs.setGameState(GameScreen.NEWGAME);
		    	break;
		    case 1:
		    	gs.setGameState(GameScreen.HIGHTSCORE);
		    	break;
		    case 2:
		    	gs.setGameState(GameScreen.SETTING);
		    	break;
		    case 3:
		    	gs.setGameState(GameScreen.GAMEHELP);
		    	break;
		    case 4:
		    	gs.setGameState(GameScreen.GAMEABOUT);
		    	break;
		    case 5:
		    	gs.setGameState(GameScreen.QUIT);
		    	break;
		    }
		    isSelect = false;
		}
	}
	
	public static void changeSoundState(boolean now){
		soundOn = now;
	}
	
    public void releaseRes(){
		bg_img = null;
		menu_img = null;
	}
	
    public static void drawBack(int id,Graphics g){
    	Image back = null;
    	try {
			if(id==0){
	    		if(bg0_img==null)
			        bg0_img = Image.createImage("/bg0.png");
	    		back = bg0_img;
			}
			if(id==1){
	    		if(bg1_img==null)
	    			bg1_img = Image.createImage("/bg1.png");
	    		back = bg1_img;
	    	}
		} catch (IOException e) {
			e.printStackTrace();
		}
		g.drawImage(back, 0, 0, 20);
    }
    
    public static void drawLoading(int percent,int x,int y,int w,int h,int frame,Graphics g){
    	percent %= 100;
    	frame %= 3;
    	try {
			if(load_img==null)
			    load_img = Image.createImage("/loading.png");
		} catch (IOException e) {
			e.printStackTrace();
		}
		g.setClip(percent*w/100, y-54, 46, 54);
		g.drawImage(load_img, percent*w/100-frame*46, y-54, 20);
		g.setClip(x, y, w, h);
		g.setColor(0x0000ff);
		g.fillRect(x, y, percent*w/100, h);
		g.setColor(0xffffff);
		g.drawRect(x, y, w, h);
    }
    
    public static void drawIcons(int id,int x,int y,Graphics g){
    	id %= 3;
    	try {
			if(icon_img==null)
			    icon_img = Image.createImage("/select.png");
		} catch (IOException e) {
			e.printStackTrace();
		}
		g.setClip(x, y, 20, 20);
		g.drawImage(icon_img, x-id*20, y, 20);
    }
    
	private GameScreen gs = null;
	private int selectIndex;
	private int x,y;
	private final int num = 6;
	private static boolean soundOn;
	private boolean isSelect;
	private boolean selectPlayer;
	private Image bg_img;
	private Image menu_img;
	private static Image icon_img;
	private static Image load_img;
	private static Image bg0_img;
	private static Image bg1_img;
}

⌨️ 快捷键说明

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