Java Ich habe versucht, einen einfachen Block zu brechen

Einführung

Ich werde heute zum ersten Mal bei Qiita posten. Um ehrlich zu sein, bin ich verwirrt, weil ich unten nicht weiß, was ich tun soll, aber vorerst möchte ich jeden Tag so viel wie möglich posten, um meine Programmiersprachenkenntnisse zu verbessern. Vielen Dank

Warum hast du dich entschieden, auf Qiita zu posten?

Um ehrlich zu sein, möchte ich es als attraktiven Punkt bei der Jobsuche hinzufügen. Selbst wenn mir gesagt wurde, dass ich Ingenieur werden wollte, ohne irgendetwas tun zu können, wusste ich, dass es fallen würde, also wollte ich es zumindest auf Qiita veröffentlichen und offenlegen, wie viel meine Fähigkeiten sind. Von nun an möchte ich weiter posten, damit nur in den Bereichen meines Interesses Fortschritte erzielt werden können.

Warum haben Sie sich für einen Blockbrecher entschieden?

Ich wollte die Spiele, die ich in der Vergangenheit gemacht habe, hier posten, weil ich sie als Schulaufgabe frei machen wollte. Außerdem liebe ich Block Breaking, Tetris und Puyopuyo, also wollte ich ein Retro-Spiel machen.

Blockbruchregeln

Jeder, der Blockbrecher gemacht hat, wird es wissen, aber es ist notwendig, einen Ball zu entwerfen, damit er nicht mit einem Schläger oder einer Stange auf den unteren Bildschirmrand zurückfällt. Wenn Sie zurückschlagen, schlagen Sie den Ball gegen den Block und löschen alle Blöcke. Wenn Sie den Block löschen, fügen Sie Punkte hinzu. Es ist ein sehr einfaches Spiel, aber es war sehr schwer zu machen. breakout.png Wie in der Abbildung gezeigt, ist dies ein Beispiel für das Brechen von Blöcken. Ich habe die Größe, Geschwindigkeit und Anzahl der Bälle geändert, weil es mit nur einem langweilig ist. Es weicht an dieser Stelle ab, aber vorerst ist es so. Der Block wird auch durch die Häufigkeit, mit der der Ball trifft, zum Verschwinden gebracht. Wenn es wie im Bild gezeigt ist, ist Gelb 3-mal, Blau 1-mal und Grün 2-mal. Schwarz verschwindet nicht, egal wie oft es trifft. Das Zeitlimit kann beliebig eingestellt werden. Aufgrund der Frist für die Einreichung von Aufträgen hätte ich die Anzahl der Blöcke nach dem Zeitlimit für das Spiel zählen sollen, aber ich konnte es nicht rechtzeitig schaffen und das Ergebnis war auf halbem Weg. Es wurde immer als spielklar angezeigt und der Fehlerzustand konnte nicht festgestellt werden. Deshalb werde ich es mehrmals posten, um es zu verbessern.

Referenzseite

http://aidiary.hatenablog.com/entry/20040918/1251373370 Diese Seite hat einen Blockbrecher gemacht. Den grundlegenden Algorithmus finden Sie hier. Einige sind ziemlich gleich, aber sehr einfach. Vielen Dank.

Java-Umgebung

java version "12.0.1" 2019-04-16 Java(TM) SE Runtime Environment (build 12.0.1+12) Java HotSpot(TM) 64-Bit Server VM (build 12.0.1+12, mixed mode, sharing)

Programmcode

Jetzt, da wir fast 900 Zeilen haben, möchte ich die Artikel separat erklären. Ich glaube nicht, dass ich es leid werde ...


import javax.swing.Timer;
import java.awt.Color;
import javax.swing.*;
import java.awt.*;
import java.text.ParseException;


public class TimerTest1 extends JFrame {
 static int x=855;
static int y=800;
    private Timer timer;
    private int countdown_sec = 5;
    private CardLayout card = new CardLayout(0, 0);

