增加首頁和音樂完成期末作品
import processing.sound.*;
SoundFile bgMusic; int x = 500; int y = 500; // 手部初始坐標 float apple_x = 150; float apple_y = 300; // 蘋果初始坐標 float bomb_x = 500; float bomb_y = 300; // 炸彈初始坐標 float banana_x = 250; float banana_y = 300; int a = 0; // 初始分數 int b; int c = 600; PImage tree; PImage apple; PImage hand; PImage bomb; PImage banana; int gameState = 0; void setup() { size(640, 696); tree = loadImage("tree.jpg"); apple = loadImage("apple.png"); banana = loadImage("banana.png"); hand = loadImage("hand.png"); bomb = loadImage("bomb.png"); // 設置遊戲初始狀態 gameState = 0; // 加載背景音樂文件 bgMusic = new SoundFile(this, "music.mp3"); } void draw() { frameRate(20); background(tree); if (gameState == 0) { drawStartScreen(); } else if (gameState == 1) { // 播放背景音樂 if (!bgMusic.isPlaying()) { bgMusic.play(); } // 遊戲進行中的畫面 move(); image(hand, x, y); image(apple, apple_x, apple_y); image(banana, banana_x, banana_y); image(bomb, bomb_x, bomb_y); hand.resize(100, 100); apple.resize(200, 164); bomb.resize(100, 100); banana.resize(100, 100); over(); font(); if (sqrt((apple_x - x)*(apple_x - x) + (apple_y - y)*(apple_y - y)) < 25) { apple(); bomb(); banana(); a += 10; } if (sqrt((banana_x - x)*(banana_x - x) + (banana_y - y)*(banana_y - y)) < 25) { apple(); bomb(); banana(); a += 20; } if (sqrt((bomb_x - x)*(bomb_x - x) + (bomb_y - y)*(bomb_y - y)) < 25) { apple(); bomb(); banana(); a -= 20; } } } void drawStartScreen() { fill(0); textSize(30); textAlign(CENTER, CENTER); text("Fruit Catcher", width/2, height/2 - 20); rectMode(CENTER); rect(width/2, height/2 + 20, 150, 50); fill(255); textSize(20); text("Start Game", width/2, height/2 + 20); } void mousePressed() { if (gameState == 0 && mouseX > width/2 - 75 && mouseX < width/2 + 75 && mouseY > height/2 + 5 && mouseY < height/2 + 35) { gameState = 1; // 切換到遊戲進行中的狀態 } } void move() { if (keyPressed) { if (key == 'a' && x != 0) { x -= 10; } if (key == 'd' && x != 600) { x += 10; } if (key == 'w' && y != 0) { y -= 10; } if (key == 's' && y != 600) { y += 10; } if (key == 'q') { exit(); } } } void apple() { apple_x = random(50, 550); apple_y = random(50, 400); } void bomb() { bomb_x = random(50, 550); bomb_y = random(50, 400); } void banana() { banana_x = random(50, 550); banana_y = random(50, 400); } void font() { fill(0); text(a, 10, 50); text(b, 550, 50); } void over() { c -= 1; b = c / 20; if (c == 0) { fill(20, 40, 240); noStroke(); rect(100, 400, 450, 60); fill(0); String s = "Your score is"; text(s, 110, 440); text(a, 450, 440); stop(); } }
沒有留言:
張貼留言