📄 mainframe.java
字号:
package snake;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.borland.jbcl.layout.*;
/**
* <p>Title: 贪食蛇游戏</p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2005</p>
* <p>Company: </p>
* @author zsb
* @version 1.0
*/
public class MainFrame extends JFrame {
private JPanel contentPane;//窗体内容网格
private JToolBar jToolBar1 = new JToolBar();//工具栏
private JButton jButton1 = new JButton();//游戏开始按钮
private JButton jButton2 = new JButton();//游戏暂停按钮
private JButton jButton3 = new JButton();//游戏退出按钮
private JButton jButton4 = new JButton();//帮助按钮
private JPanel jPanel1 = new JPanel();//游戏主体面板容器
private JPanel jPanel_PlayArea = new JPanel();//游戏区面板容器
private XYLayout xYLayout1 = new XYLayout();//容器布局管理器
private XYLayout xYLayout2 = new XYLayout();
private XYLayout xYLayout3 = new XYLayout();
private static final int UP = 1, LEFT = 2, DOWN = 3, RIGHT = 4;//贪食蛇运动方向
private static final int BEGINNER = 1, MIDDLE = 2, EXPERT = 3;//游戏级别常量
private static final int ROWS = 30;//游戏区行数
private static final int COLS = 50;//游戏区列数
private boolean isPause = false;//游戏暂停标志
private boolean isEnd;//游戏结束标志
private SnakeBody snake ;//贪食蛇
private int score = 0;//当前得分
private int level = BEGINNER;//当前游戏级别
SnakeThread thread = new SnakeThread();//游戏主线程
private GridLayout gridLayout1 = new GridLayout(ROWS, COLS, 0, 0);//游戏区布局
private JButton[][] playBlocks;//游戏区的所有方块
JPanel jPanel2 = new JPanel();
JLabel jLabel1 = new JLabel();
JLabel jLabel2 = new JLabel();
JLabel jLabel3 = new JLabel();
JLabel jLabel4 = new JLabel();
JLabel jLabel5 = new JLabel();
JLabel jLabel6 = new JLabel();
JLabel jLabel7 = new JLabel();
ButtonGroup buttonGroup1 = new ButtonGroup();
JRadioButton jRadioButton1 = new JRadioButton();
JRadioButton jRadioButton2 = new JRadioButton();
JRadioButton jRadioButton3 = new JRadioButton();
//Construct the frame
public MainFrame() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch (Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
//setIconImage(Toolkit.getDefaultToolkit().createImage(MainFrame.class.getResource("[Your Icon]")));
contentPane = (JPanel)this.getContentPane();
contentPane.setLayout(xYLayout2);
this.setResizable(false);
this.setSize(new Dimension(512, 414));
this.setTitle("贪食蛇");
this.addKeyListener(new MainFrame_this_keyAdapter(this));
jButton1.setFont(new java.awt.Font("DialogInput", 0, 12));
jButton1.setMaximumSize(new Dimension(79, 39));
jButton1.setMinimumSize(new Dimension(79, 39));
jButton1.setPreferredSize(new Dimension(79, 39));
jButton1.setFocusPainted(false);
jButton1.setText("开始");
jButton1.addActionListener(new MainFrame_jButton1_actionAdapter(this));
jButton1.addKeyListener(new MainFrame_this_keyAdapter(this));
jButton2.setText("暂停");
jButton2.addActionListener(new MainFrame_jButton2_actionAdapter(this));
jButton2.setPreferredSize(new Dimension(79, 39));
jButton2.setFocusPainted(false);
jButton2.setMinimumSize(new Dimension(79, 39));
jButton2.setMaximumSize(new Dimension(79, 39));
jButton2.setFont(new java.awt.Font("DialogInput", 0, 12));
jButton2.addKeyListener(new MainFrame_this_keyAdapter(this));
jButton3.setText("退出");
jButton3.addActionListener(new MainFrame_jButton3_actionAdapter(this));
jButton3.setPreferredSize(new Dimension(79, 39));
jButton3.setFocusPainted(false);
jButton3.setMinimumSize(new Dimension(79, 39));
jButton3.setMaximumSize(new Dimension(79, 39));
jButton3.setFont(new java.awt.Font("DialogInput", 0, 12));
jButton3.addKeyListener(new MainFrame_this_keyAdapter(this));
jButton4.setText("帮助");
jButton4.addActionListener(new MainFrame_jButton4_actionAdapter(this));
jButton4.setPreferredSize(new Dimension(79, 39));
jButton4.setFocusPainted(false);
jButton4.setMinimumSize(new Dimension(79, 39));
jButton4.setMaximumSize(new Dimension(79, 39));
jButton4.setFont(new java.awt.Font("DialogInput", 0, 12));
jButton4.addKeyListener(new MainFrame_this_keyAdapter(this));
jPanel1.setLayout(xYLayout1);
jPanel1.addKeyListener(new MainFrame_this_keyAdapter(this));
jPanel_PlayArea.setLayout(gridLayout1);
jPanel_PlayArea.setBorder(BorderFactory.createEtchedBorder());
jPanel_PlayArea.addKeyListener(new MainFrame_this_keyAdapter(this));
jLabel1.setFont(new java.awt.Font("DialogInput", 0, 12));
jLabel1.setText("得分:");
jPanel2.setLayout(xYLayout3);
jLabel2.setFont(new java.awt.Font("DialogInput", 0, 12));
jLabel2.setToolTipText("");
jLabel2.setText("0");
jLabel3.setText("穿身:");
jLabel3.setFont(new java.awt.Font("DialogInput", 0, 12));
jLabel4.setFont(new java.awt.Font("DialogInput", 0, 12));
jLabel4.setText("0");
jLabel5.setText("穿墙:");
jLabel5.setFont(new java.awt.Font("DialogInput", 0, 12));
jLabel5.setToolTipText("");
jLabel6.setText("0");
jLabel6.setFont(new java.awt.Font("DialogInput", 0, 12));
jLabel6.setToolTipText("");
jRadioButton1.setFont(new java.awt.Font("DialogInput", 0, 12));
jRadioButton1.setText("初级");
jRadioButton1.addActionListener(new MainFrame_jRadioButton1_actionAdapter(this));
jRadioButton1.addKeyListener(new MainFrame_this_keyAdapter(this));
jRadioButton2.setText("中级");
jRadioButton2.addActionListener(new MainFrame_jRadioButton2_actionAdapter(this));
jRadioButton2.addKeyListener(new MainFrame_this_keyAdapter(this));
jRadioButton2.setFont(new java.awt.Font("DialogInput", 0, 12));
jRadioButton3.setText("高级");
jRadioButton3.addActionListener(new MainFrame_jRadioButton3_actionAdapter(this));
jRadioButton3.setFont(new java.awt.Font("DialogInput", 0, 12));
jRadioButton3.addKeyListener(new MainFrame_this_keyAdapter(this));
buttonGroup1.add(jRadioButton1);
buttonGroup1.add(jRadioButton2);
buttonGroup1.add(jRadioButton3);
jRadioButton1.setSelected(true);
jLabel7.setFont(new java.awt.Font("DialogInput", 0, 12));
jLabel7.setText("等级:");
contentPane.add(jToolBar1, new XYConstraints(0, 0, 512, -1));
jToolBar1.add(jButton1, null);
jToolBar1.add(jButton2, null);
jToolBar1.add(jButton3, null);
jToolBar1.add(jButton4, null);
jToolBar1.addKeyListener(new MainFrame_this_keyAdapter(this));
contentPane.add(jPanel1, new XYConstraints(0, 43, 512, 371));
jPanel1.add(jPanel_PlayArea, new XYConstraints(0, 33, 524, 314));
jPanel1.add(jPanel2, new XYConstraints(0, 0, 498, 30));
jPanel2.add(jRadioButton1, new XYConstraints(331, 1, -1, -1));
jPanel2.add(jRadioButton3, new XYConstraints(443, 1, -1, -1));
jPanel2.add(jRadioButton2, new XYConstraints(390, 1, -1, -1));
jPanel2.add(jLabel6, new XYConstraints(245, 6, 30, -1));
jPanel2.add(jLabel7, new XYConstraints(287, 5, 47, -1));
jPanel2.add(jLabel5, new XYConstraints(197, 6, 47, -1));
jPanel2.add(jLabel4, new XYConstraints(166, 7, 26, -1));
jPanel2.add(jLabel3, new XYConstraints(120, 6, 38, -1));
jPanel2.add(jLabel1, new XYConstraints(23, 6, 39, -1));
jPanel2.add(jLabel2, new XYConstraints(62, 6, 54, -1));
//创建并初始化游戏区方块
playBlocks = new JButton[ROWS][COLS];
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
playBlocks[i][j] = new JButton();
playBlocks[i][j].setBackground(Color.lightGray);
playBlocks[i][j].setVisible(false);
jPanel_PlayArea.add(playBlocks[i][j]);
}
}
}
//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
public void start() {
//初始化游戏参数
score = 0;
isPause = false;
isEnd = false; //
jButton2.setText("暂停");
//初始化游戏区
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
playBlocks[i][j].setBackground(Color.lightGray);
playBlocks[i][j].setVisible(false);
}
}
//创建蛇身
snake = new SnakeBody();
//在游戏区内随机放置食物
int x = (int) (Math.random() * ROWS);
int y = (int) (Math.random() * COLS);
while (playBlocks[x][y].isVisible()) {
x = (int) (Math.random() * ROWS);
y = (int) (Math.random() * COLS);
}
playBlocks[x][y].setBackground(Color.yellow);
playBlocks[x][y].setVisible(true);
//初始化状态提示区
jLabel2.setText(Integer.toString(score));
jLabel4.setText(Integer.toString(snake.throughbody));
jLabel2.setText(Integer.toString(snake.throughwall));
try {
//启动游戏
thread.start();
}
catch (IllegalThreadStateException illegalThreadStateException) {}
}
class SnakeBody {
// private int snake[][] = new int [MainFrame.ROWS][MainFrame.COLS]; //0位背景,1为蛇身,2为食物,3为穿身,4为穿墙,5为墙壁
public int length = 3;
public int rows[];
public int columes[];
public int direction = RIGHT;
public int lastdirection = RIGHT;
public long speed = 300;
public int throughbody = 0;
public int throughwall = 0;
public SnakeBody() {
length = 3;
throughbody = 1;
throughwall = 1;
direction = RIGHT;
lastdirection = RIGHT;
switch(level){
case BEGINNER:speed = 300;break;
case MIDDLE:speed = 200;break;
case EXPERT:speed = 100;break;
default:speed = 200;
}
throughbody = 0;
throughwall = 0;
rows = new int[ROWS];
columes = new int[COLS];
for (int i = 0; i <= length; i++) {
rows[i] = 1;
columes[i] = length - i;
}
}
public void move() {
//去掉蛇尾
playBlocks[rows[length]][columes[length]].setVisible(false);
playBlocks[rows[length]][columes[length]].setBackground(Color.lightGray);
//显示除蛇头外蛇身
for (int i = 0; i < length; i++) {
playBlocks[rows[i]][columes[i]].setBackground(Color.green);
playBlocks[rows[i]][columes[i]].setVisible(true);
}
//移动除蛇头外蛇身
for (int i = length; i > 0; i--) {
rows[i] = rows[i - 1];
columes[i] = columes[i - 1];
}
//根据蛇身运动方向,决定蛇头位置
switch (direction) {
case UP: {
if (lastdirection == DOWN)
rows[0] += 1;
else {
rows[0] -= 1;
lastdirection = UP;
}
break;
}
case LEFT: {
if (lastdirection == RIGHT)
columes[0] += 1;
else {
columes[0] -= 1;
lastdirection = LEFT;
}
break;
}
case DOWN: {
if (lastdirection == UP)
rows[0] -= 1;
else {
rows[0] += 1;
lastdirection = DOWN;
}
break;
}
case RIGHT: {
if (lastdirection == LEFT)
columes[0] -= 1;
else {
columes[0] += 1;
lastdirection = RIGHT;
}
break;
}
}
//处理有穿墙宝物时的穿墙操作
if (throughwall != 0) {
if (rows[0] >= ROWS) {
throughwall--;
jLabel6.setText(Integer.toString(throughwall));
rows[0] = ROWS - rows[0];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -