java写迷宫游戏并加入计时功能,迷宫有三种简单、中等、困难三种模式,并写出代码

时间: 2023-07-10 15:40:38 浏览: 47
好的,下面是 Java 实现迷宫游戏并加入计时功能的完整代码,包括三种难度模式的实现: ```java import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.util.Random; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.Timer; public class MazeGame extends JPanel implements KeyListener { private static final int WIDTH = 800; // 游戏窗口的宽度 private static final int HEIGHT = 600; // 游戏窗口的高度 private static final int BLOCK_SIZE = 20; // 每个格子的大小 private static final int TIMER_DELAY = 1000; // 计时器的触发时间间隔 // 迷宫地图的数据 private int[][] map; // 迷宫地图的大小 private int rows; private int cols; // 起点和终点的坐标 private int startRow; private int startCol; private int endRow; private int endCol; // 玩家的当前位置 private int playerRow; private int playerCol; // 计时器和计时器显示的时间 private Timer timer; private int timeLeft; // 游戏的难度 private int difficulty; public MazeGame(int difficulty) { this.difficulty = difficulty; initMap(); initPlayer(); initTimer(); setPreferredSize(new Dimension(WIDTH, HEIGHT)); setFocusable(true); addKeyListener(this); } private void initMap() { if (difficulty == 1) { rows = 10; cols = 10; timeLeft = 60; } else if (difficulty == 2) { rows = 15; cols = 15; timeLeft = 120; } else if (difficulty == 3) { rows = 20; cols = 20; timeLeft = 180; } map = new int[rows][cols]; generateMaze(); } private void initPlayer() { playerRow = startRow; playerCol = startCol; } private void initTimer() { timer = new Timer(TIMER_DELAY, e -> { timeLeft--; if (timeLeft == 0) { timer.stop(); JOptionPane.showMessageDialog(this, "时间到,游戏失败!"); resetGame(); } repaint(); }); timer.start(); } private void generateMaze() { // 初始化地图,全部设置为墙 for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { map[i][j] = 1; } } // 随机生成起点和终点 Random random = new Random(); startRow = random.nextInt(rows); startCol = random.nextInt(cols); endRow = random.nextInt(rows); endCol = random.nextInt(cols); // 将起点和终点设置为通路 map[startRow][startCol] = 2; map[endRow][endCol] = 3; // 递归生成迷宫 generateMazeRecursive(startRow, startCol); } private void generateMazeRecursive(int row, int col) { int[] directions = {0, 1, 2, 3}; shuffleArray(directions); for (int direction : directions) { int newRow = row; int newCol = col; switch (direction) { case 0: // 上 newRow--; break; case 1: // 右 newCol++; break; case 2: // 下 newRow++; break; case 3: // 左 newCol--; break; } if (newRow < 0 || newRow >= rows || newCol < 0 || newCol >= cols) { continue; } if (map[newRow][newCol] == 1) { // 如果是墙,则打通墙 map[(row + newRow) / 2][(col + newCol) / 2] = 0; map[newRow][newCol] = 0; generateMazeRecursive(newRow, newCol); } } } private void shuffleArray(int[] arr) { Random random = new Random(); for (int i = arr.length - 1; i > 0; i--) { int j = random.nextInt(i + 1); int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); // 绘制迷宫地图 for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { if (map[i][j] == 1) { // 墙 g.setColor(Color.BLACK); g.fillRect(j * BLOCK_SIZE, i * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE); } else if (map[i][j] == 2) { // 起点 g.setColor(Color.GREEN); g.fillRect(j * BLOCK_SIZE, i * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE); } else if (map[i][j] == 3) { // 终点 g.setColor(Color.RED); g.fillRect(j * BLOCK_SIZE, i * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE); } else { // 空白位置 g.setColor(Color.WHITE); g.fillRect(j * BLOCK_SIZE, i * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE); } } } // 绘制玩家 g.setColor(Color.BLUE); g.fillOval(playerCol * BLOCK_SIZE + BLOCK_SIZE / 4, playerRow * BLOCK_SIZE + BLOCK_SIZE / 4, BLOCK_SIZE / 2, BLOCK_SIZE / 2); // 绘制计时器 g.setColor(Color.BLACK); g.drawString("时间:" + timeLeft + " 秒", 10, 20); } private void resetGame() { timer.stop(); initMap(); initPlayer(); initTimer(); } @Override public void keyPressed(KeyEvent e) { int keyCode = e.getKeyCode(); switch (keyCode) { case KeyEvent.VK_UP: move(-1, 0); break; case KeyEvent.VK_RIGHT: move(0, 1); break; case KeyEvent.VK_DOWN: move(1, 0); break; case KeyEvent.VK_LEFT: move(0, -1); break; } } private void move(int dRow, int dCol) { int newRow = playerRow + dRow; int newCol = playerCol + dCol; if (newRow < 0 || newRow >= rows || newCol < 0 || newCol >= cols) { return; } if (map[newRow][newCol] == 1) { // 如果是墙,则不能移动 return; } if (map[newRow][newCol] == 3) { // 如果是终点,则游戏结束 timer.stop(); JOptionPane.showMessageDialog(this, "恭喜通关!"); resetGame(); return; } playerRow = newRow; playerCol = newCol; repaint(); } @Override public void keyTyped(KeyEvent e) { } @Override public void keyReleased(KeyEvent e) { } public static void main(String[] args) { JFrame frame = new JFrame("迷宫游戏"); int difficulty = Integer.parseInt(JOptionPane.showInputDialog("请选择难度:\n1.简单\n2.中等\n3.困难")); MazeGame game = new MazeGame(difficulty); frame.add(game); frame.pack(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLocationRelativeTo(null); frame.setVisible(true); } } ``` 在上面的代码中,我们使用了 Java Swing 来绘制游戏窗口和迷宫地图。在 main 方法中,根据用户选择的难度来创建 MazeGame 对象,然后将其添加到 JFrame 中,最后显示窗口。 在 MazeGame 类中,我们通过 initMap 方法来生成迷宫地图,通过 initPlayer 方法来初始化玩家的位置,通过 initTimer 方法来初始化计时器。generateMazeRecursive 方法使用递归算法来生成迷宫,shuffleArray 方法用于打乱数组的顺序。在 paintComponent 方法中,我们通过 Graphics 对象来绘制迷宫地图、玩家和计时器。 在 keyPressed 方法中,我们根据用户按下的键来移动玩家。在 move 方法中,我们首先判断移动是否越界或者是否碰到了墙,然后判断是否到达了终点,最后更新玩家的位置并重绘游戏界面。 在 resetGame 方法中,我们停止计时器并重新生成迷宫地图、初始化玩家和计时器。在 main 方法中,我们根据用户选择的难度来创建 MazeGame 对象,并显示游戏窗口。 这样,我们就完成了 Java 实现迷宫游戏并加入计时功能的代码。

