-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWhackAMoleFillerChallenge.java
More file actions
319 lines (296 loc) · 14.1 KB
/
Copy pathWhackAMoleFillerChallenge.java
File metadata and controls
319 lines (296 loc) · 14.1 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
package com.example.WhackAMoleFillerChallenge;
// Filler code for Whack a Mole by Mr. Friedman
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.*;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
//IMPORTANT: Do not click on bombs, they will explode and deduct your HP by ten!
//IMPORTANT: When you are running out of time, click freeze! It restarts the timer!
//IMPORTANT: If your health drops to 0, you lose! Good Luck!
public class WhackAMoleFillerChallenge {
private Image mole, hole, bg, bomb, over, freeze; //Images
// size of the display area
private int windowWidth = 800, windowHeight = 600, textHeight = 35; //Setting values
private int waitSeconds = 3; //Seconds between each period of time
private int score = 0; //Score
private int health = 100; //Health Points
private int numOfmole = 10; //Number of Moles
private int numGettingout = 5; //Number of Moles popping out
private int moleSize = 75; //Size of mole image
private int holeSize = 75; //Size of hole image
private int bombSize = 75; //Size of bomb image
private int freezeSize = 75; //Size of freeze image
private double adjustValue = 1.2; //Value used to adjust the locations of the images
private int xPos[] = new int[numOfmole]; //x-position of the images
private int yPos[] = new int[numOfmole]; //y-position of the images
private boolean out[] = new boolean[numOfmole]; //array used to test if the mole is popping out
private boolean boom[] = new boolean[numOfmole]; //array used to test if it is a bomb
private boolean cold[] = new boolean[numOfmole]; //array used to test if it is a freeze
private int count = 0; //numbers of moles actually generated
private int sequence = 0; //the number position in the array
private boolean justRight = false; //boolean used to test if the number of moles is correct
private double random = 0; //variable used to generate random numbers
Timer myTimer; //timer
public void setUp(){
bg = Toolkit.getDefaultToolkit().getImage("Background.png"); //Set up background
mole = Toolkit.getDefaultToolkit().getImage("Mole.png"); //Set up moles
hole = Toolkit.getDefaultToolkit().getImage("Hole.png"); //Set up holes
bomb = Toolkit.getDefaultToolkit().getImage("Bomb.png"); //Set up bombs
over = Toolkit.getDefaultToolkit().getImage("Gameover.png"); //Set up gameover
freeze = Toolkit.getDefaultToolkit().getImage("Freeze.png"); //Set up freeze
for (int i = 0; i <= numOfmole - 1; i++){
xPos[i] = (int)(Math.random() * windowWidth / adjustValue); //generate x-position
yPos[i] = (int)(Math.random() * windowHeight / adjustValue); //generate y-position
}
for (int i = 1; i <= numGettingout; i++){
out[(int)(Math.random() * numOfmole)] = true; //determine which moles are popping out
}
while (count < numGettingout){
count = 0; //reset the value to zero
out[(int)(Math.random() * numOfmole)] = true; //set the mole to popping out
for (int i = 0; i <= numOfmole - 1; i++){
if (out[i] == true){
count++; //recount the number of moles popping out
}
}
}
count = 0; //reset the value to zero
for (int i = 0; i <= numOfmole - 1; i++){
if (out[i] == true){
count++; //recount the number of moles popping out
}
}
if (count > numGettingout){ //if there are extra moles popping out
while(count > numGettingout){
if(out[sequence] == true && sequence < numOfmole){
out[sequence] = false; //set the mole to not pop out
count--; //decrease the number by one
}
sequence++; //position in the array increases
}
}
}
public void draw(Graphics g) {
g.drawImage(bg, 0, 0, windowWidth, windowHeight, null); //draw the background
for (int i = 0; i <= numOfmole - 1; i++){
if (out[i] == true) {
g.drawImage(mole, xPos[i], yPos[i], moleSize, moleSize, null); //draw the moles
}
else if (boom[i] == true){
g.drawImage(bomb, xPos[i], yPos[i], bombSize, bombSize, null); //draw the bomb
}
else if (cold[i] == true){
g.drawImage(freeze, xPos[i], yPos[i], freezeSize, freezeSize, null); //draw the freeze
}
else {
g.drawImage(hole, xPos[i], yPos[i], holeSize, holeSize, null); //draw the holes
}
}
if (health <= 0){
g.drawImage(over, 0, 0, windowWidth, windowHeight, null); //draw gameover if health is 0 or less
}
}
// what you want to happen when the mouse is clicked
public void click(int mouseX, int mouseY) {
if (health > 0) {
for (int i = 0; i <= numOfmole - 1; i++) {
if (Math.pow(mouseX - xPos[i] - moleSize / 2, 2) + Math.pow(mouseY - yPos[i] - moleSize / 2, 2) <= Math.pow(moleSize / 2, 2) && out[i] == true) { //pythagorean theorem/circle
out[i] = false; //set the mole to underground
score++; //add the score
}
if (Math.pow(mouseX - xPos[i] - bombSize / 2, 2) + Math.pow(mouseY - yPos[i] - bombSize / 2, 2) <= Math.pow(bombSize / 2, 2) && boom[i] == true) { //pythagorean theorem/circle
boom[i] = false; //set the bomb to underground
health -= 10; //decrease the health by ten
}
if (Math.pow(mouseX - xPos[i] - freezeSize / 2, 2) + Math.pow(mouseY - yPos[i] - freezeSize / 2, 2) <= Math.pow(freezeSize / 2, 2) && cold[i] == true) { //pythagorean theorem/circle
cold[i] = false; //set the freeze to underground
myTimer.stop(); //stop/reset the timer
myTimer.start(); //start the timer
}
}
}
}
public void getOut(){
if (health > 0) { //works only if health is above 0
count = 0; //reset the count
sequence = 0; //reset the position in array
justRight = false; //reset the boolean
for (int i = 0; i <= numOfmole - 1; i++) {
out[i] = false; //set the moles underground
boom[i] = false; //set the bomb underground
cold[i] = false; //set the freeze underground
}
for (int i = 1; i <= numGettingout; i++) {
out[(int) (Math.random() * numOfmole)] = true; //set the moles popping out
}
while (justRight == false) { //if the number of moles is not enough
count = 0; //reset the value to zero
out[(int) (Math.random() * numOfmole)] = true; //set the moles to pop out
for (int i = 0; i <= numOfmole - 1; i++) {
if (out[i] == true) {
count++; //recount the number of moles popping out
}
if (count == numGettingout) {
justRight = true; //set the boolean to true if the number is correct
}
}
}
if (count > numGettingout) { //if there are extra moles popping out
while (count > numGettingout) {
if (out[sequence] == true && sequence > 0) {
out[sequence] = false; //set the mole underground
count--; //decrease the number by one
}
sequence++; //increase the position in the array
}
}
for (int i = numOfmole - 1; i >= 0; i--) {
random = Math.random() * numOfmole; //generate random number
if (out[(int) (random)] != true) { //determine if the mole is popping out
boom[(int) (random)] = true; //set the bomb to pop out
break;
}
}
for (int i = 0; i <= numOfmole - 1; i++) {
random = Math.random() * numOfmole; //generate random number
if (out[(int) (random)] != true && boom[(int) (random)] != true) { //determine if the mole is popping out or if it is a bomb
cold[(int) (random)] = true; //set the freeze to pop out
break;
}
}
}
}
// reset the game
public void reset() {
myTimer.stop(); //stop/reset timer
myTimer.start(); //start the timer
sequence = 0; //reset number position in array
score = 0; //reset score
count = 0; //reset number
health = 100; //reset health
justRight = false; //reset boolean
for (int i = 0; i <= numOfmole - 1; i++){
xPos[i] = (int)(Math.random() * windowWidth / adjustValue); //reset x-position
yPos[i] = (int)(Math.random() * windowHeight / adjustValue); //reset y-position
}
for (int i = 0; i <= numOfmole - 1; i++){
out[i] = false; //set the moles underground
boom[i] = false; //set the bomb underground
cold[i] = false; //set the freeze underground
}
for (int i = 0; i <= numGettingout - 1; i++){
out[(int)(Math.random() * numOfmole)] = true; //set the moles to pop out
}
while (justRight == false){ //while number is incorrect
count = 0; //reset the count number
out[(int)(Math.random() * numOfmole)] = true; //set the mole to pop out
for (int i = 0; i <= numOfmole - 1; i++){
if (out[i] == true){
count++; //recount the number of moles popping out
}
if (count == numGettingout){
justRight = true; //determine if the number is correct
}
}
}
if (count > numGettingout){ //if there are extra moles popping out
while(count > numGettingout){
if(out[sequence] == true && sequence < numOfmole){
out[sequence] = false; //set the mole underground
count--; //decrease the number by one
}
sequence++; //increase the position in the array
}
}
}
// MAYBE TOUCH THE BELOW CODE? //
public WhackAMoleFillerChallenge() {
setUp();
JFrame window = new JFrame();
window.setTitle("Whack A Mole");
window.setSize(windowWidth, windowHeight + textHeight);
window.setLocationRelativeTo(null);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel container = new JPanel();
container.setLayout(new BoxLayout(container, BoxLayout.Y_AXIS));
JTextArea scoreDisplay = new JTextArea();
scoreDisplay.setEditable(false);
scoreDisplay.setText("\t\tScore: 0");
JButton resetButton = new JButton("Reset");
resetButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
reset();
scoreDisplay.setText("\t\tScore: 0");
window.getContentPane().repaint();
}
});
JPanel topPanel = new JPanel();
topPanel.setPreferredSize(new Dimension(windowWidth, textHeight));
topPanel.add(resetButton);
scoreDisplay.setBackground(topPanel.getBackground());
topPanel.add(scoreDisplay);
JPanel canvas = new JPanel() {
public void paint(Graphics g) {
draw(g);
}
};
canvas.setPreferredSize(new Dimension(windowWidth, windowHeight));
canvas.addMouseListener(new MouseListener() {
@Override
public void mousePressed(MouseEvent e) {
click(e.getX(), e.getY());
scoreDisplay.setText("\t\tScore:" + " " + score + " " + "Health: " + health);
window.getContentPane().repaint();
}
@Override
public void mouseClicked(MouseEvent e) {}
@Override
public void mouseReleased(MouseEvent e) {}
@Override
public void mouseEntered(MouseEvent e) {}
@Override
public void mouseExited(MouseEvent e) {}
});
window.addComponentListener(new ComponentAdapter() {
public void componentResized(ComponentEvent componentEvent) {
windowWidth = window.getWidth();
windowHeight = window.getHeight() - textHeight;
topPanel.setPreferredSize(new Dimension(windowWidth, textHeight));
canvas.setPreferredSize(new Dimension(windowWidth, windowHeight));
}
});
container.add(topPanel);
container.add(canvas);
window.add(container);
window.setVisible(true);
canvas.revalidate();
window.getContentPane().repaint();
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
canvas.repaint();
myTimer = new Timer(waitSeconds * 1000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
getOut();
window.getContentPane().repaint();
}
});
myTimer.start();
}
public static void main(String[] args) {
new WhackAMoleFillerChallenge();
}
}