ADD file via upload

main
ppx98yace 4 months ago
parent 33b5cca9dd
commit b0fb4761c6

@ -0,0 +1,142 @@
package com.snakegame;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Main {
private static JFrame settingsFrame;
private static SoundManager soundManager;
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
soundManager = new SoundManager();
showSettingsFrame();
});
}
public static void showSettingsFrame() {
settingsFrame = new JFrame("游戏设置");
settingsFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
settingsFrame.setSize(400, 400);
settingsFrame.setLayout(new BorderLayout());
JPanel settingsPanel = new JPanel(new GridLayout(10, 2, 10, 10));
settingsPanel.setBackground(Color.WHITE);
JLabel modeLabel = new JLabel("游戏难度:");
JComboBox<String> modeComboBox = new JComboBox<>(new String[]{"简单", "困难", "地狱"});
JLabel gameModeLabel = new JLabel("游戏模式:");
JComboBox<String> gameModeComboBox = new JComboBox<>(new String[]{"单人", "双人"});
JLabel collisionLabel = new JLabel("碰撞模式:");
JComboBox<String> collisionComboBox = new JComboBox<>(new String[]{"撞墙", "穿墙"});
JLabel backgroundLabel = new JLabel("背景颜色:");
JComboBox<String> backgroundComboBox = new JComboBox<>(new String[]{"浅蓝", "白色", "浅紫", "深灰", "黑色"});
JLabel snakeColorLabel1 = new JLabel("蛇1的颜色:");
JComboBox<String> snakeColorComboBox1 = new JComboBox<>(new String[]{"深绿", "深红", "深金", "深蓝", "深紫"});
JLabel snakeColorLabel2 = new JLabel("蛇2的颜色:");
JComboBox<String> snakeColorComboBox2 = new JComboBox<>(new String[]{"深绿", "深红", "深金", "深蓝", "深紫"});
JLabel foodColorLabel = new JLabel("食物颜色:");
JComboBox<String> foodColorComboBox = new JComboBox<>(new String[]{"深红", "深绿", "深蓝", "深紫", "黄色"});
JLabel musicLabel = new JLabel("背景音乐:");
JComboBox<String> musicComboBox = new JComboBox<>(new String[]{"关闭", "音乐1", "音乐2"});
JButton startButton = new JButton("开始游戏");
settingsPanel.add(modeLabel);
settingsPanel.add(modeComboBox);
settingsPanel.add(gameModeLabel);
settingsPanel.add(gameModeComboBox);
settingsPanel.add(collisionLabel);
settingsPanel.add(collisionComboBox);
settingsPanel.add(backgroundLabel);
settingsPanel.add(backgroundComboBox);
settingsPanel.add(snakeColorLabel1);
settingsPanel.add(snakeColorComboBox1);
settingsPanel.add(snakeColorLabel2);
settingsPanel.add(snakeColorComboBox2);
settingsPanel.add(foodColorLabel);
settingsPanel.add(foodColorComboBox);
settingsPanel.add(musicLabel);
settingsPanel.add(musicComboBox);
settingsPanel.add(startButton);
settingsFrame.add(settingsPanel, BorderLayout.CENTER);
settingsFrame.pack();
settingsFrame.setLocationRelativeTo(null);
settingsFrame.setVisible(true);
startButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
GameConfig config = new GameConfig();
config.setMode((String) modeComboBox.getSelectedItem());
config.setGameMode((String) gameModeComboBox.getSelectedItem());
config.setWallCollisionEnabled("撞墙".equals(collisionComboBox.getSelectedItem()));
config.setBackgroundColor(getColorFromName((String) backgroundComboBox.getSelectedItem()));
config.setSnakeColors1(getSnakeColors((String) snakeColorComboBox1.getSelectedItem()));
config.setSnakeColors2(getSnakeColors((String) snakeColorComboBox2.getSelectedItem()));
config.setFoodColor(getColorFromName((String) foodColorComboBox.getSelectedItem()));
// 设置背景音乐
String selectedMusic = (String) musicComboBox.getSelectedItem();
if ("关闭".equals(selectedMusic)) {
soundManager.stopBackgroundMusic();
} else {
String filePath = "resources/" + selectedMusic.replace("音乐", "background") + ".wav";
soundManager.playBackgroundMusic(filePath);
}
settingsFrame.dispose();
GameWindow window = new GameWindow(config, soundManager);
window.start();
}
});
}
private static Color getColorFromName(String name) {
switch (name) {
case "浅蓝":
return new Color(173, 216, 230);
case "白色":
return Color.WHITE;
case "浅紫":
return new Color(221, 160, 221);
case "深灰":
return new Color(128, 128, 128);
case "黑色":
return Color.BLACK;
case "深绿":
return new Color(46, 204, 113);
case "深红":
return new Color(231, 76, 60);
case "深金":
return new Color(241, 196, 15);
case "深蓝":
return new Color(44, 62, 80);
case "深紫":
return new Color(142, 60, 173);
case "黄色":
return Color.YELLOW;
default:
return Color.WHITE;
}
}
private static Color[] getSnakeColors(String name) {
switch (name) {
case "深绿":
return new Color[]{new Color(46, 204, 113), new Color(39, 174, 96)};
case "深红":
return new Color[]{new Color(231, 76, 60), new Color(192, 57, 43)};
case "深金":
return new Color[]{new Color(241, 196, 15), new Color(213, 176, 15)};
case "深蓝":
return new Color[]{new Color(44, 62, 80), new Color(30, 30, 50)};
case "深紫":
return new Color[]{new Color(142, 60, 173), new Color(100, 40, 130)};
default:
return new Color[]{Color.WHITE, Color.LIGHT_GRAY};
}
}
}
Loading…
Cancel
Save