此程序为用JAVA编写的拼图小游戏,可通过简单的图片移动实现拼图,并实现音乐播放功能。(此程序只完成简单功能的实现,大佬勿喷)
程序源码下载:点击下载程序源码
软件系统实现
拼图主体为一方形区域,位于中央,其中包含九个小区域,放置图片,其中第九张为空。通过点击空白附近图片完成图片的移动。
拼图背景为自己插入的图片,不会随鼠标的点击而改变;
拼图上侧为三个按钮重新开始,音乐,提示,分别实现重新开始,音乐播放与提示部分的显示,当点击重新开始按钮时,游戏重新开始,拼图主体进行变换;
当点击音乐按钮时音乐播放停止,音乐图标变暗,当再次点击时音乐播放,音乐图标变亮;当点击提示按钮时提示显示,提示图标变亮,当再次点击时提示时,提示图标变暗,提示部分消失。
右上侧为提示部分,当点击提示时,提示部分显示,再次点击时消失。
拼图按钮与主体之间为游戏时间与移动步数的显示,其中时间每秒钟加一,图片移动时移动步数加一,当点击重新开始按钮时,游戏时间与移动步数全部变为0。
当拼图完成后拼图左侧与右侧显示恭喜成功字样,拼图边框变为红色。点击重新开始时字样消失。
代码:
Yard.java
import javax.swing.JFrame;
public class Yard extends JFrame {
Win win = new Win();
public Yard() {
this.setTitle("拼图");
this.setSize(1000, 850);
this.setLocationRelativeTo(null); // 设置窗体初始位置
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);// 不可改变大小
this.add(win);
this.setVisible(true);
}
public static void main(String args[]) {
new Yard();
}
}
Win.java
import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JTextPane;
public class Win extends JPanel implements MouseListener {
private int[][] tu = new int[3][3];
static boolean flag = false;// 判断是否成功
private boolean flag1 = false;// 判断提示按钮的显示
private int xx, yy;// 获取的拼图行列坐标
private int N = 0;// 移动步数
static int T = 0;// 计算时间
private Font font1 = new Font("华文彩云", Font.BOLD, 50);
private Font font2 = new Font("黑体", Font.BOLD, 20);
private Font font3 = new Font("黑体", Font.BOLD, 90);
private JButton b = new JButton("重新开始");
private JButton mus = new JButton("音乐");
private JButton out = new JButton("提示");
private Audio au = new Audio();// 设置音乐
private int k1 = 0, k2 = 0;// 判断音乐,提示按钮点击次数
public Win() {
final Time time = new Time();
time.start();// 开始计时
/**点击重新开始按钮后*/
b.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getSource() == b) {
Start();
time.setT(0);
flag = false;
N = 0;
T = 0;
}
}
});
/**点击音乐按钮后*/
mus.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
k1++;
/* 单击一下音乐按钮停止播放 */
if (e.getSource() == mus && k1 % 2 != 0) {
au.stop();
mus.setBackground(Color.GRAY);
}
/* 单击一下音乐按钮开始播放 */
else if (e.getSource() == mus && k1 % 2 == 0) {
au.start();
mus.setBackground(Color.cyan);
}
}
});
/**点击提示按钮后*/
out.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
k2++;
/* 单击一下提示按钮显示提示 */
if (e.getSource() == out && k2 % 2 != 0) {
flag1 = true;
out.setBackground(Color.cyan);
}
/* 再单击一下提示按钮提示消失 */
else if (e.getSource() == out && k2 % 2 == 0) {
flag1 = false;
out.setBackground(Color.gray);
}
}
});
/** 设置重新开始,音乐与提示按钮 */
b.setPreferredSize(new Dimension(140, 70));
b.setBackground(Color.cyan);
b.setFont(font2);
mus.setPreferredSize(new Dimension(140, 70));
mus.setBackground(Color.cyan);
mus.setFont(font2);
out.setPreferredSize(new Dimension(140, 70));
out.setBackground(Color.gray);
out.setFont(font2);
add(b);
add(mus);
add(out);
Start();
/** 音乐播放 */
au.init();
au.start();
/** 鼠标监听 */
this.addMouseListener(this);
}
private void Start() {
/** 将tu[i][j]赋值为1-9 */
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++) {
tu[i][j] = i * 3 + j + 1;
}
/** 打乱顺序 */
Random r = new Random();
int a, b, c;
for (int i = 0; i < 10; i++) {
a = r.nextInt(3);
b = r.nextInt(3);
c = tu[a][b];
tu[a][b] = tu[b][a];
tu[b][a] = c;
}
}
/* 绘制提示图部分 */
public void paint(Graphics g) {
super.paint(g);
if (flag1) {
g.drawRect(790, 0, 190, 190);
Image image = null;
try {
String str = "tu.png";
image = ImageIO.read(new File(str));
} catch (IOException e) {
e.printStackTrace();
}
/** 设置提示边框 */
g.setColor(Color.pink);
g.fillRect(730, 0, 200, 190);
g.setColor(Color.black);
g.setFont(font1);
g.drawString("提", 740, 80);
g.drawString("示", 740, 130);
/** 显示提示图片 */
g.drawImage(image, 810, 0, 190, 190, this);
}
}
/* 绘制拼图中的每个组件 */
public void paintComponent(Graphics g) {
super.paintComponent(g);
/** 绘制背景图 */
BufferedImage image1 = null;
try {
String str = "back1.png";
image1 = ImageIO.read(new File(str));
} catch (IOException e) {
e.printStackTrace();
}
g.drawImage(image1, 0, 0, 1000, 900, this);
/** 绘制拼图主体 */
g.drawRect(200, 200, 600, 600);
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++) {
if (tu[i][j] != 9) {
Image image = null;
try {
String str = "tu" + tu[i][j] + ".png";
image = ImageIO.read(new File(str));
} catch (IOException e) {
e.printStackTrace();
}
g.drawImage(image, 200 + 200 * j, 200 + 200 * i, 200, 200,
this);// 显示图片
}
}
g.setFont(font1);// 设置字体
/** 拼图完成时 */
if (flag) {
g.setColor(Color.yellow);
g.setFont(font3);// 设置字体
g.drawString("恭", 50, 350);
g.setColor(Color.red);
g.drawString("喜", 50, 600);
g.setColor(Color.green);
g.drawString("成", 850, 350);
g.setColor(Color.cyan);
g.drawString("功", 850, 600);
g.setColor(Color.red);
((Graphics2D) g).setStroke(new BasicStroke(15.0f));
g.drawRect(200, 200, 600, 600);
g.drawRect(195, 195, 610, 610);
}
/** 设置边框粗度 */
((Graphics2D) g).setStroke(new BasicStroke(5.0f));
/** 设置移动步数边框 */
g.setColor(Color.darkGray);
g.fillRect(350, 135, 378, 55);
g.setColor(Color.cyan);
g.setFont(font1);// 设置字体
g.drawString("移动步数:" + N, 350, 180);
/** 设置拼图主体边框 */
g.setColor(Color.green);
g.drawRect(200, 200, 600, 600);
g.setColor(Color.cyan);
g.drawRect(195, 195, 610, 610);
/** 设置游戏时间边框 */
g.setColor(Color.lightGray);
g.fillRect(350, 80, 378, 55);
g.setColor(Color.BLUE);
T = Time.getT();
g.setFont(font1);// 设置字体
g.drawString("游戏时间:" + T, 350, 125);
repaint();
}
@Override
public void mouseClicked(MouseEvent e) {
if (flag == true)
return; // 如果已经成功完成
/* 得到行数和列数 图片位置 */
int x = e.getX();
int y = e.getY();
xx = y / 200 - 1;
yy = x / 200 - 1;
move();// 图片交换
int count = 0;// 用于判断拼图是否完成
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++) {
if (tu[i][j] == i * 3 + j + 1)
count++;
}
if (count == 9)
flag = true;
}
private void move() {
/** 交换规则:图片周围为空白图片时,图片与空白位置交换 */
/* 如果图片左边为空白(图片不在最左侧) */
if (xx != 0 && tu[xx - 1][yy] == 9) {
/** 将图片与空白处交换位置 */
tu[xx - 1][yy] = tu[xx][yy];
tu[xx][yy] = 9;
N++;// 当图片移动时步数加一
}
/* 如果图片右边为空白(图片不在最右侧) */
else if (xx != 2 && tu[xx + 1][yy] == 9) {
/* 将图片与空白处交换位置 */
tu[xx + 1][yy] = tu[xx][yy];
tu[xx][yy] = 9;
N++;
}
/* 如果图片下边为空白(图片不在最下侧) */
else if (yy != 0 && tu[xx][yy - 1] == 9) {
/** 将图片与空白处交换位置 */
tu[xx][yy - 1] = tu[xx][yy];
tu[xx][yy] = 9;
N++;
}
/* 如果图片上边为空白(图片不在最上侧) */
else if (yy != 2 && tu[xx][yy + 1] == 9) {
/** 将图片与空白处交换位置 */
tu[xx][yy + 1] = tu[xx][yy];
tu[xx][yy] = 9;
N++;
}
}
@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
public static boolean getFlag() {
if (flag == false)
return false;
else
return true;
}
}
Time.java
class Time extends Thread {
static int t = 0;// 计时
public void run() {
try {
while (true) {
Thread.sleep(1000);// 暂停一秒
t++;
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static int getT() {
return t;
}
public void setT(int t) {
this.t = t;
}
}
Audio.java
import java.applet.*;
public class Audio extends Applet {
AudioClip music1;
public void init() {
music1 = newAudioClip(Audio.class.getResource("1.wav"));
}
public void start() {
music1.loop();
}
public void stop() {
music1.stop();
}
}
运行结果展示: