You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为Processing版俄罗斯方块添加玩家可控的方块承接平台?

技术求助:Processing实现「俄罗斯方块堆塔器」的平台与碰撞检测

我是一名学生,接到任务需用Processing制作一款带创新点的复古街机游戏(如Pong、Space Invaders、Breakout、俄罗斯方块等)。我构思的创新点是制作「俄罗斯方块堆塔器」:玩家无需控制下落的方块,仅通过左右移动平台来接住方块,最终堆叠成塔。目前我是编程新手,通过教程完成了基础俄罗斯方块代码且运行正常,但在添加平台及实现方块与平台的碰撞检测上遇到困难,恳请技术指导。

以下是我的基础俄罗斯方块代码:

int[][] board = new int[40][40]; // 40 columns, 40 rows
int cols = 40;
int rows = 40;
color col = color(random(70, 255), random(70, 255), random(70, 255));
int[][][] pieces = {
  {{1, 1, 1, 1}}, // I
  {{1, 1, 0}, {0, 1, 1}}, // Z
  {{0, 1, 1}, {1, 1, 0}}, // S
  {{1, 1}, {1, 1}}, // O
  {{1, 1, 1}, {0, 1, 0}}, // T
  {{0, 1, 0}, {1, 1, 1}}, // L
  {{1, 0, 0}, {1, 1, 1}}   // J
};
int[][] currentPiece;
int currentX, currentY;
int score = 0;
int lines = 0;
int timer = 0;
int level = 1;

void setup() {
  size(800, 800);
  frameRate(100); //to make the game a bit faster
  currentPiece = pieces[int(random(7))];
  currentX = int(random(39));
  currentY = 0;
}

void draw() {
  background(255);
  drawBoard();
  drawPieceFunction();
  drawText();
}

void keyPressed() {
  if (keyCode == LEFT) {
    if (!checkCollision(currentPiece, currentX-1, currentY)) {
      currentX--;
    }
  } else if (keyCode == RIGHT) {
    if (!checkCollision(currentPiece, currentX+1, currentY)) {
      currentX++;
    }
  } else if (keyCode == UP) {
    rotatePiece();
  } else if (keyCode == DOWN) {
    if (!checkCollision(currentPiece, currentX, currentY+1)) {
      currentY++;
    }
  }
}

void drawBoard() {
  for (int x = 0; x < cols; x++) {
    for (int y = 0; y < rows; y++) {
      if (board[x][y] != 0) {
        fill(0);
        rect(x*20, y*20, 20, 20);
      }
    }
  }
}

void drawPiece(int[][] piece, int x, int y) {
  for (int px = 0; px < piece.length; px++) {
    for (int py = 0; py < piece[0].length; py++) {
      if (piece[px][py] != 0) {
        fill(col);
        rect((x+px)*20, (y+py)*20, 20, 20);
      }
    }
  }
}

boolean checkCollision(int[][] piece, int x, int y) {
  for (int px = 0; px < piece.length; px++) {
    for (int py = 0; py < piece[0].length; py++) {
      if (piece[px][py] != 0) {
        if (x+px < 0 || x+px >= cols || y+py >= rows || board[x+px][y+py] != 0) {
          return true;
        }
      }
    }
  }
  return false;
}

void rotatePiece() {
  int[][] newPiece = new int[currentPiece[0].length][currentPiece.length];
  for (int x = 0; x < currentPiece.length; x++) {
    for (int y = 0; y < currentPiece[0].length; y++) {
      newPiece[y][currentPiece.length-1-x] = currentPiece[x][y];
    }
  }
  if (!checkCollision(newPiece, currentX, currentY)) {
    currentPiece = newPiece;
  }
}

void addToBoard(int[][] piece, int x, int y) {
  for (int px = 0; px < piece.length; px++) {
    for (int py = 0; py < piece[0].length; py++) {
      if (piece[px][py] != 0) {
        board[x+px][y+py] = piece[px][py];
      }
    }
  }
}

void clearLines() {
  int numLines = 0;
  for (int y = 0; y < rows; y++) {
    boolean full = true;
    for (int x = 0; x < cols; x++) {
      if (board[x][y] == 0) {
        full = false;
      }
    }
    if (full) {
      for (int yy = y; yy > 0; yy--) {
        for (int x = 0; x < rows; x++) {
          board[x][yy] = board[x][yy-1];
        }
      }
      numLines++;
    }
  }
  if (numLines > 0) {
    score += 100*numLines*numLines;
    lines += numLines;
    if (lines >= 10) {  // level up after 10 lines
      level++;
      lines -= 10;
    }
  }
}
void drawText() {  //draws the score, lines, level texts on the screen
  textAlign(LEFT);
  textSize(18);
  fill(112, 128, 144);
  text("Score: " + score, 10, height-40);
  text("Lines: " + lines, 10, height-15);
  text("Level: " + level, width-70, height-15);
}

void drawPieceFunction() {
  drawPiece(currentPiece, currentX, currentY);
  if (timer % (30 - level*2) == 0) {  // speed up over time
    currentY++;
    if (checkCollision(currentPiece, currentX, currentY)) {  // if collision, add piece to board and start new piece
      addToBoard(currentPiece, currentX, currentY-1);
      clearLines();
      currentPiece = pieces[int(random(7))];
      currentX = int(random(39));
      currentY = 0;
      if (checkCollision(currentPiece, currentX, currentY)) {  // if new piece collides, game over
        textSize(64);
        textAlign(CENTER);
        fill(112, 128, 144);
        text("Game Over", width/2, height/2);
        noLoop();
      }
    }
  }
  timer++;
}

内容的提问来源于stack exchange,提问作者cdy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 22:27:24