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

📄 maincanvas.java

📁 迷宫小游戏,请大家多多看哈,指点不足之处,不慎感激
💻 JAVA
字号:
import java.io.IOException;
import java.io.InputStream;
import java.util.Random;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;
import javax.microedition.midlet.MIDlet;

/**
 * Title: MainCanvas类
 * 
 * @version 1.00
 */

class MainCanvas extends Canvas implements Runnable,CommandListener{	
	final MIDlet midlet;
	Player player;
	Thread thread;
	Command cmdStart, cmdStop,cmdExit, cmdFirst,cmdSecond,cmdThird, cmdRandom;
	Image imgFloor, imgWall, imgSuccess, imgFailure, offImageWall,offImageFloor;
	Graphics offGraphicsWall,offGraphicsFloor;
	int wallWidth, wallHeight, topMargin, leftMargin;
	int ROW = 30, COL = 25;
	private boolean m_bRunning,m_bFailed;
	int curRow, curCol;
	long startTime,endTime;
	byte[][] map;	
	byte[][] map1 = {
			{0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1}, 
			{1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1}, 
			{1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0}, 
			{1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0}, 
			{0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 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1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1}, 
			{1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1}, 
			{1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0}, 
			{1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0}, 
			{0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1}, 
			{1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
			{0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0}, 
			{1, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0}
	};
	
	public MainCanvas(MIDlet midlet) {
		this.midlet=midlet;
		
		curRow = 0;
		curCol = 0;
		
		map=new byte[ROW][COL];
		for(int i=0;i<ROW;i++){
			for(int j=0;j<COL;j++){
				map[i][j]=map1[i][j];
			}
		}		

		try {
			imgFloor = Image.createImage("/floor.png");
			imgWall = Image.createImage("/wall.png");
			imgSuccess = Image.createImage("/success.png");
			imgFailure = Image.createImage("/failure.png");
		} catch (Exception e) {
			System.err.println(e.getMessage());
		}

		wallWidth = this.getWidth() / COL;
		wallHeight = this.getHeight() / ROW;

		topMargin = (this.getHeight() - wallHeight * ROW) / 2;
		leftMargin = (this.getWidth() - wallWidth * COL) / 2;		
		
		offImageFloor = Image.createImage(wallWidth * COL, wallHeight * ROW);
		offGraphicsFloor = offImageFloor.getGraphics();
		
		offImageWall = Image.createImage(wallWidth, wallHeight);
		offGraphicsWall = offImageWall.getGraphics();		
		
		cmdFirst=new Command("地图一",Command.ITEM,1);		
		cmdSecond=new Command("地图二",Command.ITEM,2);
		cmdThird=new Command("地图三",Command.ITEM,3);		
		cmdRandom=new Command("随机地图",Command.ITEM,4);
		cmdStart = new Command("开始", Command.ITEM, 5);
		cmdStop = new Command("停止", Command.ITEM, 6);
		cmdExit = new Command("退出", Command.CANCEL,7);
		
		this.addCommand(cmdFirst);
		this.addCommand(cmdSecond);
		this.addCommand(cmdThird);		
		this.addCommand(cmdRandom);
		this.addCommand(cmdStart);
		this.addCommand(cmdStop);
		this.addCommand(cmdExit);		
		
		this.setCommandListener(this);

		try {
			InputStream is = getClass().getResourceAsStream("/backmusic.mid");
			player = Manager.createPlayer(is, "audio/midi");
			player.setLoopCount(-1);
		} catch (IOException e) {
			System.err.println(e.getMessage());
		} catch (MediaException e) {
			System.err.println(e.getMessage());
		}
	}

	public void Start() {
		m_bRunning = true;	
		m_bFailed=false;
		// 播放音乐
		try {
			player.start();
		} catch (MediaException e) {
			e.printStackTrace();
		}
		curRow=0;
		curCol=0;
		startTime=System.currentTimeMillis();
		thread=new Thread(this);
		thread.start();
	}

	public void Stop() {
		m_bRunning = false;
		// 停止音乐
		try {
			player.stop();
		} catch (MediaException e) {
			e.printStackTrace();
		}		
	}	

	public void keyPressed(int keyCode) {
		if (m_bRunning) {
			switch(getGameAction(keyCode)){
			case Canvas.UP:
				if (curRow > 0 && map[curRow - 1][curCol] == 0) {
					curRow--;
					repaint(); // 重绘屏幕					
				}
				break;
			case Canvas.DOWN:
				if (curRow < ROW - 1 && map[curRow + 1][curCol] == 0) {
					curRow++;
					repaint(); // 重绘屏幕					
				}
				break;
			case Canvas.LEFT:
				if (curCol > 0 && map[curRow][curCol - 1] == 0) {
					curCol--;
					repaint(); // 重绘屏幕					
				}
				break;
			case Canvas.RIGHT:
				if (curCol < COL - 1 &&map[curRow][curCol + 1] == 0) {
					curCol++;
					repaint(); // 重绘屏幕					
				}
				break;			
			}
				

		}
	}

	public void paint(Graphics g) {		
		// 将屏幕填充为黑色		
		g.setColor(0, 0, 0);
		g.fillRect(0, 0, this.getWidth(), this.getHeight());

		// 绘制地板		
		offGraphicsFloor.drawImage(imgFloor, 0, 0, 0);
		g.drawImage(offImageFloor, leftMargin, topMargin, 0);		
		
		if(m_bRunning){
			// 绘制墙壁		
			offGraphicsWall.drawImage(imgWall, 0, 0, 0);
			for (int i = 0; i < ROW; i++) {
				for (int j = 0; j < COL; j++) {
					if (map[i][j] == 1) {
						g.drawImage(offImageWall, leftMargin + wallWidth * j, topMargin+ wallHeight * i, 0);
					}
				}
			}		
			//绘制起点红色圆球
			g.setColor(255, 0, 0);
			g.fillArc(leftMargin + (curCol) * wallWidth, topMargin + (curRow)* wallHeight, wallWidth, wallHeight, 0, 360);
			//绘制终点黄色方块
			g.setColor(255, 255, 0);
			g.fillRect(leftMargin + wallWidth * (COL - 1), topMargin + wallHeight * (ROW - 1), wallWidth, wallHeight);
			//显示剩余时间
			g.setFont(Font.getFont(Font.FACE_SYSTEM,Font.STYLE_BOLD,Font.SIZE_LARGE));
			g.setColor(255,255,255);
			g.drawString(String.valueOf(60-(endTime-startTime)/1000), 0, 0, 0);
		}else{
			// 绘制墙壁		
			offGraphicsWall.drawImage(imgWall, 0, 0, 0);
			for (int i = 0; i < ROW; i++) {
				for (int j = 0; j < COL; j++) {				
					g.drawImage(offImageWall, leftMargin + wallWidth * j, topMargin+ wallHeight * i, 0);					
				}
			}	
		}

		//走出迷宫,显示“恭喜”图片
		if (curRow == ROW - 1 && curCol == COL - 1 && !m_bFailed) {
			g.drawImage(imgSuccess,(this.getWidth() - imgSuccess.getWidth()) / 2, (this.getHeight() - imgSuccess.getHeight()) / 2, 0);			
			Stop();
		}
		
		//时间到了未走出迷宫,显示“失败”图片
		if(m_bFailed){
			g.drawImage(imgFailure,(this.getWidth() - imgFailure.getWidth()) / 2, (this.getHeight() - imgFailure.getHeight()) / 2, 0);
		}		
	}

	public void run() {
		while(m_bRunning){
			endTime=System.currentTimeMillis();
			if(endTime-startTime>60000){
				m_bRunning=false;
				m_bFailed=true;
			}
			repaint();
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {				
				e.printStackTrace();
			}
		}		
	}

	public void commandAction(Command cmd, Displayable dis) {
		if (cmd == cmdStart){
			Start();		
		}else if(cmd==cmdStop){
			Stop();
		}else if(cmd==cmdExit){
			midlet.notifyDestroyed();
		}else if(cmd==cmdFirst){
			for(int i=0;i<ROW;i++){
				for(int j=0;j<COL;j++){
					map[i][j]=map1[i][j];
				}
			}	
			repaint();
		}else if(cmd==cmdSecond){
			for(int i=0;i<ROW;i++){
				for(int j=0;j<COL;j++){
					map[i][j]=map2[i][j];
				}
			}		
			repaint();
		}else if(cmd==cmdThird){
			for(int i=0;i<ROW;i++){
				for(int j=0;j<COL;j++){
					map[i][j]=map3[i][j];
				}
			}	
			repaint();
		}else if(cmd==cmdRandom){
			Random r=new Random();
			for(int i=0;i<ROW;i++){
				for(int j=0;j<COL;j++){
					map[i][j]=(byte) (Math.abs(r.nextInt())%2);
				}
			}	
			map[0][0]=0;
			map[ROW-1][COL-1]=0;
			repaint();
		}
	}
}

⌨️ 快捷键说明

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