    public static void main(String[] args) throws ParseException {
          TimerTest1 frame = new TimerTest1();
         
          frame.setSize(x,y);
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.getContentPane().setBackground(new Color(0, 0, 0));
          frame.setVisible(true);
  }
    TimerTest1() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("Titelbildschirm");
        setLayout(card);
        JPanel panel = new Draw();
        //JPanel labelPanel = new JPanel();
        //JLabel label = new JLabel();
        JPanel game_over = new breakout();
        game_over.setOpaque(false);
        add(panel,"Zeichen zeichnen");
       // labelPanel.add(label);
       
      
        timer = new Timer(1000, (e) -> {
          
           
            if (countdown_sec ==0){
              add(game_over, "gameover_window");
                timer.stop();
              
                showPanel("gameover_window");
               
                return;
            }
            countdown_sec--;
        }); 
        timer.start();
    }
    
    public void showPanel(String name) {
        card.show(getContentPane(), name);
    }
    public static class Draw extends JPanel{
    public   void paintComponent(Graphics g){
draw(g);
    }
    public static  void draw(Graphics g){
      g.setFont(new Font("TimeRoman", Font.CENTER_BASELINE, 30));
      g.setColor(Color.red);
      g.drawString("Blockbrechen",300 , 200);
      g.setColor(Color.red); 
      g.drawString("Es beginnt in 5 Sekunden!", 250, 300); 
      g.drawString("Das Zeitlimit beträgt 30 Sekunden", 250, 400); 
      g.setColor(Color.red);
      g.setColor(Color.red);
      g.drawString("Bewegen Sie die Maus zur Seite, um den Ball abzuprallen", 125, 500);  
      g.setColor(Color.red);
      g.drawString("Die Pfeilspitze unten ist die Anfangsposition der Maus", 100, 600); 
      g.setColor(Color.red);
      g.drawString("↓",425 , 700);
    }
  }
  }

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.util.Timer;
import java.util.TimerTask;

   
   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;
   static int x=855;
   static int y=800;
   static int size =15;
   static final int NUM_BLOCK_ROW = 4;
   //Anzahl der Spalten im Block
    static final int NUM_BLOCK_COL = 9;
   //Anzahl der Blöcke
    static final int NUM_BLOCK = NUM_BLOCK_ROW * NUM_BLOCK_COL;
    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;
    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;
    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;
    static final int SumBlock=  NUM_BLOCK+NUM_BLOCK2+NUM_BLOCK3;
 
    //Erstellen einer zweidimensionalen Karte
      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",                        
                                 };
                                 
       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';
           }
           for (int y = 0; y <= MY+1; y++) {
             map[y][0] = 'B'; 
             map[y][MX+1] = 'B';
           }
           for (int y = 1; y <= MY; y++) {
               for (int x = 1; x <= MX; x++) {
                 map[y][x] = map_str[y-1].charAt(x-1);
               }
           }
        }
       public void displayTime(Graphics g) {
        if ( gameover ) {
       
          g.setFont(new Font("TimeRoman", Font.BOLD, 50));
          g.setColor(Color.YELLOW);
          g.drawString("GAME CLEAR", 250, 550); 
          for(int j=0;j<countball;j++){
            int ballsize =ball[j].Size();
          ball[j].Size();
                if(ballsize==0){
                return;
                }
          }
        } else {
         int dt = (int) (time - System.currentTimeMillis() * 0.001);
          g.setFont(new Font("TimeRoman", Font.BOLD, 50));
          g.setColor(Color.orange);
          g.drawString("Time: " + dt+"  ",300, 550);
                 if ( dt == 0 ) gameover = true;
        }
    }
  
      class TimeBomb extends TimerTask  {
       
       public void run(){
     
        while(true){
               for(int j=0;j<countball;j++){
               ball[j].move();
              
                  //Kollisionsbearbeitung zwischen Schläger und Ball
            int collidePos0 = racket.collideWith(ball[j]);
            
            //Wenn Sie den Schläger schlagen
            if (collidePos0 != Racket.NO_COLLISION4) {
                //Ändern Sie die Geschwindigkeit des Balls entsprechend der Position, an der der Ball schlägt
                switch (collidePos0) {
                    case Racket.LEFT4:
                        //Ich möchte nach links reflektieren, wenn es auf die linke Seite des Schlägers trifft
                        //Wenn sich der Ball nach rechts bewegt, drehen Sie ihn nach links
                        //Wenn Sie nach links gehen, lassen Sie es so wie es ist
                        if (ball[j].getVX() > 0) ball[j].boundX();
                        ball[j].boundY();
                        break;
                    case Racket.RIGHT4:
                        //Ich möchte nach rechts reflektieren, wenn es auf die rechte Seite des Schlägers trifft
                        //Wenn sich der Ball nach links bewegt, drehen Sie ihn nach rechts
                        //Wenn Sie nach rechts gehen, lassen Sie es so, wie es ist
                        if (ball[j].getVX() < 0) ball[j].boundX();
                        ball[j].boundY();
                        break;
                }
            }
               for (int i = 0; i < NUM_BLOCK; i++) {
                   //Ignorieren Sie bereits verschwundene Blöcke
                   if (block[i].isDeleted())
                       continue;
                   //Berechnen Sie die Position, an der der Block getroffen wurde
                   int collidePos = block[i].collideWith(ball[j]);
                   //Wenn Sie den Block treffen
                   if (collidePos != Block.NO_COLLISION) {
                       block[i].delete();
              
                       //Berechnen Sie die Reflexionsrichtung des Balls aus der Position, an der der Ball trifft
                       switch (collidePos) {
                           case Block.DOWN :
                           case Block.UP :
                               ball[j].boundY();
                               break;
                           case Block.LEFT :
                           case Block.RIGHT :
                               ball[j].boundX();
                               break;
                           case Block.UP_LEFT :
                           case Block.UP_RIGHT :
                           case Block.DOWN_LEFT :
                           case Block.DOWN_RIGHT :
                               ball[j].boundXY();
                               break;
                       }
                       break; //Es kann immer nur ein Block gebrochen werden
                    }
                }
               for (int i = 0; i < NUM_BLOCK1; i++) {
                   int collidePos1 = block1[i].collideWith(ball[j]);
                   if ( collidePos1 !=Unbreakblock.NO_COLLISION1) {
                       block1[i].notdelete();
                       
                       switch (collidePos1) {
                           case Unbreakblock.DOWN1 :
                           case Unbreakblock.UP1 :
                               ball[j].boundY();
                               break;
                           case Unbreakblock.LEFT1 :
                           case Unbreakblock.RIGHT1 :
                               ball[j].boundX();
                               break;
                           case Unbreakblock.UP_LEFT1 :
                           case Unbreakblock.UP_RIGHT1 :
                           case Unbreakblock.DOWN_LEFT1 :
                           case Unbreakblock.DOWN_RIGHT1 :
                               ball[j].boundXY();
                               break;
                       }
                       break; 
                   }
                }
               for (int i = 0; i < NUM_BLOCK2; i++) {
                   if (block2[i].isDeleted()){
                       continue;
                    }
                   int collidePos2 = block2[i].collideWith(ball[j]);
                   if ( collidePos2 !=Twobreakblock.NO_COLLISION2) {
                      
                           block2[i].delete();
                      
                       switch (collidePos2) {
                         
                           case Twobreakblock.DOWN2 :
                           case Twobreakblock.UP2 :
                               ball[j].boundY();
                               break;
                          
                           case Twobreakblock.LEFT2 :
                           case Twobreakblock.RIGHT2 :
                               ball[j].boundX();
                               break;
                         
                           case Twobreakblock.UP_LEFT2 :
                           case Twobreakblock.UP_RIGHT2 :
                           case Twobreakblock.DOWN_LEFT2 :
                           case Twobreakblock.DOWN_RIGHT2 :

                               ball[j].boundXY();
                               break;
                       }
                       break; 
                   }
               }
               
              for (int i = 0; i < NUM_BLOCK3; i++) {
               if (block3[i].isDeleted()){
     
                   continue;
               }
                   int collidePos3 = block3[i].collideWith(ball[j]);
                   
                   if ( collidePos3 !=Threebreakblock.NO_COLLISION3) {
                       block3[i].delete();
                      
                       
                       switch (collidePos3) {
                         
                           case Threebreakblock.DOWN3 :
                           case Threebreakblock.UP3 :
                               ball[j].boundY();
                               break;
                          
                           case Threebreakblock.LEFT3 :
                           case Threebreakblock.RIGHT3 :
                               ball[j].boundX();
                               break;
                         
                           case Threebreakblock.UP_LEFT3 :
                           case Threebreakblock.UP_RIGHT3 :
                           case Threebreakblock.DOWN_LEFT3 :
                           case Threebreakblock.DOWN_RIGHT3 :
                               ball[j].boundXY();
                               break;
                       }
                       break; 
                    }
                }
               }
               repaint();
               try {
                   Thread.sleep(20);
               }catch(InterruptedException e){
                   e.printStackTrace();
                   
               }
            }
       }
      }
        
       public void paintComponent(Graphics g){
           racket.draw(g);
           displayTime(g);
           
           for(int i=0;i<countball;i++){
               ball[i].draw(g);
           }
           
           for (int i=0;i<NUM_BLOCK;i++){
               if(!block[i].isDeleted()){
                   block[i].draw(g);
               }
           }
           for (int i=0;i<NUM_BLOCK1;i++){
               if(!block1[i].isDeleted()){
                   block1[i].draw(g);
               }
           }
          
          for (int i=0;i<NUM_BLOCK2;i++){
               if(!block2[i].isDeleted()){
                   block2[i].draw(g);
               }
           }
           
           for (int i=0;i<NUM_BLOCK3;i++){
               if(!block3[i].isDeleted()){
                   block3[i].draw(g);
               }
           }
               for (int y = 0; y <= MY; y++) {
                 for (int x = 0; x <= MX; x++) {
                   int xx = 40*x, yy = 40*y;
                   switch ( map[y][x] ) {
                     //Zeichenblöcke
             case 'B':  g.setColor(Color.green);
                        g.fillRect(xx, yy, 26, 10);
                        g.fillRect(xx+32, yy, 8, 10);
                        g.fillRect(xx, yy+15, 10, 10);
                        g.fillRect(xx+16, yy+15, 24, 10);
                        g.fillRect(xx, yy+30, 18, 10);
                        g.fillRect(xx+24, yy+30, 16, 10);
                        break;
                   }
                }
            } 
        }
       
       @Override
       public void mouseMoved(MouseEvent e){
          int x =e.getX();
         racket.move(x);
           repaint();
       }
       @Override
       public void mouseDragged(MouseEvent e){

       }
    }
        class Racket {
       public int width =120;
       public static  int height = 5;
       public static final int NO_COLLISION4 = 0;  //Nicht kollision
       public static final int LEFT4 = 1;
       public static final int RIGHT4 = 2;
                private int center;
   
        public Racket (){
       center = breakout.x/2;
   
        }       
        public int  collideWith(Ball ball) {
      //Ballkollisionsurteil
        Rectangle racketRectLeft = new Rectangle(
               center - width / 2, breakout.y - height-90,
               width/2, height);
               Rectangle racketRectRight = new Rectangle(
                center, breakout.y - height-90,
                width / 2, height);
      
        Rectangle ballRect = new Rectangle(
               ball.getX(), ball.getY(),
               ball.getSize(), ball.getSize());
        if (racketRectLeft.intersects(ballRect)) {
           return LEFT4;
        }
        else if (racketRectRight.intersects(ballRect)) {
        return RIGHT4;
        }
       return NO_COLLISION4;
        }

        public  void draw (Graphics g){
        g.setColor(Color.WHITE);
        g.fillRect(center - width/2,breakout.y-height-90,width,height);
   
        }
        public void move (int x ){
      center =x;
       if (center<(width/2)+40)
       center =(width/2)+40;
       else if (center > breakout.x - (width / 2)-55) 
           center = breakout.x - (width / 2)-55;
   
        }
        }
     class Ball  {
       
      private   int x;
       private  int y;
       private  int vx;
       private  int vy;
       private int size;
       double time;
       int remain=37;
       boolean gameover=false;
       int dt = (int) (time - System.currentTimeMillis() * 0.001);
       Random rand ;
       public Ball(int  x,int y,int vx,int vy,int size ){
           
   this.x=x;
   this.y=y;
   this.vx=vx;
   this.vy=vy;
     this.size =size;    
     time = System.currentTimeMillis() * 0.001 + remain; 
       }
        public  void draw(Graphics g){
           
   g.setColor(Color.RED);
   g.fillOval(x,y,size,size);
  
       }
       
       public int   Size(){
  return size=size-3;
       } 
       public void move(){
         
           if ( gameover ) {
         x=0;
         y=0;
            } else {
            int dt = (int) (time - System.currentTimeMillis() * 0.001);
            x += vx;
            y += vy;
          
            if ( dt == 0 ) gameover = true;
         
            }
           if (x < 40 || x > breakout.x - size-60) {
             boundX();
           }
           if (y < 40  ) {
             boundY();
           }
        }
           public void boundX(){
            vx=-vx;
           }   
           public void boundY(){
            vy=-vy;
           }
       public int getX(){
           return x;
       }
       public void boundXY(){
           vx=-vx;
           vy=-vy;
       }
       public int getY(){
           return y;
       }
       public int getSize(){
           return size;
       }
       public int getVX(){
           return vx;
       }
       public int getVY(){
           return vy;
           
       }
    public void setVX(int v){
        vx=v;
       
    }
    public void setVY(int v){
         vy=v;
      
    }
}
       class Block {
           public static final int WIDTH = 40;
           public static final int HEIGHT = 16;
           public static final int NO_COLLISION = 0; //Nicht kollision
           public static final int DOWN = 1;
           public static final int LEFT = 2;
           public static final int RIGHT = 3;
           public static final int UP = 4;
           public static final int DOWN_LEFT = 5;
           public static final int DOWN_RIGHT = 6;
           public static final int UP_LEFT = 7;
           public static final int UP_RIGHT = 8;
       
       
           //Position (Koordinaten der oberen linken Ecke)
           private int x, y;
    
           //Wurde der Ball getroffen und gelöscht?
           private boolean isDeleted;
       
           public Block(int x, int y) {
               this.x = x;
               this.y = y;
               isDeleted = false;
           }
        
           public void draw(Graphics g) {
               g.setColor(Color.CYAN);
               g.fillRect(x, y, WIDTH, HEIGHT);
       
               //Zeichne einen Rand
               g.setColor(Color.BLACK);
               g.drawRect(x, y, WIDTH, HEIGHT);
           }
           public int collideWith(Ball ball) {
               Rectangle blockRect = new Rectangle(x, y, WIDTH, HEIGHT);
       
               int ballX = ball.getX();
               int ballY = ball.getY();
               int ballSize = ball.getSize();
               if (blockRect.contains(ballX, ballY)
                   && blockRect.contains(ballX + ballSize, ballY)) {
                   //Kollision unter dem Block = Die Punkte oben links und oben rechts im Ball befinden sich innerhalb des Blocks
                   return DOWN;
               } else if (blockRect.contains(ballX + ballSize, ballY)
                   && blockRect.contains(ballX + ballSize, ballY + ballSize)) {
                   //Kollision von links vom Block = Der obere rechte und untere rechte Punkt des Balls befinden sich innerhalb des Blocks
                   return LEFT;
               } else if (blockRect.contains(ballX, ballY)
                   && blockRect.contains(ballX, ballY + ballSize)) {
                   //Kollision von der rechten Seite des Blocks = Die oberen linken und unteren linken Punkte des Balls befinden sich innerhalb des Blocks
                   return RIGHT;
               } else if (blockRect.contains(ballX, ballY + ballSize)
                   && blockRect.contains(ballX + ballSize, ballY + ballSize)) {
                   //Kollision von der Oberseite des Blocks = Die unteren linken und unteren rechten Punkte des Balls befinden sich innerhalb des Blocks
                   return UP;
               } else if (blockRect.contains(ballX + ballSize, ballY)) {
                   //Kollision von links unten im Block = Der Punkt oben rechts im Ball befindet sich innerhalb des Blocks
                   return DOWN_LEFT;
               } else if (blockRect.contains(ballX, ballY)) {
                   //Kollision von rechts unten im Block = Der obere linke Punkt des Balls befindet sich im Block
                   return DOWN_RIGHT;
               } else if (blockRect.contains(ballX + ballSize, ballY + ballSize)) {
                   //Kollision von oben links im Block = Der Punkt unten rechts im Ball befindet sich innerhalb des Blocks
                   return UP_LEFT;
               } else if (blockRect.contains(ballX, ballY + ballSize)) {
                   //Kollision von oben rechts im Block = Der untere linke Punkt des Balls befindet sich im Block
                   return UP_RIGHT;
               }
       
               return NO_COLLISION;
           }
           public void delete() {
              
       
               isDeleted = true;
           }
         
            public int getX(){
                return x;
            } 
            public int getY(){
               
               return y;
            }
            public boolean isDeleted(){
                return isDeleted;
            }
        }
     interface figure1 {
        public void draw(Graphics g);
        public int collideWith(Ball ball);
        public int getX();
        public int getY();
        public boolean isDeleted();
  
     }
