java實現(xiàn)拼圖游戲
本文實例為大家分享了java實現(xiàn)拼圖游戲的具體代碼,供大家參考,具體內(nèi)容如下
游戲說明:
設(shè)計一款拼圖游戲,要求點擊圖片按鈕,實現(xiàn)圖片按鈕的移動,直到每一個按鈕都到達指定位置游戲終止退出。
游戲設(shè)計思路:
1.準備一張圖像文件;
2.創(chuàng)建N個按鈕圖標,每個按鈕圖標里面存入一張分割后的圖片信息;
3.創(chuàng)建一個空白按鈕用于和圖標按鈕交換位置,達到移動的效果;
4.亂序,將按鈕圖標亂序,完成游戲效果;
5.創(chuàng)建一個面板添加游戲開始和游戲結(jié)束按鈕;
6.設(shè)計游戲窗口;
游戲界面設(shè)計基本結(jié)構(gòu):

代碼實現(xiàn):
Cell類----設(shè)計每個按鈕對象應(yīng)該具備的屬性功能---針對按鈕
package puzzle_game;
import java.awt.Rectangle;
import javax.swing.Icon;
import javax.swing.JButton;
@SuppressWarnings("serial")
public class Cell extends JButton{
private static int IMAGEWIDTH;//設(shè)置按鈕的寬度大小
private static int IMAGEHEIGHT;
private int ID = 0;//設(shè)置當前按鈕的指向坐標
public Cell(Icon icon, int id, int imagewidth, int height)//構(gòu)造函數(shù)初始化,傳入兩個參數(shù),一個是圖像的圖標,一個是該按鈕的數(shù)組ID
{
this.setIcon(icon);
this.ID = id;
this.IMAGEWIDTH = imagewidth;
this.IMAGEHEIGHT = height;
this.setSize(IMAGEWIDTH, IMAGEHEIGHT);
}
public void move(Direction dir)//移動
{
Rectangle rec = this.getBounds();//獲取當前對象的這個邊框
switch(dir)
{
case UP://向上移動,改變坐標
this.setLocation(rec.x, rec.y + IMAGEHEIGHT);
break;
case DOWN://向下移動
this.setLocation(rec.x, rec.y - IMAGEHEIGHT);
break;
case LEFT://向左移動
this.setLocation(rec.x - IMAGEWIDTH, rec.y);
break;
case RIGHT://向右移動
this.setLocation(rec.x + IMAGEWIDTH, rec.y);
break;
}
}
public int getID() {
return ID;
}
public int getX()
{
return this.getBounds().x;
}
public int getY()
{
return this.getBounds().y;
}
}
Direction類------方向枚舉類,存放移動的方向
package puzzle_game;
public enum Direction {
UP,
DOWN,
LEFT,
RIGHT
}
GamePanel類-----游戲面板設(shè)計類,真正的游戲思想從此開始
主要實現(xiàn)的功能有:
1.初始化面板按鈕數(shù)組,將圖像轉(zhuǎn)化成圖標然后存入按鈕中;
2.打亂數(shù)組面板中的按鈕排序,實現(xiàn)游戲娛樂功能;
3.每個按鈕添加監(jiān)聽機制,實現(xiàn)點擊按鈕后的移動功能;
package puzzle_game;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class GamePanel extends JPanel implements MouseListener{
private Cell BlankCell = null;
private int row = 4;
private int col = 4;//設(shè)置這個拼圖的行列
private Cell cells[] = new Cell[row*col];//創(chuàng)建一個按鈕對象數(shù)組
int ImageWidth;
int ImageHeight;
public GamePanel()//構(gòu)造函數(shù)
{
this.setLayout(null);
init();
}
public void init()//初始化完成以下功能--完成圖像的切割,完成圖像到圖標的轉(zhuǎn)換,完成按鈕圖標的添加,將按鈕添加到面板上,并且給每一個按鈕添加監(jiān)聽機制
{
int num = 0;
BufferedImage buf = null;
BufferedImage bufnew = null;
ImageIcon icon = null;
int width = 0;
int height = 0;
try
{
buf = ImageIO.read(new File("F:/Image/Puzzle_game/puze.jpg"));//讀取文件圖像
ImageWidth = buf.getWidth();
ImageHeight = buf.getHeight();
System.out.println("ImageWidth->"+ImageWidth+"ImageHeight->"+ImageHeight);
width = ImageWidth/col;
height = ImageHeight/row;
}catch(Exception e)
{
System.out.println(e);
}
for(int i = 0; i < row; i++)
{
for(int j = 0; j < col; j++)
{
num = i*col+j;//表示當前這個圖像的坐標id,在數(shù)組中的下標
if(num < row*col-1)
{
bufnew = buf.getSubimage(width*j, height*i, width, height);
icon = new ImageIcon(bufnew);//將圖像轉(zhuǎn)化成圖標
}
else//使最后一張圖像為空白圖像
{
icon = new ImageIcon("F:/Image/Puzzle_game/background2.jpg");//一張空白圖像
}
cells[num] = new Cell(icon, num, width, height);//添加圖標到每一個BUTTON按鈕上面
cells[num].setLocation(width*j, height*i);
}
}
BlankCell = cells[cells.length-1];//初始化空白格
for(int i = 0; i < cells.length; i++)
{
this.add(cells[i]);//將每一個按鈕添加到當前這個面板上面
if(i < cells.length-1)
cells[i].addMouseListener(this);//空白格不添加監(jiān)聽機制
}
}
public void OutOfOrder()//亂序----打亂圖片的排布順序
{
Random random = new Random();
for(int i = 0 ; i < cells.length ; i++)
{
int index1 = random.nextInt(cells.length);//cells的長度是9,但是他的上限是9,取不到9,所取值范圍是0-8
int index2 = random.nextInt(cells.length);
int x = cells[index1].getX();
int y = cells[index1].getY();//獲取下標是index1的數(shù)組元素按鈕的坐標
cells[index1].setLocation(cells[index2].getX(), cells[index2].getY());
cells[index2].setLocation(x, y);
}
}
public boolean IsWin()//判斷游戲玩家是否贏
{
for(int i = 0; i < cells.length; i++)
{
int x = cells[i].getX();
int y = cells[i].getY();
if(x/(ImageWidth/col) + y/(ImageHeight/row) != i)
{
return false;
}
}
return true;
}
public void mouseClicked(MouseEvent e)
{
Cell t = (Cell) e.getSource();
int x = BlankCell.getX();
int y = BlankCell.getY();
if(t.getY() == y && t.getX() + ImageWidth/col == x)//圖像向右走
{
t.move(Direction.RIGHT);
BlankCell.move(Direction.LEFT);
}
else if(t.getY() == y && t.getX() - ImageWidth/col == x)//圖像向左走
{
t.move(Direction.LEFT);
BlankCell.move(Direction.RIGHT);
}
else if(t.getX() == x && t.getY() + ImageHeight/row == y)//圖像向上走
{
t.move(Direction.UP);
BlankCell.move(Direction.DOWN);
}
else if(t.getX() == x && t.getY() - ImageHeight/row == y)//圖像向下走
{
t.move(Direction.DOWN);
BlankCell.move(Direction.UP);
}
if(IsWin())
{
int choice = JOptionPane.showConfirmDialog(null, "恭喜您過關(guān)了是否還來一局?", "提示", JOptionPane.YES_NO_OPTION);
if(choice == 0)//表示再來一局
{
this.OutOfOrder();
}
else//表示退出游戲
System.exit(1);
}
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
}
GameFrame類------設(shè)置游戲的打開窗口類,創(chuàng)建了一個菜單面板存放游戲開始和游戲結(jié)束兩個按鈕,并且對游戲的窗口進行了基本設(shè)置,這是整個游戲的入口。
package puzzle_game;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class GameFrame extends JFrame {
public JPanel pane1 = new JPanel();
public JButton button1 = new JButton("游戲開始");
public JButton button2 = new JButton("游戲結(jié)束");
public GameFrame()
{
super("拼圖游戲");
pane1.setLayout(new FlowLayout());
pane1.add(button1);
pane1.add(button2);
Container con = this.getContentPane();
con.add(pane1,BorderLayout.NORTH);
GamePanel gamepane = new GamePanel();
con.add(gamepane,BorderLayout.CENTER);
this.setBounds(300, 300, 600, 600);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
button1.addActionListener(new ActionListener()
{
public void actionPerformed(final ActionEvent e)
{
gamepane.OutOfOrder();
}
});
button2.addActionListener(new ActionListener()
{
public void actionPerformed(final ActionEvent e)
{
System.exit(1);
}
});
}
public static void main(String[] args) {
new GameFrame(); }
}
這是剛運行程序以后的界面,也是拼圖成功的界面,我設(shè)置的是4*4模式,你也可以根據(jù)自己的喜好設(shè)計模式諸如–2*3,3*4,都可以;

這是我點擊開始以后運行的界面,當然每次都不同,因為亂序是隨機生成的順序,那么現(xiàn)在就可以玩你自己的游戲了:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
dom4j創(chuàng)建和解析xml文檔的實現(xiàn)方法
下面小編就為大家?guī)硪黄猟om4j創(chuàng)建和解析xml文檔的實現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-06-06
Spring如何按業(yè)務(wù)模塊輸出日志到不同的文件詳解
最近做項目時有一個記錄操作日志的需求,比如某個用戶進行了查詢、刪除、修改等操作,下面這篇文章主要給大家介紹了關(guān)于Spring如何按業(yè)務(wù)模塊輸出日志到不同文件的相關(guān)資料,需要的朋友可以參考下2022-05-05
Java class文件格式之數(shù)據(jù)類型_動力節(jié)點Java學(xué)院整理
這篇文章主要介紹了Java class文件格式之數(shù)據(jù)類型的相關(guān)資料,需要的朋友可以參考下2017-06-06
Mybatis-Plus集成Sharding-JDBC與Flyway實現(xiàn)多租戶分庫分表實戰(zhàn)
這篇文章主要為大家介紹了Mybatis-Plus集成Sharding-JDBC與Flyway實現(xiàn)多租戶分庫分表實戰(zhàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-11-11

