-
Notifications
You must be signed in to change notification settings - Fork 0
/
FrogGame.java
227 lines (193 loc) · 6.49 KB
/
FrogGame.java
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
// This program is copyright VUW.
// You are granted permission to use it to construct your answer to a COMP102 assignment.
// You may not distribute it in any other way without permission.
/* Code for COMP102 Assignment
* Name:
* Usercode:
* ID:
*/
import ecs100.*;
import java.awt.Color;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.EventListener;
/** FrogGame: Core
* Game with two frogs that race to get to the bug first in order to eat it.
* (uses keys: player 1: W/A/S/D for up/left/down/right; controls the "light" frog
* player 2: I/J/K/L for up/left/down/right; controls the "dark" frog
* Frogs move around at a constant speed in an arena with an enclosing wall.
* When one of the frog reaches the bug, the frog eats it (grows in size),
* and a new bug appears at a random position within the arena walls.
*
* Controls:
* - key to start (space)
* - keys to turn the two frogs (w/a/s/d and i/j/k/l)
*
* Display:
* program constantly shows
* - the arena, the bug and the frogs
*
* Constants:
* This class should contain constants specifying the various parameters of
* the game, including the geometry of the arena and obstacle.
*/
public class FrogGame{
// Constants for the Geometry of the game.
// (You may change or add to these if you wish)
public static final int ArenaSize = 400;
public static final int LeftWall = 30;
public static final int RightWall = LeftWall+ArenaSize;
public static final int TopWall = 50;
public static final int BotWall = TopWall+ArenaSize;
public static final int BugSize = 15;
public static final int DELAY = 20; // milliseconds to delay each step.
// Fields to store the two frogs
private Frog frog1;
private Frog frog2;
// Fields to position of the centre of the bug
private double xBug;
private double yBug;
/** Constructor
* Set up the GUI,
* Draw the arena and the bug
*/
public FrogGame(){
UI.initialise();
UI.setImmediateRepaint(false);
UI.setKeyListener(this::doKey);
UI.addButton("Quit", UI::quit);
UI.setDivider(0);
UI.printMessage("Move mouse to arena and press Space to start");
UI.setKeyListener((String key) -> doKey(key));
this.drawArenaBug();
this.drawFrogs();
UI.repaintGraphics();
}
public void drawFrogs(){
this.frog1 = new Frog(LeftWall+30, (TopWall+BotWall)/2, "Up", "light");
this.frog2 = new Frog(RightWall-30, (TopWall+BotWall)/2, "Left", "dark");
this.frog1.draw();
this.frog2.draw();
}
/**
* Respond to keys.
* the space key should reset the game to have two new frogs
* the w/a/s/d keys should make the light frog turn up, to the left, down or to the right
* the i/j/k/l keys should make the dark frog turn up, to the left, down or to the right
*
* NEED TO MAKE SPACE KEY WORK
*/
public void doKey(String key){
switch (key){
case "q":
this.restartGame();
UI.println("space");
break;
case "w":
this.frog1.turnUp();
UI.println(key);
break;
case "a":
this.frog1.turnLeft();
UI.println(key);
break;
case "s":
this.frog1.turnDown();
UI.println(key);
break;
case "d":
this.frog1.turnRight();
UI.println(key);
break;
case "i":
this.frog2.turnUp();
UI.println(key);
break;
case "j":
this.frog2.turnLeft();
UI.println(key);
break;
case "k":
this.frog2.turnDown();
UI.println(key);
break;
case "l":
this.frog2.turnRight();
UI.println(key);
break;
}
}
/**
* Reset the game with two new frogs in the starting positions.
* Loop forever
* - move each frog one step,
* - if any frog has caught the bug, it grows, and a new bug appears somewhere else.
* - redraw the game (frogs, arena, and bug)
*/
private void restartGame(){
// make sure that the current game ends (if it is running)
this.frog1 = null;
this.frog2 = null;
UI.sleep(3*DELAY);
// puts a bug at a random position
this.newBug();
// make new frogs and call redraw
this.frog1 = new Frog(LeftWall+30, (TopWall+BotWall)/2, "Up", "light");
this.frog2 = new Frog(RightWall-30, (TopWall+BotWall)/2, "Left", "dark");
this.redraw();
// Run the game
while (this.frog1!=null && this.frog2!=null) {
this.frog1.move();
this.frog2.move();
if (this.frog1.touching(xBug, yBug)) {
this.frog1.grow();
this.newBug();
}
else if (this.frog2.touching(xBug, yBug)) {
this.frog2.grow();
this.newBug();
}
this.redraw();
UI.sleep(DELAY);
}
}
/**
* Redraws
* - the arena
* - the bug
* - the two frogs
*/
private void redraw(){
UI.clearGraphics();
this.drawArenaBug();
this.frog1.draw();
this.frog2.draw();
UI.repaintGraphics();
}
/**
* Draw the arena as a rectangle with a bug in it
*/
private void drawArenaBug(){
UI.setColor(Color.black);
UI.setLineWidth(2);
UI.drawRect(LeftWall, TopWall, ArenaSize, ArenaSize);
UI.setLineWidth(1);
xBug = LeftWall + BugSize/2 + Math.random()*(ArenaSize-BugSize/2);
yBug = TopWall + BugSize/2 + Math.random()*(ArenaSize-BugSize/2);
UI.drawImage("src/bug.png", xBug-BugSize/2, yBug-BugSize/2, BugSize, BugSize);
}
/**
* Defines new position for the bug and draws it
*/
private void newBug(){
xBug = LeftWall + BugSize/2 + Math.random()*(ArenaSize-BugSize/2);
yBug = TopWall + BugSize/2 + Math.random()*(ArenaSize-BugSize/2);
}
/**
* Create a new FrogGame object (which will set up the interface)
* and then call the run method on it, which will start the game running
*/
public static void main(String[] arguments){
new FrogGame();
}
}