java迷宫小游戏源代码

合集下载

一个关於迷宫算法的JAVA程序

一个关於迷宫算法的JAVA程序

一个关於迷宫算法的JAVA程序以下是一个关于迷宫算法的JAVA程序的示例,代码注释中解释了程序的工作原理和算法实现细节。

```javaimport java.util.ArrayDeque;import java.util.ArrayList;import java.util.Deque;import java.util.List;public class MazeSolverprivate int[][] maze;private int[][] solution;private int size;public MazeSolver(int[][] maze)this.maze = maze;this.size = maze.length;this.solution = new int[size][size];}//迷宫的通路为0,墙壁为1,已走过的路径为2//使用深度优先算法找到从起点到终点的路径public boolean solveMazboolean[][] visited = new boolean[size][size];return solveMazeHelper(0, 0, visited);}private boolean solveMazeHelper(int x, int y, boolean[][] visited)//检测是否到达终点if (x == size - 1 && y == size - 1)solution[x][y] = 1;return true;}//检查当前位置是否可行if (isSafe(x, y, visited))//标记当前位置为已访问visited[x][y] = true;//标记当前位置为路径的一部分solution[x][y] = 1;//递归探索当前位置的相邻位置if (solveMazeHelper(x + 1, y, visited)) // 向右移动return true;if (solveMazeHelper(x, y + 1, visited)) // 向下移动return true;if (solveMazeHelper(x - 1, y, visited)) // 向左移动return true;if (solveMazeHelper(x, y - 1, visited)) // 向上移动return true;//如果没有找到路径,取消当前位置的标记solution[x][y] = 0;}return false;}private boolean isSafe(int x, int y, boolean[][] visited) //检查坐标是否在合法范围内,且该位置没有被访问过return x >= 0 && y >= 0 && x < size && y < size &&maze[x][y] == 0 && !visited[x][y];}//使用BFS算法找到从起点到终点的最短路径public List<Integer> findShortestPatboolean[][] visited = new boolean[size][size];int[][] distance = new int[size][size];int[][] parent = new int[size][size];//初始化距离和父节点数组for (int i = 0; i < size; i++)for (int j = 0; j < size; j++)distance[i][j] = Integer.MAX_VALUE; // 初始距离为无穷大parent[i][j] = -1; // 初始父节点为无效值}}//使用BFS算法来设置距离和父节点数组Deque<int[]> queue = new ArrayDeque<>(;queue.offer(new int[]{0, 0}); // 起点入队列visited[0][0] = true;distance[0][0] = 0;int[][] directions = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}}; // 右下左上while (!queue.isEmpty()int[] current = queue.poll(;int x = current[0];int y = current[1];for (int[] direction : directions)int newX = x + direction[0];int newY = y + direction[1];if (isSafe(newX, newY, visited))visited[newX][newY] = true;queue.offer(new int[]{newX, newY});distance[newX][newY] = distance[x][y] + 1; // 更新距离parent[newX][newY] = x * size + y; // 更新父节点}}}//从终点回溯到起点,构造最短路径List<Integer> shortestPath = new ArrayList<>(;int currentX = size - 1;int currentY = size - 1;while (parent[currentX][currentY] != -1)shortestPath.add(0, currentX * size + currentY); // 将当前节点添加到路径首部int parentX = parent[currentX][currentY] / size;int parentY = parent[currentX][currentY] % size;currentX = parentX;currentY = parentY;}shortestPath.add(0, 0); // 添加起点到路径首部return shortestPath;}public void printSolutiofor (int[] row : solution)for (int cell : row)System.out.print(cell + " ");}System.out.println(;}}public static void main(String[] args)int[][] maze ={0,1,1,1,1},{0,0,1,0,1},{1,0,0,0,0},{1,1,0,1,1},{1,1,0,0,0}};MazeSolver solver = new MazeSolver(maze);solver.solveMaze(;System.out.println("Solution:");solver.printSolution(;List<Integer> shortestPath = solver.findShortestPath(;System.out.println("Shortest Path:" + shortestPath);}```此程序实现了解决迷宫问题的两种算法:深度优先和广度优先。

