-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPairs.java
More file actions
48 lines (38 loc) · 1.53 KB
/
Pairs.java
File metadata and controls
48 lines (38 loc) · 1.53 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
/*
* Class: Pairs.java
* -------------------------
* "Pairs" is a GCompound that includes PAIR_NUM of blocks of random heights.
* The game is continuously generating (and removing) such pairs.
*/
import acm.graphics.GCompound;
import acm.util.RandomGenerator;
public class Pairs extends GCompound implements Constants {
// Used to generate random heights for blocks.
RandomGenerator rg = new RandomGenerator();
// Constructor: build pairs with blocks.
public Pairs() {
// Generate PAIR_NUM of block pairs (upper and bottom).
// Each block will have a random height.
// The blocks are then added to this GCompound.
for (int i = 0; i < PAIR_NUM; i++) {
Blocks bottom = new Blocks(false);
Blocks upper = new Blocks(true);
// Each bottom block will have a range of its height.
// The distance between each bottom and upper block ranges from 20% to 40%
// of the application height.
double bottomY = rg.nextDouble(0.6 * APPLICATION_HEIGHT, 0.8 * APPLICATION_HEIGHT);
double upperY = bottomY - rg.nextDouble(0.2 * APPLICATION_HEIGHT, 0.4 * APPLICATION_HEIGHT)
- upper.getImg().getHeight();
// Add the blocks to this GCompound pair.
this.add(upper.getImg(), BLOCK_INTERVAL + (BLOCK_INTERVAL + BLOCK_WIDTH) * i, upperY);
this.add(bottom.getImg(), BLOCK_INTERVAL + (BLOCK_INTERVAL + BLOCK_WIDTH) * i, bottomY);
}
}
/*
* Method moveLeft enables the GCompound Pairs to move left. This makes the
* blocks look like they move together in one.
*/
public void moveLeft() {
this.move(-MOVE_SPEED, 0);
}
}