class Unbreakblock implements figure1 {
   public static final int WIDTH1 = 40;
   public static final int HEIGHT1 = 16;
   public static final int NO_COLLISION1= 0; 
   public static final int DOWN1= 1;
   public static final int LEFT1 = 2;
   public static final int RIGHT1 = 3;
   public static final int UP1 = 4;
   public static final int DOWN_LEFT1 = 5;
   public static final int DOWN_RIGHT1 = 6;
   public static final int UP_LEFT1 = 7;
   public static final int UP_RIGHT1 = 8;
   private int a, b;
   private boolean isDeleted;
   public Unbreakblock(int a, int b) {
       this.a = a;
       this.b = b;
       isDeleted = false;
   }
   public void draw(Graphics g) {
       g.setColor(Color.GRAY);
       g.fillRect(a, b, WIDTH1, HEIGHT1);
       g.setColor(Color.BLACK);
       g.drawRect(a, b, WIDTH1, HEIGHT1);
   }
   public int collideWith(Ball ball) {
       Rectangle blockRect = new Rectangle(a, b, WIDTH1, HEIGHT1);
   
       int ballX = ball.getX();
       int ballY = ball.getY();
       int ballSize = ball.getSize();
       if (blockRect.contains(ballX, ballY)
           && blockRect.contains(ballX + ballSize, ballY)) {
          
           return DOWN1;
       } else if (blockRect.contains(ballX + ballSize, ballY)
           && blockRect.contains(ballX + ballSize, ballY + ballSize)) {
           
           return LEFT1;
       } else if (blockRect.contains(ballX, ballY)
           && blockRect.contains(ballX, ballY + ballSize)) {
          
           return RIGHT1;
       } else if (blockRect.contains(ballX, ballY + ballSize)
           && blockRect.contains(ballX + ballSize, ballY + ballSize)) {
          
           return UP1;
       } else if (blockRect.contains(ballX + ballSize, ballY)) {
           
           return DOWN_LEFT1;
       } else if (blockRect.contains(ballX, ballY)) {
          
           return DOWN_RIGHT1;
       } else if (blockRect.contains(ballX + ballSize, ballY + ballSize)) {
         
           return UP_LEFT1;
       } else if (blockRect.contains(ballX, ballY + ballSize)) {
         
           return UP_RIGHT1;
       }
   
       return NO_COLLISION1;
    }
   public void notdelete() {
       isDeleted = false ;
   }
    public int getX(){
        return a;
    } 
    public int getY(){
       
       return b;
    }
    public boolean isDeleted(){
        return isDeleted;
    }
}
   class Twobreakblock implements figure1 {
       public static final int WIDTH2 = 40;
       public static final int HEIGHT2 = 16;
       public static final int NO_COLLISION2= 0; 
       public static final int DOWN2= 21;
       public static final int LEFT2 = 22;
       public static final int RIGHT2 = 23;
       public static final int UP2 = 24;
       public static final int DOWN_LEFT2 = 25;
       public static final int DOWN_RIGHT2 = 26;
       public static final int UP_LEFT2 = 27;
       public static final int UP_RIGHT2 = 28;
    int a, b;
        int count =2;
       private boolean isDeleted;
       public Twobreakblock(int a, int b) {
           this.a = a;
           this.b = b;
           isDeleted = false;
       }
       public void draw(Graphics g) {
           g.setColor(Color.GREEN);
           g.fillRect(a, b, WIDTH2, HEIGHT2);
   
          
           g.setColor(Color.BLACK);
           g.drawRect(a, b, WIDTH2, HEIGHT2);
       }
       public int collideWith(Ball ball) {
           Rectangle blockRect = new Rectangle(a, b, WIDTH2, HEIGHT2);
   
           int ballX = ball.getX();
           int ballY = ball.getY();
           int ballSize = ball.getSize();
           if (blockRect.contains(ballX, ballY)
               && blockRect.contains(ballX + ballSize, ballY)) {
              
               return DOWN2;
           } else if (blockRect.contains(ballX + ballSize, ballY)
               && blockRect.contains(ballX + ballSize, ballY + ballSize)) {
              
               return LEFT2;
           } else if (blockRect.contains(ballX, ballY)
               && blockRect.contains(ballX, ballY + ballSize)) {
               
               return RIGHT2;
           } else if (blockRect.contains(ballX, ballY + ballSize)
               && blockRect.contains(ballX + ballSize, ballY + ballSize)) {
               
               return UP2;
           } else if (blockRect.contains(ballX + ballSize, ballY)) {
               
               return DOWN_LEFT2;
           } else if (blockRect.contains(ballX, ballY)) {
             
               return DOWN_RIGHT2;
           } else if (blockRect.contains(ballX + ballSize, ballY + ballSize)) {
              
               return UP_LEFT2;
           } else if (blockRect.contains(ballX, ballY + ballSize)) {
              
               return UP_RIGHT2;
           }
   
           return NO_COLLISION2;
       }
       public void delete() {
                if(isDeleted==false)
                count--;
                if(count==0)  
           
           isDeleted = true  ;
        } 
        public int getX(){
            return a;
        } 
        public int getY(){
           
           return b;
        }
        public boolean isDeleted(){
            return isDeleted;
        }
     }
     
     class Threebreakblock implements figure1{
       public static final int WIDTH3 = 40;
       public static final int HEIGHT3 = 16;
       public static final int NO_COLLISION3= 0; 
       public static final int DOWN3= 1;
       public static final int LEFT3 = 2;
       public static final int RIGHT3 = 3;
       public static final int UP3 = 4;
       public static final int DOWN_LEFT3 = 5;
       public static final int DOWN_RIGHT3 = 6;
       public static final int UP_LEFT3 = 7;
       public static final int UP_RIGHT3 = 8;
       private int a, b;
        int count =3;
       private boolean isDeleted;
       public Threebreakblock(int a, int b) {
           this.a = a;
           this.b = b;
           isDeleted = false;
       }
       public void draw(Graphics g) {
           g.setColor(Color.YELLOW);
           g.fillRect(a, b, WIDTH3, HEIGHT3);
   
          
           g.setColor(Color.BLACK);
           g.drawRect(a, b, WIDTH3, HEIGHT3);
       }
       public int collideWith(Ball ball) {
           Rectangle blockRect = new Rectangle(a, b, WIDTH3, HEIGHT3);
   
           int ballX = ball.getX();
           int ballY = ball.getY();
           int ballSize = ball.getSize();
           if (blockRect.contains(ballX, ballY)
               && blockRect.contains(ballX + ballSize, ballY)) {
              
               return DOWN3;
           } else if (blockRect.contains(ballX + ballSize, ballY)
               && blockRect.contains(ballX + ballSize, ballY + ballSize)) {
              
               return LEFT3;
           } else if (blockRect.contains(ballX, ballY)
               && blockRect.contains(ballX, ballY + ballSize)) {
               
               return RIGHT3;
           } else if (blockRect.contains(ballX, ballY + ballSize)
               && blockRect.contains(ballX + ballSize, ballY + ballSize)) {
               
               return UP3;
           } else if (blockRect.contains(ballX + ballSize, ballY)) {
               
               return DOWN_LEFT3;
           } else if (blockRect.contains(ballX, ballY)) {
             
               return DOWN_RIGHT3;
           } else if (blockRect.contains(ballX + ballSize, ballY + ballSize)) {
              
               return UP_LEFT3;
           } else if (blockRect.contains(ballX, ballY + ballSize)) {
              
               return UP_RIGHT3;
           }
   
           return NO_COLLISION3;
       }
       public void delete() {
                if(isDeleted==false)
                count--;
                if(count==0)  
           isDeleted = true  ;
        } 
      
        public int getX(){
            return a;
        } 
        public int getY(){
           
           return b;
        }
        public boolean isDeleted(){
            return isDeleted;
        }
     }

