/*
* Created by JFormDesigner on Sat Jun 27 23:11:31 CST 2020
*/
package com.domain;
import java.awt.*;
import java.awt.event.*;
import java.beans.*;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Objects;
import java.util.Properties;
import javax.swing.*;
import javax.swing.border.*;
/**
* @author 123
*/
public class Main extends JFrame {
public static Main main = null;
public static void main(String[] args) throws ClassNotFoundException, UnsupportedLookAndFeelException, InstantiationException, IllegalAccessException {
main = new Main();
Snake.width = main.game.getWidth();
Snake.height = main.game.getHeight();
//启动检查
if(Snake.width % Snake.size != 0 && Snake.height % Snake.size != 0){
JOptionPane.showMessageDialog(null,"未知错误!","",JOptionPane.ERROR_MESSAGE);
System.exit(-1);
}
}
public Main() throws ClassNotFoundException, UnsupportedLookAndFeelException, InstantiationException, IllegalAccessException {
String lookAndFeel = UIManager.getSystemLookAndFeelClassName();
UIManager.setLookAndFeel(lookAndFeel);
Font font = new Font("微软雅黑", Font.PLAIN, 14);
java.util.Enumeration keys = UIManager.getDefaults().keys();
while (keys.hasMoreElements()) {
Object key = keys.nextElement();
Object value = UIManager.get(key);
if (value instanceof javax.swing.plaf.FontUIResource)
UIManager.put(key, font);
}
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
initComponents();
game.setFocusable(true); //始终获得焦点
game.requestFocus(false);
}
private int lastChoose = 0;
private int speed = Snake.NORMAL_SPEED;
private void levelItemStateChanged(ItemEvent e) {
// TODO add your code here
if (e.getStateChange() == ItemEvent.SELECTED) {
if (Objects.equals(level.getSelectedItem(), "自定义")) {
String s = null;
while (true) {
s = JOptionPane.showInputDialog(null, "自定义速度(移动一次花费的时间)", speed);
if (s == null) {
level.setSelectedIndex(lastChoose);
break;
} else if (s.matches("\\d+") && Integer.parseInt(s) > 0) {
speed = Integer.parseInt(s);
break;
}else{
JOptionPane.showMessageDialog(null,"请输入大于0的数字!","",JOptionPane.ERROR_MESSAGE);
}
}
} else {
lastChoose = level.getSelectedIndex();
}
}
}
private void levelPropertyChange(PropertyChangeEvent e) {
// TODO add your code here
}
public JPanel getGame() {
return game;
}
public JButton getStart() {
return start;
}
public JLabel getNowScore() {
return nowScore;
}
public JLabel getMaxScore() {
return maxScore;
}
private void startActionPerformed(ActionEvent e) {
int speed = 0;
if(level.getSelectedIndex() == 0){
speed = Snake.NORMAL_SPEED;
}else if(level.getSelectedIndex() == 1){
speed = Snake.A_BIT_FAST_SPEED;
}else if(level.getSelectedIndex() == 2){
speed = Snake.FAST_SPEED;
}else if(level.getSelectedIndex() == 3){
speed = Snake.VERY_FAST_SPEED;
}else if(level.getSelectedIndex() == 4){
speed = Snake.A_BIT_SLOW_SPEED;
}else if(level.getSelectedIndex() == 5){
speed = Snake.SLOW_SPEED;
}else if(level.getSelectedIndex() == 6){
speed = Snake.VERY_SLOW_SPEED;
}else if(level.getSelectedIndex() == 7){
speed = this.speed;
}
// TODO add your code here
if(Snake.isStart){
if(Snake.isStop){
Snake.toContinue();
start.setText("暂停");
Snake.isStop = false;
}else{
Snake.stop();
start.setText("继续");
Snake.isStop = true;
}
}else{
Snake.start(speed);
start.setText("暂停");
Snake.isStart = true;
Snake.isStop = false;
}
}
private void gameKeyPressed(KeyEvent e) {
// TODO add your code here
int code = e.getKeyCode();
if(code == 37 || code == 65){
//左
if(Snake.direction != Snake.EAST){
Snake.direction = Snake.WEST;
}
}else if(code == 38 || code==87){
//上
if(Snake.direction != Snake.SOUTH){
Snake.direction = Snake.NORTH;
}
}else if(code == 39 || code==68){
//右
if(Snake.direction != Snake.WEST){
Snake.direction = Snake.EAST;
}
}else if(code == 40 || code == 83){
//下
if(Snake.direction != Snake.NORTH){
Snake.direction = Snake.SOUTH;
}
}
}
private void initComponents() {
// JFormDesigner - Component initialization - DO NOT MODIFY //GEN-BEGIN:initComponents
control = new JPanel();
level = new JComboBox<>();
label1 = new JLabel();
start = new JButton();
label2 = new JLabel();
label3 = new JLabel();
nowScore = new JLabel();
maxScore = new JLabel();
game = new JPanel();
//======== this ========
setTitle("\u8d2a\u5403\u86c7");
setResizable(false);
setBackground(Color.black);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setForeground(new Color(0, 0, 2));
var contentPane = getContentPane();
contentPane.setLayout(null);
//======== control ========
{
control.setBorder(LineBorder.createBlackLineBorder());
control.setLayout(null);
//---- level ----
level.setModel(new DefaultComboBoxModel<>(new String[] {
"\u6b63\u5e38\u901f\u5ea6",
"\u6709\u70b9\u5feb",
"\u5f88\u5feb",
"\u8d85\u7ea7\u5feb",
"\u6709\u70b9\u6162",
"\u5f88\u6162",
"\u8d85\u7ea7\u6162",
"\u81ea\u5b9a\u4e49"
}));
level.setFocusable(false);
level.addItemListener(e -> levelItemStateChanged(e));
level.addPropertyChangeListener(e -> levelPropertyChange(e));
control.add(level);
level.setBounds(85, 10, 115, level.getPreferredSize().height);
//---- label1 ----
label1.setText("\u9009\u62e9\u96be\u5ea6\uff1a");
label1.setFocusable(false);
control.add(label1);
label1.setBounds(new Rectangle(new Point(15, 12), label1.getPreferredSize()));
//---- start ----
start.setText("\u5f00\u59cb");
start.setFocusable(false);
start.addActionListener(e -> startActionPerformed(e));
control.add(start);
start.setBounds(210, 10, 90, start.getPreferredSize().height);
//---- label2 ----
label2.setText("\u5f53\u524d\u5f97\u5206\uff1a");
label2.setFocusable(false);
control.add(label2);
label2.setBounds(new Rectangle(new Point(430, 15), label2.getPreferredSize()));
//---- label3 ----
label3.setText("\u6700\u9ad8\u5f97\u5206\uff1a");
label3.setFocusable(false);
control.add(label3);
label3.setBounds(new Rectangle(new Point(560, 15), label3.getPreferredSize()));
//---- nowScore ----
辣椒种子
- 粉丝: 4122
- 资源: 5737