Java game programs-push box

Source: Internet
Author: User

 

First Java game program-push box 

I grew up writing process-oriented programs and knew nothing about game programs. The original idea was to create a super girl to play on her own, and there was no idea at all, so she asked the CC teacher to guide her life.The final conclusion is that the push box is the simplest. I wrote the push box and tried to write the snake. After the two, I considered super Mary.>_< |

This box pushing program has a simple function and only one level. It is still very retarded. You can use the File Read method to expand the level.

 

The following describes the idea of creating a game program for the first time:

First,It is really annoying to do the interface.If you only want to consider the core code, you can do it in line with 20 or 30. After a graphical interface and a few functional buttons are added, a program that is so mentally retarded is almost three hundred rows ......

I divide the interface into three parts: Top, center, and bottom. There is a reset button on the top, and the middle part is the game Panel. It is useless to keep it at the bottom. The middle part is an independent class named gamepanel. It inherits the jpanel class and implementsKeylistener Interface. The basic functions and keyboard event listening of the game are independently implemented by the gamepanel class.

The game uses two-dimensional arrays to represent maps. One map [] [] Array records the basic terrain, spaces, walls, or target locations. The other array box [] [] records the location of the boxes. The player's coordinates are recorded separately using a point variable player.

A depressing problem encountered during the production process is that gamepanel in the Central region is alwaysKeyboard Events cannot be captured.. After a long time, I found that after I commented out the top button, it could be captured normally. Once the button was displayed, it would not work. Later I learned that the problem was that the central component did not obtain the input focus. AddThis. requestfocusinwindow ();After the central component is forced to obtain the input focus, the problem is solved.

 

The following are images used in the game:

Box_box1.gif box_box2.gif box_floor.gif box_wall.gif box_trg.gif box_player.gif

(Change the image path in the program)

The source code is as follows:

Import javax. Swing .*;

Import java. AWT .*;
Import java. AWT. event .*;
Import java. Io .*;
Import javax. ImageIO .*;

// Class gamepanel
// The main class of the game