使用Java创建迷宫游戏

使用Java创建迷宫游戏

使用Java创建迷宫游戏-一个实战教程迷宫游戏是一种有趣的小型游戏,玩家需要在迷宫中找到通往终点的路径。

在这个实战博客中,我们将创建一个简单的Java迷宫游戏,演示如何构建一个基本的游戏应用。

我们将使用Java编程语言和一些基本的图形库来完成这个项目。

以下是本实战博客的主要内容:项目概述准备工作创建游戏窗口生成迷宫游戏逻辑绘制迷宫和角色用户输入和游戏循环运行和测试游戏总结让我们开始吧!1. 项目概述在本项目中,我们将创建一个简单的Java迷宫游戏,它包括以下主要功能:随机生成一个迷宫地图。

在地图上绘制一个玩家角色,玩家可以通过键盘控制角色移动。

玩家的目标是找到迷宫的出口。

游戏会提供胜利和失败的反馈。

我们将使用Java编程语言和以下开发库来构建迷宫游戏:Java标准库:用于基本的Java编程。

Swing:用于创建图形用户界面(GUI)的Java库。

AWT(抽象窗口工具集):用于创建游戏窗口和绘制图形。

2. 准备工作在开始之前,确保您的开发环境已设置好。

我们将使用Java标准库、Swing和AWT来创建游戏,不需要额外的库或工具。

3. 创建游戏窗口首先,我们需要创建游戏的窗口。

我们将使用Swing库来创建一个简单的窗口,用于显示游戏画面。

javaCopy codeimport javax.swing.*;import java.awt.*;public class MazeGame extends JFrame {private static final int WIDTH = 800;private static final int HEIGHT = 600;public MazeGame() {setTitle("迷宫游戏");setSize(WIDTH, HEIGHT);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setLocationRelativeTo(null);setResizable(false);MazePanel mazePanel = new MazePanel();add(mazePanel);setVisible(true);}public static void main(String[] args) {SwingUtilities.invokeLater(() -> new MazeGame());}}在上述代码中,我们创建了一个MazeGame类,继承自JFrame,用于创建游戏窗口。

java迷宫小游戏源代码

java迷宫小游戏源代码

