這周新增了剛開始的主畫面圖片,以及分數的計分,當勝利者的那一方得一分。
int boardSize = 15;
int cellSize;
int[][] board;
int currentPlayer = 1;
boolean gameStarted = false;
PImage backgroundImage; // 新增一個 PImage 物件
boolean showBackground = true; // 控制是否顯示背景圖片
int player1Score = 0;
int player2Score = 0;
void setup() {
size(600, 600);
cellSize = width / boardSize;
board = new int[boardSize][boardSize];
backgroundImage = loadImage("background.jpg"); // 載入背景圖片
drawStartScreen();
loop(); // 啟動 draw() 函數的連續呼叫
}
void draw() {
background(255);
if (gameStarted) {
if (showBackground) {
image(backgroundImage, 0, 0, width, height);
}
drawBoard();
drawStones();
if (checkWin()) {
textSize(60);
fill(0);
textAlign(CENTER, CENTER);
text("Player " + currentPlayer + " wins!", width / 2, height / 2);
updateScore();
showBackground = true; // 顯示背景圖片
noLoop(); // 停止 draw() 函數的連續呼叫,直到重新開始遊戲
}
displayScores(); // 顯示分數
} else {
drawStartScreen();
}
}
void drawStartScreen() {
if (showBackground) {
image(backgroundImage, 0, 0, width, height);
}
textAlign(CENTER, CENTER);
textSize(32);
fill(0);
text("Game", width / 2, height / 2 - 50);
textSize(20);
text(" 'backgammon' ", width / 2, height / 2 + 50);
rectMode(CENTER);
fill(150);
rect(width / 2, height / 2 + 100, 150, 50);
fill(0);
textSize(16);
text("START", width / 2, height / 2 + 100);
}
void drawBoard() {
for (int i = 0; i < boardSize; i++) {
for (int j = 0; j < boardSize; j++) {
fill(220, 180, 120);
stroke(0);
rect(i * cellSize, j * cellSize, cellSize, cellSize);
}
}
}
void drawStones() {
for (int i = 0; i < boardSize; i++) {
for (int j = 0; j < boardSize; j++) {
if (board[i][j] == 1) {
fill(0);
ellipse(i * cellSize + cellSize / 2, j * cellSize + cellSize / 2, cellSize, cellSize);
} else if (board[i][j] == 2) {
fill(255);
ellipse(i * cellSize + cellSize / 2, j * cellSize + cellSize / 2, cellSize, cellSize);
}
}
}
}
void mousePressed() {
if (!gameStarted && mouseX > width / 2 - 75 && mouseX < width / 2 + 75 && mouseY > height / 2 + 75 && mouseY < height / 2 + 125) {
gameStarted = true;
showBackground = false; // 不顯示背景圖片
} else if (gameStarted) {
int i = mouseX / cellSize;
int j = mouseY / cellSize;
if (isValidMove(i, j)) {
board[i][j] = currentPlayer;
if (checkWin()) {
gameStarted = false;
showBackground = true; // 顯示背景圖片
loop(); // 重新啟動 draw() 函數的連續呼叫
} else {
currentPlayer = 3 - currentPlayer;
}
}
}
}
boolean isValidMove(int i, int j) {
return board[i][j] == 0;
}
boolean checkWin() {
for (int i = 0; i < boardSize; i++) {
for (int j = 0; j < boardSize; j++) {
if (board[i][j] != 0) {
if (checkLine(i, j, 1, 0)) return true;
if (checkLine(i, j, 0, 1)) return true;
if (checkLine(i, j, 1, 1)) return true;
if (checkLine(i, j, -1, 1)) return true;
}
}
}
return false;
}
boolean checkLine(int x, int y, int dx, int dy) {
int count = 0;
int player = board[x][y];
for (int i = 0; i < 5; i++) {
int newX = x + i * dx;
int newY = y + i * dy;
if (newX >= 0 && newX < boardSize && newY >= 0 && newY < boardSize && board[newX][newY] == player) {
count++;
}
}
return count == 5;
}
void updateScore() {
if (checkWin()) {
if (currentPlayer == 1) {
player1Score++;
} else if (currentPlayer == 2) {
player2Score++;
}
println("Player 1 Score: " + player1Score);
println("Player 2 Score: " + player2Score);
}
}
void displayScores() {
textSize(20);
fill(0);
textAlign(CENTER, CENTER);
text("Player 1 Score: " + player1Score, width / 2, height - 30);
text("Player 2 Score: " + player2Score, width / 2, height - 10);
}
.png)
.png)
沒有留言:
張貼留言