Improvement of game code of push box II

Source: Internet
Author: User

/*
* Problems:
* 1. The previous step is not returned;
*/
Import javax. Swing .*;
Import java. AWT .*;
Import java. AWT. event .*;
Import java. util .*;
Import java. AWT. Geom .*;

Public class pushbox {
Public static void main (string ARGs []) {
Gameframe frame = new gameframe ();
Frame. setdefaclocloseoperation (jframe. exit_on_close );
Frame. Show ();
Joptionpane. showmessagedialog (null,
"The Red Square indicates that the player/n pushes the purple box to the Blue Box to pass through/N direction keys to control/n cannot return to the previous step ");
}
}

Class gameframe extends jframe {
Static int num;

Public gameframe (){
Setsize (400,400 );
Container c = getcontentpane ();
Gamepanel Panel1 = new gamepanel ();
C. Add (Panel1 );
}
}

Class gamepanel_2 extends jpanel {
Public gamepanel_2 (){
This. setbackground (color. Black );
}
}

Class gamepanel extends jpanel implements keylistener {
Int width = 400;

Int Height = 400;

Box B;

Node man;

Node box;

Node [] target;

Int d = 1; // The moving step.

Int Sidex = 25;

Int sidey = 25;

Gameframe;

Arraylist walllist;

// Initialize the Interface Element
Public gamepanel (){
This. setbackground (color. White );
B = new box (this, width/Sidex, width/sidey );
This. Man = B. Man;
This. Box = B. box;
This.tar get = B .tar get;
This. walllist = B. walllist;

This. setfocusable (true );
This. addkeylistener (this );
}

Public void paintcomponent (Graphics g ){
Super. paintcomponent (g );
Graphics2d g2 = (graphics2d) g;

// Draw the game background, players and boxes.
G2.setcolor (color. White );
G2.fillrect (0, 0, width, height );
G2.setcolor (color. Red );
Draw (G2, man );

G2.setcolor (color. Magenta );
Draw (G2, box );
Draw (G2, B. box1 );
Draw (G2, B. box2 );
Draw (G2, B. box3 );

// Draw walls
G2.setcolor (color. darkgray );
For (INT I = 0; I <walllist. Size (); I ++ ){
Node n = (node) walllist. Get (I );
Draw (G2, N );
}
// Draw the destination.
G2.setcolor (color. Blue );
For (INT I = 0; I <target. length; I ++ ){
G2.drawrect (target [I]. x * Sidex, target [I]. y * sidey, Sidex-1,
Sidey-1 );
Ellipse2d E = new ellipse2d. Double (target [I]. x * Sidex, target [I]. Y
* Sidey, Sidex-1, sidey-1 );
G2.draw (E );
}
}

Public void keypressed (keyevent e ){
Int keycode = E. getkeycode ();
If (keycode = keyevent. vk_up ){
Box. Direction = 1; // you must determine the direction before moving
B. Move (0,-D );

} Else if (keycode = keyevent. vk_right ){
Box. Direction = 2;
B. Move (D, 0 );

} Else if (keycode = keyevent. vk_down ){
Box. Direction = 3;
B. Move (0, d );

} Else if (keycode = keyevent. vk_left ){
Box. Direction = 4;
B. Move (-D, 0 );
}
}

Public void keyreleased (keyevent e ){
}

Public void keytyped (keyevent e ){
}

Public void draw (graphics2d g, node N ){
G. fillrect (N. x * Sidex, n. y * sidey, Sidex-1, sidey-1 );
}
}

Class node {
Int X;

Int y;

Public node (int x, int y ){
This. x = X;
This. Y = y;
}
}

