游戏软件开发
学 院: 计 算 机 科 学 与 技 术
班 级: 1 2 0 1
姓 名 : 李**
学 号: 2012115010154
完成日期: 2015年 6 月
游戏拼图
此游戏为一个简单的拼图游戏,在源文件中预存7张图片并且将其分割成4*4的窗口模式,在点击开始游戏的时候将其随机打乱,然后有玩家排列成原画
运行结果
游戏代码为:
package game;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;
public class MedleyGame extends JFrame {
private JLabel modelLabel;
private JPanel centerPanel;
private JButton emptyButton;
int num = 0;
public static void main(String[] args) {
try {
MedleyGame frame = new MedleyGame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
//建立窗口构造方法
public MedleyGame() {
super();
setResizable(false);
setTitle("拼图游戏");
setBounds(100, 100, 370, 525);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//创建面板对象,并增加边框、布局
final JPanel topPanel = new JPanel();
topPanel.setBorder(new TitledBorder(null, "", TitledBorder.DEFAULT_JUSTIFICATION,
TitledBorder.DEFAULT_POSITION, null, null));
topPanel.setLayout(new BorderLayout());
getContentPane().add(topPanel, BorderLayout.NORTH);//放于上方
//创建标签放原图
modelLabel = new JLabel();
modelLabel.setIcon(new ImageIcon("image/"+ num+ "model.jpg"));
topPanel.add(modelLabel, BorderLayout.WEST);
//在右侧加个面板,添加两个按钮
JPanel eastPanel = new JPanel();
topPanel.add(eastPanel,BorderLayout.CENTER);
eastPanel.setLayout(new BorderLayout());
JButton nextButton = new JButton();
nextButton.setText("下一张");
nextButton.addActionListener(new NextButtonAction());
eastPanel.add(nextButton,BorderLayout.NORTH);
//创建按钮开局添加监听
final JButton startButton = new JButton();
startButton.setText("开局");
startButton.addActionListener(new StartButtonAction());
eastPanel.add(startButton, BorderLayout.CENTER);
//初始化中心面板,设置边框,添加按钮
centerPanel = new JPanel();
centerPanel.setBorder(new TitledBorder(null, "", TitledBorder.DEFAULT_JUSTIFICATION,
TitledBorder.DEFAULT_POSITION, null, null));
centerPanel.setLayout(new GridLayout(4, 0));
getContentPane().add(centerPanel, BorderLayout.CENTER);
//初始化图片
String[][] exactnessOrder = order();
//按排列添加按钮,设置图片
for (int row=0; row<4; row++) {
for (int col=0; col<4; col++) {
final JButton button = new JButton();
button.setName(row+""+col);
button.setIcon(new ImageIcon(exactnessOrder[row][col]));
if (exactnessOrder[row][col].equals("image/"+ num+"00.jpg"))
emptyButton = button;
button.addActionListener(new ImgButtonAction());
centerPanel.add(button);
}
}
}
//初始化图片
private String[][] order() {
String[][] exactnessOrder = new String[4][4];
for (int row=0; row<4; row++) {
for (int col=0; col<4; col++) {
exactnessOrder[row][col] = "image/"+ num+ row+ col+ ".jpg";
}
}
return exactnessOrder;
}
//随机排列图片
private String[][] reorder() {
String[][] exactnessOrder = new String[4][4];
for (int row=0; row<4; row++) {
for (int col=0; col<4; col++) {
exactnessOrder[row][col] = "image/"+ num+ row+ col+ ".jpg";
}
}
String[][] stochasticOrder = new String[4][4];
for (int row=0; row<4; row++) {
for (int col=0; col<4; col++) {
while (stochasticOrder[row][col]==null) {
int r = (int) (Math.random()*4);
int c = (int) (Math.random()*4);
if (exactnessOrder[r][c] != null) {
stochasticOrder[row][col] = exactnessOrder[r][c];
exactnessOrder[r][c] = null;
}
}
}
}
return stochasticOrder;
}
//游戏时排列图片
class ImgButtonAction implements ActionListener {
public void actionPerformed(ActionEvent e) {
String emptyName= emptyButton.getName();
char emptyRow = emptyName.charAt(0);
char emptyCol = emptyName.charAt(1);
JButton clickButton = (JButton) e.getSource();
String clickName = clickButton.getName();
char clickRow = clickName.charAt(0);
char clickCol = clickName.charAt(1);
if (Math.abs(clickRow - emptyRow) + Math.abs(clickCol - emptyCol) == 1) {
emptyButton.setIcon(clickButton.getIcon());
clickButton.setIcon(new ImageIcon("image/"+ num+ "00.jpg"));
emptyButton = clickButton;
}
}
}
//换下一张图片
class NextButtonAction implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (num==5) {
num=0;
} else {
++num;
}
modelLabel.setIcon(new ImageIcon("image/"+num+"model.jpg"));
String[][] exactnessOrder = order();
int i= 0;
for (int row=0; row<4; row++) {
for (int col=0; col<4; col++) {
JButton button = (JButton) centerPanel.getComponent(i++);
button.setIcon(new ImageIcon(exactnessOrder[row][col]));
if(exactnessOrder[row][col].equals("image/"+ num+ "00.jpg"))
emptyButton=button;
}
}
}
}
//开局排列图片
class StartButtonAction implements ActionListener {
public void actionPerformed(ActionEvent e) {
String[][] stochasticOrder = reorder();
int i= 0;
for (int row=0; row<4; row++) {
for (int col=0; col<4; col++) {
JButton button = (JButton) centerPanel.getComponent(i++);
button.setIcon(new ImageIcon(stochasticOrder[row][col]));
if(stochasticOrder[row][col].equals("image/"+ num+ "00.jpg"))
emptyButton=button;
}
}
}
}
}
小结:
通过本次的游戏程序的编写及讲解,对自己做的游戏和游戏代码本身有了充分的了解,并且熟练地学会一个小游戏开发过程与经过在项目中不断的调试程序,寻找错误,丰富了我们的编程经验,提升了编写程序的能力
本文档为【游戏软件开发】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑,
图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。