Skip to content

Commit 9a5e9f9

Browse files
dhruvwa16dhruvwa16
authored andcommitted
Day 4
1 parent 9a741e8 commit 9a5e9f9

4 files changed

Lines changed: 281 additions & 0 deletions

File tree

src/day3/DontForgetTheLyrics.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package day3;
2+
import java.applet.AudioClip;
3+
4+
import javax.swing.JApplet;
5+
import javax.swing.JOptionPane;
6+
7+
public class DontForgetTheLyrics {
8+
/*
9+
* For this game, we'll play the start of a song, and the player has to guess the rest of the lyrics.
10+
*
11+
* 1. To record a sound clip, open Audacity and record your song.
12+
*
13+
* 2. Click File -> Export Audio, and save your file on the Desktop.
14+
*
15+
* 3. Drag your file from the Desktop into the "default package" under "src".
16+
*/
17+
18+
public static void main(String[] args) {
19+
// 4. Make a pop-up to explain the game.
20+
21+
// 5. Use the playSound method to play your song.
22+
23+
// 6. Make a pop-up for the player to type their answer.
24+
25+
// 7. If they answered correctly, tell them that they were right.
26+
27+
// 8. Otherwise, tell them they are wrong, and give them the answer.
28+
29+
// 9. Record another sound and repeat steps 5-8.
30+
31+
// 10. [optional] Add a points variable that will calculate their final score.
32+
}
33+
34+
public static void playSound(String fileName) {
35+
AudioClip audioClip = JApplet.newAudioClip(new DontForgetTheLyrics().getClass().getResource(fileName));
36+
audioClip.play();
37+
}
38+
}
39+
40+

src/day3/Magic8Ball.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package day3;
2+
// Copyright Wintriss Technical Schools 2013
3+
import java.util.Random;
4+
5+
import javax.swing.JOptionPane;
6+
7+
public class Magic8Ball {
8+
9+
// 1. Make a main method that includes all the steps below….
10+
public static void main(String[] args) {
11+
12+
// 2. Make a variable that will hold a random number and put a random number into this variable using "new Random().nextInt(4)"
13+
Random rng = new Random();
14+
int x = rng.nextInt(4);
15+
// 3. Print out this variable
16+
System.out.println(x);
17+
// 4. Get the user to enter a question for the 8 ball
18+
JOptionPane.showInputDialog(null, "Enter a question");
19+
// 5. If the random number is 0
20+
if(x == 0){
21+
JOptionPane.showMessageDialog(null, "Yes");
22+
}
23+
// -- tell the user "Yes"
24+
25+
// 6. If the random number is 1
26+
if(x == 1){
27+
JOptionPane.showMessageDialog(null, "No");
28+
29+
}
30+
// -- tell the user "No"
31+
32+
// 7. If the random number is 2
33+
if(x == 2){
34+
JOptionPane.showMessageDialog(null, "Maybe you should ask Google?");
35+
36+
}
37+
// -- tell the user "Maybe you should ask Google?"
38+
39+
// 8. If the random number is 3
40+
if(x == 3){
41+
JOptionPane.showMessageDialog(null, "Ask again");
42+
}
43+
}
44+
}
45+
46+
// -- write your own answer
47+
48+
49+

src/day3/TortoiseInSpace.java

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package day3;
2+
import java.awt.KeyEventDispatcher;
3+
import java.awt.KeyboardFocusManager;
4+
import java.awt.event.KeyEvent;
5+
import java.io.File;
6+
7+
import javax.sound.sampled.AudioInputStream;
8+
import javax.sound.sampled.AudioSystem;
9+
import javax.sound.sampled.Clip;
10+
11+
import org.teachingextensions.logo.Tortoise;
12+
13+
public class TortoiseInSpace implements KeyEventDispatcher {
14+
15+
/* Make the Tortoise move around the screen when the arrow keys are pressed... */
16+
private void moveTortoise(int keyPressed) {
17+
// 0. Print out the keyPressed variable and write down the numbers for each arrow key
18+
System.out.println(keyPressed);
19+
// 1. If the up arrow is pressed, move the Tortoise up the screen.
20+
if(keyPressed == 38){
21+
Tortoise.move(10); }
22+
// 2. If the down arrow is pressed, move the Tortoise down.
23+
if(keyPressed == 40){
24+
Tortoise.move(-10);}
25+
// 3. If the left arrow is pressed, make the tortoise go left. Hint: Make sure to end with the Tortoise facing UP.
26+
if(keyPressed == 37){
27+
Tortoise.turn(-90);
28+
Tortoise.move(10);
29+
Tortoise.turn(90);
30+
}
31+
32+
// 4. If right is pressed, move the Tortoise right.
33+
if(keyPressed == 39){
34+
Tortoise.turn(90);
35+
Tortoise.move(10);
36+
Tortoise.turn(-90);
37+
38+
39+
}
40+
// 5. Then move the Tortoise to RD-2D for a surprise!
41+
}
42+
43+
44+
private void checkIfR2D2Found() throws Exception {
45+
int tortoiseLocationX = Tortoise.getX();
46+
int tortoiseLocationY = Tortoise.getY();
47+
48+
if (tortoiseLocationX <= 510 && tortoiseLocationX >= 505 && tortoiseLocationY >= 110 && tortoiseLocationY <= 115)
49+
playEureka();
50+
}
51+
52+
public static void main(String[] args) {
53+
new TortoiseInSpace().controlTheTortoise();
54+
}
55+
56+
private void controlTheTortoise() {
57+
KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(this);
58+
Tortoise.getBackgroundWindow().setBackgroundImage(
59+
"http://cdn.playbuzz.com/cdn/ae928505-70fe-4538-8fc4-034d1acef441/b7a29f96-7410-440b-9d93-3cfc0abccce9.jpg");
60+
61+
Tortoise.penUp();
62+
Tortoise.setSpeed(10);
63+
}
64+
65+
public boolean dispatchKeyEvent(KeyEvent e) {
66+
if (e.getID() == KeyEvent.KEY_PRESSED) {
67+
moveTortoise(e.getKeyCode());
68+
try {
69+
checkIfR2D2Found();
70+
} catch (Exception exception) {
71+
}
72+
}
73+
return false;
74+
}
75+
76+
public void playEureka() {
77+
System.out.println("EUREKA!");
78+
try {
79+
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File(
80+
"/Users/League/Google Drive/league-sounds/r2d2-eureka.wav"));
81+
Clip clip = AudioSystem.getClip();
82+
clip.open(audioInputStream);
83+
clip.start();
84+
} catch (Exception ex) {
85+
ex.printStackTrace();
86+
}
87+
}
88+
89+
}
90+

src/day3/TurtleTreasureHunt.java

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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

Comments
 (0)