How to Make a Buzzer Game

Written by amber d. walker
  • Share
  • Tweet
  • Share
  • Email

Many people love buzzer games like Jeopardy on TV. Did you know that creating a simple buzzer-based quiz game for your PC using Java is fairly simple, provided that you use NetBeans and the standard Swing library for your GUI and user input handling? Players can use their different keys on a single, ordinary keyboard as their buzzers, and questions can be presented through a simple game window.

Skill level:
Moderate

Other People Are Reading

Things you need

  • Java
  • NetBeans IDE

Show MoreHide

Instructions

  1. 1

    Create a Java project by click "File" and "New Project." You'll want to use "Java Application" as your project template. This tutorial will assume that you use the NetBeans IDE, but most of the steps can be performed with minimum alteration using another Java IDE, such as Eclipse. Name your project "BuzzerGame."

  2. 2

    Click on the "Design" button to go to the GUI form designer in NetBeans, and you should be presented with a small, blank window in the centre of your screen. From the palette pane on the right side of your window, drag a label into your window and place it somewhere near the centre. Right-click that label and choose "Change Variable Name." Change the variable name from "jLabel1" to "questionLabel." Click the "Source" button.

  3. 3

    Create and initialise some variables. You'll need to store at least two variables for this program, which are the key codes on the keyboard that will represent each player's buzzer. Write the following:

    int playerOneKeyCode; int playerTwoKeyCode;

    Go to the BuzzerGame default constructor and, at the bottom, add the following two lines of code:

    playerOneKeyCode = java.awt.KeyEvent.VK_A; playerTwoKeyCode = java.awt.KeyEvent.VK_L;

    This will set player one as using the "A" key on the keyboard as his buzzer, and player two as using the "L" key. The VK stands for "Virtual Key code," and the Javadoc for KeyEvent has constants defined for every key on a standard U.S. keyboard, plus a few more.

  4. 4

    Set up the question. There is one last line to add to the constructor.

    questionLabel.setText("How many stars are on the U.S. flag?");

  5. 5

    Set up user input. Click on the "Design" button again, and right-click anywhere on the form (but not on the label you defined earlier). Choose "Events," "Key," and "KeyPressed" from the pop-up menu. This will automatically create a skeleton function "formKeyPressed" in your source code, and take you to it. Add the following code to this skeleton function:

    if (evt.getKeyCode() == playerOneKeyCode) { String answer = JOptionPane.showInputDialog("Player one, give your answer."); if (answer.equals("50")) { JOptionPane.showMessageDialog(root Pane, "Yes!"); } else { JOptionPane.showMessageDialog(root Pane, "No!"); }

    } else if (evt.getKeyCode() == playerTwoKeyCode) { String answer = JOptionPane.showInputDialog("Player two, give your answer."); if (answer.equals("50")) { JOptionPane.showMessageDialog(root Pane, "Yes!"); } else { JOptionPane.showMessageDialog(root Pane, "No!");} }

Tips and warnings

  • This tutorial gives you the basic functionality for your buzzer game, and a number of enhancements can be made. Question could have its own class, stored in a java.util.ArrayList that is iterated through in a game loop. Score can be kept and a couple of JLabel objects placed on the form to report each player's score. Players could be asked by message dialogues to choose their own buzzer keys. Finally, a player class could be created to store multiple players, also stored in a dynamic java.util.ArrayList, potentially allowing dozens of players at once.
  • Notice that the program, when it compares the user's answer against the right answer, uses the code answer.equals("50"). It is very important to remember that, when comparing Java strings with the usual == operator, the strings are not compared, but rather their addresses in memory! To compare the text of the strings and not merely their memory addresses, the aString.equals(anotherString) function must be used.

Don't Miss

Filter:
  • All types
  • Articles
  • Slideshows
  • Videos
Sort:
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the eHow.co.uk site, you consent to the use of cookies. For more information, please see our Cookie policy.