相关推荐

最新推荐

recommend-type

C#使用读写锁三行代码简单解决多线程并发的问题

本文主要介绍了C#使用读写锁三行代码简单解决多线程并发写入文件时提示“文件正在由另一进程使用,因此该进程无法访问此文件”的问题。需要的朋友可以参考借鉴
recommend-type

Java 获取当前时间及实现时间倒计时功能【推荐】

主要介绍了Java 获取当前时间及实现时间倒计时功能 ,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
recommend-type

Java 实现倒计时功能(由秒计算天、小时、分钟、秒)

最近做项目遇到这样的需求,天、小时、分钟、秒的数值都是隔开的,服务器端只返回一个时间戳长度,怎么实现这样的功能呢?下面小编给大家带来了Java 实现倒计时功能的方案,需要的朋友参考下吧
recommend-type

TCP 四种定时器(重传定时器,坚持计时器,保活定时器,时间等待计时器)

主要介绍了TCP 四种定时器,重传定时器,坚持计时器,保活定时器,时间等待计时器的相关资料,需要的朋友可以参考下
recommend-type

Java计时新姿势StopWatch详解

主要介绍了Java计时新姿势StopWatch,最近公司来了个大佬,从他那里学到不少东西,其中一个就是计时的新姿势「StopWatch」,需要的朋友可以参考下
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

list根据id查询pid 然后依次获取到所有的子节点数据

可以使用递归的方式来实现根据id查询pid并获取所有子节点数据。具体实现可以参考以下代码: ``` def get_children_nodes(nodes, parent_id): children = [] for node in nodes: if node['pid'] == parent_id: node['children'] = get_children_nodes(nodes, node['id']) children.append(node) return children # 测试数
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。