/** ****************************************************************************** * HOMEWORK 1, 15-211 ****************************************************************************** * * This is an interactive graphical application in which you navigate the * Snake by using the keyboard arrow keys. * * The game has several options that allow you to change the level of the game, * the background color, and to specify whether or not the caterpillar can pass * through itself. * * * * @author V.Adamchik * @date 12/29/04 * @see Queue * @see Snake *****************************************************************************/ /***************************************************************************** There is no need to modify this file. *****************************************************************************/ import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; import javax.swing.border.Border; public class GameApplet extends JApplet implements ActionListener, KeyListener, MouseListener, Runnable { private Snake player; private Thread animate; private int level = 1; private boolean firstClick = true; private JFormattedTextField displayWindow, levelWindow; private JCheckBoxMenuItem selfCrossing; public void init() { setSize(500, 400); /* Create the menu bar and the menus */ JMenuBar menubar = new JMenuBar(); setJMenuBar(menubar); JMenu levelsMenu = new JMenu("Levels"); menubar.add(levelsMenu); JMenu optionsMenu = new JMenu("Options"); menubar.add(optionsMenu); JMenu helpMenu = new JMenu("Help"); menubar.add(helpMenu); /* Create menu items for "Levels" */ JMenuItem level1 = new JMenuItem("Beginner"); level1.setAccelerator( KeyStroke.getKeyStroke("ctrl B") ); level1.addActionListener(this); levelsMenu.add(level1); JMenuItem level2 = new JMenuItem("Intermediate"); level2.setAccelerator( KeyStroke.getKeyStroke("ctrl I") ); level2.addActionListener(this); levelsMenu.add(level2); JMenuItem level3 = new JMenuItem("Advanced"); level3.setAccelerator( KeyStroke.getKeyStroke("ctrl A") ); level3.addActionListener(this); levelsMenu.add(level3); /* Create menu items for "Options" */ JMenuItem start = new JMenuItem("New Game"); start.setAccelerator( KeyStroke.getKeyStroke("S") ); start.addActionListener(this); optionsMenu.add(start); JMenuItem pause = new JMenuItem("Pause"); pause.setAccelerator( KeyStroke.getKeyStroke("P") ); pause.addActionListener(this); optionsMenu.add(pause); JMenuItem cont = new JMenuItem("Continue"); cont.setAccelerator( KeyStroke.getKeyStroke("C") ); cont.addActionListener(this); optionsMenu.add(cont); // Add a separating line to the menu. optionsMenu.addSeparator(); selfCrossing = new JCheckBoxMenuItem("Allow self-crossing"); selfCrossing.setSelected(true); optionsMenu.add(selfCrossing); optionsMenu.addSeparator(); JMenu background = new JMenu("Background Color"); optionsMenu.add(background); background.add("Magenta").addActionListener(this); background.add("Cyan").addActionListener(this); background.add("Yellow").addActionListener(this); background.add("Blue").addActionListener(this); background.add("White").addActionListener(this); /* Create menu items for "Help" */ JMenuItem instr = new JMenuItem("Instructions"); instr.setAccelerator( KeyStroke.getKeyStroke("H") ); instr.addActionListener(this); helpMenu.add(instr); helpMenu.addSeparator(); // Add a separating line to the menu. JMenuItem feedback = new JMenuItem("Send Feedback"); helpMenu.add(feedback); // it is NOT implemented /* Create the score window. */ menubar.add(new JLabel(" ")); displayWindow = new JFormattedTextField(); displayWindow.setValue("0"); displayWindow.setColumns(3); displayWindow.setForeground(Color.blue); displayWindow.setEditable(false); JLabel label1 = new JLabel("Score: "); label1.setLabelFor(displayWindow); menubar.add(label1); menubar.add(displayWindow); /* Create the level window. */ menubar.add(new JLabel(" ")); levelWindow = new JFormattedTextField(); levelWindow.setValue("Beginner"); levelWindow.setColumns(8); levelWindow.setForeground(Color.blue); levelWindow.setEditable(false); JLabel label2 = new JLabel("Level: "); label2.setLabelFor(levelWindow); menubar.add(label2); menubar.add(levelWindow); menubar.add(new JLabel(" ")); /* Create the Snake window. */ player = new Snake(getSize().width, getSize().height-30);//, food); player.addKeyListener(this); player.addMouseListener(this); /* Draw a border around the play field */ Border redline = BorderFactory.createLineBorder(Color.red); Border raisedbevel = BorderFactory.createRaisedBevelBorder(); Border loweredbevel = BorderFactory.createLoweredBevelBorder(); Border compound = BorderFactory.createCompoundBorder(raisedbevel, loweredbevel); compound = BorderFactory.createCompoundBorder(redline, compound); player.setBorder(compound); /* add the play panel to the main panel */ getContentPane().add(player); setVisible(true); } //runs after the applet is initialized and any time it has been "paused" public void start() { //if no animation is running if(animate == null) { animate = new Thread(this); animate.start(); } } //runs while the applet is active public void run() { //while an animation should be running while(animate != null) { try { //a frame rate depends on the game level animate.sleep(115 + 15*(3-level)); player.move(); updateScore(); } catch(InterruptedException e) {} } } //runs when the animation is "paused" public void stop() { animate = null; repaint(); } /** * Request a current score from the player * */ public void updateScore() { displayWindow.setText(player.getScore()+""); } /** * Actions handler * */ public void actionPerformed(ActionEvent e) { String command = e.getActionCommand(); if(command.equals("Pause")) { player.pause(false); } else if(command.equals("Continue")) { player.pause(true); } else if(command.equals("New Game")) { player.pause(true); player.draw(level); } else if(command.equals("Beginner")) { level = 1; player.draw(1); levelWindow.setText("Beginner"); } else if(command.equals("Intermediate")) { level = 2; player.draw(2); levelWindow.setText("Intermediate"); } else if(command.equals("Advanced")) { level = 3; player.draw(3); levelWindow.setText("Advanced"); } else if (command.equals("Magenta")) player.setBackground(new Color(1f,0.651f,1f)); else if (command.equals("Cyan")) player.setBackground(new Color(0.5945f,1f,1f)); else if (command.equals("Yellow")) player.setBackground(new Color(1f,1f,0.501961f)); else if (command.equals("Blue")) player.setBackground(new Color(0.65f,0.827f,01f)); else if (command.equals("White")) player.setBackground(Color.white); else if (command.equals("Instructions")) { JOptionPane.showMessageDialog(null, "S - New Game\nP - Pause\nC - Continue\n\nUse arrow keys for navigation\n\nctrl+B - Beginner level\nctrl+I - Intermediate level\nctrl+A - Advanced level"); } if(selfCrossing.isSelected()) player.setCrossing(true); else player.setCrossing(false); } /** * KeyListener handlers * */ public void keyPressed (KeyEvent e) { switch(e.getKeyCode()) { case KeyEvent.VK_DOWN: player.setDirection('D'); break; case KeyEvent.VK_UP: player.setDirection('U'); break; case KeyEvent.VK_LEFT: player.setDirection('L'); break; case KeyEvent.VK_RIGHT: player.setDirection('R'); break; } } public void keyReleased(KeyEvent e){} public void keyTyped(KeyEvent e){} /** * MouseListener handlers * */ public void mousePressed(MouseEvent e) { player.requestFocus(); // Request the focus } public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public void mouseReleased(MouseEvent e) {} public void mouseClicked(MouseEvent e) { if(firstClick) //to start the game { firstClick = false; player.pause(true); player.draw(level); } } }