Week13
確定完要使用貪食蛇作為主題,在網路上找了一推資料
最後找到一個之前有人做過的貪食蛇程式碼作為主體
https://github.com/paper/processing-snake/blob/master/snake/snake.pde
1.執行程式就直接開始遊戲
2.劃出蛇、食物
3.吃到紅點蛇會加長並分數會增加
4.蛇碰到自己身體會死掉
5.蛇碰到邊緣會死掉
6.進入死亡介面有你這場的分數和歷史上最高的分數
7.進入死亡介面按R重新開始新的一局
障礙物:
直接問chatGPT,蛇碰到會死掉、不能直接生在蛇、食物身上、在關卡內隨機生成
ArrayList<int[]> obstacles = new ArrayList<int[]>(); // 儲存障礙物座標的列表
// 檢查座標是否與障礙物和蛇的身體碰撞
boolean checkCollision(int checkX, int checkY) {
// 檢查是否與障礙物碰撞
for (int i = 0; i < obstacles.size(); i++) {
int obstacleX = obstacles.get(i)[0];
int obstacleY = obstacles.get(i)[1];
if (checkX == obstacleX && checkY == obstacleY) {
return true;
}
}
void generateRandomObstacles() {
// 產生障礙物的隨機座標
int obstacleX = int(random(1, 600 / w - 1)) * w;
int obstacleY = int(random(1, 600 / h - 1)) * h;
// 檢查障礙物位置是否有效
boolean isValid = checkObstacleValidity(obstacleX, obstacleY);
// 如果位置無效,重新產生座標
while (!isValid) {
obstacleX = int(random(1, 600 / w - 1)) * w;
obstacleY = int(random(1, 600 / h - 1)) * h;
isValid = checkObstacleValidity(obstacleX, obstacleY);
}
// 加入有效的障礙物座標到清單中
int[] obstacle = {obstacleX, obstacleY};
obstacles.add(obstacle);
Obstacles++;
}
boolean checkObstacleValidity(int obstacleX, int obstacleY) {
// 檢查障礙物是否與蛇頭發生碰撞
if (obstacleX == snakeHeadX && obstacleY == snakeHeadY) {
return false;
}
// 檢查障礙物是否在蛇的5個單位內
for (int i = 0; i < snakeLength; i++) {
float distance = dist(obstacleX, obstacleY, x[i], y[i]);
if (distance < 5 * w) {
return false;
}
}
return true;
}
// 檢查是否與蛇的身體碰撞
for (int i = 0; i < snakeLength; i++) {
if (checkX == x[i] && checkY == y[i]) {
return true;
}
}
return false;
}
void drawObstacles() {
fill(#333333); // 障礙物的顏色,可以根據需要更改
for (int i = 0; i < obstacles.size(); i++) {
int obstacleX = obstacles.get(i)[0];
int obstacleY = obstacles.get(i)[1];
rect(obstacleX, obstacleY, w, h);
}
}
boolean checkObstacleCollision() {
for (int i = 0; i < obstacles.size(); i++) {
int obstacleX = obstacles.get(i)[0];
int obstacleY = obstacles.get(i)[1];
if (snakeHeadX == obstacleX && snakeHeadY == obstacleY) {
return true; // 蛇頭碰到障礙物,遊戲結束
}
}
return false; // 沒有碰到障礙物
}
星星(黃色的點):
碰到後會使障礙物消失、在畫面上隨機生成、不能在蛇和食物和障礙物身上出生
void drawStar() {
// 星星的颜色
if (starExists) {
fill(#FFAA0A); // 設定星星的顏色,這裡使用黃色
rect(starX, starY, w, h);
}
// 標記星星存在
starKey=true;
}
void generateRandomStar() {
boolean starGenerated = false;
// 生成星星的隨機坐標,直到生成在蛇的五格內
while (!starGenerated) {
starX = int(random(1, 600 / w - 1)) * w;
starY = int(random(1, 600 / h - 1)) * h;
// 檢查星星是否在蛇的五格內
boolean insideSnake = false;
for (int i = 0; i < snakeLength; i++) {
if (starX == x[i] && starY == y[i]) {
insideSnake = true;
break;
}
}
// 如果星星不在蛇的五格內,標記星星已生成
if (!insideSnake) {
starGenerated = true;
}
}
// 標記星星存在
starExists = true;
}
突然有一天,我想到想把貪食蛇跟音樂做結合,
1.在開始畫面增加音樂
2.吃到食物後有音效
3.死亡時也有音效
4.加一些到達一定的分數播放不同的音樂,來增加想要重複遊玩的動機
最後我找到一個適合關卡設計的音樂
在前七個蛇吃到的食物都很正常
在第七個吃到食物後,會生出很多障礙物並且要吃到黃色的點來消除障礙物,增加遊玩的樂趣
而第四點我設置6種不同結束的音樂,來增加想要重複遊玩的動機
但是!
因為加入太多音樂導致執行時等待時間過久,正在考慮減少大量音樂。
期中的互動影片:
https://youtu.be/bBnG8iLA_eU?si=Cfh3mm4otKNNh7xj
沒有留言:
張貼留言