Task Description
Task 12 mainly uses two-dimensional array to realize the map design of tank game, and draws the map by drawing method, the effect of map is shown in Figure 12-1.
Figure 12-1 Tank Game Map Support knowledge 12.1 two-dimensional array
There is only one subscript dimension relative to a one-dimensional array, and the two-dimensional array has two subscript dimensions, and the table represents the rows and columns of the array, and figure 12-2 describes the dimensions of one-dimensional and two-dimensional arrays.
Figure 12-2 Array Dimension 12.2 dimension View
From the perspective of the dimension, it can be seen that the one-dimensional array gets the array elements only through the column subscript: Array[col], whereas the two-dimensional array has a row dimension, so getting the array element requires a line subscript and a column subscript: Array[row][col].
The creation of two-dimensional arrays is similar to the creation of one-dimensional arrays:
Create a two-dimensional array with a size of 3 rows, 6 columns
int[][] array = new INT[3][6];
The first number represents the number of rows, and the second number represents the number of columns. The above statement represents the creation of a two-dimensional array of 3 rows and 6 columns, and the default value of the array element is 0, as shown in Figure 12-3.
Fig. 12-3 two dimensional array structure
With the subscript of rows and columns, we can access or assign values to the specified two-dimensional array element:
Assigning and accessing the specified element
System.out.println (array[1][2]);//assignment before
array[1][2] = ten;
System.out.println (array[1][2]);//After assignment
After the assignment, the element value of column 1th row 2nd of the array is 10, as shown in Figure 12-4.
Figure 12-4 An array of assigned values
In a one-dimensional array, iterating through the elements in an array is a repetitive process, changing only the column subscript of an array, so the effect of traversing an array is achieved through a for loop and a loop variable i.
A two-dimensional array has one more row dimension, so two for loops are required, the first for loop is responsible for the number of rows, and the second for loop is responsible for the number of columns.
int[][] array = new INT[3][6];
Number of loops for
(int row = 0; row < 3; row++) {
//circular columns for
(int col = 0; col < 6; col++) {
System.out.pri NT (Array[row][col] + "");
}
Print line wrap
System.out.println ();
12.3 Object View
From an object's point of view, the essence of a two-dimensional array is a one-dimensional array of elements that are stored in a one-dimensional array:
Defines a one-dimensional array, storing the element as an integer
int[] array01 = new int[2];
Understanding two-dimensional arrays in the view of object morality/
/First create a one-dimensional array of size 3, where the element is a one-dimensional array of size 2
int[][] array02 = new int[3][2];
A one-dimensional array is compared to a two-dimensional array and the element storage is shown in Figure 12-5.
Fig. 12-5 comparison between one-dimensional array and two-dimensional array
To traverse a two-dimensional array with an object's viewpoint:
int[] array01 = new int[2];
System.out.println ("Print one-dimensional array:" + array01);
int[][] array = new INT[3][2];
System.out.println ("Traversing a two-dimensional array");
for (int i = 0; i < Array.Length i++) {
//print elements stored in one-dimensional array: one-dimensional array
System.out.println ("first" + i + "elements:" + array[i]);
}
At this point, the following information will be printed on the console:
Print one-dimensional array: [i@21e8bf76 traversal of the No. 0 element of a two-dimensional
array:
[i@3771ed5e
1th element: [i@1896d2c2
2nd element: [i@55e6cb2a
Array[i] Printing is not a numeric value, but a bunch of symbols that represent an object (one-dimensional array) in Array[i. To iterate through the elements in these arrays, you must use circular traversal again:
int[][] array = new INT[3][2];
Iterate through the one-dimensional array
for (int i = 0; i < Array.Length i++) {
//traverse the array element (one-dimensional array) in a one-dimensional array of arrays for
(int j = 0; J < Array[i].length; J + +) {
System.out.print (Array[i][j] + "");
}
System.out.println ();
}
Mission Implementation
Create a new Java project "Project12" and put a wall picture in the "project12/img" folder. Task 12.1 New tank class
The Tank class has a position, picture, speed property, four-direction move method, as shown in Listing 12-1.
Code Listing 12-1: "Project/src/tank.java"
public class Tank {
int x;
int y;
int speed =;
String url = "Img/p1tanku.gif";
Construct method, initialize tank position public
Tank (int x, int y) {
this.x = x;
This.y = y;
}
Four direction Move method public
void MoveUp () {
URL = "Img/p1tanku.gif";
This.y = this.y-speed;
}
public void MoveDown () {
url = ' img/p1tankd.gif ';
This.y = This.y + speed;
}
public void MoveLeft () {
url = ' img/p1tankl.gif ';
This.x = this.x-speed;
}
public void MoveRight () {
url = ' img/p1tankr.gif ';
This.x = this.x + speed;
}
}
Task 12.2 New map Class
The map class merely describes the structure of the map, uses two-dimensional array walls in the map class to represent the structure of the map, the value of the German element in the array is 1 for the wall, 0 for the open space, and the map class as shown in Listing 12-2.
Code Listing 12-2: "Project/src/map.java"
public class Map {
//Create map
int[][] Walls = {
1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 1} ,
{1, 0, 0, 0, 0, 0, 0, 0, 0, 1}, {1, 1, 1, 0, 0, 0, 1, 1, 0, 1}, {1, 0, 1, 1, 0, 0, 1, 0, 0, 1
},
{1, 0, 0 , 0, 0, 0, 1, 0, 0, 1},
{1, 0, 0, 1, 1, 1, 1, 0, 0, 1}, {1, 0, 0, 0, 0, 0, 0, 0,
0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
};
Determines whether the position is a wall, is the wall returns true, otherwise returns false public
Boolean iswall (int row, int col) {
if (walls[row][col) = = 1) {
return true;
} else{return
false;}}
Task 12.3 New Gamepanel class
Panel classes are primarily responsible for drawing maps, and if the elements in a two-dimensional array are 1, draw as shown in Listing 12-3.
Code Listing 12-3: "Project/src/gamepanel.java"
Mport Java.awt.Color;
Import Java.awt.Graphics;
Import Java.awt.Image;
Import Javax.swing.ImageIcon;
Import Javax.swing.JPanel;
public class Gamepanel extends jpanel{
//Create maps Map
= new ();
Draw map size
int size =;
@Override public
void Paint (Graphics g) {
//TODO auto-generated method Stub
Super.paint (g);
This.setbackground (color.black);
Get the Wall picture resource
imageicon icon = new ImageIcon ("Img/wall.gif");
Convert the picture resource to the information that G can handle
image image = Icon.getimage ();
Loop through the walls array in the Map object for
(int row = 0; row < map.walls.length; row++) {for
(int col = 0; col < map.walls[r Ow].length; col++) {
if (map.iswall (row, col)) {
//Draw wall, array column col corresponding x axis, row row for Y axis
g.drawimage (image, col * size, row * size , size, size, this);}}}}
Task 12.4 New test class
The test class is responsible for creating the frames and gamepanel panels and displaying the panels in the frame, as shown in Listing 12-4.
Code Listing 12-4: "Project/src/test.java"
Import Javax.swing.JFrame;
public class Test {public
static void Main (string[] args) {
JFrame frame = new JFrame ("Tankgame");
Gamepanel Gamepanel = new Gamepanel ();
Frame.add (Gamepanel);
Frame.setsize ();
Frame.setvisible (True);
}