A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 奋斗的懒人 初级黑马   /  2018-3-14 23:07  /  1850 人查看  /  29 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

游戏说明:
设计一款拼图游戏,要求点击图片按钮,实现图片按钮的移动,直到每一个按钮都到达指定位置游戏终止退出。
游戏设计思路:
1.准备一张图像文件;
2.创建N个按钮图标,每个按钮图标里面存入一张分割后的图片信息;
3.创建一个空白按钮用于和图标按钮交换位置,达到移动的效果;
4.乱序,将按钮图标乱序,完成游戏效果;
5.创建一个面板添加游戏开始和游戏结束按钮;
6.设计游戏窗口;

游戏界面设计基本结构:

代码实现:

Cell类----设计每个按钮对象应该具备的属性功能---针对按钮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;//设置按钮的宽度大小    private static int IMAGEHEIGHT;    private int ID = 0;//设置当前按钮的指向坐标    public Cell(Icon icon, int id, int imagewidth, int height)//构造函数初始化,传入两个参数,一个是图像的图标,一个是该按钮的数组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;    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
Direction类------方向枚举类,存放移动的方向package puzzle_game;public enum Direction {    UP,    DOWN,    LEFT,    RIGHT}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
GamePanel类-----游戏面板设计类,真正的游戏思想从此开始主要实现的功能有:1.初始化面板按钮数组,将图像转化成图标然后存入按钮中;2.打乱数组面板中的按钮排序,实现游戏娱乐功能;3.每个按钮添加监听机制,实现点击按钮后的移动功能;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;//设置这个拼图的行列    private Cell cells[] = new Cell[row*col];//创建一个按钮对象数组    int ImageWidth;    int ImageHeight;    public GamePanel()//构造函数    {        this.setLayout(null);        init();    }    public void init()//初始化完成以下功能--完成图像的切割,完成图像到图标的转换,完成按钮图标的添加,将按钮添加到面板上,并且给每一个按钮添加监听机制    {        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,在数组中的下标                if(num < row*col-1)                {                    bufnew = buf.getSubimage(width*j, height*i, width, height);                    icon = new ImageIcon(bufnew);//将图像转化成图标                }                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);//将每一个按钮添加到当前这个面板上面            if(i < cells.length-1)                cells.addMouseListener(this);//空白格不添加监听机制        }    }    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的数组元素按钮的坐标            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.getX();            int y = cells.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, "恭喜您过关了是否还来一局?", "提示", 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) {    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
GameFrame类------设置游戏的打开窗口类,创建了一个菜单面板存放游戏开始和游戏结束两个按钮,并且对游戏的窗口进行了基本设置,这是整个游戏的入口。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("游戏结束");    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();    }}

29 个回复

倒序浏览
厉害了啊
回复 使用道具 举报
回复 使用道具 举报
回复 使用道具 举报
回复 使用道具 举报
回复 使用道具 举报
gzgd 高级黑马 2018-5-8 13:48:26
7#
回复 使用道具 举报
gzgd 高级黑马 2018-5-8 13:48:29
8#
回复 使用道具 举报
gzgd 高级黑马 2018-5-8 13:48:41
9#
回复 使用道具 举报
回复 使用道具 举报
回复 使用道具 举报
回复 使用道具 举报
回复 使用道具 举报
回复 使用道具 举报
不错不错
回复 使用道具 举报
回复 使用道具 举报
回复 使用道具 举报
回复 使用道具 举报
回复 使用道具 举报
回复 使用道具 举报
12下一页
您需要登录后才可以回帖 登录 | 加入黑马