1、迷宫 迷宫一、 程序:自动产生迷宫/ 范例文件:AutoMaze.javaimport java.applet.*;import java.awt.*;import java.awt.event.*;public class AutoMaze extends Applet int GridX=15,GridY=15; / 迷宫格数 int Si,Sj,Ei,Ej; / 入口与出口 int maze = new int3030; / 迷宫数组 int enter=20,width=20; / 迷宫入口位置与每格的大小 Button btn; Panel p; Image wall1, wall2
2、; MediaTracker MT; public void init() / 取得外墙图片 MT = new MediaTracker(this); wall1 = getImage(getDocumentBase(),Images/wall1.gif); wall2 = getImage(getDocumentBase(),Images/wall2.gif); MT.addImage(wall1,0); MT.addImage(wall2,0); try showStatus(图像加载中(Loading Images).); MT.waitForAll(); catch(Interrupt
3、edException E) /没有进行异常处理 btn = new Button(建立迷宫); / 按下按钮自动产生迷宫数组并绘制迷宫 btn.addActionListener( new ActionListener() public void actionPerformed(ActionEvent e) int i,j; Si=1;Sj=1;Ei=GridY;Ej=GridX; / 设定出入口 for (i=0;i=GridY+1;i+) for (j=0;j=GridX+1;j+) if (i=0 | j=0 | i=GridY+1 | j=GridX+1) mazeij=15; /
4、设定外墙 else mazeij=3; / 初始迷宫内部 genmaze(Ei,Ej); / 产生迷宫 mazeEiEj=mazeEiEj & 0xd; / 拆掉入口右墙 repaint(); ); p = new Panel(); p.add(btn); setLayout(new BorderLayout(); / 设定版面配置 add(p, South); / 递归产生迷宫数组 public void genmaze(int i, int j) int n; mazeij |= 0x4; / 标示此格已设定 while (mazeij+1=3 | mazei+1j=3 | mazeij-
5、1=3 | mazei-1j=3) / 如果不是外墙 n=(int)(4*Math.random()+1); / 决定下一个位置 if (n=1 & mazeij+1=3) / 向右走 mazeij &= 0xd; / 拆掉右墙 genmaze(i,j+1); / 递归继续走访下一个位置 else if (n=2 & mazei-1j=3) / 向下走 mazeij &= 0xe; / 拆掉上墙 genmaze(i-1,j); / 递归继续走访下一个位置 else if (n=3 & mazeij-1=3) / 向左走 mazeij-1 &= 0xd; / 拆掉右墙 genmaze(i,j-1
6、); / 递归继续走访下一个位置 else if (n=4 & mazei+1j=3) / 向上走 mazei+1j &= 0xe; / 拆掉上墙 genmaze(i+1,j); / 递归继续走访下一个位置 / 绘制迷宫 public void paint(Graphics g) int x, y, i, j; / 清除屏幕 g.clearRect(0,0,(GridX+3)*width,(GridY+6)*width); / 绘制上外墙 g.drawImage(wall1,enter,enter,(GridX+1)*width,2*width,0,0,100,100,this); / 绘制左
7、外墙 g.drawImage(wall2,0,2*width,width,(GridY+1)*width+5,0,0,100,100,this); / 绘制下外墙 g.drawImage(wall1,enter,(GridY+1)*width,(GridX+1)*width,(GridY+2)*width,0,0,100,100,this); / 绘制右外墙 g.drawImage(wall2,GridX*width,width,(GridX+1)*width,GridY*width,0,0,100,100,this); / 画迷宫内部 for (i=1;i=GridY;i+) for (j=
8、1;j=GridX;j+) x=(j-1)*width+enter; y=(i-1)*width+enter; / 画上墙 if (mazeij & 1)=1) g.drawImage(wall1,x-width/5,y,x+width,y+width,0,0,100,100,this); / 画右墙 if (mazeij & 2)=2) g.drawImage(wall2,x,y,x+width,y+width,0,0,100,100,this); 二、地图拼接技巧/ 程序:斜角地图拼接/ 范例文件:Map45.javaimport java.awt.*;import java.applet
9、.*;import java.awt.event.*;public class Map45 extends Applet int AppletWidth, AppletHeight,floorW,floorH,key; Image floor,OffScreen; Graphics drawOffScreen; MediaTracker MT; public void init() setBackground(Color.white); /设定背景颜色 AppletWidth = getSize().width; /取得Applet的高度 AppletHeight = getSize().he
10、ight; /取得Applet的宽度 /取得图像 MT = new MediaTracker(this); floor = getImage(getDocumentBase(),Images/floor.gif); MT.addImage(floor,0); try showStatus(图像加载中(Loading Images).); MT.waitForAll(); catch(InterruptedException E) /没有进行异常处理 /建立次画面 OffScreen = createImage(AppletWidth,AppletHeight); drawOffScreen =
11、 OffScreen.getGraphics(); floorW = floor.getWidth(this); floorH = floor.getHeight(this); public void paint(Graphics g) int X,Y; / 绘制地板,内循环每次绘制两排 for(int i=0; i = 16; i+) for(int j=0; j = 10; j+) X = j*floorW; Y = i*floorH; drawOffScreen.drawImage(floor,X,Y,X+floorW,Y+floorH,0,0,floorW,floorH,this);
12、X += floorW/2; Y += floorH/2; drawOffScreen.drawImage(floor,X,Y,X+floorW,Y+floorH,0,0,floorW,floorH,this); /将次画面贴到主画面中 g.drawImage(OffScreen,0,0,this); 三、/ 程序:立体坐标定位/ 范例文件:Map45Rotate.javaimport java.awt.*;import java.applet.*;import java.awt.event.*;public class Map45Rotate extends Applet int Apple
13、tWidth, AppletHeight,floorW,floorH,key; Image floor,OffScreen; Graphics drawOffScreen; MediaTracker MT; public void init() setBackground(Color.white); /设定背景颜色 AppletWidth = getSize().width; /取得Applet的高度 AppletHeight = getSize().height; /取得Applet的宽度 /取得图像 MT = new MediaTracker(this); floor = getImage
14、(getDocumentBase(),Images/floor.gif); MT.addImage(floor,0); try showStatus(图像加载中(Loading Images).); MT.waitForAll(); catch(InterruptedException E) /没有进行异常处理 /建立次画面 OffScreen = createImage(AppletWidth,AppletHeight); drawOffScreen = OffScreen.getGraphics(); floorW = floor.getWidth(this); floorH = floo
15、r.getHeight(this); public void paint(Graphics g) int X,Y; / 绘制地板 for(int i=1; i = 15; i+) for(int j=1; j = 15; j+) X = rotateX(j,i); / X 坐标转换 Y = rotateY(j,i); / Y 坐标转换 drawOffScreen.drawImage(floor,X,Y,X+floorW, Y+floorH,0,0,floorW,floorH,this); /将次画面贴到主画面中 g.drawImage(OffScreen,0,0,this); / X 坐标转换
16、 private int rotateX(int j, int i) int x; / 根据图片大小先算出原来坐标 x = (j-i)*floorW + 450; / 进行坐标转换 x = (int) (x*Math.cos(60*Math.PI/180); / 返回坐标值 return x; / Y 坐标转换 private int rotateY(int j, int i) int x, y; / 根据图片大小先算出原来坐标 x = j*floorW + 150; y = -i*floorH; / 进行坐标转换 y = (int ) -(y*Math.cos(-15*Math.PI/180
17、) + x*Math.sin(60*Math.PI/180)*Math.sin(-15*Math.PI/180); y = y - i * floorH/2; / 返回坐标值 return y ; 四、/ 程序:平面迷宫制作/ 范例文件:Maze1.javaimport java.awt.*;import java.applet.*;import java.awt.event.*;public class Maze1 extends Applet implements KeyListener, Runnable int ImageWidth,ImageHeight,ImageX,ImageY,
18、 AppletWidth, AppletHeight,floorW,key; int sx; / 角色贴图来源位置 char direction = R; / 角色贴图方向判断 Image character,floor,block1,block2,OffScreen; Thread newThread; Graphics drawOffScreen; MediaTracker MT; Sprite s; / 角色状态对象 / 迷宫数组 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
19、,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,1,1,1,2,0,1,0,1,1,0,0,2,2, 2,1,0,0,0,0,0,0,0,0,0,1,1,1,1,1,2,2, 2,1,0,1,0,1,1,0,1,1,0,0,1,0,2,0,2,2, 2,1,0,0,0,0,1,0,0,2,0,0,0,0,2,2,2,2, 2,1,0,0,2,0,1,1,1,2,0,1,0,0,2,1,1,2, 2,1,1,1,1,0,0,0,1,2,0,0,0,0,1,1,0,2, 2,0,0,0,0,0,0,0,1,0,0,1,1,0,2,1,0,2
20、, 2,0,2,0,1,0,0,0,1,0,1,0,0,0,2,1,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; public void init() addKeyListener(this); /注册事件处理函数 setBackground(Color.white); /设定背景颜色 AppletWidth = getSize().width; /取得Applet的高度 AppletHeight = getS
21、ize().height; /取得Applet的宽度 sx = 0; /取得图像 MT = new MediaTracker(this); character = getImage(getDocumentBase(),Images/character.gif); floor = getImage(getDocumentBase(),Images/floor.jpg); block1 = getImage(getDocumentBase(),Images/block1.gif); block2 = getImage(getDocumentBase(),Images/block2.gif); MT
22、.addImage(character,0); MT.addImage(floor,0); MT.addImage(block1,0); MT.addImage(block2,0); try showStatus(图像加载中(Loading Images).); MT.waitForAll(); catch(InterruptedException E) /没有进行异常处理 /建立次画面 OffScreen = createImage(AppletWidth,AppletHeight); drawOffScreen = OffScreen.getGraphics(); floorW = flo
23、or.getWidth(this); ImageWidth = character.getWidth(this) / 3; / 单元格图像的高度 ImageHeight = character.getHeight(this) / 4; / 单元格图像的宽度 ImageX = floorW; /图像的X坐标 ImageY = floorW/3; /图像的Y坐标 / 初始角色物件 s = new Sprite(ImageX, ImageY, ImageWidth, ImageHeight, maze); / 打开动画循环线程 public void start() newThread = new
24、Thread(this); newThread.start(); public void stop() newThread = null; public void update(Graphics g) /update()函数 paint(g); /只单纯调用paint()函数 public void paint(Graphics g) /将次画面贴到主画面中 g.drawImage(OffScreen,0,0,this); / = 绘图 = public void run() while(true) drawOffScreen.clearRect(0,0,AppletWidth,AppletH
25、eight); / 绘制地板 for(int i=0; i = AppletHeight; i+= floorW) for(int j=0; j = AppletWidth; j+=floorW) drawOffScreen.drawImage(floor,j,i,j+floorW,i+floorW, 0,0,floorW,floorW,this); / 根据迷宫数组绘制障碍物 for(int i=0; i 14; i+) for(int j=0; j s.SizeW*2) sx = 0; try Thread.sleep(200); catch (InterruptedException e) /=实现KeyListener界面= public void keyTyped(KeyEvent e) public void keyPressed(KeyEvent e) key = e.getKeyCode(); if(key = KeyEvent.VK_RIGHT) / 按向右键 direction = R; / 改变角色绘图方向 s.moveRight();
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1