我如何洗牌这些块并做出赢/输的游戏状态?

问题描述 投票:0回答:1

我正在尝试在处理过程中对这个游戏进行编码,您必须按正确的顺序单击四个图块。如果您以正确的顺序单击图块,它应该显示“您赢了!”在控制台中,如果你搞砸了,它会说“再试一次”并重置。瓷砖应该在每次尝试开始时随机洗牌。

但是,在我点击完所有图块之前,游戏让我失败并重置。我该如何解决?这是我到目前为止所拥有的:

PImage[] images = new PImage[4];
int[] order = {1, 2, 3, 4};
int[] shuffledOrder;
int currentIndex = 0;
boolean gameWon = false;

void setup() {
  size(400, 400);
  images[0] = loadImage("1.jpg");
  images[1] = loadImage("2.jpg");
  images[2] = loadImage("3.jpg");
  images[3] = loadImage("4.jpg");
  
  shuffleImages();
}

void draw() {
  background(255);
  
  int imageSize = width / 2;
  
  // Display shuffled images in a 2x2 grid
  int index = 0;
  for (int i = 0; i < 2; i++) {
    for (int j = 0; j < 2; j++) {
      image(images[shuffledOrder[index] - 1], j * imageSize, i * imageSize, imageSize, imageSize);
      index++;
    }
  }
}

void mouseClicked() {
  int imageSize = width / 2;
  int clickedIndex = mouseY / imageSize * 2 + mouseX / imageSize;
  
  if (!gameWon) {
    if (clickedIndex == order[currentIndex]) {
      currentIndex++;
      if (currentIndex == 4) {
        gameWon = true;
        println("You won!");
        currentIndex = 0;
        shuffleImages();
      }
    } else {
      println("Try again");
      currentIndex = 0;
      shuffleImages();
    }
  }
}

void shuffleImages() {
  shuffledOrder = shuffleArray(order);
}

int[] shuffleArray(int[] array) {
  int[] newArray = array.clone();
  for (int i = newArray.length - 1; i > 0; i--) {
    int index = (int) random(i + 1);
    int temp = newArray[index];
    newArray[index] = newArray[i];
    newArray[i] = temp;
  }
  return newArray;
}

java processing sequence game-development
1个回答
0
投票

mouseClicked 函数中的一个 if else 大括号放错了位置

© www.soinside.com 2019 - 2024. All rights reserved.