-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.pde
129 lines (114 loc) · 3.27 KB
/
Game.pde
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
/*
Class managing the game logic and the current game state
by Flocksserver http://flocksserver.de
*/
class Game {
int lives;
int score;
float difficulty;
Catcher catcher;
CoinShower shower;
StatsPrinter statsPrinter;
//Array of all current coins to draw
ArrayList<Coin> ingameCoins;
public Game (){
init();
}
void init() {
lives = 5;
score = 0;
//Velocity the coins falling down, depends on display height.
difficulty = gameHeight/900;
ingameCoins = new ArrayList<Coin>();
catcher = new Catcher();
shower = new CoinShower();
statsPrinter = new StatsPrinter();
}
boolean gameOver() {
return lives <= 0;
}
void execute() {
if (!gameOver()) {
catcher.draw();
shower.start(ingameCoins);
checkIngameCoins();
setDifficulty();
statsPrinter.print(lives, score);
}else{
state = stateWaitToRestart;
}
}
/*
Check if a coin touchs the catcher -> increase score by 1
Check if a coin has fallen out of the screen -> decrease live by 1
*/
void checkIngameCoins( ) {
for (int i = 0; i < ingameCoins.size(); i++) {
Coin coin = ingameCoins.get(i);
boolean isCatched = isInCatcherZone(coin);
boolean isOut = isInOutZone(coin);
if (isCatched) score++;
if (isOut) lives--;
if (isCatched || isOut) {
coin.init();
shower.waitingStack.push(coin);
ingameCoins.remove(coin);
} else {
coin.velocity = difficulty;
coin.display();
}
}
}
/*
Check if coin touches the catcher
*/
boolean isInCatcherZone(Coin coin) {
return isInYZone(coin) && isInXZone(coin);
}
/*
Check if coin is out of screen.
If coin y position is greater then the display width
the coin has fallen out of the screen
*/
boolean isInOutZone(Coin coin) {
return coin.yPos + coin.coinHeigth/2 >= gameHeight-1;
}
/*
Check the coins y position in comparision to the catchers y position
*/
boolean isInYZone(Coin coin) {
boolean coinLowerBound =
coin.yPos + coin.coinHeigth/2 >= catcher.yPos - catcher.catcherHeight/2 &&
coin.yPos + coin.coinHeigth/2 <= catcher.yPos + catcher.catcherHeight/2;
boolean coinUpperBound =
coin.yPos - coin.coinHeigth/2 >= catcher.yPos - catcher.catcherHeight/2 &&
coin.yPos - coin.coinHeigth/2 <= catcher.yPos + catcher.catcherHeight/2;
return coinLowerBound || coinUpperBound;
}
/*
Check the coins x position in comparision to the catchers x position
*/
boolean isInXZone(Coin coin) {
boolean coinRightBound =
coin.xPos + coin.coinWidth/2 >= catcher.xPos - catcher.catcherWidth/2 &&
coin.xPos + coin.coinWidth/2 <= catcher.xPos + catcher.catcherWidth/2;
boolean coinLeftBound =
coin.xPos - coin.coinWidth/2 >= catcher.xPos - catcher.catcherWidth/2 &&
coin.xPos - coin.coinWidth/2 <= catcher.xPos + catcher.catcherWidth/2;
return coinRightBound || coinLeftBound;
}
/*
Increase difficulty -> velocity if the score level is reached
*/
void setDifficulty( ) {
if (score > 20) {
difficulty = (gameHeight/900) * 1.3;
}
if (score > 50) {
difficulty = (gameHeight/900) * 1.6;
}
if (score > 100) {
difficulty = (gameHeight/900) * 2;
}
}
}