Der Einzug ist schmutzig. .. .. Es beschreibt den Zustand, in dem die Aufgabe eingereicht wurde. Jede Breakout-Klasse speichert einen Block, einen Ball und eine Instanzvariable eines Schlägers, der den Ball abprallt.

breakout03.png

Recommended Posts

Java Ich habe versucht, einen einfachen Block zu brechen
Ich habe versucht, den Block mit Java zu brechen (1)
Ich habe versucht, eine Clova-Fähigkeit in Java zu erstellen
Ich habe versucht, eine Anmeldefunktion mit Java zu erstellen
Ich habe versucht, mit Java zu interagieren
Ich habe versucht, das Java-Lernen zusammenzufassen (1)
Ich habe jetzt versucht, Java 8 zusammenzufassen
Ich habe versucht, eine Java EE-Anwendung mit OpenShift zu modernisieren.
Ich habe versucht, in Java von einer Zeichenfolge in einen LocalDate-Typ zu konvertieren
Ich habe einen RESAS-API-Client in Java erstellt
Ich habe versucht, Java-Lambda-Ausdrücke zusammenzufassen
Ich habe versucht, eine einfache Karten-App in Android Studio zu erstellen
Ich habe versucht, eine einfache Anwendung mit Dockder + Rails Scaffold zu erstellen
[Java] Ich habe versucht, mit der Grabmethode ein Labyrinth zu erstellen ♪
Ich habe versucht, eine einfache Gesichtserkennungs-Android-Anwendung mit OpenCV zu erstellen
Ich habe versucht, eine Standardauthentifizierung mit Java durchzuführen
Ich war süchtig nach einem einfachen Test von Jedis (Java-> Redis-Bibliothek)
Ich habe versucht, ein Personalmanagement-Tool zu entwickeln
Ich habe Java gemacht, um (a == 1 && a == 2 && a == 3) immer wahr zu machen
Ich habe versucht, eine Website für das Studium von DUO3.0 zu entwickeln.
Ich habe versucht, eine Java-Methode von ABCL zu verwenden
Java-Anfänger haben versucht, mit Spring Boot eine einfache Webanwendung zu erstellen
Ich wollte (a == 1 && a == 2 && a == 3) in Java wahr machen
Ich habe versucht, eine LINE-Klon-App zu erstellen
Ich habe versucht, neunundneunzig in Java auszugeben
Ich habe versucht, mit AI "A3RT" eine Talk-App in Java zu erstellen.
Ich habe versucht, Alexa-Fähigkeiten mit Java zu erstellen
Ich habe versucht, eine Website zu entwickeln, um Ausgaben zu erfassen.
Ich habe versucht, einen Server mit Netty zu implementieren
Ich habe versucht, Java in einem Mac-Terminal auszuführen
Ich habe versucht, mit Javafx ein einfaches Spiel zu machen ① "Lass uns Glücksspiel finden" (unvollendet)
[Java] Ich habe versucht, über den Verbindungspool eine Verbindung mit Servlet (Tomcat) & MySQL & Java herzustellen
[Azure] Ich habe versucht, eine Java-App für die Erstellung von kostenlosen Web-Apps zu erstellen. [Anfänger]
[Kleine Geschichte] Ich habe versucht, die Java-ArrayList etwas komfortabler zu gestalten
Ich habe ein einfaches Berechnungsproblemspiel in Java gemacht
Ich habe versucht, TCP / IP + BIO mit JAVA zu implementieren
Ich habe versucht, eine Ramen-Shop-Sharing-Website zu entwickeln.
[Java 11] Ich habe versucht, Java auszuführen, ohne mit Javac zu kompilieren
[Java] Ich habe versucht, Paizas B-Rang-Problem zu lösen
Ich habe versucht, SQS mit AWS Java SDK zu betreiben
# 2 [Anmerkung] Ich habe versucht, neunundneunzig mit Java zu berechnen.
Ich habe versucht, Log4j2 auf einem Java EE-Server zu verwenden
Ich habe versucht, mit OCR eine PDF-Datei mit Java zu verarbeiten
Ich habe versucht, Sterling Sort mit Java Collector zu implementieren
[Java] Ich habe versucht, die Yahoo API-Produktsuche zu implementieren
Ich habe versucht, die Methode der gegenseitigen Teilung von Eugrid in Java zu implementieren
~ Ich habe jetzt versucht, funktionale Programmierung mit Java zu lernen ~
Ich habe versucht, ein Aktienchart mit Java (Jsoup) zu kratzen.
Ich habe versucht herauszufinden, was sich in Java 9 geändert hat
Ich habe Drools (Java, InputStream) ausprobiert.
[Java] Zeichnen Sie ein einfaches Muster
Ich habe versucht, Java REPL zu verwenden
Ich habe versucht, yum-cron zu verifizieren
Ich habe versucht, Metaprogrammierung mit Java
Ich habe versucht, mit Java und Spring eine Funktion / einen Bildschirm für den Administrator einer Einkaufsseite zu erstellen
Ich habe versucht, ein einfaches Spiel mit Javafx zu machen ① "Lass uns Glücksspiel finden" (unvollendete Version ②)
[Azure] Ich habe versucht, eine kostenlose Java-App zu erstellen ~ Mit FTP verbinden ~ [Anfänger]
[Java] Ich möchte ein Byte-Array in eine Hexadezimalzahl konvertieren
Ich habe versucht, die Grundlagen von Kotlin und Java zusammenzufassen