I tried to break a block with java (1)

things to do

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.

Diagram of class structure

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. java1.png

Program code

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.

Breakout class description

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;

Features in this code

-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.

In other words, what did you do

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.

Code for the number of blocks and screen size


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

Features in this code

・ 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

In other words, what did you do

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.

Code for the initial position of the block


 //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",                        
                                 };

Features in this code

-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.

Code of ball function and block position


 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);
               }
           }
        }

Features in this code

-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

In other words, what did you do

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.

Code-free part


 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.

Impressions

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

I tried to break a block with java (1)
java I tried to break a simple block
I tried to create a java8 development environment with Chocolatey
I tried to modernize a Java EE application with OpenShift.
I tried to make Basic authentication with Java
I tried to implement TCP / IP + BIO with JAVA
I tried to create a Clova skill in Java
I tried to make a login function in Java
I tried OCR processing a PDF file with Java
I tried to implement Stalin sort with Java Collector
[Java] I tried to connect using a connection pool with Servlet (tomcat) & MySQL & Java
I tried UDP communication with Java
I tried to summarize Java learning (1)
I tried to summarize Java 8 now
[Rails] I tried to create a mini app with FullCalendar
I want to make a list with kotlin and java!
I want to make a function with kotlin and java!
Even in Java, I want to output true with a == 1 && a == 2 && a == 3
I tried to convert a string to a LocalDate type in Java
I tried to create a padrino development environment with Docker
I tried OCR processing a PDF file with Java part2
I tried to create a shopping site administrator function / screen with Java and Spring
[Azure] I tried to create a Java application for free ~ Connect with FTP ~ [Beginner]
I tried to summarize Java lambda expressions
I tried to get started with WebAssembly
I tried playing with BottomNavigationView a little ①
I tried using OpenCV with Java + Tomcat
I tried to implement ModanShogi with Kinx
A story that I struggled to challenge a competition professional with Java
I want to make a button with a line break with link_to [Note]
I tried to make an Android application with MVC now (Java)
[Java] I tried to make a maze by the digging method ♪
I tried to make a group function (bulletin board) with Rails
I tried to manage struts configuration with Coggle
I tried learning Java with a series that beginners can understand clearly
I tried to manage login information with JMX
I did Java to make (a == 1 && a == 2 && a == 3) always true
I tried to develop a DUO3.0 study website.
Even in Java, I want to output true with a == 1 && a == 2 && a == 3 (PowerMockito edition)
[iOS] I tried to make a processing application like Instagram with Swift
I tried hitting a Java method from ABCL
I tried to implement deep learning in Java
I want to use java8 forEach with index
I tried to make a Web API that connects to DB with Quarkus
I tried to build a Firebase application development environment with Docker in 2020
I tried to create a LINE clone app
I want to create a dark web SNS with Jakarta EE 8 with Java 11
I tried to make a talk application in Java using AI "A3RT"
I want to ForEach an array with a Lambda expression in Java
I tried to create Alexa skill in Java
I tried to develop a website to record expenses.
I tried to implement a server using Netty
I tried running Java on a Mac terminal
I tried to create a portfolio with AWS, Docker, CircleCI, Laravel [with reference link]
I tried to implement a function equivalent to Felica Lite with HCE-F of Android
Even in Java, I want to output true with a == 1 && a == 2 && a == 3 (Javassist second decoction)
I tried to make a simple game with Javafx ① "Let's find happiness game" (unfinished)
[Android] I tried to make a material list screen with ListView + Bottom Sheet
I tried to clone a web application full of bugs with Spring Boot
[Azure] I tried to create a Java application for free-Web App creation- [Beginner]
[Small story] I tried to make the java ArrayList a little more convenient