7
7
import android .view .View ;
8
8
import android .view .ViewGroup ;
9
9
import android .view .ViewTreeObserver ;
10
+ import android .widget .Button ;
10
11
import android .widget .ImageView ;
11
12
import android .widget .TextView ;
12
13
import android .widget .Toast ;
16
17
import java .util .List ;
17
18
import java .util .Random ;
18
19
20
+ import tech .torque .popper .utils .HighScoreHelper ;
21
+ import tech .torque .popper .utils .SimpleAlertDialog ;
22
+ import tech .torque .popper .utils .SoundHelper ;
23
+
19
24
public class MainActivity extends AppCompatActivity implements Balloon .BalloonListener {
20
25
21
26
public static final int MIN_ANIMATION_DELAY = 500 ;
22
27
public static final int MAX_ANIMATION_DELAY = 1500 ;
23
28
public static final int MIN_ANIMATION_DURATION = 1000 ;
24
29
public static final int MAX_ANIMATION_DURATION = 8000 ;
25
30
public static final int NUMBER_OF_PINS = 5 ;
31
+ private static final int BALLOONS_PER_LEVEL = 10 ;
26
32
27
33
private ViewGroup mContentView ;
28
34
private int [] mBalloonColors = new int [3 ];
@@ -32,6 +38,10 @@ public class MainActivity extends AppCompatActivity implements Balloon.BalloonLi
32
38
TextView mScoreDisplay , mLevelDisplay ;
33
39
private List <ImageView > mPinImages = new ArrayList <>();
34
40
private List <Balloon > mBalloons = new ArrayList <>();
41
+ private Button mGoButton ;
42
+ private boolean mPlaying , mGameStopped = true ;
43
+ private int mBalloonsPopped ;
44
+ private SoundHelper mSoundHelper ;
35
45
36
46
37
47
@ Override
@@ -65,25 +75,64 @@ public void onGlobalLayout() {
65
75
mPinImages .add ((ImageView ) findViewById (R .id .pushpin4 ));
66
76
mPinImages .add ((ImageView ) findViewById (R .id .pushpin5 ));
67
77
78
+ mGoButton = (Button ) findViewById (R .id .go_button );
79
+
80
+
68
81
mScoreDisplay = (TextView ) findViewById (R .id .score_display );
69
82
mLevelDisplay = (TextView ) findViewById (R .id .level_display );
70
83
71
84
updateDisplay ();
85
+
86
+ mSoundHelper = new SoundHelper (this );
87
+ mSoundHelper .prepareMusicPlayer (this );
88
+ }
89
+
90
+ private void startGame () {
91
+ mScore = 0 ;
92
+ mLevel = 0 ;
93
+ mPinsUsed = 0 ;
94
+ for (ImageView pin : mPinImages
95
+ ) {
96
+ pin .setImageResource (R .drawable .pin );
97
+
98
+ }
99
+ mGameStopped = false ;
100
+ startLevel ();
101
+ mSoundHelper .playMusic ();
72
102
}
73
103
74
104
private void startLevel () {
75
105
mLevel ++;
76
106
updateDisplay ();
77
107
BalloonLauncher launcher = new BalloonLauncher ();
78
108
launcher .execute (mLevel );
109
+ mPlaying = true ;
110
+ mBalloonsPopped = 0 ;
111
+ mGoButton .setText ("Stop game" );
112
+ }
113
+
114
+ private void finishLevel () {
115
+ Toast .makeText (this , String .format ("You finished level %d" , mLevel ), Toast .LENGTH_SHORT ).show ();
116
+ mPlaying = false ;
117
+ mGoButton .setText (String .format ("Start level %d" , mLevel + 1 ));
79
118
}
80
119
81
120
public void goButtonClickHandler (View view ) {
82
- startLevel ();
121
+ if (mPlaying ) {
122
+ gameOver (false );
123
+ } else if (mGameStopped ) {
124
+ startGame ();
125
+ } else {
126
+ startLevel ();
127
+ }
83
128
}
84
129
85
130
@ Override
86
131
public void popBalloon (Balloon balloon , boolean userTouch ) {
132
+
133
+ mBalloonsPopped ++;
134
+ mSoundHelper .playSound ();
135
+
87
136
mContentView .removeView (balloon );
88
137
mBalloons .remove (balloon );
89
138
@@ -103,18 +152,36 @@ public void popBalloon(Balloon balloon, boolean userTouch) {
103
152
Toast .makeText (this , "Missed that one!" , Toast .LENGTH_SHORT ).show ();
104
153
}
105
154
}
155
+
156
+ if (mBalloonsPopped == BALLOONS_PER_LEVEL ) {
157
+ finishLevel ();
158
+ }
159
+
106
160
updateDisplay ();
107
161
}
108
162
109
- private void gameOver (boolean b ) {
163
+ private void gameOver (boolean allPinsUsed ) {
110
164
Toast .makeText (this , "Game Over!" , Toast .LENGTH_SHORT ).show ();
165
+ mSoundHelper .pauseMusic ();
166
+
111
167
for (Balloon balloon : mBalloons
112
168
) {
113
169
mContentView .removeView (balloon );
114
170
balloon .setPopped (true );
115
171
116
172
}
117
173
mBalloons .clear ();
174
+ mPlaying = false ;
175
+ mGameStopped = true ;
176
+ mGoButton .setText ("Start game" );
177
+
178
+ if (allPinsUsed ) {
179
+ if (HighScoreHelper .isTopScore (this , mScore )) {
180
+ HighScoreHelper .setTopScore (this , mScore );
181
+ SimpleAlertDialog dialog = SimpleAlertDialog .newInstance ("New High Score!" , String .format ("Your new high score is %d" , mScore ));
182
+ dialog .show (getSupportFragmentManager (), null );
183
+ }
184
+ }
118
185
}
119
186
120
187
private void updateDisplay () {
@@ -138,7 +205,7 @@ protected Void doInBackground(Integer... params) {
138
205
int minDelay = maxDelay / 2 ;
139
206
140
207
int balloonsLaunched = 0 ;
141
- while (balloonsLaunched < 3 ) {
208
+ while (mPlaying && balloonsLaunched < BALLOONS_PER_LEVEL ) {
142
209
143
210
// Get a random horizontal position for the next balloon
144
211
Random random = new Random (new Date ().getTime ());
0 commit comments