发布网友 发布时间:2022-04-24 00:42
共2个回答
热心网友 时间:2023-10-16 02:50
package snake;
import javax.swing.UIManager;
import java.awt.*;
/**
* <p>Title: 贪食蛇游戏</p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2005</p>
* <p>Company: </p>
* @author zsb
* @version 1.0
*/
public class Snake {
private boolean packFrame = false;
//Construct the application
public Snake() {
MainFrame frame = new MainFrame();
//Validate frames that have preset sizes
//Pack frames that have useful preferred size info, e.g. from their layout
if (packFrame) {
frame.pack();
}
else {
frame.validate();
}
//Center the window
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
frame.setVisible(true);
}
//Main method
public static void main(String[] args) {
/* try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch(Exception e) {
e.printStackTrace();
}*/
new Snake();
}
}
package snake;
import java.awt.*;
import javax.swing.*;
/**
* <p>Title: 贪食蛇游戏</p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2005</p>
* <p>Company: </p>
* @author zsb
* @version 1.0
*/
public class Help_Dialog extends JDialog {
JPanel panel1 = new JPanel();
BorderLayout borderLayout1 = new BorderLayout();
JTextArea jTextArea1 = new JTextArea();
private String help = "游戏说明:\n1 :方向键控制蛇移动的方向."+
"\n2 :按开始键开始游戏."+
"\n3 :按暂停键可以暂停游戏,再按暂停键能继续玩游戏."+
"\n4 :*为普通食物,吃一个得100分."+
"\n5 :青色为穿身宝物,吃一个得100分,该宝物允许玩家穿过一次蛇身"+
"\n6 :红色为穿墙宝物,吃一个得200分,该宝物允许玩家穿过一次墙壁."+
"\n7 :当分数到达一定时,会自动升级.当到达最高级别时就没有升级了.";
GridBagLayout gridBagLayout1 = new GridBagLayout();
public Help_Dialog(Frame frame, String title, boolean modal) {
super(frame, title, modal);
try {
jbInit();
pack();
}
catch(Exception ex) {
ex.printStackTrace();
}
}
public Help_Dialog() {
this(null, "", false);
}
private void jbInit() throws Exception {
panel1.setLayout(borderLayout1);
jTextArea1.setBackground(SystemColor.control);
jTextArea1.setEditable(false);
jTextArea1.setText("游戏说明:\n1 :方向键控制蛇移动的方向.\n2 :按开始键开始游戏.\n3 :按暂停键可以暂停游戏,再按暂停键能继续玩游戏.\n4 :*为普通食物,吃一个得100分.\n" +
"5 :青色为穿身宝物,吃一个得100分,该宝物允许玩家穿过一次蛇身\n6 :红色为穿墙宝物,吃一个得100分,该宝物允许玩家穿过一次墙壁.\n" +
"7 :当分数到达一定时,会自动升级.当到达最高级别时就没有升级了.");
this.setResizable(false);
this.setTitle("帮助");
this.getContentPane().setLayout(gridBagLayout1);
getContentPane().add(panel1, new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0
,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 45, 83));
panel1.add(jTextArea1, BorderLayout.CENTER);
}
}
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) {}
}
//完整的http://hi.baidu.com/cslg0000/blog/category/Java
热心网友 时间:2023-10-16 02:50
10分?