📄 wormframe.java
字号:
package worm;import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.border.*;import java.applet.*;import java.net.*;public class WormFrame extends JFrame{ private JPanel contentPane; private JMenuBar menuBar = new JMenuBar(); private JMenu menuFile = new JMenu(); private JMenuItem menuFileExit = new JMenuItem(); private JMenu menuHelp = new JMenu(); private JMenuItem menuHelpAbout = new JMenuItem(); private JMenuItem menuFileNew = new JMenuItem(); private JMenuItem menuFilePause = new JMenuItem(); private JLabel statusBar = new JLabel(); /* protected properties, used in both WormFrame and panel (class ScreenPanel) */ protected Dimension screenSize = new Dimension(600,400); protected ScreenPanel panel = new ScreenPanel(); /* current key status currentKey==0 (default) : left arrow button is clicked currentKey==1 : right arrow button is clicked currentKey==2 : up arrow button is clicked currentKey==3 : down arrow button is clicked */ protected int currentKey=0; //default left arrow /* level define the game's level from 1 to 5 */ int level = 1; /* the player's total score */ int score = 0; /* if you got enought leverscore in lower lever, you will enter in higher level */ int levelScore=20; //////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////// //begin of class ScreenPanel public class ScreenPanel extends JPanel implements Runnable { private Border border1; /* class snake(thread) is declare in file Snake.java */ private Snake snake = null; /* the food point, which will be eated by snake*/ private CPoint foodPoint=null; /* foodRadius the radius of food point */ int foodRadius = 5; public ScreenPanel() { try { jbInit(); } catch(Exception ex) { ex.printStackTrace(); } } /* game start, the snake begin move in the screen */ public void start() { if(snake!=null) { snake.stop(); } foodPoint = null; snake = new Snake(this); snake.start(); } /* game pause, the snake don's move */ public void pause() { if (snake!=null) snake.suspend(); } /* game go on, the snake go on move*/ public void goOn() { if(snake!=null) snake.resume() ; } /*the WormFrame class implements the Interface Runnable, so it must define the methord run(). the method is very important in the whole program. */ public void run() { while(true) { /* sleep time changed by the level */ try { /* second will be changed with the level(1~5) */ int second = 120-20*level; snake.sleep(second); } catch(InterruptedException ex) { } /* place a food point in the screen */ placePoint(); /* judge whether the snake ea*/ boolean grow =false; if(snake.eatPoint(foodPoint)) { //eatFoodSound(); grow = true; score ++; if (score%levelScore==0) { level++; } foodPoint = null; } snake.setDirecotion(currentKey); if (snake.move(grow)) { menuFilePause.setEnabled(false); statusBar.setText("Snake Dead! Score "+score); snake.stop(); } if(level==6) { win(); } else { statusBar.setText("Level "+String.valueOf(level)+" Score "+String.valueOf(score)); } this.repaint() ; } } public void paint(Graphics g) { super.paint(g); if(this.foodPoint!=null) foodPoint.paint(g); if (snake!=null) snake.paint(g); } private void jbInit() throws Exception { this.setBackground(Color.black); border1 = BorderFactory.createBevelBorder(BevelBorder.LOWERED,Color.gray,Color.darkGray,Color.darkGray,Color.gray); this.setBorder(border1); } private void placePoint() { int index = (int)(Math.random()*11); Color color = Color.red; switch(index) { case 0: color = Color.red ; break; case 1: color = Color.yellow ; break; case 2: color = Color.blue ; break; case 3: color = Color.green ; break; case 4: color = Color.pink; break; case 5: color = Color.white ; break; case 6: color = Color.magenta; break; case 7: color = new Color(50,0,180); break; case 8: color = new Color(80,180,250); break; case 9: color = new Color(255,100,250); break; case 10: color = new Color(160,255,160); break; } if (foodPoint==null) { int r = snake.getRadius(); int rx = (int)(Math.random()* (screenSize.width-2*r))+r; int ry = (int)(Math.random()*(screenSize.height-2*r))+r; foodPoint = new CPoint(rx,ry,this.foodRadius ,color); foodPoint.show(); } } private void win() { try { URL codeBase = new URL("file:" + System.getProperty("user.dir") + "/win.wav"); AudioClip audioClip = Applet.newAudioClip(codeBase); audioClip.play(); } catch(MalformedURLException ex) { } menuFilePause.setEnabled(false); statusBar.setText("Congratulation! You winned!"); snake.stop(); } private void eatFoodSound() { try { URL codeBase = new URL("file:" + System.getProperty("user.dir") + "/eat.wav"); AudioClip audioClip = Applet.newAudioClip(codeBase); audioClip.play(); } catch(MalformedURLException ex) { } } } //end of class ScreenPanel //////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////// /**Construct the frame*/ public WormFrame() { enableEvents(AWTEvent.WINDOW_EVENT_MASK); try { jbInit(); } catch(Exception e) { e.printStackTrace(); } } /**Component initialization*/ private void jbInit() throws Exception { setIconImage(Toolkit.getDefaultToolkit().createImage(WormFrame.class.getResource("icon.gif"))); this.setSize(screenSize.width+4,screenSize.height+68); this.setResizable(false); this.setTitle("贪吃蛇"); this.addKeyListener(new java.awt.event.KeyAdapter() { public void keyPressed(KeyEvent e) { this_keyPressed(e); } }); statusBar.setText(" "); //begin of menu's setting menuFile.setText("File"); menuFile.setMnemonic('F'); menuFileNew.setMnemonic('N'); menuFileNew.setText("New Game"); menuFileNew.setAccelerator(javax.swing.KeyStroke.getKeyStroke(78, java.awt.event.KeyEvent.CTRL_MASK, false)); menuFileNew.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { menuFileNew_actionPerformed(e); } }); menuFilePause.setText("Pause"); menuFilePause.setMnemonic('P'); menuFilePause.setEnabled(false); menuFilePause.setAccelerator(javax.swing.KeyStroke.getKeyStroke(80, java.awt.event.KeyEvent.CTRL_MASK, false)); menuFilePause.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { menuFilePause_actionPerformed(e); } }); menuFileExit.setText("Exit"); menuFileExit.setAccelerator(javax.swing.KeyStroke.getKeyStroke(88, java.awt.event.KeyEvent.ALT_MASK, false)); menuFileExit.setMnemonic('X'); menuFileExit.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { menuFileExit_actionPerformed(e); } }); menuHelp.setText("Help"); menuHelp.setMnemonic('H'); menuHelpAbout.setText("About"); menuHelpAbout.setMnemonic('A'); menuHelpAbout.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { menuHelpAbout_actionPerformed(e); } }); menuFile.add(menuFileNew); menuFile.add(menuFilePause); menuFile.addSeparator(); menuFile.add(menuFileExit); menuHelp.add(menuHelpAbout); menuBar.add(menuFile); menuBar.add(menuHelp); menuBar.setBorder(BorderFactory.createEmptyBorder() ); this.setJMenuBar(menuBar); //end of menu's setting contentPane = (JPanel) this.getContentPane(); contentPane.setLayout(new BorderLayout(5,5)); contentPane.add(statusBar, BorderLayout.SOUTH); contentPane.add(panel, BorderLayout.CENTER); } /**Overridden so we can exit when window is closed*/ protected void processWindowEvent(WindowEvent e) { super.processWindowEvent(e); if (e.getID() == WindowEvent.WINDOW_CLOSING) { menuFileExit_actionPerformed(null); } } /* KeyEvent----when the direction arrow button is clicked */ private void this_keyPressed(KeyEvent e) { int key = e.getKeyCode(); switch(key ) { case KeyEvent.VK_LEFT: currentKey =0; break; case KeyEvent.VK_RIGHT: currentKey =1; break; case KeyEvent.VK_UP: currentKey =2; break; case KeyEvent.VK_DOWN: currentKey =3; break; } } /**File | New Game action performed*/ private void menuFileNew_actionPerformed(ActionEvent e) { currentKey=0; level = 1; score = 0; menuFilePause.setEnabled(true); panel.start() ; } /**File | Pasuse(/Go On) action performed*/ void menuFilePause_actionPerformed(ActionEvent e) { String text = menuFilePause.getText(); if(text.equals("Pause")) { this.panel.pause(); menuFilePause.setText("Go On"); } else { this.panel.goOn(); menuFilePause.setText("Pause"); } } /**File | Exit action performed*/ private void menuFileExit_actionPerformed(ActionEvent e) { System.exit(0); } /**Help | About action performed*/ private void menuHelpAbout_actionPerformed(ActionEvent e) { WormFrame_AboutBox dlg = new WormFrame_AboutBox(this); Dimension dlgSize = dlg.getPreferredSize(); Dimension frmSize = getSize(); Point loc = getLocation(); dlg.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x, (frmSize.height - dlgSize.height) / 2 + loc.y); dlg.setModal(true); dlg.show(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -