Following taught you to play games --JAVA GUI Sokoban (a) do a good job, we need to do the main game
1. Create a new window and create MainGame.java
This time we structure roughly like this (below), above JMenuBar as a menu bar, add menu (choose off, replay, map editor, on), below a game interface. The rest does not matter
package cn.edu.caztc.sokobangame;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
/**
* 游戏主体
*
* @author 莫言情难忘
*
*/
public class MainGame extends JFrame implements MapConfig {
// 游戏面板
JPanel panel;
public static void main(String[] args) {
new MainGame();
}
public MainGame() {
// TODO Auto-generated constructor stub
this.setTitle("推箱子");
this.setSize(900, 950);
this.setLayout(new FlowLayout());
this.setDefaultCloseOperation(3);
// 设置窗体居中
this.setLocationRelativeTo(null);
// 不可拉伸
this.setResizable(false);
JMenuBar menuBar = new JMenuBar();
this.add(menuBar, BorderLayout.NORTH);
JMenu menu = new JMenu("菜单");
menuBar.add(menu);
JMenuItem menuItem = new JMenuItem("选关");
menuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// System.out.println("选关");
}
});
menu.add(menuItem);
JMenuItem menuItem_1 = new JMenuItem("重新开始");
menuItem_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// 重新开始
}
});
menu.add(menuItem_1);
JMenuItem menuItem_2 = new JMenuItem("关于");
menuItem_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// System.out.println("关于");
JOptionPane.showMessageDialog(null, "莫言情难忘开发,csdn:莫言情难忘,个人QQ:1179307527", "关于",
JOptionPane.PLAIN_MESSAGE);
}
});
JMenu menu_1 = new JMenu("\u81EA\u5B9A\u4E49");
menuBar.add(menu_1);
JMenuItem menuItem_3 = new JMenuItem("地图编辑器");
menuItem_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
test demo = new test();
// demo.open();
}
});
menu_1.add(menuItem_3);
menuBar.add(menuItem_2);
panel = new JPanel();
this.add(panel);
this.setVisible(true);
}
}
2. Read the map data
We are in a tutorial of how to save, how to take this time out.
Where the parameter level is the level of meaning, that we read the map of the first few off
Diy parameters you choose clearance is that I am the developer to do the map or the players make their own maps
/**
* 读取地图数据
* @param level 关卡
* @param diy
*/
void GetMAP(int level,boolean diy) {
String stringdiy = "";
if (diy) {
stringdiy="diy";
}
try {
DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream(path+"\\" + stringdiy + level + ".map")));
int i = in.readInt();
int j = in.readInt();
for (int ii = 0; ii < i; ii++) {
for (int jj = 0; jj < j; jj++) {
map1[ii][jj][0] = in.readInt();
if (map1[ii][jj][0] == 5) {
playex = ii;
playey = jj;
map1[ii][jj][0] = 1;
}
}
}
} catch (Exception e) {
// TODO: handle exception
}
}
3. Map to achieve
And a part of the same, we need to create a panel inner class, used to construct the map.
Several of the values in the interface MapConfig
/**
* 自定义内部游戏面板类
*
* @author 莫言情难忘
*
*/
class MyPanel extends JPanel {
@Override
public void paint(Graphics g) {
super.paint(g);
for (int i = 0; i < MapHeight / eleHeight; i++) {
for (int j = 0; j < MapWidth / eleWidth; j++) {
g.drawImage(GetGameImage(map1[i][j][0]), getDrawX(j), getDrawY(i), eleWidth, eleHeight, null);
}
}
g.drawImage(icon106.getImage(), getDrawX(playey), getDrawY(playex), eleWidth, eleHeight, null);
}
// 将数组下标转化成对应的图片左上角坐标
public int getDrawX(int j) {
int x = j * 50;
return x;
}
// 将数组下标转化成对应的图片左上角坐标
public int getDrawY(int i) {
int y = i * 50;
return y;
}
}
4. Integration Code
Integrate the above code, the main game read 1.map default. The main method moves to test.java test. There is need for the D drive folder (Sokoban), there is a map file 1.map. 1.map documents are available in XXX download.
package cn.edu.caztc.sokobangame;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.FileInputStream;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
/**
* 游戏主体
*
* @author 莫言情难忘
*
*/
public class MainGame extends JFrame implements MapConfig {
int[][][] map1 = new int[18][18][1];// 地图数组
int playex = 0;// 玩家坐标
int playey = 0;// 玩家坐标
boolean diy = false;//
int level = 1;// 关卡
// 游戏面板
JPanel panel;
public MainGame() {
// TODO Auto-generated constructor stub
this.setTitle("推箱子");
this.setSize(900, 950);
this.setLayout(new FlowLayout());
this.setDefaultCloseOperation(3);
// 设置窗体居中
this.setLocationRelativeTo(null);
// 不可拉伸
this.setResizable(false);
JMenuBar menuBar = new JMenuBar();
this.add(menuBar, BorderLayout.NORTH);
JMenu menu = new JMenu("菜单");
menuBar.add(menu);
JMenuItem menuItem = new JMenuItem("选关");
menuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// System.out.println("选关");
}
});
menu.add(menuItem);
JMenuItem menuItem_1 = new JMenuItem("重新开始");
menuItem_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// 重新开始
}
});
menu.add(menuItem_1);
JMenuItem menuItem_2 = new JMenuItem("关于");
menuItem_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// System.out.println("关于");
JOptionPane.showMessageDialog(null, "莫言情难忘开发,csdn:莫言情难忘,个人QQ:1179307527", "关于",
JOptionPane.PLAIN_MESSAGE);
}
});
JMenu menu_1 = new JMenu("\u81EA\u5B9A\u4E49");
menuBar.add(menu_1);
JMenuItem menuItem_3 = new JMenuItem("地图编辑器");
menuItem_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
menu_1.add(menuItem_3);
menuBar.add(menuItem_2);
GetMAP(level, diy);
// 创建游戏面板
panel = setpanel();
this.add(panel);
this.setVisible(true);
}
/**
* 读取地图数据
*
* @param level 关卡
* @param diy
*/
void GetMAP(int level, boolean diy) {
String stringdiy = "";
if (diy) {
stringdiy = "diy";
}
try {
DataInputStream in = new DataInputStream(
new BufferedInputStream(new FileInputStream(PATH + "\\" + stringdiy + level + ".map")));
int i = in.readInt();
int j = in.readInt();
for (int ii = 0; ii < i; ii++) {
for (int jj = 0; jj < j; jj++) {
map1[ii][jj][0] = in.readInt();
if (map1[ii][jj][0] == 5) {
playex = ii;
playey = jj;
map1[ii][jj][0] = 1;
}
}
}
} catch (Exception e) {
// TODO: handle exception
}
}
/**
* 自定义内部游戏面板类
*
* @author 莫言情难忘
*
*/
class MyPanel extends JPanel {
@Override
public void paint(Graphics g) {
super.paint(g);
for (int i = 0; i < MAP_HEIGHT / SOUREC_HEIGHT; i++) {
for (int j = 0; j < MAP_WIDTH / SOUREC_WIDTH; j++) {
g.drawImage(GetGameImage(map1[i][j][0]), getDrawX(j), getDrawY(i), SOUREC_WIDTH, SOUREC_HEIGHT,
null);
}
}
g.drawImage(icon106.getImage(), getDrawX(playey), getDrawY(playex), SOUREC_WIDTH, SOUREC_HEIGHT, null);
}
// 将数组下标转化成对应的图片左上角坐标
public int getDrawX(int j) {
int x = j * 50;
return x;
}
// 将数组下标转化成对应的图片左上角坐标
public int getDrawY(int i) {
int y = i * 50;
return y;
}
}
/**
* 设置游戏面板
*/
public JPanel setpanel() {
JPanel panel = new MyPanel();
panel.setPreferredSize(new Dimension(MAP_WIDTH, MAP_HEIGHT));
panel.setLayout(null);
panel.setBackground(Color.black);
return panel;
}
/**
* 获取到数字对应的图片
*
* @param i 数字
* @return
*/
Image GetGameImage(int i) {
if (i > 5) {
i = 0;
}
return allicons[i].getImage();
}
}
Well, as a man Polytechnic write blog I was quite strenuous, but fortunately, more than just the code.
Mo Yan love memorable 1179307527
See more:
Taught you do Sokoban game --JAVA GUI (a)
Taught you do Sokoban game --JAVA GUI (ii)
Taught you do Sokoban game --JAVA GUI (c)
Taught you do Sokoban game --JAVA GUI (d)
Taught you do Sokoban game --JAVA GUI (V)