The previous article is below
https://qiita.com/strict39275/items/41da2ad3bb8fd60228e0 Even if I think about it carefully, the code did not reach my ideal, but I would like to improve it from now on. In this article, I would like to explain each class. If you explain everything suddenly, it will be a tremendous amount of time, so next time it seems better to explain each class carefully one by one. I'll do it.
It is a class structure diagram made by shiracamus. It feels like the breakout class is the main, and the instance variables of the class are created in it.
I would like to explain one by one, including reflection.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.util.Timer;
import java.util.TimerTask;
It seems better to declare the import statement at the beginning of the code. That way, it's easier to see what library this code uses. By entering a declaration such as import javax.swing. You will be able to use the classes created by java. In other words, you can display the screen and enter mouse events without having to write your own code. Most of the import statements use on-demand imports. If you import the on-demand type, for example, you can use all the functions in javax.util. In other words, if you add ✳︎ at the end of the import statement, you will not have to worry about errors. However, you should be careful because you will not know which function to use.
This time I will only explain the breakout class
public class breakout extends JPanel implements MouseMotionListener{
static int countball =15;
Racket racket;
Ball[] ball;
Block[] block;
Unbreakblock[] block1;
Twobreakblock[] block2;
Threebreakblock[] block3;
double time;
int remain=37;
boolean gameover=false;
-The breakout class inherits the JPanel function and has the function to actually display the screen. In that panel, I typed implements to enable mouse functionality -Declare it as a static class variable and set the number of balls to 15. -Declare instance variables racket, ballblock1 block2, block3 that are not declared static, and take variables that have the functions of each class. -The time from execution until the gameover value becomes True is set to 37 seconds. -Since the initial state is not the gameover state, the initial boolean value is set to false.
This is where we will lay the foundation for making a game. Even if you make a game, it does not mean that it is not displayed on the screen, and if you do not have a mouse function, you can not even use a racket that bounces the ball, so this code once created an environment that can be used like that. I want to make a ball, but I can't play the game without knowing how many, so I defined the initial value for the time being.
static int x=855;
static int y=800;
static int size =15; //I don't need it because the number of balls is declared in countball
//Number of light blue blocks
static final int NUM_BLOCK_ROW = 4;
//Number of columns in the block
static final int NUM_BLOCK_COL = 9;
//Number of blocks
static final int NUM_BLOCK = NUM_BLOCK_ROW * NUM_BLOCK_COL;
//Number of gray blocks
static final int NUM_BLOCK_ROW1 = 8;
static final int NUM_BLOCK_COL1 = 1;
static final int NUM_BLOCK1 = NUM_BLOCK_ROW1 * NUM_BLOCK_COL1;
//Number of green blocks
static final int NUM_BLOCK_ROW2 = 8;
static final int NUM_BLOCK_COL2 = 3;
static final int NUM_BLOCK2 = NUM_BLOCK_ROW2 * NUM_BLOCK_COL2;
//Number of yellow blocks
static final int NUM_BLOCK_ROW3 = 8;
static final int NUM_BLOCK_COL3 = 4;
static final int NUM_BLOCK3 = NUM_BLOCK_ROW3 * NUM_BLOCK_COL3;
//Find the number of all blocks
static final int SumBlock= NUM_BLOCK+NUM_BLOCK2+NUM_BLOCK3;
Forget static int size = 15
・ The horizontal size of the panel was 855 and the vertical length was 800. -Declare the number of blocks here. ROW is the number of columns in the block COL is the number of rows in the block -Multiply to find the number of blocks. For example, if the column is 8 rows and 4 then a block of 8 × 4 = 32 is created. -Calculate the number of light blue, gray, green, and yellow blocks ・ Sumblock is the total of each
I defined the size of the game screen and also declared the number of blocks. Here you can freely change the number of blocks. Since I want to inherit the value when the number of this block decreases, I declared it as static and also used it as a class variable. You didn't need to declare final.
//Creating a 2D map
private char[][] map;
private int MX = 20, MY = 18;
private String[] map_str = {
" B",
" B",
" B",
" B",
" B",
" B",
" B",
" B",
" B",
" B",
" B",
" B",
" B",
" B",
" B",
" B",
" B",
" B",
" B",
};
-Create a variable that actually draws a block with a char type map variable. -The range of the block to be drawn is 20 horizontal and 18 vertical. ・ The location of the block is only on the right
Here, the reason why the blocks are also placed on the left wall and the upper wall is because it is defined as an initial value in the future code.
public breakout(){
time = System.currentTimeMillis() * 0.001 + remain;
addMouseMotionListener(this);
racket =new Racket();
ball = new Ball[countball];
Random rand = new Random();
int n=countball;
int num[] = new int [n];
for(int i=0;i<n;i++){
num[i]= 40+rand.nextInt(700);
ball[0]=new Ball(num[0],250,5,-6,7);
ball[1]=new Ball(num[1],260,-5,-3,10);
ball[2]=new Ball(num[2],420,4,6,8);
ball[3]=new Ball(num[3],480,-5,2,10);
ball[4]=new Ball(num[4],590,5,-6,11);
ball[5]=new Ball(num[5],550,-5,-3,12);
ball[6]=new Ball(num[6],570,4,6,13);
ball[7]=new Ball(num[7],480,-5,2,14);
ball[8]=new Ball(num[8],490,5,-6,8);
ball[9]=new Ball(num[9],400,-5,-3,8);
ball[10]=new Ball(num[10], 350,4,6,9);
ball[11]=new Ball(num[11],400,-5,2,10);
ball[12]=new Ball(num[12],390,-5,-3,10);
ball[13]=new Ball(num[13],500,4,6,10);
ball[14]=new Ball(num[14],530,-5,2,7);
}
block = new Block[NUM_BLOCK];
block1 = new Unbreakblock[NUM_BLOCK1];
block2 = new Twobreakblock[NUM_BLOCK2];
block3 =new Threebreakblock[NUM_BLOCK3];
for (int i = 0; i < NUM_BLOCK_ROW; i++) {
for (int j = 0; j < NUM_BLOCK_COL; j++) {
int x =2* j * Block.WIDTH + Block.WIDTH+50;
int y =2* i * Block.HEIGHT + Block.HEIGHT+600;
block[i * NUM_BLOCK_COL + j] = new Block(x, y);
}
}
for ( int c = 0; c < NUM_BLOCK_ROW1; c++) {
for (int d = 0; d < NUM_BLOCK_COL1; d++) {
int a = 2*c * Unbreakblock.WIDTH1 + Unbreakblock.WIDTH1+50;
int b = d * Unbreakblock.HEIGHT1 + Unbreakblock.HEIGHT1+450;
block1[d * NUM_BLOCK_COL1 + c] = new Unbreakblock(a, b);
}
}
for ( int c = 0; c < NUM_BLOCK_ROW2; c++) {
for (int d = 0; d < NUM_BLOCK_COL2; d++) {
int a = 2*c * Twobreakblock.WIDTH2 + Twobreakblock.WIDTH2+50;
int b = 2*d * Twobreakblock.HEIGHT2 + Twobreakblock.HEIGHT2+300;
block2[c* NUM_BLOCK_COL2 + d] = new Twobreakblock(a, b);
}
}
for ( int c = 0; c < NUM_BLOCK_ROW3; c++) {
for (int d = 0; d < NUM_BLOCK_COL3; d++) {
int a =2* c * Threebreakblock.WIDTH3 + Threebreakblock.WIDTH3+50;
int b = 5*d * Threebreakblock.HEIGHT3 + Threebreakblock.HEIGHT3+60;
block3[c * NUM_BLOCK_COL3 + d] = new Threebreakblock(a, b);
}
}
TimeBomb timeBomb = new TimeBomb();
Timer timer = new Timer();
timer.schedule(timeBomb, 5000);
map = new char[MY+2][MX+2];
for (int x = 0; x <= MX+1; x++) {
map[0][x] = 'B';
// map[MY+1][x] = 'B';I don't need
}
for (int y = 0; y <= MY+1; y++) {
map[y][0] = 'B';
//map[y][MX+1] = 'B'; //I don't need
}
for (int y = 1; y <= MY; y++) {
for (int x = 1; x <= MX; x++) {
map[y][x] = map_str[y-1].charAt(x-1);
}
}
}
-Add to main the double type time that has a time limit, and set the current time limit to x0.001. -Here, use the instance variables of the Racket and Ball classes. -Declare rand to make the coordinates of the side of the ball random -The coordinates of the side of the ball are assigned to the array num at random from 0 to 700 following 40. That is, the horizontal coordinates of the 15 balls are randomly generated, and the values are assigned to num. -The arguments declared in the Ball class are, from the left, the horizontal coordinates of the ball, the vertical coordinates of the ball, the horizontal velocity of the ball, and the vertical velocity of the ball. The vertical coordinates were manually entered so that they did not overlap the block coordinates. -The number of blocks was used as it was, and instance variables were created in block, block1, block2, and block3. -In order to enter the vertical and horizontal coordinates, multiple loops were made with the for statement, and the block coordinates were set at x, y, a, b, etc., respectively. -Even if the program is executed by timer.schedule (timeBomb, 5000) ;, the ball and block functions are stopped for the first 5 seconds. -Defined the initial position of block placement. The place where you commented that you do not need a block is a function that allows you to place blocks other than walls, so you do not need it
For the first time, I defined the function of the ball, the position of the block, and the position of the green wall. The vertical coordinates of the ball are set to manual so that the coordinates of the block and the ball do not overlap. Otherwise, the ball will be in the block, and the balloon will break and the contents will come out, so the vertical coordinates are not random. Some code was labeled as BBB last time, but you can put blocks here as well.
for(int i=0;i<n;i++){
num[i]= 40+rand.nextInt(700);
}
ball[0]=new Ball(num[0],250,5,-6,7);
ball[1]=new Ball(num[1],260,-5,-3,10);
ball[2]=new Ball(num[2],420,4,6,8);
ball[3]=new Ball(num[3],480,-5,2,10);
ball[4]=new Ball(num[4],590,5,-6,11);
ball[5]=new Ball(num[5],550,-5,-3,12);
ball[6]=new Ball(num[6],570,4,6,13);
ball[7]=new Ball(num[7],480,-5,2,14);
ball[8]=new Ball(num[8],490,5,-6,8);
ball[9]=new Ball(num[9],400,-5,-3,8);
ball[10]=new Ball(num[10], 350,4,6,9);
ball[11]=new Ball(num[11],400,-5,2,10);
ball[12]=new Ball(num[12],390,-5,-3,10);
ball[13]=new Ball(num[13],500,4,6,10);
ball[14]=new Ball(num[14],530,-5,2,7);
But I feel like there is no problem. You didn't have to actually compile and declare instances multiple times, and num [i] = 40 + rand.nextInt (700); was enough to get inside the for statement.
The method of breakout class, the class after that, and TimerTask will be done next time. To be honest, I somehow understood what I wanted to do, but there was too much useless code to write. There are too many questions. It's a pity that it's too halfway. If you make it too perfect, time will disappear, so I would like to cut it to some extent and add it.
Recommended Posts