Experience in creating standalone Russian games (III)

Source: Internet
Author: User

So far, the piece object has been created, so there is a complete set of "Materials". It is necessary to enter the stage of the game-mainframe. To make things perform on the stage quickly, there must be actors on the stage. therefore, in mainframe, the reference currentpiece indicating the currently moving piece object and the reference nextpiece of the next Square are provided.

Private piece currentpiece;
Private piece nextpiece;

Note that the current movement of the big square is running in gamepanel. (It is suggested that the game interface consists of two parts: gamepanel, gamepanel, and tool interface. Gamepanel (Private gamepanel;) is provided in mainframe ;)

The gamepanel class in the inherited jpanel provides the paint method to draw the current large block to the interface. But how can we get the current sports big square in the mainframe of the game interface? The method is to provide a mainframe reference mainframe in gamepanel, (mainframe ;),

A constructor is provided to initialize mainframe.

Public gamepanel (mainframe ){
This. Mainframe = mainframe;
}

Provides a method for the mainframe class to obtain the current dashboard.

Public piece getcurrentpiece (){
Return this. currentpiece;
}

Public void paint (Graphics g ){
// Draw the background
G. drawimage (this. Background, 0, 0,
This. getwidth (), this. getheight (), null );
 
// Draw the square of the current motion
/**
* Since squares are moved in gamepanel on the game interface, the piece method is defined here.
*/
Piece currentpiece = This. Mainframe. getcurrentpiece ();
Imageutil. paintpiece (G, currentpiece );
 
}

Here we will regard gamepanel as a two-dimensional array. Each square is an element of a two-dimensional array, and then draw these small squares into gamepanel in the painting. When the huge square falls, the gamepanel redraws based on the two-dimensional array. Note that the square element in a two-dimensional array is a square object that only contains locations but does not contain images. Therefore, you need to provide a constructor in the square class.

/*
* Initialize the position and image of the square. Elements used to store square 2D arrays with images as gamepanel
*/
Public Square (INT beginx, int beginy ){
This. Image = image;
This. beginx = beginx;
This. beginy = beginy;
}

When a piece object drops, we need to record the square with an image in the piece to the mainframe. Add the square to the two-dimensional array if the square falls. The following is a two-dimensional array of the initialization interface.

/**
* The game interface is regarded as a plane composed of two-dimensional arrays.
* When a piece object is dropped, you need to add all the square objects in the piece to the mainframe.
* That is, in gamepanel. Provides a two-dimensional array in the mainframe class to store a square object.
* Each dropped piece stores all its square objects in a two-dimensional array.
* Note that only the two-dimensional array is initialized, and the two-dimensional array is drawn in gamepanel.
*/
Private void initsquares (){

Int xsize = This. gamepanel. getwidth ()/piece. square_border;
Int ysize = This. gamepanel. getheight ()/piece. square_border;

This. squares = new square [xsize] [ysize];
For (INT I = 0; I <this. squares. length; I ++ ){
For (Int J = 0; j <this. squares [I]. length; j ++ ){
This. squares [I] [J] = New Square (piece. square_border * I,
Piece. square_border * j );
}
}
}

This method is called in manframe so that the two-dimensional array can be initialized when the program is running, and then drawn in gamepanel. Here, the mainframe class provides

Private square [] [] squares; and

// Obtain the two-dimensional array of gamepanel.
Public Square [] [] getsquares (){
// Todo auto-generated method stub
Return this. Square

}

In this way, you can draw the gamepanel interface in the gamepanel class (in the painting). The Code is as follows:

Public void paint (Graphics g ){
// Draw the background
G. drawimage (this. Background, 0, 0,
This. getwidth (), this. getheight (), null );
 
// Draw the square of the current motion
/**
* Since squares are moved in gamepanel on the game interface, the piece method is defined here.
*/
Piece currentpiece = This. Mainframe. getcurrentpiece ();
Imageutil. paintpiece (G, currentpiece );
 
//
Square [] [] squares = This. Mainframe. getsquares ();
 
If (squares = NULL) return;
// Draw the code used to store two-dimensional arrays,
For (INT I = 0; I <squares. length; I ++ ){
For (Int J = 0; j <squares [I]. length; j ++ ){
Square S = squares [I] [J];
If (s! = NULL ){
G. drawimage (S. getimage (), S. getbeginx (), S. getbeginy (), this );
/*
* G. drawimage (null, S. getbeginx (), S. getbeginy (), this );
* The two-dimensional array code is used to store square with images. It can be found that S. getimage () is changed to null.
* Alternatively, because the square that draws a two-dimensional array does not have an image.
*/

}
}
 
}
At this point, the complete class of gamepanel is designed as follows:

Public class gamepanel extends jpanel {
Mainframe mainframe;

/**
* Obtain the background image card.
*/
Private image background =
Imageutil. getimage ("images/background.jpg ");

Public gamepanel (mainframe ){
This. Mainframe = mainframe;
}

Public void paint (Graphics g ){
// Draw the background
G. drawimage (this. Background, 0, 0,
This. getwidth (), this. getheight (), null );
 
// Draw the square of the current motion
/**
* Since squares are moved in gamepanel on the game interface, the piece method is defined here.
*/
Piece currentpiece = This. Mainframe. getcurrentpiece ();
Imageutil. paintpiece (G, currentpiece );
 
//
Square [] [] squares = This. Mainframe. getsquares ();
 
If (squares = NULL) return;
// Draw the code used to store two-dimensional arrays,
For (INT I = 0; I <squares. length; I ++ ){
For (Int J = 0; j <squares [I]. length; j ++ ){
Square S = squares [I] [J];
If (s! = NULL ){
G. drawimage (S. getimage (), S. getbeginx (), S. getbeginy (), this );
/*
* G. drawimage (null, S. getbeginx (), S. getbeginy (), this );
* The two-dimensional array code is used to store square with images. It can be found that S. getimage () is changed to null.
* Alternatively, because the square that draws a two-dimensional array does not have an image.
*/

}
}
 
}
}
}

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.