I'm creating a game where the user has to play the piano and questions are displayed asking for specific keys to be pressed. So far, I have a subclass of World called Piano which contains the questions and key bindings, and also have of Actor called Key which plays notes when the corresponding keys on the keyboard are pressed.
How would I be able to make it so when a key is pressed, It registers it so that once all the keys have been pressed it asks the next question?.
import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot)
public class Piano extends World
{
private String[] whiteKeys =
{ "A", "S", "D", "F", "G", "H", "J", "K", "L", ";", "'", "#" };
private String[] whiteNotes =
{ "3c", "3d", "3e", "3f", "3g", "3a", "3b", "4c", "4d", "4e", "4f", "4g" };
private String[] blackKeys =
{ "W", "E", "", "T", "Y", "U", "", "O", "P", "", "]" };
private String[] blackNotes =
{ "3c#", "3d#", "", "3f#", "3g#", "3a#", "", "4c#", "4d#", "", "4f#", "",};
/**
* Make the piano. This means mostly, apart from defining the size,
* making the keys and placing them into the world.
*/
public Piano()
{
super(800, 450, 1);
makeKeys();
showMessage();
}
/**
* Create the piano keys (white and black) and place them in the world.
*/
private void makeKeys()
{
// make the white keys
for(int i = 0; i < whiteKeys.length; i++) {
Key key = new Key(whiteKeys[i], whiteNotes[i]+".wav", "Red-key.png", "Red-key-down.png");
addObject(key, 54 + (i*63), 140);
}
// make the black keys
for(int i = 0; i < whiteKeys.length-1; i++) {
if( ! blackKeys[i].equals("") ) {
Key key = new Key(blackKeys[i], blackNotes[i]+".wav", "black-key.png", "black-key-down.png");
addObject(key, 85 + (i*63), 86);
}
}
}
public void showMessage()
{
GreenfootImage bg = getBackground();
bg.setColor(Color.WHITE);
bg.drawString("Play notes", 25, 320);
}
}import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot)
public class Key extends Actor
{
private boolean isDown;
private String key;
private String sound;
private String upImage;
private String downImage;
/**
* Create a new key linked to a given keyboard key, and
* with a given sound.
*/
public Key(String keyName, String soundFile, String img1, String img2)
{
sound = soundFile;
key = keyName;
upImage = img1;
downImage = img2;
setImage(upImage);
isDown = false;
}
/**
* Do the action for this key.
*/
public void act()
{
if (!isDown && Greenfoot.isKeyDown(key)) {
play();
setImage(downImage);
isDown = true;
}
if (isDown && !Greenfoot.isKeyDown(key)) {
setImage(upImage);
isDown = false;
}
}
/**
* Play the note of this key.
*/
public void play()
{
Greenfoot.playSound(sound);
}
}
