前言
人类建造迷宫已有5000年的历史。在世界的不同文化发展时期,这些奇特的建筑物始终吸引人们沿着弯弯曲曲、困难重重的小路吃力地行走,寻找真相。迷宫类小游戏应运而生。在游戏中,迷宫被表现为冒险舞台里,藏有各式各样奇妙与谜题或宝藏的危险区域。型态有洞窟、人工建筑物、怪物巢穴、密林或山路等。迷宫内有恶徒或凶猛的生物(真实存在或想像物体都有)徘徊,其中可能会有陷阱、不明设施、遗迹等。
《简单迷宫》游戏是用java语言实现,采用了swing技术进行了界面化处理,设计思路用了面向对象思想。
主要需求
方向键控制移动,角色走出迷宫,游戏胜利。
主要设计
1、构建游戏地图面板
2、设定迷宫地图,包含可走的通道,不可走的墙体,还有出口位置
3、键盘的上下左右按键,来控制角色的移动
4、角色移动的算法,通道可走,遇到墙体不可走
5、走到终点,有成功通关的提示。
功能截图
游戏开始页面
移动界面
通关的界面
代码实现
窗口布局
public class MainApp extends JFrame {
public MainApp(){
setTitle("简易迷宫游戏");
MapPanel panel=new MapPanel();
Container contentPane = getContentPane();
contentPane.add(panel);
pack();
}
public static void main(String[] args) {
MainApp app=new MainApp();
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
app.setVisible(true);
}
}
核心算法
public class MapPanel extends JPanel implements KeyListener {
private static final int WIDTH = 450;
private static final int HEIGHT = 450;
private static final int ROW = 15;
private static final int COLUMN = 15;
private static final int SIZE = 30;
private static final byte FLOOR = 0;
private static final byte WALL = 1;
private static final byte END = 2;
private byte[][] map = {
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 1},
{1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1},
{1, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1},
{1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1},
{1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1},
{1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1},
{1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1},
{1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1},
{1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1},
{1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1},
{1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1},
{1, 1, 1, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 2, 1}
};
private Image floorImage;
private Image wallImage;
private Image heroImage;
private Image endImage;
private int x, y;
private static final byte LEFT = 0;
private static final byte RIGHT = 1;
private static final byte UP = 2;
private static final byte DOWN = 3;
public MapPanel() {
setPreferredSize(new Dimension(WIDTH, HEIGHT));
loadImage();
this.x = 1;
this.y = 1;
setFocusable(true);
addKeyListener(this);
}
public void paintComponent(Graphics g) {
drawMap(g);
drawRole(g);
}
private void drawRole(Graphics g) {
g.drawImage(heroImage, x * SIZE, y * SIZE, SIZE, SIZE, this);
}
private void loadImage() {
ImageIcon icon = new ImageIcon(getClass().getResource("images/floor.png"));
floorImage = icon.getImage();
icon = new ImageIcon(getClass().getResource("images/wall.gif"));
wallImage = icon.getImage();
icon = new ImageIcon(getClass().getResource("images/hero.png"));
heroImage = icon.getImage();
icon = new ImageIcon(getClass().getResource("images/end.png"));
endImage = icon.getImage();
}
private void drawMap(Graphics g) {
for (int i = 0; i < ROW; i++) {
for (int j = 0; j < COLUMN; j++) {
switch (map[i][j]) {
case 0:
g.drawImage(floorImage, j * SIZE, i * SIZE, this);
break;
case 1:
g.drawImage(wallImage, j * SIZE, i * SIZE, this);
break;
case 2:
g.drawImage(endImage, j * SIZE, i * SIZE, SIZE, SIZE, this);
default:
break;
}
}
}
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
switch (keyCode) {
case KeyEvent.VK_LEFT:
move(LEFT);
break;
case KeyEvent.VK_A:
move(LEFT);
break;
case KeyEvent.VK_RIGHT:
move(RIGHT);
break;
case KeyEvent.VK_D:
move(RIGHT);
break;
case KeyEvent.VK_UP:
move(UP);
break;
case KeyEvent.VK_W:
move(UP);
break;
case KeyEvent.VK_DOWN:
move(DOWN);
break;
case KeyEvent.VK_S:
move(DOWN);
break;
default:
break;
}
repaint();
if (isFinish(x, y)) {
JOptionPane.showMessageDialog(this, "恭喜通关!");
}
}
@Override
public void keyReleased(KeyEvent e) {
}
private boolean isAllowMove(int x, int y) {
if (x < COLUMN && y < ROW) {
return map[y][x] != 1;
}
return false;
}
private void move(int event) {
switch (event) {
case LEFT:// 左移
if (isAllowMove(x - 1, y)) {
x--;
}
break;
case RIGHT:// 右移
if (isAllowMove(x + 1, y)) {
x++;
}
break;
case UP:// 上移
if (isAllowMove(x, y - 1)) {
y--;
}
break;
case DOWN:// 下移
if (isAllowMove(x, y + 1)) {
y++;
}
default:
break;
}
}
private boolean isFinish(int x, int y) {
return map[y][x] == END;
}
}
总结
通过此次的《简易迷宫》游戏实现,让我对swing的相关知识有了进一步的了解,对java这门语言也有了比以前更深刻的认识。
java的一些基本语法,比如数据类型、运算符、程序流程控制和数组等,理解更加透彻。java最核心的核心就是面向对象思想,对于这一个概念,终于悟到了一些。
源码获取
源码下载地址:传送门------->
可关注博主后,私聊博主免费获取
今天是持续写作的第 5 / 100 天。 可以关注我,点赞我、评论我、收藏我啦。
|