Guest User

Untitled

a guest
May 26th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.02 KB | None | 0 0
  1. import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot)
  2. import java.awt.Font;
  3. /**
  4. * A piano that can be played with the computer keyboard.
  5. *
  6. * @author: Martina Nobile
  7. * @class: Mr.Boss
  8. * @courseCode: ICS3U1
  9. * @date: May 12 2016
  10. *
  11. */
  12. public class Piano extends World
  13. {
  14. //initializes white keys, black keys, and their corresponding sounds
  15. private String [] whiteKeys = {"q", "w", "e", "r", "t", "y", "u", "i", "o", "p", "[", "]"};
  16. private String [] blackKeys = {"2", "3", "", "5", "6", "7", "", "9", "0", "", "="};
  17. private String [] whiteNotes = {"3c", "3d", "3e", "3f", "3g", "3a", "3b", "4c", "4d", "4e", "4f", "4g"};
  18. private String [] blackNotes = {"3c#", "3d#", "", "3f#", "3g#", "3a#", "", "4c#", "4d#", "", "4f#"};
  19. //initializes the screen and its height
  20. private Visualizer screen = new Visualizer();
  21. private static final int SCREEN_HEIGHT = 450;
  22.  
  23. /**
  24. * Make the piano.
  25. */
  26. public Piano()
  27. {
  28. super(800, 340+SCREEN_HEIGHT, 1);
  29. whiteKeys();
  30. blackKeys();
  31. addObject(screen, getWidth()/2, screen.getImage().getHeight()/2);
  32. prepare();
  33. }
  34.  
  35. public void prepare()
  36. {
  37. Wombat wombat = new Wombat();
  38. addObject(wombat, 75, 75);
  39. for(int i = 0; i < 15; i++)
  40. {
  41. Leaf leaf = new Leaf();
  42. addObject(leaf, Greenfoot.getRandomNumber(770)+15, Greenfoot.getRandomNumber(310)+15);
  43. }
  44. showText("Click 'Run', then use your keyboard to play.", 400, 745);
  45. showText("The white keys move the wombat forward and the black keys turn it.", 400, 770);
  46. }
  47.  
  48. public void whiteKeys()
  49. {
  50. for(int i = 0; i < whiteKeys.length; i++)
  51. {
  52. //adds keys based on array length
  53. //passes letter on keyboard the corresponds with the note, sound file, and the colour of the key to the Key class
  54. Key wKey = new Key(whiteKeys[i], whiteNotes[i] + ".wav", "white");
  55. wKey.setVisualizer(screen);
  56. int width = wKey.getImage().getWidth();
  57. int height = wKey.getImage().getHeight();
  58. //adds the key to the world
  59. addObject(wKey, i*(width)+54, height/2+SCREEN_HEIGHT);
  60. }
  61. }
  62.  
  63. public void blackKeys()
  64. {
  65. for(int i = 0; i < blackKeys.length; i++)
  66. {
  67. if(!(blackKeys[i].equals("")))
  68. {
  69. //adds keys based on array length
  70. //passes letter on keyboard that corresponds with the note, sound file, and the color of the key to the Key class
  71. Key bKey = new Key(blackKeys[i], blackNotes[i] + ".wav", "black");
  72. bKey.setVisualizer(screen);
  73. int height = bKey.getImage().getHeight();
  74. //adds the key to the world
  75. addObject(bKey, i*63+84, height/2+SCREEN_HEIGHT);
  76. }
  77. }
  78. }
  79. }//end of Piano class
  80.  
  81. public class Wombat extends Actor
  82. {
  83. private String [] whiteKeys = {"q", "w", "e", "r", "t", "y", "u", "i", "o", "p", "[", "]"};
  84. private String [] blackKeys = {"2", "3", "5", "6", "7", "9", "0", "="};
  85. private boolean isDown;
  86.  
  87.  
  88. public Wombat()
  89. {
  90. //sets isDown to false to initialize that the keys at rest are not being pressed
  91. isDown = false;
  92. }
  93.  
  94. public void act()
  95. {
  96. white();
  97. black();
  98. eat();
  99. }
  100.  
  101. private void white()
  102. {
  103. for(int i = 0; i < whiteKeys.length; i++)
  104. {
  105. //if key has just been pressed move the wombat
  106. if(!isDown && Greenfoot.isKeyDown(whiteKeys[i]))
  107. {
  108. move(1);
  109. isDown = true;
  110. }
  111. //if the key was just pressed but is not being currently pressed reset isDown to false
  112. if(isDown && !Greenfoot.isKeyDown(whiteKeys[i]))
  113. {
  114. isDown = false;
  115. }
  116. //if the wombat hits the edge of the screen, turn it
  117. if(isAtEdge())
  118. {
  119. turn(1);
  120. }
  121. }
  122. }
  123.  
  124. private void black()
  125. {
  126. for(int a = 0; a < blackKeys.length; a++)
  127. {
  128. //if key has just been pressed move the wombat
  129. if(!isDown && Greenfoot.isKeyDown(blackKeys[a]))
  130. {
  131. turn(3);
  132. isDown = true;
  133. }
  134. //if the key was just pressed but is not being currently pressed reset isDown to false
  135. if(isDown && !Greenfoot.isKeyDown(blackKeys[a]))
  136. {
  137. isDown = false;
  138. }
  139. //if the wombat hits the edge of the screen, turn it
  140. if(isAtEdge())
  141. {
  142. turn(1);
  143. }
  144. }
  145. }
  146.  
  147. private void eat()
  148. {
  149. //gets information about the height and width of the wombat image
  150. GreenfootImage wombat = getImage();
  151. int width = wombat.getWidth();
  152. int height = wombat.getHeight();
  153. //if the wombat is touching a leaf, remove the leaf and increase the size of the wombat image
  154. if(isTouching(Leaf.class))
  155. {
  156. removeTouching(Leaf.class);
  157. wombat.scale(width+2, height+2);
  158. }
  159. }
  160. }//end of Wombat class
  161.  
  162. import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
  163. import java.awt.Color;
  164. /**
  165. * Write a description of class Visualizer here.
  166. *
  167. * @author (your name)
  168. * @version (a version number or a date)
  169. */
  170. public class Visualizer extends Actor
  171. {
  172. //represents the visualizer screen
  173. public GreenfootImage imageS;
  174. //declares an array of different colours for each of the keys
  175. private Color[] colors = new Color[20];
  176.  
  177. public Visualizer()
  178. {
  179. //a random colour is created for each of the 20 keys
  180. for(int i = 0; i < 20; i++)
  181. {
  182. colors[i] = new Color(Greenfoot.getRandomNumber(255), Greenfoot.getRandomNumber(255), Greenfoot.getRandomNumber(255), 100);
  183. }
  184. //the visualizer screen is given dimensions, a colour, and then set as the class image
  185. imageS = new GreenfootImage(800, 450);
  186. Color white = new Color(254, 254, 254, 100);
  187. imageS.setColor(white);
  188. imageS.fill();
  189. setImage(imageS);
  190. }
  191.  
  192. public void visualize(Key k)
  193. {
  194. //for each of the keys, the width, height, x, and y co-ordinates are determined
  195. int width = k.getImage().getWidth();
  196. int x = k.getX() - width / 2;
  197. int drawHeight = imageS.getHeight() - width / 2;
  198. int y = Greenfoot.getRandomNumber(drawHeight);
  199. //variable is declared to file through the colour array
  200. int index = getKeyNumber(k);
  201. //colour is set, then rectangular space above the key is drawn in
  202. imageS.setColor(colors[index]);
  203. imageS.fillRect(x, y, width, drawHeight);
  204. }
  205.  
  206. public int getKeyNumber(Key k)
  207. {
  208. //files through each of the keys and assigns them a number
  209. if (k == null){
  210. return 0;
  211. }
  212. int keyNum = (k.getX() - 54) / 31;
  213. if (keyNum > 4){
  214. keyNum--;
  215. }
  216. if (keyNum > 11){
  217. keyNum--;
  218. }
  219. if (keyNum > 16){
  220. keyNum--;
  221. }
  222. return keyNum;
  223. }
  224. }//end of Visualizer class
  225.  
  226. public class Key extends Actor
  227. {
  228. private boolean isDown;
  229. private String key, sound, keyColour;
  230. private Visualizer screen;
  231.  
  232.  
  233. /**
  234. * Create a new key.
  235. */
  236. public Key(String keyName, String keySound, String colour)
  237. {
  238. //receives information from Piano about the key name, sound, and colour
  239. //sets the class image as a key based on the colour it is passed
  240. isDown= false;
  241. key = keyName;
  242. sound = keySound;
  243. keyColour = colour;
  244. setImage((keyColour) + "-key.png");
  245. }
  246.  
  247. /**
  248. * Do the action for this key.
  249. */
  250. public void act()
  251. {
  252. //if the key has just been pressed, sets the image to key down and plays the note
  253. if(!isDown && Greenfoot.isKeyDown(key))
  254. {
  255. setImage((keyColour) + "-key-down.png");
  256. playSound();
  257. isDown = true;
  258. }
  259. //if the key was jut pressed but is no longer being played, set isDown to false
  260. if(isDown && !Greenfoot.isKeyDown(key))
  261. {
  262. setImage((keyColour) + "-key.png");
  263. isDown = false;
  264. }
  265. }
  266.  
  267. public void playSound()
  268. {
  269. //plays a note based on the sound file passed to it
  270. Greenfoot.playSound(sound);
  271. if (screen != null)
  272. {
  273. screen.visualize(this);
  274. }
  275. }
  276.  
  277. //sets the visualizer
  278. public void setVisualizer(Visualizer visualizer)
  279. {
  280. screen = visualizer;
  281. }
  282. }//end of Key class
  283.  
  284. /**
  285. * Is a picture of a leaf.
  286. *
  287. * @author Martina Nobile
  288. */
  289. public class Leaf extends Actor
  290. {
  291. public void act()
  292. {
  293.  
  294. }
  295. }
Add Comment
Please, Sign In to add comment