-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFrog.java
More file actions
266 lines (215 loc) · 7.22 KB
/
Copy pathFrog.java
File metadata and controls
266 lines (215 loc) · 7.22 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
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Dimension;
import java.util.Random;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.awt.Image.*;
import java.io.*;
import javax.imageio.*;
import java.awt.image.ImageObserver;
public class Frog {//================================================================================================
// Variables ================================================================================================================================================
private int xCoordinate;
private int yCoordinate;
public int xVelocity;
public final int sizeX=50;
public final int sizeY=50;
public BufferedImage still;//This is the basic frog
public BufferedImage jumpUp;//The frog image to be applied when it jumps
public BufferedImage jumpDown;
public BufferedImage jumpLeft;
public BufferedImage jumpRight;
public BufferedImage fire;
public BufferedImage ice;
public ImageObserver observer;
public int count;
public Boolean left;
public Boolean right;
public Boolean up;
public Boolean down;
private int movementTimer;
private final int defaultTimerValue = 400;
private int timerResetValue;
public boolean frozen;
public boolean burned;
//================================================================================================================================================
// Constructor ================================================================================================================================================
public Frog() {
//Initialize the frog at the center of the bottom segment
xCoordinate= Frogger.WIDTH/2-25;
yCoordinate= Frogger.HEIGHT-sizeY;
try{
still=ImageIO.read(new File("Frog.png"));//Read in the image of the frog and set it as the static variable. The image is included in the code files
jumpUp=ImageIO.read(new File("frogJumpUp.png"));
jumpDown=ImageIO.read(new File("frogJumpDown.png"));
jumpLeft=ImageIO.read(new File("frogJumpLeft.png"));
jumpRight=ImageIO.read(new File("frogJumpRight.png"));
fire=ImageIO.read(new File("Fire.png"));
ice=ImageIO.read(new File("Ice.png"));
}catch (IOException e){
System.out.println("Image file not found");
System.exit(0);
}//you can't play without a frog
movementTimer=defaultTimerValue;
timerResetValue=defaultTimerValue;
}//Constructor
//================================================================================================================================================
// Methods ================================================================================================================================================
public int getTime(){
return movementTimer;
}//getTime
public void resetTimer(){
movementTimer=timerResetValue;
if (timerResetValue>=154)
timerResetValue-=4;
}//resetTimer
public void tickTimer(){
movementTimer--;
}//tickTimer
public void checkTimer(String biome){
if (biome.equals("Desert")&&getTime()==0)
burned=true;
if (biome.equals("Tundra")&&getTime()==0)
frozen=true;
}//checkTimer
public void drawMovementTimer(Graphics g, String biome){
g.setColor(Color.RED);
g.fillRect(30, 16, getTime()/3, 30);
g.setColor(Color.BLACK);
String i;
g.drawString("Movement timer. Move quickly so you don't " + (i = biome.equals("Tundra") ? "freeze!" : "burn your feet!"), 30, 12);
}//drawMovementTimer
public int getY(){
return yCoordinate;
}
public int getX(){
return xCoordinate;
}
public void jump(char c){
/*////////////////////////////////////////////////////////////////
*This moves the frog in the direction given by wasd. If you leave the screeen, it pulls you back on
*
*////////////////////////////////////////////////////////////////
resetTimer();
switch (c){
case 'w':
yCoordinate -= 50;
up=true;
down=false;
right=false;
left=false;
count=10;
break;
case 's':
yCoordinate += 50;
down=true;
right=false;
left=false;
up=false;
count=10;
break;
case 'a':
xCoordinate -= 50;
left=true;
right=false;
down=false;
up=false;
count=10;
break;
case 'd':
xCoordinate += 50;
right=true;
down=false;
left=false;
up=false;
count=10;
break;
default:
return;
}
if (xCoordinate==-sizeX)
xCoordinate=0;
if (xCoordinate==Frogger.WIDTH)
xCoordinate=Frogger.WIDTH-sizeX;
if(yCoordinate==Frogger.HEIGHT)
yCoordinate=Frogger.HEIGHT-sizeY;
}//jump
public void draw(Graphics g, String biome){
//Obviously, this draws the frog
drawTemp(g, biome);
if(count>0 && up){
g.drawImage(jumpUp, xCoordinate, yCoordinate, observer);
count--;
}
else if(count>0 && down){
g.drawImage(jumpDown, xCoordinate, yCoordinate, observer);
count--;
}
else if(count>0 && right){
g.drawImage(jumpRight, xCoordinate, yCoordinate, observer);
count--;
}
else if(count>0 && left){
g.drawImage(jumpLeft, xCoordinate, yCoordinate, observer);
count--;
}
else{
g.drawImage(still, xCoordinate, yCoordinate, observer);
left=false;
right=false;
down=false;
up=false;
count--;
}
}//draw
private void drawTemp (Graphics g, String biome){
if (biome.equals("Desert")){
if (movementTimer==0){
g.setColor(Color.WHITE);
g.drawString("You got burnt!", getX(), getY()+sizeY+18);
if (getY()==Frogger.HEIGHT-sizeY){
g.drawString("You got burnt!", getX(), Frogger.HEIGHT-sizeY-8);
}
g.setColor(Color.RED);
g.drawImage(fire, getX(), getY(), observer);
}
else if (movementTimer<=150){
g.setColor(Color.RED);
if (movementTimer%2==0){
g.drawImage(fire, getX(), getY(), observer);
}
g.setColor(Color.WHITE);
g.drawString("It's getting hot...", getX(), getY()+sizeY+18);
if (getY()==Frogger.HEIGHT-sizeY){
g.drawString("It's getting hot...", getX(), Frogger.HEIGHT-sizeY-8);
}
}
}
if (biome.equals("Tundra")){
if (movementTimer==0){
g.setColor(Color.WHITE);
g.drawString("You froze!", getX(), getY()+sizeY+18);
if (getY()==Frogger.HEIGHT-sizeY){
g.drawString("You froze!", getX(), Frogger.HEIGHT-sizeY-8);
}
g.setColor(Color.BLUE);
g.drawImage(ice, getX(), getY(), observer);
}
else if (movementTimer<=150){
g.setColor(Color.BLUE);
if (movementTimer%2==0)
g.drawImage(ice, getX(), getY(), observer);
g.setColor(Color.WHITE);
g.drawString("It's getting cold...", getX(), getY()+sizeY+18);
if (getY()==Frogger.HEIGHT-sizeY){
g.drawString("It's getting cold...", getX(), Frogger.HEIGHT-sizeY-8);
}
}
}
}//drawTemp
//================================================================================================================================================================================================
}//Frog================================================================================================================================================================================================