帮朋友写的迷宫小游戏程序java//作者:LaoCooonimport java.awt.Graphics; import javax.swing.ImageIcon;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;import java.awt.event.*; import javax.swing.*; import java.awt.*;class mMaze extends Frame{ Color redColor;Random Random=new Random();int mapI=Random.getRandom();MapArray MapArray=new MapArray();int[] map = MapArray.getMapArray(mapI);static ImageIcon wall= new ImageIcon("wall.jpg");final ImageIcon tortoise= new ImageIcon("tortoise.gif");int xl=0,yl=1,speed=30;int x=0,y=1;public mMaze(){addKeyListener(new KeyAdapter(){public void keyPressed(KeyEvent e){if(e.getKeyCode()== KeyEvent.VK_UP){System.out.println("\n Go Up"); if(map[(yl-1)*29+xl]!=1) yl-=1; }else if(e.getKeyCode()== KeyEvent.VK_DOWN){ System.out.println("\n Go Down"); if(map[(yl+1)*29+xl]!=1) yl+=1; } else if(e.getKeyCode()== KeyEvent.VK_LEFT){ System.out.println("\n Go Left"); if(map[yl*29+(xl-1)]!=1) xl-=1; } else if(e.getKeyCode()== KeyEvent.VK_RIGHT){ System.out.println("\n Go Right"); if(map[yl*29+(xl+1)]!=1) xl+=1; } else System.out.println(e.getKeyChar());if(y==27&&x==28) System.out.println("\n You Win~!");repaint(); } } );setSize(890,910); setVisible(true); setLocation(400,200);addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ dispose(); System.exit(0); } } ); }public void paint(Graphics g){g.drawImage(tortoise.getImage(), xl*speed+8, yl*speed+30, null);int i=0,j=0;for ( i = 0; i < 29; i++)for(j=0;j<29;j++)if(map[i*29+j]==1) g.drawImage(wall.getImage(), j*30+8, i*30+30, null);}}public class Maze{public static void main(String[] args){ new mMaze(); }}回复•2楼•2013-05-16 10:34•删除 |••LaoCooon•class MapArray{public int[] getMapArray(int i){int[] map ={1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1, 1,1,1,0,1,0,1,1,1,0,1,0,1,0,1,1,1,0,1,0,1,1,1,0,1,0,1,0,1, 1,0,0,0,1,0,0,0,1,0,0,0,1,0,1,0,0,0,1,0,0,0,1,0,1,0,1,0,1, 1,0,1,1,1,1,1,0,1,1,1,1,1,0,1,0,1,1,1,1,1,0,1,0,1,0,1,0,1, 1,0,1,0,0,0,0,0,1,0,0,0,1,0,1,0,1,0,1,0,0,0,1,0,1,0,1,0,1, 1,0,1,0,1,1,1,1,1,0,1,0,1,0,1,0,1,0,1,0,1,1,1,0,1,0,1,0,1, 1,0,0,0,1,0,0,0,0,0,1,0,1,0,0,0,1,0,1,0,0,0,0,0,1,0,1,0,1, 1,0,1,1,1,0,1,1,1,1,1,0,1,1,1,0,1,0,1,1,1,1,1,1,1,0,1,0,1, 1,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,1,0,0,0,0,0,1,0,0,0,1,0,1, 1,0,1,0,1,1,1,0,1,0,1,1,1,0,1,0,1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,1,1,1,1,1,1,1,1,1,1,0,1, 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1, 1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1, 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,0,0,1, 1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1, 1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,1,0,0, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,};if(i<0&&i>4) i=1;switch(i){case 0:map=map;break;case 1:map=map1;break;case 2:map=map2;break;case 3:map=map3;break;case 4:map=map4;break;default: map=map;}return map;}}•••LaoCooon•import java.util.Date;import java.util.Calendar;class Random{public int getRandom(){Calendar c = Calendar.getInstance(); int second = c.get(Calendar.SECOND); //System.out.println(second%5); return second%5;}}回复•4楼•2013-05-16 10:37•删除 |•。

java小游戏源代码

java小游戏源代码

