Java-based Puzzle games

Source: Internet
Author: User


Project requirements:

Users can select the pictures to be carried out through the catalogue, and the photos are divided into small pieces of 3*3 after processing.

Place the small block in the 3*3 box, where the bottom right corner is left white;

Press the top and bottom direction key, move the box in the small pieces of photos, until stitching out the original image, the game is over;

Information already available:

Http://blog.sina.com.cn/s/blog_5e3ab00c0100igqh.html

Http://blog.sina.com.cn/s/blog_5e3ab00c0100ipz6.html

Refer to the above source code to complete your own design.

Task table:

Completes the dialog box to locate the directory function (low priority);

To standardize the positioning of the image, split processing (high priority);

Post the segmented picture in the frame (high priority);

Add the listening to the keyboard, redraw the image, form the effect of moving, each move to determine whether to complete the puzzle (high priority);

How can I tell if the current initial puzzle is spelled? (High priority)

Task 2:

Need to split the picture, refer to the source code directory as follows:

According to the code name, we can go to the split to see if we need the content, found that the divID method in the class, just complete the image segmentation work, this part of the code can be used.

Public bufferedimage[][] divID (int type)

{

Try

{

if (filename = = null)

return null;

BufferedImage image = Imageio.read (new File (path));

int len = Level[type];

int cal = Image.getwidth ()/Len;

int row = Image.getheight ()/Len;

BufferedImage [] subimage = new bufferedimage[row][cal];

for (int i = 0; i < row; i++)

for (int j = 0; J < Cal; J + +)

SUBIMAGE[I][J] = Image.getsubimage (J*len, I*len, Len, Len);

return subimage;

}

catch (Exception e)

{

return null;

}

}

}

Task 3: Post the segmented picture in the frame (high priority);

Here is to deal with the split image, for the moment we follow the reference source code to see how he did:

public void Menunewclick ()

{

Split sp = Split.get ();

BufferedImage [] image;

if (!sp.set (GetFileName ()) | | (image = Sp.divid (GetType ())) = = null)

{

Joptionpane.showmessagedialog (NULL, "File" +getfilename () + "not exists!\nplease select again~");

Return

}

Startgame ();

This.setsize (Fwidth, fheight);

This.setvisible (TRUE);

Intlen = Split.level[gettype ()];

Introw = Image.length;

intcal = Image[0].length;

Gover = new Gameover (this);

JButton [] button = new jbutton[row][cal];

Matrix matrix = new Matrix (button, panel[0], Len, Gover);

Matrix.init (image);

As can be seen from the above code, the call to the image segmentation method is in image = Sp.divid (GetType ()), while further processing of the image falls in the Init method of the matrix part.

public void Init (BufferedImage [] image)

{

...

icon = new ImageIcon (image[d/cal][d%cal]);

Button[i/cal][i%cal].seticon (icon);

}

}

From the above code is visible, the main idea: the image of the split into an icon object, and then set the Icon object to the button, and in the Matrix initialization function:

Public Matrix (JButton [] b, JPanel p, Intlen, Gameover g) {

...

for (int i = 0; i < row; i++)

for (int j = 0; J < Cal; J + +)

{

BUTTON[I][J] = new JButton ();

Button[i][j].setbounds (J*len, I*len, Len, Len);

Button[i][j].addactionlistener (New ButtonClick (button, pint, Matrix, i,j, Gover));

Panel.add (Button[i][j]);

}

}

}

The above code points to the general direction of our design: The entire panel is divided into 3*3 areas, in which there are no gaps in order to place 9 buttons, and then the image is divided into small pieces of 3*3, and then each small block into the icon type affixed to the surface of each button, so complete the game static settings.

Task 4: Add the listening to the keyboard, redraw the image, form the effect of moving, each move to determine whether to complete the puzzle (high priority);

This task can be decomposed to a total of sub tasks:

How to produce the effect of moving.

How to determine whether the puzzle is complete.

Follow the overall design in task 3 to achieve the effect of the move: either move the button's position in the Panel or reset the icon on the button, which is just a rough technical route and needs to be specific:

The specific implementation of each route difficulty;

The difficulty of collaboration between the technical route and the successor task;

 

inspiration: In addition to considering the difficulty of the technical route, it is necessary to consider the difficulty of coordination with other technologies .

Don't want to do it first. Then run the train in your head and look at the technical difficulties of the above two technical routes:

If the way to reset icon, that is, each time along the key direction, the former icon to be blank, and to determine whether the puzzle is already in the completion state, only need to check the icon on each button is arranged in order;

If you use the way to move a button, but only with the icon to move together, because we want only the icon movement, so the comparison between the two ways, more reasonable is the first way.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.