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

📄 mygamecanvas.java

📁 自己做的J2ME手机游戏开发进入届面
💻 JAVA
字号:
import java.io.IOException;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;

public class MyGameCanvas extends GameCanvas implements Runnable ,CommandListener{
	private int MenuY,MenuX,y;
	GameMain gameMain;
	private boolean isRun;
	Command commad=new Command("返回", Command.BACK, 0);
Image img,img1,img2,img3,img4;
int a;
	protected MyGameCanvas() {
		super(true);
		MenuY = 60;
		MenuX = 60;
		y=0;
		isRun = true;
		a=0;
		
		
		Thread t = new Thread(this);
		t.start();
	}

	private void userInput() {
		int keyCode = this.getKeyStates();
		if(a==4){//控制飞机
			if ((keyCode & this.DOWN_PRESSED) != 0) {
				if(MenuY>=260){}
				else{MenuY = MenuY + 15;}}
			
			
			if ((keyCode & this.UP_PRESSED) != 0) {
				if(MenuY<=0){}
				else{MenuY = MenuY -15;}}
			if ((keyCode & this.LEFT_PRESSED) != 0) {
				if(MenuX<=0){}
				else {MenuX = MenuX - 15;}}
			if ((keyCode & this.RIGHT_PRESSED) != 0) {
				if(MenuX>=220){}
				else {MenuX = MenuX + 15;}}
		
		}
		if(a==0){//控制主菜单
			if ((keyCode & this.DOWN_PRESSED) != 0) {
				
				if (MenuY >= 180) {
					MenuY = 60;
				} else {
					MenuY = MenuY + 32;
				
				}
				
			}
			if ((keyCode & this.UP_PRESSED) != 0) {
				
				if (MenuY <= 60) {
					MenuY = 188;
				} else {
					MenuY = MenuY - 32;
				
				}
				
			}
	
		}
	if(a==2)	{//控制背景图滚动
		if ((keyCode & this.DOWN_PRESSED) != 0) {
			if(y<=-110){}
			else{y-=10;}
			
		}
		 if ((keyCode & this.UP_PRESSED) != 0) {
			
			if(y>=0){}
			else{y+=10;}
			
		}
		
	}
		
	}
public void keyPressed(){
	int keyCode = this.getKeyStates();
	if ((keyCode & this.FIRE_PRESSED) != 0) {
		if(MenuY==188){gameMain.exit();}
		if(MenuY==60){a=4;}
		if(MenuY==92){a=1;}
		if(MenuY==124){a=2;}
		if(MenuY==156){a=3;}
	}
}
	private void drawScreen(Graphics g) {
		if(a==0){
			this.removeCommand(commad);
			g.setColor(255, 255, 255);
			g.fillRect(0, 0, this.getWidth(), this.getHeight());
			g.setColor(0, 0, 0);
			 
			try {
				img1 = Image.createImage("/GameMenu.png");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			g.drawImage(img1, 0, 0, Graphics.TOP | Graphics.LEFT);
			g.setColor(255, 255, 255);
			g.drawRect(54, MenuY, 125, 25);
			}
		
		if(a==1){
			
			this.addCommand(commad);
			this.setCommandListener(this);
			g.setColor(255, 255, 255);
			g.fillRect(0, 0, this.getWidth(), this.getHeight());
			g.setColor(0, 0, 0);
			 
			try {
				img1 = Image.createImage("/bang.png");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			g.drawImage(img1, 0, 0, Graphics.TOP | Graphics.LEFT);
			}
			
				
				if(a==2){
					
				g.setColor(255, 255, 255);
				g.fillRect(0, 0, this.getWidth(), this.getHeight());
				g.setColor(0, 0, 0);
				 
				try {
					this.addCommand(commad);
					this.setCommandListener(this);
					img2 = Image.createImage("/beijing.png");
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				g.drawImage(img2, 0, y, Graphics.TOP | Graphics.LEFT);
				}
				if(a==3){
					this.addCommand(commad);
					this.setCommandListener(this);
					g.setColor(255, 255, 255);
					g.fillRect(0, 0, this.getWidth(), this.getHeight());
					g.setColor(0, 0, 0);
					 
					try {
						img3 = Image.createImage("/guizhe.png");
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					g.drawImage(img3, 0, 0, Graphics.TOP | Graphics.LEFT);
					}
				if(a==4){
					this.addCommand(commad);
					this.setCommandListener(this);
					g.setColor(255, 255, 255);
					g.fillRect(0, 0, this.getWidth(), this.getHeight());
					g.setColor(0, 0, 0);
					 
					try {
						img4 = Image.createImage("/myship.png");
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					g.drawImage(img4, MenuX, MenuY, Graphics.TOP | Graphics.LEFT);
					}
		this.flushGraphics();
	}

	public void run() {
		
		Graphics g = this.getGraphics();
		while (isRun) {
			userInput();
			keyPressed();
			drawScreen(g);
			try {
				Thread.sleep(20);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}

	public void commandAction(Command c, Displayable d) {
		if(c.getCommandType()==Command.BACK){a=0;}
		
	}
}

⌨️ 快捷键说明

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