2023年12月7日 星期四

互動技術 week13

 week13

加上時間,分數,地鼠隨機出現在洞的位置

小問題:點地鼠計分時間會重製

PImage img, imgb;
int state = 0;
int holeCount = 6; // 黑洞數量
float[] holeX = new float[holeCount]; // 黑洞的X位置
float[] holeY = new float[holeCount]; // 黑洞的Y位置
float moleX, moleY; // 地鼠圖片位置
boolean moleClicked = false; // 判斷地鼠是否被點擊
int score = 0; // 分數
int timeLeft = 30; // 初始時間為30秒
int lastTime = 0;

void setup() {
  size(1200, 800);

  // 黑洞位置的設置
  holeX[0] = width / 5;
  holeY[0] = height / 2.5;
  holeX[1] = width / 2;
  holeY[1] = height / 2.5;
  holeX[2] = width / 1.25;
  holeY[2] = height / 2.5;
  holeX[3] = width / 5;
  holeY[3] = height / 1.25;
  holeX[4] = width / 2;
  holeY[4] = height / 1.25;
  holeX[5] = width / 1.25;
  holeY[5] = height / 1.25;

  resetMolePosition(); // 初始化地鼠位置
}

void draw() {
  if (state == 0) {
    drawStartScreen();
  } else if (state == 1) {
    frameRate(5);
    drawCircles();
    if (!moleClicked) {
      img = loadImage("gopher.png");
      image(img, moleX - 75, moleY - 120, 150, 150); // 地鼠圖片位置
    }
    displayScore();
    displayTimer();
    if (millis() - lastTime > 1000) {
      lastTime = millis();
      timeLeft--;
      if (timeLeft <= 0) {
        state = 0; // 時間到,遊戲結束
        // 可以在這裡處理時間到的相應操作,比如顯示遊戲結束畫面或其他操作
      }
    }
  }
}

void drawStartScreen() {
  background(255);
  fill(0);
  textSize(50);
  textAlign(CENTER, CENTER);
  text("START GAME", width / 2, height / 2);
}

void drawCircles() {
  imgb = loadImage("grass.png");
  image(imgb, 0, 0, width, height);
  // 繪製所有黑洞
  for (int i = 0; i < holeCount; i++) {
    ellipse(holeX[i], holeY[i], 150, 50);
  }
}

void displayScore() {
  fill(0);
  textSize(40);
  textAlign(LEFT);
  text("Score: " + score, 20, 40);
}

void displayTimer() {
  fill(0);
  textSize(30);
  textAlign(RIGHT);
  text("Time: " + timeLeft, width - 20, 40);
}

void resetMolePosition() {
  int randomIndex = int(random(holeCount)); // 隨機選擇一個黑洞位置
  moleX = holeX[randomIndex];
  moleY = holeY[randomIndex];
  moleClicked = false;
  timeLeft = 30; // 重置時間為30秒
}

void mousePressed() {
  if (state == 0 && mouseX > width / 2 - 100 && mouseX < width / 2 + 100 && mouseY > height / 2 - 25 && mouseY < height / 2 + 25) {
    state = 1;
  } else if (state == 1 && !moleClicked) {
    for (int i = 0; i < holeCount; i++) {
      if (dist(mouseX, mouseY, holeX[i], holeY[i]) < 75 && dist(mouseX, mouseY, moleX, moleY) < 75) {
        // 判斷是否點擊到地鼠且地鼠未被點擊
        moleClicked = true;
        score++;
        resetMolePosition(); // 重新設置地鼠位置
        break; 
      }
    }
  }
}



沒有留言:

張貼留言