Week15
玩家實際遊玩後會發現,警察誤捕民眾的錯誤訊息不明顯,故新增"警察觸碰到民眾時顯示一秒紅色畫面"
int x = 500, y = 500; // 警察初始座標
float thief_x = 150, thief_y = 300; // 小偷初始座標
float people_x = 500, people_y = 300; // 民眾初始座標
int a = 0; // 初始分數
int b; //剩餘遊戲時間
int c = 900; // 900豪秒=15s
PImage road, thiefImg, police, peopleImg;int flashDuration = 30; // 紅色畫面持續的幀數
int flashTimer = 0; // 控制紅色畫面持續時間的計時器
boolean flashActive = false; // 標誌,表示紅色畫面是否啟用
void setup() {
size(800, 800);
road = loadImage("road.jpg");
thief = loadImage("thief.png");
police = loadImage("police.png");
people = loadImage("people.png");
}
void draw() {
frameRate(35);
background(road); // 此處使用全域的 road 變數
if (flashActive) {
// 顯示紅色畫面
fill(255, 0, 0, 150); // 使用半透明的紅色
noStroke();
rect(0, 0, width, height);
flashTimer--;
// 檢查紅色畫面是否持續時間結束
if (flashTimer <= 0) {
flashActive = false;
flashTimer = flashDuration;
}
}
move();
image(police, x, y);
image(thief, thief_x, thief_y);
image(people, people_x, people_y);
over();
if (sqrt((thief_x - x) * (thief_x - x) + (thief_y - y) * (thief_y - y)) < 25) {
thief();
people();
a += 20; // 計算警察與小偷距離判斷觸碰小偷得分
}
if (sqrt((people_x - x) * (people_x - x) + (people_y - y) * (people_y - y)) < 25) {
thief();
people();
a -= 50;
flashActive = true; // 當警察捕捉到民眾時啟用紅色畫面
}
font();
}
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;
}
}
}
void thief() {
thief_x = random(50, 600);
thief_y = random(50, 350);
}
void people() {
people_x = random(150, 600);
people_y = random(50, 400);
}
void font() {
fill(0);
textSize(24);
text("Score: " + a, 10, 50);
text("Time: " + b + "s", 550, 50);
}
void over() {
c -= 1;
b = c / 60;
if (c == 0) {
fill(25, 40, 240);
noStroke();
rect(200, 400, 450, 60);
fill(0);
String s = "Your score is: ";
text(s, 220, 440);
text(a, 450, 440);
noLoop();
}
}
覺得遊戲還是太單調,想讓小偷及民眾會自由四處移動
int x = 500, y = 500; // 警察初始座標 float thief_x = 150, thief_y = 300; // 小偷初始座標 float thief_speedX = 3.0; // 小偷的水平速度 float thief_speedY = 1.5; // 小偷的垂直速度 float people_x = 500, people_y = 300; // 民眾初始座標 float people_speedX = -2.0; // 民眾的水平速度 float people_speedY = 2.0; // 民眾的垂直速度 int a = 0; // 初始分數 int b; //剩餘遊戲時間 int c = 900; // 900豪秒=15s PImage road, thief, police, people; int flashDuration = 30; // 紅色畫面持續的幀數 int flashTimer = 0; // 控制紅色畫面持續時間的計時器 boolean flashActive = false; // 標誌,表示紅色畫面是否啟用 void setup() { size(800, 800); road = loadImage("road.jpg"); thief = loadImage("thief.png"); police = loadImage("police.png"); people = loadImage("people.png"); } void draw() { frameRate(35); background(road); if (flashActive) { // 顯示紅色畫面 fill(255, 0, 0, 150); // 使用半透明的紅色 noStroke(); rect(0, 0, width, height); flashTimer--; // 檢查紅色畫面是否持續時間結束 if (flashTimer <= 0) { flashActive = false; flashTimer = flashDuration; } } move(); image(police, x, y); moveThief(); image(thief, thief_x, thief_y); movePeople(); image(people, people_x, people_y); over(); if (sqrt((thief_x - x) * (thief_x - x) + (thief_y - y) * (thief_y - y)) < 25) { thief(); people(); a += 20; } if (sqrt((people_x - x) * (people_x - x) + (people_y - y) * (people_y - y)) < 25) { thief(); people(); a -= 50; flashActive = true; } font(); } 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; } } } void moveThief() { thief_x += thief_speedX; thief_y += thief_speedY; // 檢查小偷是否超出畫面範圍,如果是,則反向移動 if (thief_x < 0 || thief_x > width - thief.width) { thief_speedX *= -1; } if (thief_y < 0 || thief_y > height - thief.height) { thief_speedY *= -1; } } void movePeople() { people_x += people_speedX; people_y += people_speedY; // 檢查民眾是否超出畫面範圍,如果是,則反向移動 if (people_x < 0 || people_x > width - people.width) { people_speedX *= -1; } if (people_y < 0 || people_y > height - people.height) { people_speedY *= -1; } } void thief() { // 隨機刷新小偷位置 thief_x = random(50, 600); thief_y = random(50, 350); } void people() { // 隨機刷新民眾位置 people_x = random(150, 600); people_y = random(50, 400); } void font() { fill(0); textSize(24); text("Score: " + a, 10, 50); text("Time: " + b + "s", 550, 50); } void over() { c -= 1; b = c / 60; if (c == 0) { fill(25, 40, 240); noStroke(); rect(200, 400, 450, 60); fill(0); String s = "Your score is: "; text(s, 220, 440); text(a, 450, 440); noLoop(); } }
沒有留言:
張貼留言