Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot)
- import java.awt.Font;
- /**
- * A piano that can be played with the computer keyboard.
- *
- * @author: Martina Nobile
- * @class: Mr.Boss
- * @courseCode: ICS3U1
- * @date: May 12 2016
- *
- */
- public class Piano extends World
- {
- //initializes white keys, black keys, and their corresponding sounds
- private String [] whiteKeys = {"q", "w", "e", "r", "t", "y", "u", "i", "o", "p", "[", "]"};
- private String [] blackKeys = {"2", "3", "", "5", "6", "7", "", "9", "0", "", "="};
- private String [] whiteNotes = {"3c", "3d", "3e", "3f", "3g", "3a", "3b", "4c", "4d", "4e", "4f", "4g"};
- private String [] blackNotes = {"3c#", "3d#", "", "3f#", "3g#", "3a#", "", "4c#", "4d#", "", "4f#"};
- //initializes the screen and its height
- private Visualizer screen = new Visualizer();
- private static final int SCREEN_HEIGHT = 450;
- /**
- * Make the piano.
- */
- public Piano()
- {
- super(800, 340+SCREEN_HEIGHT, 1);
- whiteKeys();
- blackKeys();
- addObject(screen, getWidth()/2, screen.getImage().getHeight()/2);
- prepare();
- }
- public void prepare()
- {
- Wombat wombat = new Wombat();
- addObject(wombat, 75, 75);
- for(int i = 0; i < 15; i++)
- {
- Leaf leaf = new Leaf();
- addObject(leaf, Greenfoot.getRandomNumber(770)+15, Greenfoot.getRandomNumber(310)+15);
- }
- showText("Click 'Run', then use your keyboard to play.", 400, 745);
- showText("The white keys move the wombat forward and the black keys turn it.", 400, 770);
- }
- public void whiteKeys()
- {
- for(int i = 0; i < whiteKeys.length; i++)
- {
- //adds keys based on array length
- //passes letter on keyboard the corresponds with the note, sound file, and the colour of the key to the Key class
- Key wKey = new Key(whiteKeys[i], whiteNotes[i] + ".wav", "white");
- wKey.setVisualizer(screen);
- int width = wKey.getImage().getWidth();
- int height = wKey.getImage().getHeight();
- //adds the key to the world
- addObject(wKey, i*(width)+54, height/2+SCREEN_HEIGHT);
- }
- }
- public void blackKeys()
- {
- for(int i = 0; i < blackKeys.length; i++)
- {
- if(!(blackKeys[i].equals("")))
- {
- //adds keys based on array length
- //passes letter on keyboard that corresponds with the note, sound file, and the color of the key to the Key class
- Key bKey = new Key(blackKeys[i], blackNotes[i] + ".wav", "black");
- bKey.setVisualizer(screen);
- int height = bKey.getImage().getHeight();
- //adds the key to the world
- addObject(bKey, i*63+84, height/2+SCREEN_HEIGHT);
- }
- }
- }
- }//end of Piano class
- public class Wombat extends Actor
- {
- private String [] whiteKeys = {"q", "w", "e", "r", "t", "y", "u", "i", "o", "p", "[", "]"};
- private String [] blackKeys = {"2", "3", "5", "6", "7", "9", "0", "="};
- private boolean isDown;
- public Wombat()
- {
- //sets isDown to false to initialize that the keys at rest are not being pressed
- isDown = false;
- }
- public void act()
- {
- white();
- black();
- eat();
- }
- private void white()
- {
- for(int i = 0; i < whiteKeys.length; i++)
- {
- //if key has just been pressed move the wombat
- if(!isDown && Greenfoot.isKeyDown(whiteKeys[i]))
- {
- move(1);
- isDown = true;
- }
- //if the key was just pressed but is not being currently pressed reset isDown to false
- if(isDown && !Greenfoot.isKeyDown(whiteKeys[i]))
- {
- isDown = false;
- }
- //if the wombat hits the edge of the screen, turn it
- if(isAtEdge())
- {
- turn(1);
- }
- }
- }
- private void black()
- {
- for(int a = 0; a < blackKeys.length; a++)
- {
- //if key has just been pressed move the wombat
- if(!isDown && Greenfoot.isKeyDown(blackKeys[a]))
- {
- turn(3);
- isDown = true;
- }
- //if the key was just pressed but is not being currently pressed reset isDown to false
- if(isDown && !Greenfoot.isKeyDown(blackKeys[a]))
- {
- isDown = false;
- }
- //if the wombat hits the edge of the screen, turn it
- if(isAtEdge())
- {
- turn(1);
- }
- }
- }
- private void eat()
- {
- //gets information about the height and width of the wombat image
- GreenfootImage wombat = getImage();
- int width = wombat.getWidth();
- int height = wombat.getHeight();
- //if the wombat is touching a leaf, remove the leaf and increase the size of the wombat image
- if(isTouching(Leaf.class))
- {
- removeTouching(Leaf.class);
- wombat.scale(width+2, height+2);
- }
- }
- }//end of Wombat class
- import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
- import java.awt.Color;
- /**
- * Write a description of class Visualizer here.
- *
- * @author (your name)
- * @version (a version number or a date)
- */
- public class Visualizer extends Actor
- {
- //represents the visualizer screen
- public GreenfootImage imageS;
- //declares an array of different colours for each of the keys
- private Color[] colors = new Color[20];
- public Visualizer()
- {
- //a random colour is created for each of the 20 keys
- for(int i = 0; i < 20; i++)
- {
- colors[i] = new Color(Greenfoot.getRandomNumber(255), Greenfoot.getRandomNumber(255), Greenfoot.getRandomNumber(255), 100);
- }
- //the visualizer screen is given dimensions, a colour, and then set as the class image
- imageS = new GreenfootImage(800, 450);
- Color white = new Color(254, 254, 254, 100);
- imageS.setColor(white);
- imageS.fill();
- setImage(imageS);
- }
- public void visualize(Key k)
- {
- //for each of the keys, the width, height, x, and y co-ordinates are determined
- int width = k.getImage().getWidth();
- int x = k.getX() - width / 2;
- int drawHeight = imageS.getHeight() - width / 2;
- int y = Greenfoot.getRandomNumber(drawHeight);
- //variable is declared to file through the colour array
- int index = getKeyNumber(k);
- //colour is set, then rectangular space above the key is drawn in
- imageS.setColor(colors[index]);
- imageS.fillRect(x, y, width, drawHeight);
- }
- public int getKeyNumber(Key k)
- {
- //files through each of the keys and assigns them a number
- if (k == null){
- return 0;
- }
- int keyNum = (k.getX() - 54) / 31;
- if (keyNum > 4){
- keyNum--;
- }
- if (keyNum > 11){
- keyNum--;
- }
- if (keyNum > 16){
- keyNum--;
- }
- return keyNum;
- }
- }//end of Visualizer class
- public class Key extends Actor
- {
- private boolean isDown;
- private String key, sound, keyColour;
- private Visualizer screen;
- /**
- * Create a new key.
- */
- public Key(String keyName, String keySound, String colour)
- {
- //receives information from Piano about the key name, sound, and colour
- //sets the class image as a key based on the colour it is passed
- isDown= false;
- key = keyName;
- sound = keySound;
- keyColour = colour;
- setImage((keyColour) + "-key.png");
- }
- /**
- * Do the action for this key.
- */
- public void act()
- {
- //if the key has just been pressed, sets the image to key down and plays the note
- if(!isDown && Greenfoot.isKeyDown(key))
- {
- setImage((keyColour) + "-key-down.png");
- playSound();
- isDown = true;
- }
- //if the key was jut pressed but is no longer being played, set isDown to false
- if(isDown && !Greenfoot.isKeyDown(key))
- {
- setImage((keyColour) + "-key.png");
- isDown = false;
- }
- }
- public void playSound()
- {
- //plays a note based on the sound file passed to it
- Greenfoot.playSound(sound);
- if (screen != null)
- {
- screen.visualize(this);
- }
- }
- //sets the visualizer
- public void setVisualizer(Visualizer visualizer)
- {
- screen = visualizer;
- }
- }//end of Key class
- /**
- * Is a picture of a leaf.
- *
- * @author Martina Nobile
- */
- public class Leaf extends Actor
- {
- public void act()
- {
- }
- }
Add Comment
Please, Sign In to add comment