-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.java
More file actions
366 lines (322 loc) · 10.5 KB
/
game.java
File metadata and controls
366 lines (322 loc) · 10.5 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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.swing.*;
import java.util.*;
public class game extends JPanel implements KeyListener
{
frog Leaf;
fly Nut;
Color backgroundColor;
boolean gameOver = false;
int score = 0;
int miss = 0;
int flyMoveAmount = 20;
boolean paused = false;
int highScore = 0;
boolean isOpening = true;
String[] gems = {"assets/images/green.png", "assets/images/blue.png", "assets/images/pink.png"};
ArrayList<hearts> lives = new ArrayList<hearts>();
hearts live1 = new hearts(285, 10);
hearts live2 = new hearts(365, 10);
hearts live3 = new hearts(445, 10);
File background;
BufferedImage backimage;
File instructions;
BufferedImage instructionsimage;
private static String errMessage = "[game.java] Important files not found. Please manually restore or redownload the missing program files.";
//CONSTRUCTOR
public game(){
gameOver = true;
backgroundColor = new Color(243, 255, 240);
Leaf = new frog(250, 600);
Nut = new fly((int)Math.random() * 800, 200);
lives.add(live1);
lives.add(live2);
lives.add(live3);
try {
background = new File("assets/images/background.png");
backimage = ImageIO.read(background);
instructions = new File("assets/images/instructions.png");
instructionsimage = ImageIO.read(instructions);
} catch (Exception e) {
System.err.println(errMessage);
e.printStackTrace();
System.exit(-1);
}
}
//RESTART FUNCTION
public void restart(){
gameOver = false;
miss = 0;
score = 0;
Nut.setGoldStatus(false);
Leaf.show();
for(hearts life : lives){
life.show();
}
paused = false;
}
//FLY'S RIGHT TO LEFT MOVEMENT AND OFFSCREEN COLLISION LOGIC
public void collide(){
if(Nut.getX() <= -250){
if(Nut.life()){
miss++;
lives.get(lives.size() - miss).hide();
}
flyMoveAmount = (int)Math.random() * 30 + 30;
if(!paused){
Nut.aliveYes();
}
Nut.moveHeight();
Nut.transform();
}
if(Nut.getX() + 140 >= 1150){
if(Nut.life()){
miss++;
lives.get(lives.size() - miss).hide();
}
flyMoveAmount = -1*((int)Math.random() * 30 + 30);
if(!paused){
Nut.aliveYes();
}
Nut.moveHeight();
Nut.transform();
}
repaint();
}
//UNUSED
public void keyPressed(KeyEvent e) {
char c = e.getKeyChar();
repaint();
}
//UNUSED
public void keyTyped(KeyEvent e) {
char c = e.getKeyChar();
//repaint();
}
//KEYBOARD INPUT FUNCTIONS
public void keyReleased(KeyEvent e) {
char c = e.getKeyChar();
//STICKS OUT TONGUE
if(c == ' '){
if(!isOpening){
Leaf.tongueOut();
try {
Thread.sleep(220);
}catch(InterruptedException x) {}
Leaf.tongueIn();
}
if(isOpening){
isOpening = false;
if(paused){
paused = false;
}
else {
restart();
}
}
//INCREASES SCORE IF FLY IS CAUGHT
if((!(Leaf.getX() > Nut.getX() + 35) && !(Leaf.getX() + 185 < Nut.getX())) && Nut.life()){
playToungeSound();
Nut.aliveNo();
if(Nut.getGoldStatus() == true){
score += 2;
} else{
score++;
}
if(highScore < score){
highScore = score;
}
collide();
}
try {
Thread.sleep(400);
}catch(InterruptedException x) {}
}
//RESTART
else if((c == 'r' || c == 'R') && !isOpening){
restart();
}
//EASTER EGGS
if(c == 'S'){
Nut.aliveNo();
paused = true;
repaint();
String s = "";
try {
s = (JOptionPane.showInputDialog("")).toLowerCase();
} catch (NullPointerException error){}
//SPRIG
if(s.equals("sprig")){
Leaf.sprig();
if(Leaf.getSprig() == true){
JOptionPane.showMessageDialog(this, "Sprig, the best frog friend - in this world, and every other world.");
playTrueColorsSound();
}
}
//CALAMITY GEMS
else if(s.equals("calamity")){
if(lives.get(0).getChange() == false){
int i = 0;
for(hearts life : lives){
life.change_to_calamity(gems[i], "assets/images/dark.png");
i++;
}
JOptionPane.showMessageDialog(this, "Gems of witt, heart, and strength can bring or stop calamity.");
}
else {
for(hearts life : lives){
life.change_to_lily();
}
}
}
//LEAF THE FROG
else if(s.equals("leaf")){
Leaf.makeOriginal();
if(Leaf.getOriginal() == true){
JOptionPane.showMessageDialog(this, "Leaf is the original and official nickname of the Hoppit Frog.");
}
}
paused = false;
}
else if(c == 'i' || c == 'I'){
if(!isOpening){
Nut.aliveNo();
paused = true;
isOpening = true;
}
}
if(c == 'q' || c == 'Q'){
System.exit(0);
}
repaint();
}
////////////////////////////////////////////////////
//RUNS GAME LOGIC AND PLAYS THE GAME
public void run(){
Leaf.show();
for(hearts life : lives){
life.show();
}
while(miss < 4){
try {
Thread.sleep(70);
}catch(InterruptedException e) {}
Nut.move(flyMoveAmount);
paintImmediately(0, 0, 1000, 1000);
collide();
end();
if(gameOver){
Nut.aliveNo();
Leaf.hide();
for(hearts life : lives){
life.hide();
}
}
repaint();
}
}
//PLAYS TRUE COLORS SOUND EFFECT
public static synchronized void playToungeSound() {
new Thread(new Runnable() {
public void run() {
try {
Clip clip = AudioSystem.getClip();
String URL = "assets/audio/tongue_out.wav";
AudioInputStream inputStream = AudioSystem.getAudioInputStream(getClass().getResource(URL));
clip.open(inputStream);
clip.start();
} catch (Exception e) {
System.err.println(errMessage);
e.printStackTrace();
}
}
}).start();
}
//PLAYS TRUE COLORS SOUND EFFECT
public static synchronized void playTrueColorsSound() {
new Thread(new Runnable() {
public void run() {
try {
Clip clip = AudioSystem.getClip();
String URL = "assets/audio/Amphibia_TrueColorsCredits.wav";
AudioInputStream inputStream = AudioSystem.getAudioInputStream(getClass().getResource(URL));
clip.open(inputStream);
clip.start();
} catch (Exception e) {
System.err.println(errMessage);
e.printStackTrace();
}
}
}).start();
}
//ENDS THE GAME
public void end(){
if(miss >= 3){
gameOver = true;
}
}
//DRAWS ALL OBJECTS ONTO SCREEN (FROG, FLY BACKGROUND, TEXT)
public void paintComponent(Graphics g){
super.paintComponent(g);
g.drawImage(backimage, 0, 0, null);
//PAINTS FROG, FLY, AND LIVES
if(!isOpening){
Leaf.paintComponent(g);
Nut.paintComponent(g);
for(hearts life : lives){
life.paintComponent(g);
}
}
if(gameOver){
Leaf.hide();
Nut.aliveNo();
}
//TITLE, SCORE, AND NAME CREDITS
String highScoreText = "High Score: " + highScore;
String scoreText = "Score: " + score;
String title = "Hoppit";
String name1 = "Ayaan Modak";
String name2 = "Github: @Iced-code";
//PAINTS ON-SCREEN TEXT
g.setColor(Color.BLACK);
g.setFont(new Font("Verdana", Font.BOLD, 17));
g.drawString(name1, 5, 737);
g.drawString(name2, 5, 757);
g.setFont(new Font("Verdana", Font.BOLD, 50));
g.drawString(title, 20, 60);
//DISPLAYS GAME HUD
if(!gameOver && !paused){
g.setFont(new Font("Verdana", Font.BOLD, 40));
g.drawString(scoreText, 565, 55);
g.setFont(new Font("Verdana", Font.BOLD, 25));
g.drawString(highScoreText, 565, 100);
}
else if(!gameOver && paused){
g.setFont(new Font("Verdana", Font.BOLD, 40));
g.drawString("PAUSED", 565, 55);
}
//DISPLAYS OPENING INSTRUCTIONS SCREEN
if(isOpening){
g.drawImage(instructionsimage, 100, 100, null);
}
//PAINTS GAME OVER SCREEN
if(gameOver && !isOpening){
g.setColor(Color.RED);
g.setFont(new Font("Verdana", Font.BOLD, 50));
String gameEnd = "Game Over";
g.drawString(gameEnd, 255, 320);
g.setColor(Color.BLACK);
g.drawString(highScoreText, 230, 415);
g.setFont(new Font("Verdana", Font.PLAIN, 35));
String r = "Press 'R' to restart";
g.drawString(r, 250, 465);
}
}
}