首页 贪吃蛇源代码

贪吃蛇源代码

举报
开通vip

贪吃蛇源代码贪吃蛇源代码 import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Snake extends JFrame implements ActionListener { JMenuBar Control; JMenu File,Help; JMenuItem Exit,About; private JPanel contentPane; //窗体内容网格 private JButton btnSta...

贪吃蛇源代码
贪吃蛇源代码 import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Snake extends JFrame implements ActionListener { JMenuBar Control; JMenu File,Help; JMenuItem Exit,About; private JPanel contentPane; //窗体内容网格 private JButton btnStart = new JButton("开始游戏"); //游戏开始按钮 JButton b1 = new JButton("重新开始"); //重新开始按钮 private JButton btnPause = new JButton("暂停"); //游戏暂停按钮 private JButton btnExit = new JButton("退出"); //游戏退出按 private JPanel pnlTop = new JPanel(); //顶部按钮和分数面板 private JPanel pnlLeft = new JPanel(); //左侧面板 private JPanel playPanel = new JPanel(); //游戏区面板 private BorderLayout borderLayout1 = new BorderLayout(); //容器布局管理器 private BorderLayout borderLayout2 = new BorderLayout(); private GridLayout rbtnLayout = new GridLayout(10, 1, 1, 1); private static final int UP = 1,LEFT = 2,DOWN = 3,RIGHT = 4;//蛇运动方向 private static final int ROWS = 35; //游戏区行数 private static final int COLS = 50; //游戏区列数 private boolean isPause = false; //游戏暂停标志 private boolean isEnd; //游戏结束标志 private SnakeBody snake; //贪食蛇 private int score = 0; //当前得分 SnakeThread thread = new SnakeThread(); //游戏主线程 private GridLayout grid1 = new GridLayout(ROWS,COLS,0,0); //游戏区布局 private JButton[][] blocks; //游戏区的所有方块 JPanel jPanel2 = new JPanel(); JLabel jLabel1 = new JLabel("得分:");//分数 JLabel lblScroe = new JLabel("0"); ButtonGroup buttonGroup1 = new ButtonGroup();//游戏级数的设置 JRadioButton rbtnLow = new JRadioButton("初级", true); JRadioButton rbtnMid = new JRadioButton("中级"); JRadioButton rbtnHigh = new JRadioButton("高级"); class WindowListener extends WindowAdapter {//退出窗口动作 public void windowClosing(WindowEvent e) { System.exit(0); } } public Snake() { super("贪食蛇游戏"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); contentPane = (JPanel)this.getContentPane(); contentPane.setLayout(borderLayout2); Control = new JMenuBar(); File = new JMenu("文件"); Help = new JMenu("帮助"); Exit = new JMenuItem("退出"); About = new JMenuItem("关于制作"); About.addActionListener(this); this.setResizable(false); this.setSize(new Dimension(600, 414)); keyAction keyAct = new keyAction(); this.addKeyListener(keyAct); btnStart.addKeyListener(keyAct); b1.addKeyListener(keyAct); btnPause.addKeyListener(keyAct); btnExit.addKeyListener(keyAct); rbtnLow.addKeyListener(keyAct); rbtnMid.addKeyListener(keyAct); rbtnHigh.addKeyListener(keyAct); btnAction btnAct = new btnAction(); btnStart.addActionListener(btnAct); b1.addActionListener(btnAct); About.addActionListener(btnAct); addWindowListener(new WindowListener()); btnPause.addActionListener(btnAct); btnExit.addActionListener(btnAct); rbtnLow.addActionListener(btnAct); rbtnMid.addActionListener(btnAct); rbtnHigh.addActionListener(btnAct); pnlLeft.setLayout(borderLayout1); playPanel.setLayout(grid1); playPanel.setBackground(Color.green); playPanel.setBorder(BorderFactory.createEtchedBorder()); jPanel2.setLayout(rbtnLayout); buttonGroup1.add(rbtnLow); buttonGroup1.add(rbtnMid); buttonGroup1.add(rbtnHigh); rbtnLow.setSelected(true); pnlLeft.add(playPanel); pnlLeft.add(jPanel2, BorderLayout.WEST); jPanel2.add("f1", rbtnLow); jPanel2.add("f2", rbtnMid); jPanel2.add("f3", rbtnHigh); pnlTop.add(btnStart); pnlTop.add(b1); pnlTop.add(btnPause); pnlTop.add(btnExit); pnlTop.add(jLabel1); pnlTop.add(lblScroe); File.add(Exit); Help.add(About); Control.add(File); Control.add(Help); contentPane.add(pnlTop, BorderLayout.SOUTH); contentPane.add(pnlLeft, BorderLayout.CENTER); contentPane.add(Control,"North"); Exit.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent exit){ System.exit(0); }}); //创建并初始化游戏区方块 blocks = new JButton[ROWS][COLS]; for (int i = 0; i < ROWS; i++) { for (int j = 0; j < COLS; j++) { blocks[i][j] = new JButton(); blocks[i][j].setBackground(Color.lightGray); blocks[i][j].setVisible(false); playPanel.add(blocks[i][j]); } } } public static void main(String[] args) { Snake app = new Snake(); app.validate(); app.setVisible(true); } public void start() { snake = new SnakeBody(); //创建蛇身 if (rbtnLow.isSelected()) snake.setSpeed(300); if (rbtnMid.isSelected()) snake.setSpeed(200); if (rbtnHigh.isSelected()) snake.setSpeed(100); score = 0; isPause = false; isEnd = false; // btnPause.setText("暂停"); //初始化游戏区 for (int i = 0; i < ROWS; i++) { for (int j = 0; j < COLS; j++) { blocks[i][j].setBackground(Color.lightGray); blocks[i][j].setVisible(false); } } //在游戏区内随机放置食物 int x = (int) (Math.random() * ROWS); int y = (int) (Math.random() * COLS); while (blocks[x][y].isVisible()) { x = (int) (Math.random() * ROWS); y = (int) (Math.random() * COLS); } blocks[x][y].setBackground(Color.black); blocks[x][y].setVisible(true); try { thread.start(); } catch (IllegalThreadStateException illegalThreadStateException) {} } class SnakeBody { public int row[]; public int col[]; public int len = 3, direction = RIGHT, lastdirection = RIGHT; public long speed = 300; public SnakeBody() { len = 3; direction = RIGHT; lastdirection = RIGHT; row = new int[ROWS]; col = new int[COLS]; for (int i = 0; i <= len; i++) { row[i] = 1; col[i] = len - i; } } public void setSpeed(int s) { speed = s; } public void move() { blocks[row[len]][col[len]].setVisible(false); //去掉蛇尾 blocks[row[len]][col[len]].setBackground(Color.white); //修改颜色 //显示蛇身 for (int i = 0; i < len; i++) { blocks[row[i]][col[i]].setBackground(Color.red); blocks[row[i]][col[i]].setVisible(true); } //移动蛇身 for (int i = len; i > 0; i--) { row[i] = row[i - 1]; col[i] = col[i - 1]; } //根据蛇身运动方向,决定蛇头位置 switch (direction) { case UP: { if (lastdirection == DOWN) row[0] += 1; else { row[0] -= 1; lastdirection = UP; } break; } case LEFT: { if (lastdirection == RIGHT) col[0] += 1; else { col[0] -= 1; lastdirection = LEFT; } break; } case DOWN: { if (lastdirection == UP) row[0] -= 1; else { row[0] += 1; lastdirection = DOWN; } break; } case RIGHT: { if (lastdirection == LEFT) col[0] -= 1; else { col[0] += 1; lastdirection = RIGHT; } break; } } //当蛇头碰到墙时,蛇头碰到蛇身时,游戏结束 if (row[0] >= ROWS || row[0] < 0 || col[0] >= COLS || col[0] < 0 || blocks[row[0]][col[0]].getBackground().equals(Color.red)) { isEnd = true; int n=JOptionPane.showConfirmDialog(null, "是否退出游戏?","确认对话框",JOptionPane.YES_NO_OPTION); if(n==JOptionPane.YES_OPTION){ System.exit(0); } } //吃豆 if (blocks[row[0]][col[0]].getBackground().equals(Color.black)) { score += 10; lblScroe.setText(Integer.toString(score)); if (score % 200 == 0 && speed > 100) { JOptionPane.showMessageDialog(null, "恭喜你过关了,准备进入下一关"); speed -= 100; if (speed == 200) rbtnMid.setSelected(true); if (speed == 100) rbtnHigh.setSelected(true); } } //吃豆后,蛇身加长,并随机显示下一个豆 if (blocks[row[0]][col[0]].getBackground().equals(Color.black)) { len++; int x, y; x = (int) (Math.random() * ROWS); y = (int) (Math.random() * COLS); while (blocks[x][y].isVisible()) { x = (int) (Math.random() * ROWS); y = (int) (Math.random() * COLS); } blocks[x][y].setBackground(Color.black); blocks[x][y].setVisible(true); } blocks[row[0]][col[0]].setBackground(Color.red); blocks[row[0]][col[0]].setVisible(true); //显示蛇头 } } class SnakeThread extends Thread { public void run() { while (true) { try { Thread.sleep(snake.speed); //控制移动速度 if (!isEnd && !isPause) { snake.move(); //移动蛇身 } if (isEnd) { //游戏结束 btnStart.setEnabled(true); } } catch (Exception ex) {} } } } public void actionPerformed(ActionEvent e){ if(e.getSource()==About){ helpWindow hw=new helpWindow(); hw.setTitle("帮助窗口"); hw.setVisible(true); } } class helpWindow extends JFrame{ helpWindow(){ init(); setBounds(300,400,400,350); setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); } void init(){ JTextArea text1=new JTextArea(" 贪吃蛇是款经过我们第十战 队一起合作出来的结果,它的诞生包含\n了大家的辛苦劳动。再者感谢我们的技术顾问洪同学。本款游戏是通过按\n钮”上“”下“”左“”右“四个键来控制蛇在绿色地图里面的移动来吃\n掉地图里随机出现的黑色食物,每吃掉一个食物蛇身就会多出一节。当然\n吃的越多身子就越长,那样难度也就越高。当蛇撞到墙或者自己的身子,\n那么蛇就会死亡。到那时就只能重新开始游戏了。游戏分为3个等级限制,\n每个等级的区别就是在速度上的区别,初级的速度比较慢、中级有所提高、\n高级的速度就更快了。当道高级是一般最担心的就是蛇会不会撞到墙了。\n希望我们的游戏能给你带来娱乐,感谢你的支持。在这里我们感谢老师的\n协作,百度的支持。\n\n\n 10计科2班第十战功队 "); add(text1); text1.setEditable(false); } } class keyAction extends KeyAdapter { public void keyPressed(KeyEvent e) { if (!isEnd && !isPause) { //根据用户按键,设置蛇运动方向 if (e.getKeyCode() == KeyEvent.VK_UP) { snake.direction = UP; } if (e.getKeyCode() == KeyEvent.VK_DOWN) { snake.direction = DOWN; } if (e.getKeyCode() == KeyEvent.VK_LEFT) { snake.direction = LEFT; } if (e.getKeyCode() == KeyEvent.VK_RIGHT) { snake.direction = RIGHT; } } } } //按钮监听响应处理类 private class btnAction implements ActionListener { public void actionPerformed(ActionEvent ae) { Object source = ae.getSource(); if (source.equals(btnStart)) { btnStart.setEnabled(false); start(); } if (source.equals(b1)) { b1.setEnabled (true); start(); } if (source.equals(btnPause)) { if (isPause == true) { btnPause.setText("暂停"); } if (isPause == false) { btnPause.setText("继续"); } isPause = !isPause; } if (source.equals(btnExit)) { int n=JOptionPane.showConfirmDialog(null, "是否退出游戏?","确认对话框",JOptionPane.YES_NO_OPTION); if(n==JOptionPane.YES_OPTION){ System.exit(0); } } if (source.equals(rbtnLow)) { snake.setSpeed(300); } if (source.equals(rbtnMid)) { snake.setSpeed(200); } if (source.equals(rbtnHigh)) { snake.setSpeed(1s00); } } } }
本文档为【贪吃蛇源代码】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
下载需要: 免费 已有0 人下载
最新资料
资料动态
专题动态
is_591137
暂无简介~
格式:doc
大小:39KB
软件:Word
页数:16
分类:互联网
上传时间:2017-10-12
浏览量:16