|
| 1 | +package day3; |
| 2 | +import java.awt.KeyEventDispatcher; |
| 3 | +import java.awt.KeyboardFocusManager; |
| 4 | +import java.awt.event.KeyEvent; |
| 5 | +import java.net.MalformedURLException; |
| 6 | + |
| 7 | +import javax.swing.JOptionPane; |
| 8 | + |
| 9 | +import org.teachingextensions.logo.ImageBackground; |
| 10 | +import org.teachingextensions.logo.Paintable; |
| 11 | +import org.teachingextensions.logo.Tortoise; |
| 12 | + |
| 13 | +/** Note: You will need the latest version of the TKP jar: http://school.wintrisstech.org/jars/TeachingKidsProgramming.jar **/ |
| 14 | + |
| 15 | +public class TurtleTreasureHunt implements KeyEventDispatcher { |
| 16 | + |
| 17 | + int tortoiseSpeed = 5; |
| 18 | + |
| 19 | + private void goUp() { |
| 20 | + // 1. Make the tortoise move up the screen |
| 21 | + Tortoise.move(10); |
| 22 | + } |
| 23 | + |
| 24 | + private void goDown() { |
| 25 | + // 2. make the tortoise move down the screen |
| 26 | + Tortoise.move(-10); |
| 27 | + } |
| 28 | + |
| 29 | + private void goLeft() { |
| 30 | + // 3. make the tortoise move left (3 lines of code) |
| 31 | + // Hint: the turn() method lags more than setAngle() |
| 32 | + Tortoise.setAngle(-90); |
| 33 | + Tortoise.move(50); |
| 34 | + Tortoise.setAngle(0); |
| 35 | + |
| 36 | + |
| 37 | + } |
| 38 | + |
| 39 | + private void goRight() { |
| 40 | + // 4. make the tortoise move right |
| 41 | + Tortoise.setAngle(90); |
| 42 | + Tortoise.move(50); |
| 43 | + Tortoise.setAngle(0); |
| 44 | + } |
| 45 | + |
| 46 | + private void spaceBarWasPressed() { |
| 47 | + int tortoiseLocationX = Tortoise.getX(); |
| 48 | + int tortoiseLocationY = Tortoise.getY(); |
| 49 | + |
| 50 | + // 5. Print out the variables for tortoiseLocationX and tortoiseLocationY |
| 51 | + System.out.println(tortoiseLocationX); |
| 52 | + System.out.println ( tortoiseLocationY); |
| 53 | + // 6. If tortoise is at same location as the little girl, |
| 54 | + // make a pop-up tell the Tortoise where to go next |
| 55 | + if(tortoiseLocationX == 520 && tortoiseLocationY==280){ |
| 56 | + JOptionPane.showMessageDialog(null, "Go to the eye"); |
| 57 | + } |
| 58 | + // 7. Give the user subsequent clues at different locations on the image |
| 59 | + // (pirate robot, swamp, parrots, etc.) |
| 60 | + if(tortoiseLocationX == 170 && tortoiseLocationY==50){ |
| 61 | + JOptionPane.showMessageDialog(null, "Go to the little boy"); |
| 62 | + } |
| 63 | + if(tortoiseLocationX == 120 && tortoiseLocationY==290){ |
| 64 | + JOptionPane.showMessageDialog(null, "You win"); |
| 65 | + } |
| 66 | + |
| 67 | + } |
| 68 | + |
| 69 | + private void go() { |
| 70 | + KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(this); |
| 71 | + /* |
| 72 | + * If you want to use your own background, download the image you want, and change the following line to point to it like: new |
| 73 | + * ImageBackground("file:/Users/joonspoon/Desktop/dinosaurLand.jpg"); |
| 74 | + */ |
| 75 | + Paintable backgroundImage = new ImageBackground("file:/Users/League/Google Drive/league-images/treasure_hunt.jpg"); |
| 76 | + Tortoise.getBackgroundWindow().addPaintable(backgroundImage); |
| 77 | + Tortoise.penUp(); |
| 78 | + JOptionPane.showMessageDialog(null, "Ask the little girl for help with your quest. Press the space bar to ask."); |
| 79 | + } |
| 80 | + |
| 81 | + public boolean dispatchKeyEvent(KeyEvent e) { |
| 82 | + if (e.getID() == KeyEvent.KEY_PRESSED) { |
| 83 | + if (e.getKeyCode() == KeyEvent.VK_RIGHT) |
| 84 | + goRight(); |
| 85 | + else if (e.getKeyCode() == KeyEvent.VK_LEFT) |
| 86 | + goLeft(); |
| 87 | + else if (e.getKeyCode() == KeyEvent.VK_UP) |
| 88 | + goUp(); |
| 89 | + else if (e.getKeyCode() == KeyEvent.VK_DOWN) |
| 90 | + goDown(); |
| 91 | + else if (e.getKeyCode() == KeyEvent.VK_SPACE) |
| 92 | + spaceBarWasPressed(); |
| 93 | + } |
| 94 | + return false; |
| 95 | + } |
| 96 | + |
| 97 | + public static void main(String[] args) throws MalformedURLException { |
| 98 | + new TurtleTreasureHunt().go(); |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | + |
0 commit comments