Java Implementation push Box games

Source: Internet
Author: User

Package test1;//for calling Test2 packages import test2.*;import java.awt.*;import javax.swing.*;p ublic class APP extends Jframe{public static void Main (string[] args) {//TODO auto-generated method Stubapp A = new APP ();} Public APP () {new members ();}}

Package Test2;import Java.awt.event;import Java.awt.font;import java.awt.event.keyevent;import Java.awt.event.keylistener;import Javax.swing.icon;import Javax.swing.imageicon;import Javax.swing.JFrame;import Javax.swing.jlabel;import Javax.swing.joptionpane;public Class members extends JFrame implements keylistener{// Defines a JLabel array to hold the position of the sheep JLabel [][]sheep = new jlabel[12][16];//0 represents the open space, 1 means the tree int[][] Datas = {{ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},{1,1,0,0,0,0,0,0,0,1,1,1,0,0,0,1},{ 1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1},{1,0,0,0,0,0,0,0,0,1,1,1,0,0,0,1},{1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1},{ 1,0,0,0,0,0,0,0,0,1,1,1,0,0,0,1},{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},{ 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}};// Position of the Wolf int wx,wy;/* * Case of num Value change * 1. When the sheep enter the cage, Num+1 * 2. When the sheep leave the cage, NUM-1 * 3. Num does not change when the sheep is left from a cage into another cage */// The total number of sheep entering the box at the beginning int num = 0;//total number of cages int = 3;//Constructor Public members () {/* * If the image is enlarged first and then put down will be small to cover, can not see * picture has size. Put small pictures on top of the big picture * So add picture components when there is order, to pay attention to the small on the large top *///small picture//barrier design treeinit ();//Do Cage targetinit ();//push box character initialization wolfinit ();/ Sheep initialization sheepinit ();//Background picture, big//Add background image to form backgroundinit ();//Set entire form setform ();//Register Monitor This.addkeylistener (this);} Set the entire form private void Setform () {//TODO auto-generated method Stubthis.settitle ("Push Box Game"); This.setsize (825,645);// Prevents the user from changing the form size this.setresizable (false); this.setdefaultcloseoperation (jframe.exit_on_close);// The Settings window is centered to show this.setlocationrelativeto (null); This.setvisible (true);} Background image initialization private void Backgroundinit () {//TODO auto-generated method Stubicon i = new ImageIcon ("Floor.png");//Use JLabel system Background JLabel LAB_BG = new JLabel (i);//Set the position and size of the component to be added lab_bg.setbounds (0, 0, 800, 600);//Add this thing to the form This.add (LAB_BG);} Where the sheep are located initialize private void Sheepinit () {//TODO auto-generated method stub//Three sheep icon i = new ImageIcon ("7.png"); JLabel jb1 = new JLabel (i); Jb1.setbounds (6 *, 4 *, +); This.add (JB1);//The value of the position of the sheep is set to 4datas[4][6] = 4;sheep[4][6] = JB1; JLabel JB2 = New JLabel (i); Jb2.setbounds (6 *, 6 *), This.add (JB2);d atas[6][6] = 4;sheep[6][6] = JB2; JLabel jb3 = new JLabel (i) jb3.setbounds (6 *, ten *, +), This.add (JB3);d atas[10][6] = 4;sheep[10][6] = jb3;} JLabel jb = null;private void Wolfinit () {//TODO auto-generated method stub//where is the character originally located? WX = 4; wy = 5;//Use a picture to simulate the character//1. Create a picture, character picture icon i = new ImageIcon ("3.png");//2. Use JLabel components to simulate people JB = new JLabel (i);//3. Set the character in Where is the display location on the screen//where the character's display position is more reasonable? ----------------Jb.setbounds (wx*50, WY*50, 50, 50);//4. Put this character in the form This.add (JB);} The position of the cage is initialized with private void Targetinit () {//TODO auto-generated method Stubicon i = new ImageIcon ("Target.png"); JLabel jb1 = new JLabel (i); jb1.setbounds (* 50,50,50) This.add (JB1);d atas[10][14] = 8; JLabel jb2 = new JLabel (i); jb2.setbounds (* *, ten *, 8); This.add (JB2);d atas[10][13] =; JLabel jb3 = new JLabel (i), jb3.setbounds (9 *, 8), This.add (JB3);d atas[9][14] (= +); The initialization of the Tree private void Treeinit () {//TODO auto-generated methodStubicon k = new ImageIcon ("Tree.png"); JLabel t = null;for (int i = 0;i < Datas.length;i + +) {for (int j = 0;j < Datas[i].length;j + +) {if (datas[i][j] = = 1) {T = new JLabel (k); T.setbounds (j*50, I*50, N.); This.add (t);}}} Determine if victory private void Victory () {if (num = = total) {//Design a bullet box that prompts the game to complete icon i = new ImageIcon ("6.png"); Joptionpane.showmessagedialog (NULL, "Game Over", "push Box", 2,i);/* If you want to set the level, add the information here * Note to change the value of NUM * Based on the number of levels, the datas array is designed as a three-dimensional amount * /}} @Overridepublic void keytyped (KeyEvent e) {//TODO auto-generated method stub} @Overridepublic void Keypressed ( KeyEvent e) {//TODO auto-generated method stub/* * Datas value case * 0 Open space * 1 trees * 4 Sheep * 8 Empty cage * 12 Shepherd's Cage * Combine these values to see the following code *//* * W Up * D Right * S down * A to the left * Note a blind area, this problem is considered for a long time, in the Java coordinate system, the axis is the horizontal direction for the x axis, the vertical direction is the y axis * and in the array first horizontal direction, after the vertical direction, so in the datas array to fill the value of first y after x */if Etkeycode () = = Keyevent.vk_enter) {/* * Each keystroke is discussed in the following cases * 1. The Wave trees * 2. Wolf Sheep Tree * 3. Wolf Sheep * 4. Wolf Sheep Cage * 5. Wolf Shepherd Cage Tree * 6. Wolf Shepherd's cage Sheep 7. Wolf Shepherd's Cage Shepherd's cage * None of the above is handled, because it cannot be moved * 8. Wolf Space * 9. Wolf Empty Cage * 10. Wolf Sheep Open Space * 11. Wolf Sheep Empty cage * 12. Wolf ShepherdCage Open Space * 13. The Wolf Shepherd's cage empty cage * These situations need to be changed accordingly, see Code */if (datas[wy-1][wx] = = 1) {return;} if (datas[wy-1][wx] = = 4 && datas[wy-2][wx] = = 1) {return;} if (datas[wy-1][wx] = = 4 && datas[wy-2][wx] = = 4) {return;} if (datas[wy-1][wx] = = 4 && datas[wy-1][wx] = =) {return;} if (datas[wy-1][wx] = = && DATAS[WY-2][WX] = = 1) {return;} if (datas[wy-1][wx] = = && DATAS[WY-2][WX] = = 4) {return;} if (datas[wy-1][wx] = = && DATAS[WY-2][WX] = =) {return;} if (datas[wy-1][wx] = = 0) {WY-= 1;//coordinates do not get the int type. Note Force type conversions int x = (int) jb.getlocation (). GetX (); int y = (int) jb.getlocation (). GetY (); jb.setlocation (x, y-50); Icon i = new I Mageicon ("1.png"); Jb.seticon (i); return;} if (datas[wy-1][wx] = = 8) {WY-= 1;//coordinates do not get the int type. Note Force type conversions int x = (int) jb.getlocation (). GetX (); int y = (int) jb.getlocation (). GetY (); jb.setlocation (x, y-50); Icon i = new I Mageicon ("1.png"); Jb.seticon (i); return;} if (datas[wy-1][wx] = = 4 && datas[wy-2][wx] = = 0) {DATAS[WY-1][WX] = 0;datas[wy-2][wx] = 4;} if (datas[wy-1][WX] = = 4 && datas[wy-2][wx] = = 8) {DATAS[WY-1][WX] = 0;datas[wy-2][wx] = 12;num + +;} if (datas[wy-1][wx] = = && Datas[wy-2][wx] = = 0) {DATAS[WY-1][WX] = 8;datas[wy-2][wx] = 4;num--;} if (datas[wy-1][wx] = = && DATAS[WY-2][WX] = = 8) {DATAS[WY-1][WX] = 8;datas[wy-2][wx] = 12;} Sheep[wy-1][wx].setlocation (wx*50, wy*50-100); SHEEP[WY-2][WX] = sheep[wy-1][wx];sheep[wy-1][wx] = null;wy-= 1;// Coordinates do not get the int type. Note Force type conversions int x = (int) jb.getlocation (). GetX (); int y = (int) jb.getlocation (). GetY (); jb.setlocation (x, y-50); Icon i = new I Mageicon ("1.png"); Jb.seticon (i); Victory (); return;} else if (e.getkeycode () = = Keyevent.vk_d) {if (datas[wy][wx+1] = = 1) {return;} if (datas[wy][wx+1] = = 4 && datas[wy][wx+2] = = 1) {return;} if (datas[wy][wx+1] = = 4 && datas[wy][wx+2] = = 4) {return;} if (datas[wy][wx+1] = = 4 && datas[wy][wx+2] = =) {return;} if (datas[wy][wx+1] = = && datas[wy][wx+2] = = 1) {return;} if (datas[wy][wx+1] = = && datas[wy][wx+2] = = 4) {return;}if (datas[wy][wx+1] = = && datas[wy][wx+2] = =) {return;} if (datas[wy][wx+1] = = 0) {WX + = 1;//coordinates do not get the int type. Note Force type conversions int x = (int) jb.getlocation (). GetX (); int y = (int) jb.getlocation (). GetY (); jb.setlocation (x + y); Icon i = new I Mageicon ("2.png"); Jb.seticon (i); return;} if (datas[wy][wx+1] = = 8) {WX + = 1;//coordinates do not get the int type. Note Force type conversions int x = (int) jb.getlocation (). GetX (); int y = (int) jb.getlocation (). GetY (); jb.setlocation (x + y); Icon i = new I Mageicon ("2.png"); Jb.seticon (i); return;} if (datas[wy][wx+1] = = 4 && datas[wy][wx+2] = = 0) {datas[wy][wx+1] = 0;datas[wy][wx+2] = 4;} if (datas[wy][wx+1] = = 4 && datas[wy][wx+2] = = 8) {datas[wy][wx+1] = 0;datas[wy][wx+2] = 12;num + +;} if (datas[wy][wx+1] = = && datas[wy][wx+2] = = 0) {datas[wy][wx+1] = 8;datas[wy][wx+2] = 4;num--;} if (datas[wy][wx+1] = = && datas[wy][wx+2] = = 8) {datas[wy][wx+1] = 8;datas[wy][wx+2] = 12;} Sheep[wy][wx+1].setlocation (wx*50+100, wy*50); sheep[wy][wx+2] = sheep[wy][wx+1];sheep[wy][wx+1] = null;WX + = 1;//coordinates do not get the int type. Note Force type conversions int x = (int) jb.getlocation (). GetX (); int y = (int) jb.getlocation (). GetY (); jb.setlocation (x + y); Icon i = new I Mageicon ("2.png"); Jb.seticon (i); Victory (); return;} else if (e.getkeycode () = = keyevent.vk_s) {if (datas[wy+1][wx] = = 1) {return;} if (datas[wy+1][wx] = = 4 && datas[wy+2][wx] = = 1) {return;} if (datas[wy+1][wx] = = 4 && datas[wy+2][wx] = = 4) {return;} if (datas[wy+1][wx] = = 4 && datas[wy+2][wx] = =) {return;} if (datas[wy+1][wx] = = && DATAS[WY+2][WX] = = 1) {return;} if (datas[wy+1][wx] = = && DATAS[WY+2][WX] = = 4) {return;} if (datas[wy+1][wx] = = && DATAS[WY+2][WX] = =) {return;} if (datas[wy+1][wx] = = 0) {WY + = 1;//coordinates are not of type int. Note Force type conversions int x = (int) jb.getlocation (). GetX (); int y = (int) jb.getlocation (). GetY (); jb.setlocation (x, y +); Icon i = new I Mageicon ("3.png"); Jb.seticon (i); return;} if (datas[wy+1][wx] = = 8) {WY + = 1;//coordinates are not of type int. Note Force type conversions int x = (int) jb.getlocation (). GetX (); int y = (int) jb.getlocation (). GetY (); jb.setlocation (x, y +), Icon i = new ImageIcon ("3.png"); Jb.seticon (i); return;} if (datas[wy+1][wx] = = 4 && datas[wy+2][wx] = = 0) {DATAS[WY+1][WX] = 0;datas[wy+2][wx] = 4;} if (datas[wy+1][wx] = = 4 && datas[wy+2][wx] = = 8) {DATAS[WY+1][WX] = 0;datas[wy+2][wx] = 12;num + +;} if (datas[wy+1][wx] = = && Datas[wy+2][wx] = = 0) {DATAS[WY+1][WX] = 8;datas[wy+2][wx] = 4;num--;} if (datas[wy+1][wx] = = && DATAS[WY+2][WX] = = 8) {DATAS[WY+1][WX] = 8;datas[wy+2][wx] = 12;} Sheep[wy+1][wx].setlocation (wx*50, wy*50+100); SHEEP[WY+2][WX] = sheep[wy+1][wx];sheep[wy+1][wx] = Null;wy + = 1;// Coordinates do not get the int type. Note Force type conversions int x = (int) jb.getlocation (). GetX (); int y = (int) jb.getlocation (). GetY (); jb.setlocation (x, y +); Icon i = new I Mageicon ("3.png"); Jb.seticon (i); Victory (); return;} else if (e.getkeycode () = = Keyevent.vk_a) {if (datas[wy][wx-1] = = 1) {return;} if (datas[wy][wx-1] = = 4 && datas[wy][wx-2] = = 1) {return;} if (datas[wy][wx-1] = = 4 && datas[wy][wx-2] = = 4) {return;} if (dATAS[WY][WX-1] = = 4 && datas[wy][wx-2] = =) {return;} if (datas[wy][wx-1] = = && Datas[wy][wx-2] = = 1) {return;} if (datas[wy][wx-1] = = && datas[wy][wx-2] = = 4) {return;} if (datas[wy][wx-1] = = && Datas[wy][wx-2] = =) {return;} if (datas[wy][wx-1] = = 0) {WX-= 1;//coordinates do not get the int type. Note Force type conversions int x = (int) jb.getlocation (). GetX (); int y = (int) jb.getlocation (). GetY (); Jb.setlocation (x-50, y); Icon i = new I Mageicon ("4.png"); Jb.seticon (i); return;} if (datas[wy][wx-1] = = 8) {WX-= 1;//coordinates do not get the int type. Note Force type conversions int x = (int) jb.getlocation (). GetX (); int y = (int) jb.getlocation (). GetY (); Jb.setlocation (x-50, y); Icon i = new I Mageicon ("4.png"); Jb.seticon (i); return;} if (datas[wy][wx-1] = = 4 && datas[wy][wx-2] = = 0) {Datas[wy][wx-1] = 0;datas[wy][wx-2] = 4;} if (datas[wy][wx-1] = = 4 && datas[wy][wx-2] = = 8) {Datas[wy][wx-1] = 0;datas[wy][wx-2] = 12;num + +;} if (datas[wy][wx-1] = = && Datas[wy][wx-2] = = 0) {Datas[wy][wx-1] = 8;datas[wy][wx-2] = 4;num--;} if (datas[Wy][wx-1] = = && datas[wy][wx-2] = = 8) {Datas[wy][wx-1] = 8;datas[wy][wx-2] = 12;} Sheep[wy][wx-1].setlocation (wx*50-100, wy*50); Sheep[wy][wx-2] = sheep[wy][wx-1];sheep[wy][wx-1] = null;wx-= 1;// Coordinates do not get the int type. Note Force type conversions int x = (int) jb.getlocation (). GetX (); int y = (int) jb.getlocation (). GetY (); Jb.setlocation (x-50, y); Icon i = new I Mageicon ("4.png"); Jb.seticon (i); Victory (); return;}} @Overridepublic void keyreleased (KeyEvent e) {//TODO auto-generated method stub}}



Pictures attached, if you want to find me to link

Java Implementation push Box games

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.