2024年1月4日 星期四

WEEK17

 WEEK17 期末


點箭頭(原理是打地鼠遊戲














最終執行畫面 




















以上為遊戲進行的畫面


最終程式:

PImage img, hammericon1, hammericon2, mouse1, mouse2, mainhole, button1, start, end, returnbutton;
int condition;
int time, score;
int[][] gopher =
  {
  {0, 0, 0},
  {0, 0, 0},
  {0, 0, 0}
};

// 生成箭頭的函式
void generateGopher() {
  int now = int(random(9)); // 隨機選擇位置
  gopher[now / 3][now % 3] = 30 + int(random(300)); // 設定箭頭出現的時間
}

// 初始化設定
void setup() {
  size(718, 664); // 設定視窗大小
  time = 15; // 遊戲時間
  score = 0; // 初始分數
  // 加載圖片資源
  start = loadImage("start.png");
  button1 = loadImage("button1.png");
  img = loadImage("background.png");
  hammericon1 = loadImage("hammericon1.png");
  hammericon2 = loadImage("hammericon2.png");
  mouse1 = loadImage("mouse01.png");
  mouse2 = loadImage("mouse02.png");
  mainhole = loadImage("mainhole.png");
  end = loadImage("end.png");
  returnbutton = loadImage("return.png");
  generateGopher(); // 生成初始的箭頭
}

// 繪製遊戲畫面
void draw() {
  if (condition == 0) {
    // 開始畫面
    imageMode(CORNER);
    image(start, 0, -42, 725, 720);
    image(button1, 210, 400, 280, 280);
  }
  if (condition == 1) {
    // 遊戲進行中
    background(255);
    image(img, 360, 350);
    imageMode(CENTER);
    score();
    time();
    for (int i = 0; i < 3; i++) {
      for (int j = 0; j < 3; j++) {
        // 繪製箭頭和九宮格
        if (gopher[i][j] > 0) {
          image(mouse1, 150 + j * 200, 250 + i * 150, 110, 100);
          gopher[i][j]--;
          if (gopher[i][j] == 0) generateGopher();
        } else if (gopher[i][j] < 0) {
          image(mouse2, 150 + j * 200, 250 + i * 150, 110, 100);
          gopher[i][j]++;
        } else {
          image(mainhole, 150 + j * 200, 260 + i * 150, 110, 100);
        }
      }
    }
  }
  // 遊戲結束畫面
  if (condition == 2) {
    image(end, 347, 340);
    fill(2);
    textSize(110);
    text(score, 310, 500);
    image(returnbutton, 650, 640, 200, 200);
  }
}

// 滑鼠點擊
void mousePressed() {
  if (condition == 0) {
    // 在開始畫面點擊開始遊戲
    if (mouseButton == LEFT && dist(360, 580, mouseX, mouseY) < 60) {
      condition = 1; // 開始遊戲
    }
  }
  if (condition == 2) {
    // 在遊戲結束畫面點擊返回
    if (mouseButton == LEFT && dist(650, 640, mouseX, mouseY) < 50) {
      condition = 0;
      time = 15;
      score = 0;
    }
  }
  int i = mouseY / 222, j = mouseX / 240;
  if (gopher[i][j] > 0) {
    gopher[i][j] = -60; // 點中箭頭
    score++;
  }
  if (gopher[i][j] < 0) {
    generateGopher(); // 重新生成箭頭
  }
}

// 顯示分數
void score() {
  fill(220);
  textSize(50);
  text("Score: " + score, 90, 105);
}

// 顯示時間
void time() {
  fill(0);
  textSize(30);
  text("Time: " + time, 502, 80);
  if (time > 0 && frameCount % 60 == 0) {
    time = time - 1;
  } else if (time == 0) {
    condition = 2; // 時間結束即遊戲結束
  }
}

沒有留言:

張貼留言