今天我們要來繼續完善主畫面的內容,目前就剩下SettingPanel還沒有完成
首先設定出SettingPanel,先暫時只給他一個回到主畫面的功能,先保證能夠流暢切換畫面剩下的後面再加
import javax.swing.*;
import java.awt.*;
public class SettingPanel extends JPanel{
static final int PANEL_WIDTH = 800;
static final int PANEL_HEIGHT = 500;
// 接收GameFrame參數
GameFrame gameFrame;
// 回主畫面的按鈕
JButton homeButton;
SettingPanel(GameFrame frame){
setButtons();
this.setPreferredSize(new Dimension(PANEL_WIDTH, PANEL_HEIGHT));
this.gameFrame = frame;
this.setLayout(null);
this.setBackground(Color.darkGray);
}
private void setButtons(){
homeButton = new JButton("← Home");
homeButton.setFont(new Font("Times New Roman", Font.ITALIC, 30));
homeButton.setForeground(Color.WHITE);
homeButton.setBackground(Color.darkGray);
homeButton.setBorderPainted(false);
homeButton.setBounds(25, 400, 200, 50);
homeButton.addActionListener(e->gameFrame.showPanel("Menu"));
this.add(homeButton);
}
}
接著我打算在Setting中增加設定難度的功能
要做到這點,需要先去學JRaidoButton和ButtonGroup的組合
// 先建立提示文字、3個難度選項與一個群組
JLabel setDifficulty;
JRadioButton easyButton;
JRadioButton mediumButton;
JRadioButton hardButton;
ButtonGroup difficultyGroup;
private void setDifficulty(){
// 設置提示文字
setDifficulty = new JLabel("Difficulty:");
setDifficulty.setFont(new Font("MV Boli", Font.BOLD, 30));
setDifficulty.setForeground(Color.WHITE);
setDifficulty.setBackground(Color.darkGray);
setDifficulty.setBounds(50, 90, 200, 50);
// 設置按鈕資料
easyButton = new JRadioButton("Easy");
easyButton.setFont(difficultyFont);
easyButton.setForeground(Color.WHITE);
easyButton.setBackground(Color.darkGray);
easyButton.setBorderPainted(false);
easyButton.setFocusPainted(false);
easyButton.setBounds(250, 100, 100, 30);
// 設置按鈕的觸發事件,點擊時將難度設為easy(200ms更新一次的速度)
easyButton.addActionListener(e->{
difficulty = 200;
gameFrame.setDifficulty(difficulty);
});
// 其他兩個也是
mediumButton = new JRadioButton("Medium");
mediumButton.setFont(difficultyFont);
mediumButton.setForeground(Color.WHITE);
mediumButton.setBackground(Color.darkGray);
mediumButton.setBorderPainted(false);
mediumButton.setFocusPainted(false);
mediumButton.setBounds(400, 100, 150, 30);
mediumButton.addActionListener(e->{
difficulty = 150;
gameFrame.setDifficulty(difficulty);
});
// 預設難度為medium,設置為預選
mediumButton.setSelected(true);
hardButton = new JRadioButton("Hard");
hardButton.setFont(difficultyFont);
hardButton.setForeground(Color.WHITE);
hardButton.setBackground(Color.darkGray);
hardButton.setBorderPainted(false);
hardButton.setFocusPainted(false);
hardButton.setBounds(600, 100, 150, 30);
hardButton.addActionListener(e->{
difficulty = 100;
gameFrame.setDifficulty(difficulty);
});
// 設置群組,將三個RadioButton都加進去
difficultyGroup = new ButtonGroup();
difficultyGroup.add(easyButton);
difficultyGroup.add(mediumButton);
difficultyGroup.add(hardButton);
// 然後分別將"每個"獨立的都加進去,群組不用加,群組是拿來綁定選項的
this.add(setDifficulty);
this.add(easyButton);
this.add(mediumButton);
this.add(hardButton);
}
接著GameFrame, GamePanel, GameOverPanel都需要有對應的difficulty變數與method,用於傳遞資料
GameFrame新增
private int difficulty = 150; // 初始設為medium難度,速度為150ms
// 兩個get&set method,用來修改與取得資料
public void setDifficulty(int difficulty){this.difficulty = difficulty;}
public int getDifficulty(){return difficulty;}
GamePanel修改預設速度邏輯,移除INITIAL_SPEED
int difficulty = 150; // 預設difficulty速度為150ms
int delay = difficulty; // 設置delay數值為difficulty(後續還會變動,所以用這個delay變數)
int scoreFactor = 4; // 修改難度邏輯,中等難度每4分升一級,簡單為5,困難為3
public void startGame(){
// 確認遊戲要開始時,先取得使用者選擇的難度資料,並修改scoreFactor變數
difficulty = gameFrame.getDifficulty();
delay = difficulty;
if(difficulty == 100) scoreFactor = 3;
else if(difficulty == 150) scoreFactor = 4;
else scoreFactor = 5;
}
private void move(){
// 之前是3分就升一級,現在改為根據難度決定升級分數
if(score % scoreFactor == 0 && level < 5){
// ...
}
}
最後是將difficulty傳給GameOverPanel,並顯示在結束畫面
String difficulty;
public void setScore(){
this.score = gameFrame.getScore();
int dif = gameFrame.getDifficulty();
if(dif == 100) difficulty = "Hard";
else if(dif == 150) difficulty = "Medium";
else difficulty = "Easy";
gameOverLabel2.setText("<html>You Scored " + score +
" in " + difficulty + "!</html>");
}
這樣就完成了可選擇的難度設置