今天要新增關卡倒數計時、還有解決氣球左右移動會卡住的問題
程式碼
boolean gameStarted = false; // 遊戲是否已經開始 // 初始畫面 int numCircles = 20; float[] circleX = new float[numCircles]; float[] circleY = new float[numCircles]; float[] circleRadius = new float[numCircles]; float[] circleSpeedX = new float[numCircles]; float[] circleSpeedY = new float[numCircles]; // 氣球的設定 int numBalloons = 5; float[] x = new float[numBalloons]; int[] y = new int[numBalloons]; int radius = 25; int[] clicks = new int[numBalloons]; color[] colors = new color[numBalloons]; boolean[] visible = new boolean[numBalloons]; float[] balloonSpeedX = new float[numBalloons]; // 新增 x 方向的移動速度 PFont myFont; int score = 0; int state = 0; // 0: start, 1: first, 2: second PImage img; void setup() { size(500, 450); // 初始化背景中的圓圈 for (int i = 0; i < numCircles; i++) { circleX[i] = random(width); circleY[i] = random(height); circleRadius[i] = random(10, 30); circleSpeedX[i] = random(1, 3); circleSpeedY[i] = random(1, 3); } img = loadImage("frog.jpg"); // 初始化氣球遊戲 initAllBalloon(); myFont = createFont("粉圓體.ttf", 100); textFont(myFont); } void drawButton() { fill(#FFCA95); rect(370, 390, 120, 40,30,30,30,30); fill(0); textSize(25); text("再來一次", 430, 410); } void initAllBalloon() { for (int i = 0; i < numBalloons; i++) { initBalloon(i); } } int time = 15; int time2 = 20; void draw() { if (!gameStarted && state == 0) { // 繪製背景中的圓圈 background(0); for (int i = 0; i < numCircles; i++) { noFill(); stroke(173, 216, 230); // 水藍色 strokeWeight(1.5); // 線的粗細 ellipse(circleX[i], circleY[i], circleRadius[i] * 2, circleRadius[i] * 2); circleX[i] += circleSpeedX[i]; circleY[i] += circleSpeedY[i]; checkCircleBoundaries(i); } // 在這裡添加其他進入遊戲前的畫面元素 fill(255); textSize(45); textAlign(CENTER, CENTER); text("點擊開始遊戲", width / 2, height / 2); } else if (state == 1) { // 第一關 background(#FFEEBF); fill(0); stroke(0); textSize(20); textAlign(CENTER, CENTER); text("Score: " + score, width / 2, 30); text("Time: " + time, width / 4, 30); if(frameCount % 60 == 0 && time > 0){ time--; } // 繪製氣球 for (int i = 0; i < numBalloons; i++) { if (visible[i]) { drawBalloon(i); } } if(time == 0){ state=3; } }else if (state == 2) { // 第二關 background(#ECD1FF); fill(0); stroke(0); textSize(20); textAlign(CENTER, CENTER); text("Score: " + score, width / 2, 30); text("Time: " + time2, width / 4, 30); if(frameCount % 60 == 0 && time2 > 0){ time2--; } if(time2 == 0){ state=3; } // 繪製氣球 for (int i = 0; i < numBalloons; i++) { if (visible[i]) { drawBalloon(i); // 更新氣球的 x 座標,實現左右移動 x[i] += balloonSpeedX[i]; //checkBalloonBoundaries(i); // 限制氣球左右移動範圍在 100 到 250 之間 if (x[i] > 400 - radius) { x[i] = 400 - radius; balloonSpeedX[i] = -balloonSpeedX[i]; } else if (x[i] < 50 + radius) { x[i] = 50 + radius; balloonSpeedX[i] = -balloonSpeedX[i]; } } } }else if (state == 3) { // 遊戲結束畫面 image(img, 0, 0, 600, 450); fill(#FFD1E2); rect(150, 185, 200, 80); fill(0); textSize(45); text("遊戲結束", width / 2, height / 2); drawButton(); } } void mousePressed() { if (!gameStarted && state == 0) { // 開始遊戲 gameStarted = true; state++; for (int i = 0; i < numCircles; i++) { circleX[i] = random(width); circleY[i] = random(height); circleRadius[i] = random(5, 30); circleSpeedX[i] = random(1, 3); circleSpeedY[i] = random(1, 3); } } else { for (int i = 0; i < numBalloons; i++) { float distance = dist(mouseX, mouseY, x[i], y[i]); if (distance < radius && clicks[i] < 3 && visible[i]) { clicks[i]++; if (clicks[i] == 1) { colors[i] = color(255, 165, 0); // 橘色 } else if (clicks[i] == 2) { colors[i] = color(0, 128, 0); // 綠色 } else if (clicks[i] == 3) { visible[i] = false; score += 10; } } } } if (score == 50) { score = 0; state ++; initAllBalloon(); } if (mouseX > 370 && mouseX < 370 +120 && mouseY > 390 && mouseY < 390 + 40) { // 如果返回按钮被點擊 state = 0; gameStarted = false; initAllBalloon(); } } void drawBalloon(int a) { // 繪製氣球 fill(colors[a]); ellipse(x[a], y[a], radius * 2, radius * 2); line(x[a], y[a] + radius, x[a], y[a] + radius * 2); fill(0); triangle(x[a] - 2.5, y[a] + 30, x[a], y[a] + 26, x[a] + 2.5, y[a] + 30); //text(""+balloonSpeedX[a]+" "+x[a] , x[a], y[a]); } void initBalloon(int b) { boolean overlapping; do { overlapping = false; x[b] = int(random(60, 450)); y[b] = int(random(60, 375)); for (int j = 0; j < numBalloons; j++) { if (j != b && visible[j]) { float distance = dist(x[b], y[b], x[j], y[j]); if (distance < radius * 2) { overlapping = true; break; } } } } while (overlapping); clicks[b] = 0; colors[b] = color(255, 0, 0); // 初始顏色為紅色 visible[b] = true; balloonSpeedX[b] = random(-2, 2); // 初始化 x 方向的移動速度 } /*void checkBalloonBoundaries(int b) { // 檢查氣球是否超出邊界,如果是就往反方向移動 if (x[b] > width - radius || x[b] < radius) { balloonSpeedX[b] = -balloonSpeedX[b]; } }*/ void checkCircleBoundaries(int c) { // 檢查圓圈是否超出邊界,如果是就往反方向移動 if (circleX[c] - circleRadius[c] < 0 || circleX[c] + circleRadius[c] > width) { circleSpeedX[c] *= -1; } if (circleY[c] - circleRadius[c] < 0 || circleY[c] + circleRadius[c] > height) { circleSpeedY[c] *= -1; } }
發現到有些balloon無法移動的原因是
多了
void checkBalloonBoundaries(int b) {
// 檢查氣球是否超出邊界,如果是就往反方向移動
if (x[b] > width - radius || x[b] < radius) {
balloonSpeedX[b] = -balloonSpeedX[b];
}
}
會使balloon重複一直往右往左(因為符合條件所以重複,變成無法動)
再來就是將x從用int宣告改成用float宣告
倒數計時



沒有留言:
張貼留言