PFont chineseFont;
// 引入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); // 設置畫布大小
chineseFont = createFont("MicrosoftYaHeiLight-48.vlw", 32);
textFont(chineseFont);
// 黑洞位置的設置
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(); // 繪製遊戲初始畫面
drawStart2Screen();
}
if (state == 1) {
drawChooseScreen();
}
if (state == 6) {
drawRuleScreen();
}
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 (score >= 30) {
state = 4;
score = 0;
}
if (timeLeft <= 0 && score < 30){
state = 5;
score = 0;
}
}
}
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 (score >= 30) {
state = 4;
score = 0;
}
if (hardtimeLeft <= 0 && score < 30){
state = 5;
score = 0;
}
}
}
else if (state == 4){
drawWinScreen();
}
else if (state == 5){
drawloseScreen();
}
}
void drawStartScreen() {
background(255); // 背景設置為白色
fill(0); // 文字填充為黑色
textSize(50); // 設置文字大小
textAlign(CENTER, CENTER); // 文字居中對齊
img = loadImage("gopher.png"); // 預先載入地鼠圖片
image(img, width / 2 - 75, height / 2+50, 150, 150);
text("START GAME", width / 2, height / 2); // 顯示開始遊戲文字
}
void drawStart2Screen() {
int r = int(random(256));
int g = int(random(256));
int b = int(random(256));
fill(r, g, b);
textSize(200); // 設置文字大小
textAlign(CENTER, CENTER); // 文字居中對齊
text("打地鼠", width / 2, height / 4); // 顯示開始遊戲文字
}
void drawRuleScreen() {
background(255); // 背景設置為白色
fill(0); // 文字填充為黑色
textSize(50); // 設置文字大小
textAlign(CENTER, CENTER); // 文字居中對齊
text("規則說明", width/2, height/5);
text("簡單:在45秒內獲得30分", width/2, height/2.25);
text("困難:在30秒內獲得30分", width/2, height/1.5);
textSize(60);
fill(255, 0, 0); // Change color for better visibility
text("下一頁", width/2, height - 60);
}
void drawChooseScreen() {
background(255); // 背景設置為白色
fill(0); // 文字填充為黑色
textSize(50); // 設置文字大小
textAlign(CENTER, CENTER); // 文字居中對齊
text("簡單", width/3.5 , height / 2); // 顯示開始遊戲文字
text("困難", 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 = 0; // 返回狀態 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; // 返回狀態 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 = 6; // Start the game
} else if (state == 6 && mousePressed && mouseX > width / 2 - 50 && mouseX < width / 2 + 50 && mouseY > height - 70 && mouseY < height - 30) {
state = 1; // Proceed to the next page
} 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 Mode selected
} else if (mouseX > width / 1.5 - 50 && mouseX < width / 1.5 + 50 && mouseY > height / 2 - 25 && mouseY < height / 2 + 25) {
state = 3; // HARD Mode selected
}
}
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循環,確保只計分一次
}
}
}
}
首頁
沒有留言:
張貼留言