-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLevel2.java
More file actions
75 lines (60 loc) · 1.79 KB
/
Copy pathLevel2.java
File metadata and controls
75 lines (60 loc) · 1.79 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
import java.awt.Graphics2D;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Random;
public class Level2 implements Level {
private final int level;
private int score;
private Background background;
private ArrayList<CatTreat> catTreats = new ArrayList<>();
private long lastTreatTime = 0;
private final int TREAT_INTERVAL = 4000; // 4 seconds
private final int MAX_TREATS = 10;
private int treatsSpawned = 0;
public Level2(GamePanel panel) {
this.level = 1;
this.score = 0;
this.background = new Background(panel, "images/level2_bg.png", 200);
}
private void spawnTreat() {
Random rand = new Random();
int x = rand.nextInt(900) + 50;
int y = rand.nextInt(200) + 400;
catTreats.add(new CatTreat(x, y, 40, 40));
treatsSpawned++;
}
public void drawBackground(Graphics2D g2) {
background.draw(g2);
for (CatTreat treat : catTreats) treat.draw(g2);
}
public void update(Cindy cindy) {
long now = System.currentTimeMillis();
if (treatsSpawned < MAX_TREATS && now - lastTreatTime > TREAT_INTERVAL) {
spawnTreat();
lastTreatTime = now;
}
Iterator<CatTreat> it = catTreats.iterator();
while (it.hasNext()) {
if (it.next().checkCollision(cindy)) {
it.remove();
cindy.addScore(2);
SoundManager.getInstance().playClip("cattreat", false);
}
}
}
public void moveBackground(int direction) {
background.move(direction);
}
public void increaseScore(int n) {
this.score += n;
}
public int getScore() {
return this.score;
}
public Background getBackground() {
return background;
}
public int getLevel() {
return this.level;
}
}