Class gamepanel extends jpanel implements keylistener
{
Private Static final long serialversionuid = 1l;
 
Private char [] [] map; // map state
Private char [] [] box;
Private int Sx, Sy; // map size
Private point player, P; // point of player
Private int num = 0; // Number of boxs
Private point [] boxs; // points of boxs;
// Private point [] Trg; // points of target
// Private point [] path;
// Private int steps;
Private Point [] dir;
 
Private int size;
Private Image imgfloor;
Private Image imgwall;
Private Image imgplayer;
Private Image imgbox1;
Private Image imgbox2;
Private Image imgtrg;
 
// Method
Public GamePanel (char B [] [], int w, int h, int size) throws Exception
{

SX = B. length;
SY = B [0]. length;
Num = 0;

Map = new char [sX] [sY];
Box = new char [sX] [sY];
Player = new Point ();
P = new point ();
Boxs = new point [SX * sy];

Dir = new point [4];
Dir [0] = new point (0,-1); // left
Dir [1] = new point (-1, 0); // up
Dir [2] = new point (0, 1); // right
Dir [3] = new point (1, 0); // down

File parent = new file ("E:/my document/my university/Java/workspace/Program/src/images /");

Imgfloor = ImageIO. Read (new file (parent, "box_floor.gif "));
Imgwall = ImageIO. Read (new file (parent, "box_wall.gif "));
Imgplayer = ImageIO. Read (new file (parent, "box_player.gif "));
Imgtrg = ImageIO. Read (new file (parent, "box_trg.gif "));
Imgbox1 = ImageIO. Read (new file (parent, "box_box1.gif "));
Imgbox2 = ImageIO. Read (new file (parent, "box_box2.gif "));

This. size = size;

// Set Map
For (INT I = 0; I <SX; I ++ ){
For (Int J = 0; j <sy; j ++ ){
Switch (B [I] [J]) {
Case 'p': // player
Map [I] [J] = '.';
P. setlocation (I, j );
Break;
Case 'X': // target
Map [I] [J] = 'X ';
Break;
Case 'B': // box
Map [I] [J] = '.';
Boxs [num ++] = new point (I, j );
Break;
Case '#': // Wall
Map [I] [J] = '#';
Break;
Case '.': // floor
Map [I] [J] = '.';
Break;
Default:
Map [I] [J] = '.';
Break;
}
}
} // End set Map

Reset (); // reset Game
}
 
Public void reset (){
Player. setLocation (p );
For (int I = 0; I <sX; I ++)
For (int j = 0; j <sY; j ++)
Box [I] [j] = '';
For (int I = 0; I <Num; I ++)
Box [boxs [I]. x] [boxs [I]. y] = 'B ';
// Steps = 0;
Repaint ();
}
 
Public void undo (){

}
 
Public boolean judge (){
For (int I = 0; I <sX; I ++)
For (int j = 0; j <sY; j ++)
If (box [I] [j] = 'B' & map [I] [j]! = 'X ')
Return false;
Return true;
}
 
Public void paint (Graphics g ){
// Draw map
For (int I = 0; I <sX; I ++ ){
For (int j = 0; j <sY; j ++ ){
Switch (map [I] [j]) {
Case '.':
G. drawImage (imgfloor, j * size, I * size, null );
Break;
Case '#':
G. drawImage (imgwall, j * size, I * size, null );
Break;
Case 'X ':
G. drawimage (imgfloor, J * size, I * size, null );
Break;
}
If (Box [I] [J] = 'B' & map [I] [J] = 'X') g. drawimage (imgbox2, J * size, I * size, null );
Else if (Box [I] [J] = 'B') g. drawimage (imgbox1, J * size, I * size, null );
Else if (Map [I] [J] = 'X') g. drawimage (imgtrg, J * size, I * size, null );
}
}
// Draw player
G. drawimage (imgplayer, player. y * size, player. x * size, null );

G. drawrect (0, 0, Sy * size, SX * size );

// Get focus
This. requestfocusinwindow ();/***********/
}
 
 
/* Methods of keylistener */
 
Public void keypressed (keyevent e ){
Int x = player. X;
Int y = player. Y;
Int K, x0, y0;

K = E. getkeycode ()-37;

If (k <0 | K> = 4) return;

X + = dir [K]. X;
Y + = dir [K]. Y;

If (x <0 | x> = SX | Y <0 | Y> = Sy) return;
If (Map [x] [Y] = '#') return;

If (Box [x] [Y]! = 'B' & (Map [x] [Y] = '.' | map [x] [Y] = 'X ')){
Player. x = X;
Player. Y = y;
}
If (Box [x] [Y] = 'B '){
X0 = x + dir [K]. X;
Y0 = Y + dir [K]. Y;
If (x0 <0 | y0 <0 | x0> = SX | y0> = Sy) return;
If (Map [x0] [y0]! = '#' & Box [x0] [y0]! = 'B '){
Box [x] [Y] = '';
Box [x0] [y0] = 'B ';
Player. x = X;
Player. Y = y;
}
}

Repaint ();

If (Judge ()){
Joptionpane. showmessagedialog (null, "you win! ");
Reset ();
}
}
 
Public void keyreleased (keyevent e ){}
Public void keytyped (keyevent e ){}
 
}

 
// Class pushbox
// Creat jframe to play the game

Public class pushbox extends jframe
{
Private Static final long serialversionuid = 1l;
 
Private jpanel top;
Private gamepanel center;
Private jpanel bottom;
 
Private jbutton breset; // button of Reset
Private jbutton bundo; // button of undo
 
Private int width, height, size;
Private int Sx, Sy;
 
// Method of Constitution
Public PushBox (char [] [] B) throws Exception {
Super ("PushBox Game ");

// Set Window
Size = 35;
SX = B. length;
SY = B [0]. length;
Width = size * sY + 10;
Height = size * sX + 100;

Top = new JPanel ();
Center = new GamePanel (B, width, height-100, size); // Panel of Game
Bottom = new JPanel ();
Breset = new JButton ("Reset ");
Bundo = new JButton ("mongoundo ");

// Add Listener
Center. addKeyListener (center );
Breset. addActionListener (new ResetListener ());
Bundo. addactionlistener (New undolistener ());

Top. Add (breset );
// Top. Add (bundo );
Add (top, "North ");
Add (center, "center ");
Add (bottom, "South ");

Setsize (width, height );
Setvisible (true );
}
 
// Classes of listener
Class resetlistener implements actionlistener {
Public void actionreceivmed (actionevent e ){
Center. Reset ();
}
}
Class undolistener implements actionlistener {
Public void actionreceivmed (actionevent e ){
Center. Undo ();
}
}

// Method main
Public static void main (string [] ARGs) throws exception {
Char [] [] B = new char [5] [6];

B [0] = "# X # P". tochararray ();
B [1] = "#####.". tochararray ();
B [2] = ". B .. # ..". tochararray ();
B [3] = "X... B...". tochararray ();
B [4] = "#...". tochararray ();

Pushbox P = new pushbox (B );
P. setdefaclocloseoperation (jframe. exit_on_close );
}
}

 

 

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.