Society & Culture & Entertainment Games

How to Make a Buzzer Game

    • 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). 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 center 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 center. Right-click that label and choose "Change Variable Name." Change the variable name from "jLabel1" to "questionLabel." Click the "Source" button.

    • 3). Create and initialize 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). 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). 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(rootPane, "Yes!");
      } else { JOptionPane.showMessageDialog(rootPane, "No!");
      }

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

You might also like on "Society & Culture & Entertainment"

#

How to Setup a PS3 Wireless

#

Azada FAQs

#

Mahjong Rules

#

Playing Video Games to Get Rich

#

What is DC Power Supply Used for?

#

Hula Hoop Dangers

#

Download Free PSP Games

Leave a reply