forked from League-Archive/IntroToJavaWorkshop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScaryMaze.java
More file actions
executable file
·183 lines (119 loc) · 4.69 KB
/
ScaryMaze.java
File metadata and controls
executable file
·183 lines (119 loc) · 4.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package section5;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class ScaryMaze extends JPanel implements Runnable, MouseMotionListener {
/*********** SOUND ***************
* Some computers are unable to play sounds.
* If you cannot play sound on this computer, set canPlaySounds to false.
* If you are not sure, ask your teacher
* *****************/
boolean canPlaySounds = true;
boolean started = false;
BufferedImage maze;
final int frameWidth = 500;
final int frameHeight = 500;
ScaryMaze() throws Exception {
//1. Use this online tool (https://www.pixilart.com/) to make a maze image. Size = 500x500
// The maze must be drawn using 4 different colors, one each for: START, END, BACKGROUND, and path.
// Download your maze image to the computer.
// Drag and drop it into the section5 package
//2. Change the line of code below so that it uses YOUR maze's file name
maze = ImageIO.read(getClass().getResource("standardMaze.png"));
//3. Run the program. Do you see your maze? Don't continue until you do.
// Leave this code here! It will allow our program to know when the mouse moves.
addMouseMotionListener(this);
}
@Override
public void mouseMoved(MouseEvent e) {
int mouseX = e.getX();
int mouseY = e.getY();
int mouseColor = maze.getRGB(mouseX, mouseY);
//4. Print the mouseColor variable (Hint: use syso)
//5. Run your program and move your mouse over the START COLOR. A number will be printed to the console
int startColor=0;
//6. Change the value of this startColor variable to the number printed in the previous step.
// Leave this code here!
// It makes sure the game will not start until the mouse has visited the start area.
if (!started && mouseColor==startColor) {
started = true;
}
//7. Make a new int variable for the background color of the maze
//8. Run the program and move the mouse over the BACKGROUND COLOR.
// Use the number that is printed to the console to set the background color variable
if (started) {
//9. If the mouse falls off the path (which means it is on the background color)
// call the scare method - scare();
//13. If the mouse is on the end color, pop up a message to tell them they won!
// (you will need to get the number of the END COLOR by moving the mouse over it)
}
}
private void scare() {
//Scare your player with scary messages, sounds and pictures
System.out.println("BOO!");
if (canPlaySounds) {
/********** PLAY A SOUND ***************/
//10. Find a scary sound and put it in the section5 package (where you put your maze picture).
// You can find a sound on freesound.org. Log in as leagueofamazing/code4life.
//11. Play the scary sound. Hint: use the playScarySound method with the name of your sound file
}
/********** SHOW A PICTURE ***************/
//12. Find a scary image and drop it into the section5 package.
// Use the showScaryImage method below and send it the name of your picture file
}
/********** DO NOT CHANGE THE CODE BELOW THIS LINE ***************/
private void playScarySound(String soundName) {
File sound = new File("src/section5/"+soundName);
if (sound.exists()) {
try {
Clip clip = AudioSystem.getClip();
clip.open(AudioSystem.getAudioInputStream(sound));
clip.start();
Thread.sleep(clip.getMicrosecondLength()/1000);
}
catch (Exception e) {
System.out.println("Could not play this sound");
}
}
else {
System.out.println("File does not exist");
}
}
private void showScaryImage(String imageName) {
try {
maze = ImageIO.read(getClass().getResource(imageName));
} catch (Exception e) {
System.err.println("Could not find this image: " + imageName);
}
repaint();
}
public static void main(String[] args) throws Exception {
SwingUtilities.invokeLater(new ScaryMaze());
}
@Override
public void run() {
JFrame frame = new JFrame("Scary Maze");
frame.add(this);
setPreferredSize(new Dimension(frameWidth, frameHeight));
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setVisible(true);
}
@Override
public void paintComponent(Graphics g) {
g.drawImage(maze, 0, 0, null);
}
@Override
public void mouseDragged(MouseEvent e) {}
}