j a v a小游戏源代码 Document number:NOCG-YUNOO-BUYTT-UU986-1986UTJava小游戏第一个Java文件:import class GameA_B {public static void main(String[] args) {Scanner reader=new Scanner;int area;"Game Start…………Please enter the area:(1-9)" +'\n'+"1,2,3 means easy"+'\n'+"4,5,6 means middle"+'\n'+"7,8,9 means hard"+'\n'+"Please choose:");area=();switch((area-1)/3){case 0:"You choose easy! ");break;case 1:"You choose middle! ");break;case 2:"You choose hard! ");break;}"Good Luck!");GameProcess game1=new GameProcess(area);();}}第二个Java文件:import class GameProcess {int area,i,arrcount,right,midright,t;int base[]=new int[arrcount],userNum[]=newint[area],sysNum[]=new int[area];Random random=new Random();Scanner reader=new Scanner;GameProcess(int a){area=a;arrcount=10;right=0;midright=0;t=0;base=new int[arrcount];userNum=new int[area];sysNum=new int[area];for(int i=0;i<arrcount;i++){base[i]=i;// }}void process(){rand();while(right!=area){scanf();compare();print();check();}}void rand(){for(i=0;i<area;i++){t=(arrcount);//sysNum[i]=base[t];delarr(t);}}void delarr(int t){for(int j=t;j<arrcount-1;j++)base[j]=base[j+1];arrcount--;}void scanf(){"The system number has created!"+"\n"+"Please enter "+area+" Numbers");for(int i=0;i<area;i++){userNum[i]=();}}void check(){if(right==area)"You win…………!");}boolean check(int i){return true;}void compare(){int i=0,j=0;right=midright=0;for(i=0;i<area;i++){for(j=0;j<area;j++){if(userNum[i]==sysNum[j]){if(i==j)right++;elsemidright++;}}}}void print(){" A "+right+" B "+midright);}}。

求解迷宫全部路径的Java源程序

求解迷宫全部路径的Java源程序
fd.setVisible(true);
String fpath=fd.getDirectory();
String fname=fd.getFile();
String si=fpath+fname;
f.dispose();
try{
RandomAccessFile rwFile=new RandomAccessFile(si,"rw");
{
System.out.print("("+r+","+c+","+di+")"+" ");
}
}//end class StackLink
////////////////////////////////////
class SingleLinkListForStack
{
public StackLink first;
*/
int a[][]={{0,0,1,0,0,0,1,0},
{0,0,1,0,0,0,1,0},
{0,0,0,0,1,1,0,1},
{0,1,1,1,0,0,1,0},
{0,0,0,1,0,0,0,0},
m=4;n=9;startrow=1;startcol=1;endrow=4;endcol=9;
*/
Frame f=new Frame("FileDialog");
f.setSize(600,500);
FileDialog fd=new FileDialog(f,"File Dialog For Save",FileDialog.SAVE);

用Java编写动物迷宫游戏

用Java编写动物迷宫游戏

用Java编写动物迷宫游戏分第一部分:游戏简介邮递员小白兔需要走迷宫送四封信选一幅迷宫图在这幅迷宫上有五个小房子,小兔子要把信送到五个小房子中另外还需要一个控制小兔子方向的遥控器,小兔子可以向东西南北四个方向移动算法:1、首先在迷宫上画出方格,在游戏中用变量fangge表示用方向箭头控制小白兔从一个方格移动到另一个方格上,一直到送完信为止其中每个方格可在游戏中画一个矩形框,可显示小白兔的位置,移动位置后先擦去原来画的矩形,然后画一个新的矩形,在每个方格上可显示出可以移动的方向,点示图中的方向键,可以移动到下一个方格的位置,现在我们要在每个方格里放一种动物,而且我们还要把这个迷宫的图片去掉,注意:我们用Java编写游戏,不是用VB编写游戏,我们这个游戏中没有任何图片,这是一个文字游戏。

方格编号动物名称可以移动的方向1 小熊猫1号向东走可到2号方格2 大象2号向东=7;向西=1;向南=33 鳄鱼东南西北4 大熊猫西南北5 梅花鹿南第一个送信的房子6 黑熊西北7 老虎东西8 狮子东9 花豹东西南10 骆驼东西南看到这么多的动物,很容易让人联想到动物园里的动物吧!游戏要求走完所有的方格,看完所有的动物就算打完这个游戏了!第二部分:编写动物迷宫游戏游戏变量(一共有三个全局变量):fangge设为整数值,代表以上表格中的方格编号还有四个枚举量,为东西南北,在代码中用NORTH SOUTH EAST WEST 表示Direction设为字符串,代表东西南北四个方向Exits(3) 代表一个数组,初始化为0,如果移动到一个新的方格,则Exits(Direction)的值为新的方格编号,如果值为零,则表示不可以朝这个方向移动这个游戏的名称为Migong,出现在代码public class中。

第三部分:游戏代码import java.util.Scanner;public class Migong {public static void main(String[] args) {Scanner in=new Scanner(System.in);System.out.println("-------动物迷宫游戏--------");System.out.println("请按方向行走 (1.东 2.西 3.南 4.北)");int person=in.nextInt();Int fangge=1int[] exits;exits=new int[4];exits[0]=0;exits[1]=0;exits[2]=0;exits[3]=0;int i;i=person-1;boolean a=exits[i]==0;boolean b=exits[i]!=0;if (a){System.out.println("你不能往这个方向走");}else{fangge=exits[i];}switch(fangge){case 1:exits[0]=2;exits[1]=0;exits[2]=0;exits[3]=0;System.out.println("你看到的是小熊猫,可以向东走");break;case 2:exits[0]=7;exits[1]=1;exits[2]=3;exits[3]=0;System.out.println("你看到的是大象,可以向东、向南、向西走"); break;case 3:exits[0]=4;exits[1]=8;exits[2]=2;exits[3]=10;System.out.println("你看到的是鳄鱼,可以向东、向西、向南、向北走");break;case 4:exits[0]=0;exits[1]=3;exits[2]=6;exits[3]=5;System.out.println("你看到的是大熊猫,可以向西、向南、向北走");break;case 5:exits[0]=0;exits[1]=0;exits[2]=4;exits[3]=0;System.out.println("你看到的是梅花鹿,可以向南走");break;case 6:exits[0]=0;exits[1]=9;exits[2]=0;exits[3]=4;System.out.println("你看到的是黑熊,可以向西、向北走");break;}}}注:本游戏代码只编写了前6个方格,为了简单一些,更容易看懂。

java解决迷宫问题,gui界面,非常实用

java解决迷宫问题,gui界面,非常实用

package com.mingrui.recursion;import java.awt.event.*;import java.awt.*;import javax.swing.*;public class Test19_8 extends JFrame{private Cell[][] board=new Cell[8][8];private JButton jbFindPath=new JButton("找到出路");private JButton jbClearPant=new JButton("清除路线");private JPanel jpUp,jpBut;Test19_8(){setSize(345,256);setResizable(false);setDefaultCloseOperation(3);setLocationRelativeTo(null);jpUp=new JPanel();jpBut=new JPanel();jpUp.setLayout(new GridLayout(8,8,2,2));for(int i=0;i<8;i++){for(int j=0;j<8;j++){board[i][j]=new Cell();jpUp.add(board[i][j]);}}add(jpUp,BorderLayout.CENTER);jpBut.add(jbFindPath);jpBut.add(jbClearPant);add(jpBut,BorderLayout.SOUTH);jbFindPath.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){findPath();}});jbClearPant.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){clearPath();}});setVisible(true);}public static void main(String [] args){new Test19_8();}public void findPath(){if(findPath(0,0)){//JOptionPane.showMessageDialog(null, "找到了,我找到了");System.out.println("找到路了");}else//JOptionPane.showMessageDialog(null,"没有了出路,人生就是悲哀");System.out.println("没有出路");}public boolean findPath(int row,int col){board[row][col].visit();if(row==7&&col==7){board[row][col].selectedCell();return true;}if((row>0)&&!board[row-1][col].blocked()&&!board[row-1][col].marked( )&&!board[row-1][col].visited()){block(row,col);if(findPath(row-1,col)){board[row][col].selectedCell();return true;}unblock(row,col);}if((row<7)&&!board[row+1][col].blocked()&&!board[row+1][col].marked( )&&!board[row+1][col].visited()){block(row,col);if(findPath(row+1,col)){board[row][col].selectedCell();return true;}unblock(row,col);}if((col>0)&&!board[row][col-1].blocked()&&!board[row][col-1].marked( )&&!board[row][col-1].visited()){block(row,col);if(findPath(row,col-1)){board[row][col].selectedCell();return true;}unblock(row,col);}if((col<7)&&!board[row][col+1].blocked()&&!board[row][col+1].marked( )&&!board[row][col+1].visited()){block(row,col);if(findPath(row,col+1)){board[row][col].selectedCell();return true;}unblock(row,col);}return false;}public void clearPath(){for(int i=0;i<8;i++){for(int j=0;j<8;j++){board[i][j].desSelectedCell();}}}public void block(int row,int col){if(row>0)board[row-1][col].block();if(row<7)board[row+1][col].block();if(col>0)board[row][col-1].block();if(col<7)board[row][col+1].block();}public void unblock(int row,int col){if(row>0)board[row-1][col].unblock();if(row<7)board[row+1][col].unblock();if(col>0)board[row][col-1].unblock();if(col<7)board[row][col+1].unblock();}}class Cell extends JPanel{private boolean visited=false;private boolean marked=false;private boolean cellSelected=false;private boolean blocked=false;Cell(){setBackground(Color.red);addMouseListener(new MouseAdapter(){public void mousePressed(MouseEvent e) {marked = !marked;repaint();}});}protected void paintComponent(Graphics g){super.paintComponent(g);if(marked){g.drawLine(0, 0, getSize().width, getSize().height);g.drawLine(getSize().width, 0, 0, getSize().height);}}public boolean visited(){return visited;}public void visit(){visited=true;}public boolean marked(){return marked;}public void block(){blocked=true;}public void unblock(){blocked=false;}public boolean blocked(){return blocked;}public void selectedCell(){setBackground(Color.green);repaint();}public void desSelectedCell(){ setBackground(Color.red);repaint();}}。

java迷宫

java迷宫

import javax.microedition.lcdui.Display;import javax.microedition.midlet.MIDlet;import javax.microedition.midlet.MIDletStateChangeException;public class MazeMIDlet extends MIDlet {static Display display = null;private MazeGameCanvas mazeGameCanvas = null;public MazeMIDlet() {mazeGameCanvas = new MazeGameCanvas();// TODO Auto-generated constructor stub}protected void destroyApp(boolean arg0) throws MIDletStateChangeException {// TODO Auto-generated method stub}protected void pauseApp() {// TODO Auto-generated method stub}protected void startApp() throws MIDletStateChangeException {display = Display.getDisplay(this);display.setCurrent(mazeGameCanvas);// TODO Auto-generated method stub}}******************************************************************************* import java.io.IOException;import javax.microedition.lcdui.Alert;import javax.microedition.lcdui.AlertType;import javax.microedition.lcdui.Graphics;import javax.microedition.lcdui.Image;import javax.microedition.lcdui.game.GameCanvas;import yerManager;import javax.microedition.lcdui.game.Sprite;import javax.microedition.lcdui.game.TiledLayer;public class MazeGameCanvas extends GameCanvas implements Runnable{ Sprite car;TiledLayer wall;TiledLayer grass;LayerManager layerManager;int[][] cells = {{2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2},{2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 2, 1, 2, 1, 1, 1, 2, 1, 2},{2, 1, 2, 2, 2, 1, 2, 1, 2, 2, 2, 1, 2, 1, 2, 1, 2, 2, 2, 1, 2},{2, 1, 1, 1, 2, 1, 2, 1, 1, 1, 2, 1, 2, 1, 2, 1, 1, 1, 1, 1, 2},{2, 1, 2, 1, 2, 1, 2, 2, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2},{2, 1, 2, 1, 2, 1, 2, 1, 1, 1, 2, 1, 1, 1, 1, 1, 2, 1, 2, 1, 2},{2, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 1, 2, 1, 2, 2, 2, 1, 2, 2, 2},{2, 1, 2, 1, 2, 1, 1, 1, 1, 1, 1, 1, 2, 1, 2, 1, 2, 1, 1, 1, 2},{2, 1, 2, 1, 2, 1, 2, 1, 2, 2, 2, 1, 2, 2, 2, 1, 2, 1, 2, 2, 2},{2, 1, 2, 1, 1, 1, 2, 1, 2, 1, 2, 1, 2, 1, 1, 1, 1, 1, 2, 1, 2},{2, 1, 2, 2, 2, 1, 2, 2, 2, 1, 2, 2, 2, 1, 2, 2, 2, 2, 2, 1, 2},{2, 1, 1, 1, 1, 1, 1, 1, 2, 1, 2, 1, 1, 1, 1, 1, 1, 1, 2, 1, 2},{2, 1, 2, 2, 2, 2, 2, 1, 2, 1, 2, 2, 2, 1, 2, 2, 2, 1, 2, 1, 2},{2, 1, 1, 1, 2, 1, 2, 1, 1, 1, 2, 1, 1, 1, 1, 1, 2, 1, 1, 1, 2},{2, 2, 2, 1, 2, 1, 2, 1, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 1, 2},{2, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 2, 1, 2},{2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2},{2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 2},{2, 1, 2, 1, 2, 1, 2, 2, 2, 2, 2, 1, 2, 1, 2, 2, 2, 1, 2, 1, 2},{2, 1, 2, 1, 2, 1, 1, 1, 1, 1, 2, 1, 2, 1, 1, 1, 2, 1, 2, 1, 2},{2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2} };private int[] seq = {0, 1, 2};private int carX=1;private int carY=1;private int vwX=0;private int vwY=0;private int vwWidth;private int vwHeight;private static final int CELL_WIDTH = 24;private static final int CELL_HEIGHT = 24;static final byte GRASS = 1;static final byte WALL = 2;static final byte BORDER = 72;public MazeGameCanvas() {super(false);setFullScreenMode(true); //全屏模式下再取宽和长,不要在定义的时候初始化宽和长vwWidth = getWidth();vwHeight = getHeight();Image imageCar=null;Image imageBg=null;try {imageBg=Image.createImage("/bg.png");imageCar = Image.createImage("/car.png");} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}car=new Sprite(imageCar,CELL_WIDTH,CELL_HEIGHT);//宽高一样car.setFrameSequence(seq);car.setPosition(carX*CELL_WIDTH,carY*CELL_HEIGHT);grass=new TiledLayer(cells[0].length,cells.length,imageBg,CELL_WIDTH,CELL_HEIGHT);wall=new TiledLayer(cells[0].length,cells.length,imageBg,CELL_WIDTH,CELL_HEIGHT);for(int i=0;i<cells.length;i++){for(int j=0;j<cells[0].length;j++){grass.setCell(j,i,0);wall.setCell(j, i, 0);if(cells[i][j]==GRASS){grass.setCell(j, i, GRASS);}elsewall.setCell(j, i, WALL);}}layerManager=new LayerManager();layerManager.append(car);layerManager.append(wall);layerManager.append(grass);Graphics g=getGraphics();layerManager.paint(g, 0, 0);layerManager.setViewWindow(vwX, vwY, vwWidth, vwHeight);new Thread(this).start();}//下面写出将地图数组放到grass和wall两个层上//下面写出建立一个层管理器layerManager,并加上三个层car,wall,grass,并绘出layerManagerpublic void run() {Graphics g = getGraphics();int x,y;while (true) {int keyState = getKeyStates();if ((keyState & LEFT_PRESSED) != 0) {car.setTransform(Sprite.TRANS_NONE);x=carX-1;y=carY;movePosition(x,y);//佯动或者真动}else if ((keyState & RIGHT_PRESSED) != 0) {car.setTransform(Sprite.TRANS_MIRROR);x=carX+1;y=carY;movePosition(x,y);}else if ((keyState & UP_PRESSED) != 0) {car.setTransform(Sprite.TRANS_ROT90);x=carX;y=carY-1;movePosition(x,y);}else if ((keyState & DOWN_PRESSED) != 0) {car.setTransform(Sprite.TRANS_ROT270);x=carX;y=carY+1;movePosition(x,y);}car.nextFrame();layerManager.paint(g, 0, 0);//移动到下一帧要重新画flushGraphics();try {Thread.sleep(200);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}private void movePosition(int x, int y) {car.setPosition(x*CELL_HEIGHT, y*CELL_WIDTH);if (car.collidesWith(wall, false)) {car.setPosition(carX*CELL_HEIGHT, carY*CELL_WIDTH);return;}carX = x;carY = y;if (carY == cells.length-1 && carX == cells[0].length-2) {Alert alert = new Alert("congraulation");alert.setString("end");alert.setType();alert.setTimeout(Alert.FOREVER);MazeMIDlet.display.setCurrent(alert, this);}int cx = carX * CELL_WIDTH;int cy = carY * CELL_WIDTH;if (cx < vwX + BORDER && vwX>=CELL_WIDTH) {vwX -= CELL_WIDTH;layerManager.setViewWindow(vwX, vwY, vwWidth, vwHeight);} else if (cx > vwX + vwWidth - BORDER&& vwX + vwWidth<=grass.getWidth() - CELL_WIDTH) {vwX += CELL_WIDTH;layerManager.setViewWindow(vwX, vwY, vwWidth, vwHeight);}if (cy < this.vwY + BORDER && vwY>=CELL_HEIGHT) {vwY -= CELL_HEIGHT;layerManager.setViewWindow(vwX, vwY, vwWidth, vwHeight);} else if (cy > this.vwY + vwHeight - BORDER&& vwY + vwHeight<=grass.getHeight() - CELL_HEIGHT) { vwY += CELL_HEIGHT;layerManager.setViewWindow(vwX, vwY, vwWidth, vwHeight);}}}。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

帮朋友写的迷宫小游戏程序java//作者:LaoCooonimport java.awt.Graphics; import javax.swing.ImageIcon;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;import java.awt.event.*; import javax.swing.*; import java.awt.*;class mMaze extends Frame{ Color redColor;Random Random=new Random();int mapI=Random.getRandom();MapArray MapArray=new MapArray();int[] map = MapArray.getMapArray(mapI);static ImageIcon wall= new ImageIcon("wall.jpg");final ImageIcon tortoise= new ImageIcon("tortoise.gif");int xl=0,yl=1,speed=30;int x=0,y=1;public mMaze(){addKeyListener(new KeyAdapter(){public void keyPressed(KeyEvent e){if(e.getKeyCode()== KeyEvent.VK_UP){System.out.println("\n Go Up"); if(map[(yl-1)*29+xl]!=1) yl-=1; }else if(e.getKeyCode()== KeyEvent.VK_DOWN){ System.out.println("\n Go Down"); if(map[(yl+1)*29+xl]!=1) yl+=1; } else if(e.getKeyCode()== KeyEvent.VK_LEFT){ System.out.println("\n Go Left"); if(map[yl*29+(xl-1)]!=1) xl-=1; } else if(e.getKeyCode()== KeyEvent.VK_RIGHT){ System.out.println("\n Go Right"); if(map[yl*29+(xl+1)]!=1) xl+=1; } else System.out.println(e.getKeyChar());if(y==27&&x==28) System.out.println("\n You Win~!");repaint(); } } );setSize(890,910); setVisible(true); setLocation(400,200);addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ dispose(); System.exit(0); } } ); }public void paint(Graphics g){g.drawImage(tortoise.getImage(), xl*speed+8, yl*speed+30, null);int i=0,j=0;for ( i = 0; i < 29; i++)for(j=0;j<29;j++)if(map[i*29+j]==1) g.drawImage(wall.getImage(), j*30+8, i*30+30, null);}}public class Maze{public static void main(String[] args){ new mMaze(); }}回复•2楼•2013-05-16 10:34•删除 |••LaoCooon•class MapArray{public int[] getMapArray(int i){int[] map ={1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1, 1,1,1,0,1,0,1,1,1,0,1,0,1,0,1,1,1,0,1,0,1,1,1,0,1,0,1,0,1, 1,0,0,0,1,0,0,0,1,0,0,0,1,0,1,0,0,0,1,0,0,0,1,0,1,0,1,0,1, 1,0,1,1,1,1,1,0,1,1,1,1,1,0,1,0,1,1,1,1,1,0,1,0,1,0,1,0,1, 1,0,1,0,0,0,0,0,1,0,0,0,1,0,1,0,1,0,1,0,0,0,1,0,1,0,1,0,1, 1,0,1,0,1,1,1,1,1,0,1,0,1,0,1,0,1,0,1,0,1,1,1,0,1,0,1,0,1, 1,0,0,0,1,0,0,0,0,0,1,0,1,0,0,0,1,0,1,0,0,0,0,0,1,0,1,0,1, 1,0,1,1,1,0,1,1,1,1,1,0,1,1,1,0,1,0,1,1,1,1,1,1,1,0,1,0,1, 1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,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1, 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1, 1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1, 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,0,0,1, 1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1, 1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,1,0,0, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,};if(i<0&&i>4) i=1;switch(i){case 0:map=map;break;case 1:map=map1;break;case 2:map=map2;break;case 3:map=map3;break;case 4:map=map4;break;default: map=map;}return map;}}•••LaoCooon•import java.util.Date;import java.util.Calendar;class Random{public int getRandom(){Calendar c = Calendar.getInstance(); int second = c.get(Calendar.SECOND); //System.out.println(second%5); return second%5;}}回复•4楼•2013-05-16 10:37•删除 |•。

相关文档
最新文档