java 迷宫代码_老鼠走迷宫代码 JAVA

  • Post author:
  • Post category:java


展开全部

//老鼠走迷宫程序

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class Maze1 extends JPanel implements KeyListener

{ private int Width, Height,blockWidth, blockHeight;

//character是老鼠,floor是地板,block1是砖,block2是另一种e69da5e887aa3231313335323631343130323136353331333330343237砖

private Image character, floor, block1, block2;

//backGround是背景,offScreen是用于双缓冲用的次画面

private Image backGround, offScreen;

private Graphics drawOffScreen;

//这个类用来控制老鼠的移动

private SpriteAndMaze spriteAndMaze;

//是否进行了初始化

private boolean init = false;

//地图

private int[][] maze =

{

{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},

{0,0,0,0,0,1,0,0,2,0,0,1,1,0,0,0,2,2},

{2,0,1,0,1,0,0,0,2,0,1,0,0,1,1,0,2,2},

{2,0,0,0,0,0,1,1,2,0,1,0,1,0,0,0,0,2},

{2,1,0,0,0,0,0,0,0,0,0,0,1,0,1,1,0,2},

{2,1,0,1,0,1,1,0,1,1,0,0,1,0,0,0,0,2},

{2,1,0,0,0,0,1,0,0,2,0,0,0,0,2,0,2,2},

{2,1,0,0,2,0,1,1,1,2,0,1,0,0,2,0,1,2},

{2,1,1,1,1,0,0,0,1,2,0,0,0,0,1,0,0,2},

{2,0,0,0,0,0,0,0,1,0,0,1,1,0,2,1,0,2},

{2,0,2,0,1,0,0,0,1,0,1,0,0,0,0,0,0,2},

{2,1,2,0,0,0,1,1,1,0,1,0,1,1,2,1,0,2},

{2,0,0,0,2,0,0,0,1,1,0,0,0,0,0,0,0,0},

{2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}};

//老鼠的宽和高

private int characterWidth, characterHeight;

//初始化程序

public void initial()

{

addKeyListener(this);

setBackground(Color.white);

//取得JPanel的高度,宽度

Width = this.getWidth();

Height =this.getHeight();

//mediaTracker对象是用来跟踪影像的加载情况的,

//影像加载完毕之后才可以显示出来,否则就会出错

MediaTracker mediaTracker = new MediaTracker(this);

//获得image对象

character = Toolkit.getDefaultToolkit().getImage(“character.gif”);

floor = Toolkit.getDefaultToolkit().getImage(“floor.gif”);

block1 = Toolkit.getDefaultToolkit().getImage(“block1.gif”);

block2 = Toolkit.getDefaultToolkit().getImage(“block2.gif”);

//把几个image对象都放在mediaTracker里,第二个参数是加载的优先权,目前设

//成0,即优先权都相同

mediaTracker.addImage(character,0);

mediaTracker.addImage(floor,0);

mediaTracker.addImage(block1,0);

mediaTracker.addImage(block2,0);

try

{

//等待影像加载完毕

mediaTracker.waitForAll();

}

catch(InterruptedException e)

{ e.printStackTrace(); }

//spriteAndMaze对象的构造,0,1是老鼠的初始坐标,maze是迷宫数组对象

spriteAndMaze = new SpriteAndMaze(0, 1, maze);

//砖的宽度

blockWidth = Width / maze[0].length;

//砖的高度

blockHeight = Height / maze.length;

//老鼠的宽度

characterWidth = Width / maze[0].length;

//老鼠的高度

characterHeight = Height / maze.length;

//建立次画面

offScreen = this.createImage(Width, Height);

drawOffScreen = offScreen.getGraphics();

// 建立地图

backGround = createImage(Width, Height);

Graphics g = backGround.getGraphics();

// 画地板

for(int i = 0; i < maze.length; i++)

{

for(int j = 0; j < maze[0].length; j++)

{

g.drawImage(floor, j*blockWidth,i*blockHeight, blockWidth,blockHeight, this);

}

}

// 画障碍物

for(int i = 0; i < maze.length; i++)

{

for(int j = 0; j < maze[0].length; j++)

{

switch(maze[i][j])

{

case 1:

g.drawImage(block1, j*blockWidth,i*blockHeight, blockWidth,blockHeight, this);

break;

case 2:

g.drawImage(block2, j*blockWidth,i*blockHeight, blockWidth,blockHeight, this);

break;

default:

g.drawImage(floor,j*blockWidth, i*blockHeight,blockWidth, blockHeight, this);

}

}

}

this.grabFocus();

init = true;

repaint();

}

public void paintComponent(Graphics g)

{

if(init == false)

{

return;

}

// 在次画面上贴地图

drawOffScreen.drawImage(backGround, 0, 0, this);

//在次画面上贴老鼠

drawOffScreen.drawImage(

character,

spriteAndMaze.getLocationX()*blockWidth,

spriteAndMaze.getLocationY()*blockHeight,

characterWidth,

characterHeight,

this);

//将次画面贴到主画面中

g.drawImage(offScreen, 0, 0,this);

}

public void update(Graphics g)

{

paint(g);

}

//=====实现KeyListener接口=====

public void keyTyped(KeyEvent e) { }

public void keyPressed(KeyEvent e)

{

int key = e.getKeyCode();

switch(key)

{

case KeyEvent.VK_RIGHT:

spriteAndMaze.moveRight();

break;

case KeyEvent.VK_LEFT:

spriteAndMaze.moveLeft();

break;

case KeyEvent.VK_UP:

spriteAndMaze.moveUp();

break;

case KeyEvent.VK_DOWN:

spriteAndMaze.moveDown();

break;

}

repaint();

}

public void keyReleased(KeyEvent e) {}

public static void main(String args[])

{

JFrame f = new JFrame();

Container c = f.getContentPane();

f.setSize(600,600);

Maze1 m = new Maze1();

c.add(m);

f.show();

//调用初始化程序

m.initial();

}

}

class SpriteAndMaze

{

private int x, y;//老鼠的x,y坐标

private int maze[][];//迷宫数组

public SpriteAndMaze() {}

//构造函数

public SpriteAndMaze(int x, int y)

{

this.x = x;

this.y = y;

}

public SpriteAndMaze(int x, int y, int[][] maze)

{

this(x, y);

this.maze = maze;

}

//老鼠向上移

public void moveUp()

{

if(isMovable(x, y-1))

{

y–;

}

}

//老鼠向下移

public void moveDown()

{

if(isMovable(x, y+1))

{

y++;

}

}

//老鼠向右移

public void moveRight()

{

if(isMovable(x+1, y))

{

x++;

}

}

//老鼠向左移

public void moveLeft()

{

if(isMovable(x-1, y))

{

x–;

}

}

//是否能移动,如果前方有障碍物,返回假

private boolean isMovable(int x, int y)

{

if(y < 0 || x < 0 || x >= maze[0].length || y >=maze.length)

return false;

if(maze[y][x] == 0)

return true;

else

return false;

}

public int getLocationX()

{ return x;

}

public int getLocationY()

{

return y;

}

public void setLocationX(int locationX)

{ this.x = locationX;

}

public void setLocationY(int locationY)

{

this.y= locationY;

}

public int[][] getMaze()

{

return maze;

}

public void setMaze(int[][] maze)

{

this.maze = maze;

}

}

追问

这个我也有不过没有修改迷宫和计时功能,也不能找出最短路径

2Q==

已赞过

已踩过<

你对这个回答的评价是?

评论

收起



版权声明:本文为weixin_30313409原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。