This document contains the code for a Simon game controller class in Java. The class initializes 4 colored buttons in a grid layout, loads sound effects for the buttons and a "razz" sound. It plays sequences of button presses by iterating through a Song object and checks if the user correctly presses the buttons. If correct, the button flashes, otherwise it plays the razz sound and restarts the round.
This document contains the code for a Simon game controller class in Java. The class initializes 4 colored buttons in a grid layout, loads sound effects for the buttons and a "razz" sound. It plays sequences of button presses by iterating through a Song object and checks if the user correctly presses the buttons. If correct, the button flashes, otherwise it plays the razz sound and restarts the round.
This document contains the code for a Simon game controller class in Java. The class initializes 4 colored buttons in a grid layout, loads sound effects for the buttons and a "razz" sound. It plays sequences of button presses by iterating through a Song object and checks if the user correctly presses the buttons. If correct, the button flashes, otherwise it plays the razz sound and restarts the round.
public class SimonController extends Controller implements NoisyButtonListener {
private static final int BUTTONCOUNT = 4; // the number of distinct sounds //corre sponding to the game //butt ons public NoisyButton button; private static final int COLORINTENSITY = 180; // how bright to make buttons private int delay = 300; private AudioClip nastyNoise; // a razzing noise
public AudioClip tone0, tone1, tone2, tone3; // button noises
private ButtonCollection collect = new ButtonCollection(); private Song song = new Song(); private NoisyButton rndmbutton; private Iterator irtr;
// create the display of four buttons on the screen
public void begin() { setSize(400, 400);
// buttons should appear in a grid
getContentPane().setLayout(new GridLayout(2, 2));
// load the nasty noise
nastyNoise = getAudio("razz.au");
// create an array of colors for the buttons
Color shades[] = new Color[BUTTONCOUNT]; shades[0] = new Color(COLORINTENSITY, 0, 0); shades[1] = new Color(0, COLORINTENSITY, 0); shades[2] = new Color(0, 0, COLORINTENSITY); shades[3] = new Color(COLORINTENSITY, COLORINTENSITY, 0);
for (int buttonNum = 0; buttonNum < BUTTONCOUNT; buttonNum++)
{ AudioClip curSound = getAudio("tone." + buttonNum + ".au"); // create button using csound and color // add it to the ButtonCollection button = new NoisyButton(curSound, shades[buttonNum]); collect.ata(button, buttonNum); // add yourself as a listener button.addListener(this); // add it to the contentPane getContentPane().add(button); } // validate // create and add buttons // add the panel of buttons to the window
validate();
round(); }
// Check to see if the player clicked the expected button
public void noisyButtonClicked(NoisyButton theButton) { //round(); if (theButton == irtr.next() ) { theButton.flash(); if(!irtr.hasNext()){ round(); } } else { nastyNoise.play(); song= new Song(); round(); }
public void round(){
song.atal(collect.randomButton()); new SongPlayer(song); irtr = song.getIterator(); } }