// 引入Processing中所需的圖片庫
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 = 45;
int hardtimeLeft = 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(); // 繪製遊戲初始畫面
}
if (state == 1) {
drawChooseScreen();
}
if (state == 2) {
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 && score >= 30) {
state = 4;
}
if (timeLeft <= 0 && score <= 29){
state = 5;
}
}
}
if (state == 3) {
frameRate(5);
drawCircles(); // 繪製黑洞圖案
if (!moleClicked) {
img = loadImage("gopher.png");
image(img, moleX - 75, moleY - 120, 150, 150); // 繪製地鼠圖片
}
displayScore(); // 顯示分數
displayHardTimer(); // 顯示計時器
if (millis() - lastTime > 1000) { // 計時器每秒減少一次
lastTime = millis();
hardtimeLeft--;
if (hardtimeLeft <= 0 && score >= 30) {
state = 4;
}
if (hardtimeLeft <= 0 && score <= 29){
state = 5;
}
}
}
else if (state == 4){
drawWinScreen();
}
else if (state == 5){
drawloseScreen();
}
}
void drawStartScreen() {
background(255); // 背景設置為白色
fill(0); // 文字填充為黑色
textSize(50); // 設置文字大小
textAlign(CENTER, CENTER); // 文字居中對齊
text("START GAME", width / 2, height / 2); // 顯示開始遊戲文字
}
void drawChooseScreen() {
background(255); // 背景設置為白色
fill(0); // 文字填充為黑色
textSize(50); // 設置文字大小
textAlign(CENTER, CENTER); // 文字居中對齊
text("EASY", width/3.5 , height / 2); // 顯示開始遊戲文字
text("HARD", width/1.5 , height / 2); // 顯示開始遊戲文字
}
void drawWinScreen() {
background(255); // 背景設置為白色
fill(0); // 文字填充為黑色
textSize(50); // 設置文字大小
textAlign(CENTER, CENTER); // 文字居中對齊
text("YOU WIN", width / 2, height / 2); // 顯示開始遊戲文字
// 顯示返回鍵
fill(0); // 返回鍵填充顏色
rect(width - 200, height - 100, 150, 50); // 返回鍵的矩形
fill(255); // 返回鍵上文字的顏色
textAlign(CENTER, CENTER);
text("MENU", width - 125, height - 75); // 返回鍵上的文字
// 檢測滑鼠點擊事件
if (mousePressed && mouseX > width - 200 && mouseX < width - 50 && mouseY > height - 100 && mouseY < height - 50) {
state = 1; // 返回狀態 0
}
}
void drawloseScreen() {
background(255, 0, 0); // 背景設置為紅色
fill(0); // 文字填充為黑色
textSize(50); // 設置文字大小
textAlign(CENTER, CENTER); // 文字居中對齊
text("YOU LOSE", width / 2, height / 2); // 顯示失敗文字
// 顯示返回鍵
fill(0); // 返回鍵填充顏色
rect(width - 200, height - 100, 150, 50); // 返回鍵的矩形
fill(255); // 返回鍵上文字的顏色
textAlign(CENTER, CENTER);
text("MENU", width - 125, height - 75); // 返回鍵上的文字
// 檢測滑鼠點擊事件
if (mousePressed && mouseX > width - 200 && mouseX < width - 50 && mouseY > height - 100 && mouseY < height - 50) {
state = 0; // 返回主菜單狀態
}
}
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(50); // 設置文字大小
textAlign(LEFT); // 文字左對齊
text("Score: " + score, 20, 40); // 顯示分數
}
void displayTimer() {
fill(0); // 文字填充為黑色
textSize(50); // 設置文字大小
textAlign(RIGHT); // 文字右對齊
text("Time: " + timeLeft, width - 20, 40); // 顯示剩餘時間
}
void displayHardTimer() {
fill(0); // 文字填充為黑色
textSize(50); // 設置文字大小
textAlign(RIGHT); // 文字右對齊
text("Time: " + hardtimeLeft, width - 20, 40); // 顯示剩餘時間
}
void resetMolePosition() {
int randomIndex = int(random(holeCount)); // 隨機選擇一個黑洞位置
moleX = holeX[randomIndex];
moleY = holeY[randomIndex];
moleClicked = false;
}
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) {
if (mouseX > width / 3.5 - 50 && mouseX < width / 3.5 + 50 && mouseY > height / 2 - 25 && mouseY < height / 2 + 25) {
state = 2; // EASY 選擇
} else if (mouseX > width / 1.5 - 50 && mouseX < width / 1.5 + 50 && mouseY > height / 2 - 25 && mouseY < height / 2 + 25) {
state = 3; // HARD 選擇
}
} else if (state == 2 && !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; // 跳出for循環,確保只計分一次
}
}
} else if (state == 3 && !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; // 跳出for循環,確保只計分一次
}
}
}
}
贏的介面
輸的介面
沒有留言:
張貼留言