JavaFX俄罗斯方块没有按预期工作

时间:2017-11-04 15:12:38

标签: java javafx

有人可以解释如何解决这个问题吗? DeleteRows()方法无法正常工作。

这是一个俄罗斯方块游戏,我正在尝试删除行。当块向下移动时,块仍被计为0,因此新块将通过它们。但这只发生在移动块的顶行:

package application;

import java.util.*;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.EventHandler;
import javafx.scene.*;
import javafx.scene.input.*;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.*;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class Tetris extends Application {
  public static final int MOVE_AMOUNT = 25;
  public static final int SIZE = 25;
  public static int XLIMIT = SIZE * 10;
  public static int YLIMIT = SIZE * 24;
  public static int[][] GRID = new int[XLIMIT/SIZE][YLIMIT/SIZE];
  private static Pane group = new Pane();
  private static Shape object;
  private static Scene scene = new Scene(group, XLIMIT, YLIMIT);

  public static void main(String[] args) { launch(args); }
  @Override public void start(Stage stage) throws Exception {
    for(int[] a: GRID){
        Arrays.fill(a, 0);
    }
    for(int i = 0; i <= XLIMIT; i+= SIZE){
        Line a = new Line(i, 0, i, YLIMIT);
        group.getChildren().add(a);
    }
    for(int i = 0; i <= YLIMIT; i+= SIZE){
        Line a = new Line(0, i, XLIMIT, i);
        group.getChildren().add(a);
    }
    for(int i = 0; i <= YLIMIT; i+= SIZE){
        Text a = new Text("" + i);
        a.setY(i);
        group.getChildren().add(a);
    }
    for(int i = SIZE; i < XLIMIT; i+= SIZE){
        Text a = new Text("" + i);
        a.setY(10);
        a.setX(i);
        group.getChildren().add(a);
    }
    Shape a = TetrisHolder.createRect();
    group.getChildren().addAll(a.a, a.b, a.c, a.d);
    moveOnKeyPress(scene, a.a, a.b, a.c, a.d);
    object = a;
    stage.setScene(scene);
    stage.show();
    Timer myTimer=new Timer();
    TimerTask task =new TimerTask() {
      @Override
      public void run() {
          Platform.runLater(new Runnable(){
              public void run(){
                  CheckDown(object);
              }
          });
        }
      };
      myTimer.schedule(task,0,300);

    }

  private void moveOnKeyPress(Scene scene, Rectangle rect, Rectangle rect2, Rectangle rect3, Rectangle rect4) {
    scene.setOnKeyPressed(new EventHandler<KeyEvent>() {
      @Override public void handle(KeyEvent event) {
        Shape shape = new Shape(rect, rect2, rect3, rect4);
        switch (event.getCode()) {
          case RIGHT: 
              TetrisHolder.CheckRight(shape);
              break;
          case DOWN:  
              CheckDown(shape);
              break;
          case LEFT:  
              TetrisHolder.CheckLeft(shape);
              break;
          case UP:
              //TetrisHolder.CheckTurn(shape);
              break;
        }
      }
    });
  }

  private void CheckTurn(Shape shape){

  }

  private void DeleteRows(Pane pane){
      ArrayList<Node> rects = new ArrayList<Node>();
      ArrayList<Integer> lines = new ArrayList<Integer>();
      int full = 0;
      for(int i = 0; i < GRID[0].length; i++){
          for(int j = 0; j < GRID.length; j++){
              if(GRID[j][i] == 1)
                  full++;
          }
          if(full == GRID.length)
              lines.add(i/*+lines.size()*/);
          full = 0;
      }
      for(Node node: pane.getChildren()) {
           if(node instanceof Rectangle) {
                rects.add(node);
            }
        }
      if(lines.size() > 0)
          do{
              for(Node node: rects){
                  Rectangle a = (Rectangle)node;
                  if(a.getY() == lines.get(0)*SIZE){
                      GRID[(int)a.getX()/SIZE][(int)a.getY()/SIZE] = 0;
                      pane.getChildren().remove(node);
                  }
                  if(a.getY() < lines.get(0)*SIZE){
                      GRID[(int)a.getX()/SIZE][(int)a.getY()/SIZE] = 0;
                      a.setY(a.getY() + SIZE);
                      GRID[(int)a.getX()/SIZE][(int)a.getY()/SIZE] = 1;
                  }
              }
              lines.remove(0);
              rects.clear();
              for(Node node: pane.getChildren()) {
                   if(node instanceof Rectangle) {
                        rects.add(node);
                    }
                }
          } while(lines.size() > 0);
  }

  private void CheckDown(Shape shape){
      if((shape.c.getY() == YLIMIT - SIZE) || checkA(shape) || checkB(shape) || checkC(shape) || checkD(shape)){
          GRID[(int)shape.a.getX()/SIZE][(int)shape.a.getY()/SIZE] = 1;
          GRID[(int)shape.b.getX()/SIZE][(int)shape.b.getY()/SIZE] = 1;
          GRID[(int)shape.c.getX()/SIZE][(int)shape.c.getY()/SIZE] = 1;
          GRID[(int)shape.d.getX()/SIZE][(int)shape.d.getY()/SIZE] = 1;
          DeleteRows(group);
          Shape a = TetrisHolder.createRect();
          object = a;
          group.getChildren().addAll(a.a, a.b, a.c, a.d);
          moveOnKeyPress(shape.a.getScene(), a.a, a.b, a.c, a.d);
          }
      if(shape.c.getY() + MOVE_AMOUNT < YLIMIT){
          int checka = GRID[(int)shape.a.getX()/SIZE][((int)shape.a.getY()/SIZE) + 1];
          int checkb = GRID[(int)shape.b.getX()/SIZE][((int)shape.b.getY()/SIZE) + 1];
          int checkc = GRID[(int)shape.c.getX()/SIZE][((int)shape.c.getY()/SIZE) + 1];
          int checkd = GRID[(int)shape.d.getX()/SIZE][((int)shape.d.getY()/SIZE) + 1];
          if(checka == 0 && checka == checkb && checkb == checkc && checkc == checkd){
              shape.a.setY(shape.a.getY() + MOVE_AMOUNT);
              shape.b.setY(shape.b.getY() + MOVE_AMOUNT);
              shape.c.setY(shape.c.getY() + MOVE_AMOUNT);
              shape.d.setY(shape.d.getY() + MOVE_AMOUNT);
              }
          }
      }

  private boolean checkA(Shape shape){
      return (GRID[(int)shape.a.getX()/SIZE][((int)shape.a.getY()/SIZE) + 1] == 1);
  }
  private boolean checkB(Shape shape){
      return (GRID[(int)shape.b.getX()/SIZE][((int)shape.b.getY()/SIZE) + 1] == 1);
  }
  private boolean checkC(Shape shape){
      return (GRID[(int)shape.c.getX()/SIZE][((int)shape.c.getY()/SIZE) + 1] == 1);
  }
  private boolean checkD(Shape shape){
      return (GRID[(int)shape.d.getX()/SIZE][((int)shape.d.getY()/SIZE) + 1] == 1);
  }
}

这是代码。问题出在DeleteRows()方法。

0 个答案:

没有答案