Class box {

Boolean end = false;

Int Maxx;

Int Maxy;

Boolean [] [] A; // the entire game area is a Boolean two-dimensional array.

Node man;

Node [] target; // storage destination

Node box;

Node box1;

Node box2;

Node box3;

Node [] wall; // storage Wall

Node [] B; // array B storage box.

Gamepanel panel;

// When a box is pushed to the target, the list stores the destination node.
Arraylist list = new arraylist ();

// Storage wall.
Arraylist walllist = new arraylist ();

Public static int left = 4;

Public static int up = 1;

Public static int right = 2;

Public static int down = 3;

Public static int direction = 0;

// In The Box constructor, fill the game screen with a Boolean two-dimensional array and define players, boxes, and destinations.
Public box (gamepanel P, int Maxx, int Maxy ){
Panel = P;
This. Maxx = Maxx;
This. Maxy = Maxy;
A = new Boolean [Maxx] [Maxy];
For (INT I = 0; I <Maxx; I ++ ){
For (Int J = 0; j <Maxy; j ++ ){
A [I] [J] = false;
}
}
// Define the initial position of the box, player, and destination
Int x = 5;
Int y = 8;
Man = new node (x, y );
A [x] [Y] = true;
// Define the location of the box in the game and set it to true;
B = new node [4];
Box = new node (10, 9 );
A [10] [9] = true;
B [0] = box;
Box1 = new node (6, 7 );
A [6] [7] = true;
B [1] = box1;
Box2 = new node (11, 8 );
A [11] [8] = true;
B [2] = box2;
Box3 = new node (8, 8 );
A [8] [8] = true;
B [3] = box3;

// Define the wall and destination.
Createwall ();
Target = new node [4];
Target [0] = new node (7, 9 );
Target [3] = new node (6, 9 );
Target [1] = new node (7, 10 );
Target [2] = new node (6, 10 );
}

Public void move (INT D1, int D2 ){
A [Man. x] [Man. Y] = false;
Man. x + = D1;
Man. Y + = d2;
// If a player encounters a wall, the player cannot move forward. Otherwise, push the box.
If (A [Man. x] [Man. Y]) {
If (isoutofwall ()){
Movedisable (man, D1, D2 );
} Else {
For (INT I = 0; I <B. length; I ++ ){
If (man. x = B [I]. X & man. Y = B [I]. Y ){
B [I]. x + = D1;
B [I]. Y + = d2;
// Use an if statement to determine whether the boxes overlap. If the boxes overlap, they cannot move forward;
If (A [B [I]. x] [B [I]. Y]) {
Movedisable (man, D1, D2 );
Movedisable (B [I], D1, D2 );
} Else {
// You must set the box position to true. Otherwise, the box will not be moved once.
A [B [I]. x] [B [I]. Y] = true;
// If all the boxes are pushed in place, the game ends.
// In this program, the isarrive method should be used after the box is set to true.
Isarrive (B [I]);
}
}
}
}
}
Panel. repaint ();
If (end ){
Joptionpane. showmessagedialog (null, "over ");
System. Exit (0 );
}
}

// Generate a game map
Public void createwall (){
For (INT I = 5; I <12; I ++ ){
Node wballs = new node (I, 5 );
Walllist. Add (Wils );
}
For (INT I = 11; I <14; I ++ ){
Node wballs = new node (I, 6 );
Walllist. Add (Wils );
}
Walllist. Add (new node (13, 7 ));
Walllist. Add (new node (13, 8 ));
Walllist. Add (new node (13, 9 ));
Walllist. Add (new node (12, 9 ));
Walllist. Add (new node (12, 10 ));
For (INT I = 12; I> 4; I --){
Node wballs = new node (I, 11 );
Walllist. Add (Wils );

}
Walllist. Add (new node (5, 10 ));
Walllist. Add (new node (4, 10 ));
Walllist. Add (new node (4, 9 ));
Walllist. Add (new node (4, 8 ));
Walllist. Add (new node (4, 8 ));
Walllist. Add (new node (4, 7 ));
Walllist. Add (new node (5, 7 ));
Walllist. Add (new node (5, 6 ));

Walllist. Add (new node (7, 7 ));
Walllist. Add (new node (8, 7 ));
Walllist. Add (new node (9, 7 ));

Walllist. Add (new node (8, 9 ));
Walllist. Add (new node (8, 10 ));
// Set the object of the wall to true
For (INT I = 0; I <walllist. Size (); I ++ ){
Node n = (node) walllist. Get (I );
A [n. x] [N. Y] = true;
}
}

// Players cannot move forward when they hit the wall
Public void movedisable (node N, int D1, int D2 ){
Switch (Direction ){
Case 4:
N. X-= D1;
Break;
Case 1:
N. Y-= d2;
Break;
Case 2:
N. X-= D1;
Break;
Case 3:
N. Y-= d2;
Break;
}
}

// Determine whether a player has crossed the boundary
Public Boolean isoutofwall (){
For (INT I = 0; I <walllist. Size (); I ++ ){
Node n = (node) walllist. Get (I );
If (man. x = n. X & man. Y = n. y ){
Return true;
}
}
Return false;
}

// If all the boxes are in place, the game ends.
Public void isarrive (node N ){
For (INT I = 0; I <target. length; I ++ ){
If (target [I]. x = n. X & target [I]. Y = n. y )){
List. Add (target [I]);
} Else if (target [I]. x = man. X & target [I]. Y = man. Y ){
List. Remove (target [I]);
}
}
If (list. Size () = target. Length ){
End = true;
}
}
}

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.