Bubble Shooter Game in Unity: Part 10

In order to move the grid down the screen we only need to make a few adjustments to the code. Before we had a variable controlling the number of visible rows at the beginning of the game; but with a scrolling grid you don’t need that. The grid should be entirely visible now, with a fixed gap of empty rows at the bottom of the grid which will add an initial distance between visible cells and the shooters (as well as the necessary empty rows which will need to add the player’s bullets which do not create an immediate match.)

All the changes are done to the Grid class:

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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
 
public class Grid : MonoBehaviour {
 
	public int ROWS = 20;
 
	public int COLUMNS = 14;
 
	public float TILE_SIZE = 0.68f;
 
	public float GRID_SPEED = 0.12f;
 
	public float changeTypeRate = 0.5f;
 
	public int emptyLines = 16;
 
	public GameObject gridBallGO;
 
	[HideInInspector]
	public float GRID_OFFSET_X = 0;
 
	[HideInInspector]
	public  float GRID_OFFSET_Y = 0;
 
	[HideInInspector]
	public List<List<Ball>> gridBalls;
 
	private List<Ball> matchList;
 
	private List<Ball.BALL_TYPE> typePool;
 
	private Ball.BALL_TYPE lastType;
 
	private int bullets = 0;
 
	void Start () {
 
		//If you want to add new lines every 10 shots, uncomment the line below
		//EventManager.OnShootBall += HandleShootBall;
 
		matchList = new List<Ball> ();
		lastType = (Ball.BALL_TYPE)Random.Range (0, 5);
		typePool = new List<Ball.BALL_TYPE> ();
 
		var i = 0;
		var total = 10000;
		while (i < total) {
			typePool.Add (GetBallType ());
			i++;
		}
 
		Shuffle(typePool);
 
		BuildGrid ();
 
//		gridBalls [0] [0].gameObject.SetActive (false);
	}
 
	void AddLine () {
		//does top line have visible bubbles
		var emptyFirstRow = true;
		foreach (var b in gridBalls[0]) {
			if (b.gameObject.activeSelf) {
				emptyFirstRow = false;
				break;
			}
		}
 
		if (!emptyFirstRow) {
			var r = ROWS - 2;
			while (r >= 0) {
				foreach (var b in gridBalls[r]) {
					if (b.gameObject.activeSelf) {
						gridBalls [r + 1] [b.column].gameObject.SetActive (true);
						gridBalls [r + 1] [b.column].SetType (b.type);
					} else {
						gridBalls [r + 1] [b.column].gameObject.SetActive (false);
					}
				}
				r--;
			}
		}
 
		foreach (var b in gridBalls[0]) {
			b.SetType (typePool [0]);
			typePool.RemoveAt (0);
			b.gameObject.SetActive (true);
		}
	}
 
	void BuildGrid ()
	{
		gridBalls = new List<List<Ball>> ();
 
 
		GRID_OFFSET_X = (COLUMNS * TILE_SIZE) * 0.5f;
		GRID_OFFSET_Y = (ROWS * TILE_SIZE);
 
		GRID_OFFSET_X -= TILE_SIZE * 0.5f;
		GRID_OFFSET_Y -= TILE_SIZE * 0.5f;
 
 
		for (int row = 0; row < ROWS; row++) {
 
			var rowBalls = new List<Ball>();
 
			for (int column = 0; column < COLUMNS; column++) {
 
				var item = Instantiate (gridBallGO) as GameObject;
				var ball = item.GetComponent<Ball>();
 
				ball.SetBallPosition(this, column, row);
				ball.SetType (typePool[0]);
				typePool.RemoveAt (0);
 
				ball.transform.parent = gameObject.transform;
				rowBalls.Add (ball);
 
				if (gridBalls.Count > ROWS - emptyLines) {
					ball.gameObject.SetActive (false);
				}
			}
 
			gridBalls.Add(rowBalls);
		}
 
		var p = transform.position;
		p.y -= 4.7f;
		transform.position = p;
 
	}
 
	public void AddBall (Ball collisionBall, Bullet bullet) {
 
		var neighbors = BallEmptyNeighbors(collisionBall);
		var minDistance = 10000.0f;
		Ball minBall = null;
		foreach (var n in neighbors) {
			var d = Vector2.Distance (n.transform.position, bullet.transform.position);
			if ( d < minDistance ) {
				minDistance = d;
				minBall = n;
			}
		}
		bullet.gameObject.SetActive (false);
		minBall.SetType (bullet.type);
		minBall.gameObject.SetActive (true);
 
		CheckMatchesForBall (minBall);
 
	}
 
 
	public void CheckMatchesForBall (Ball ball) {
 
		matchList.Clear ();
 
		for (int row = 0; row < ROWS; row++) {
			for (int column = 0; column < COLUMNS; column++) {
				gridBalls [row] [column].visited = false;
			}
		}
 
		//search for matches around ball
		var initialResult = GetMatches( ball );
		matchList.AddRange (initialResult);
 
		while (true) {
 
			var allVisited = true;
			for (var i = matchList.Count - 1; i >= 0 ; i--) {
				var b = matchList [i];
				if (!b.visited) {
					AddMatches (GetMatches (b));
					allVisited = false;
				}
			}
 
			if (allVisited) {
				if (matchList.Count > 2) {
 
					foreach (var b in matchList) {
						b.gameObject.SetActive (false);
					}
 
					CheckForDisconnected ();
 
					//remove disconnected balls
					var i = 0;
					while (i < ROWS) {
						foreach (var b in gridBalls[i]) {
							if (!b.connected && b.gameObject.activeSelf) {
								b.gameObject.SetActive (false);
							}
						}
						i++;
					}
				}
				return;
			}
		}
	}
 
 
	void CheckForDisconnected () {
		//set all balls as disconnected
		foreach (var r in gridBalls) {
			foreach (var b in r) {
				b.connected = false;
			}
		}
		//connect visible balls in first row 
		foreach (var b in gridBalls[0]) {
			if (b.gameObject.activeSelf)
				b.connected = true;
		}
 
		//now set connect property on the rest of the balls
		var i = 1;
		while (i < ROWS) {
			foreach (var b in gridBalls[i]) {
				if (b.gameObject.activeSelf) {
					var neighbors = BallActiveNeighbors (b);
					var connected = false;
 
					foreach (var n in neighbors) {
						if (n.connected) {
							connected = true;
							break;
						}
					}
 
					if (connected) {
						b.connected = true;
						foreach (var n in neighbors) {
							if (n.gameObject.activeSelf) {
								n.connected = true;
							}
						}
					} 
				}
			}
			i++;
		}
	}
 
 
	List<Ball> GetMatches (Ball ball) {
		ball.visited = true;
		var result = new List<Ball> () { ball };
		var n = BallActiveNeighbors (ball);
 
		foreach (var b in n) {
			if (b.type == ball.type) {
				result.Add (b);
			}
		}
 
		return result;
	}
 
	void AddMatches (List<Ball> matches) {
		foreach (var b in matches) {
			if (!matchList.Contains (b))
				matchList.Add (b);
		}
	}
 
 
 
	Ball.BALL_TYPE GetBallType () {
		var random = Random.Range (0.0f, 1.0f);
		if (random > changeTypeRate) {
			lastType = (Ball.BALL_TYPE)Random.Range (0, 5);
		}
		return lastType;
	}
 
 
 
	List<Ball> BallEmptyNeighbors (Ball ball) {
		var result = new List<Ball> ();
		if (ball.column + 1 < COLUMNS) {
			if (!gridBalls [ball.row] [ball.column + 1].gameObject.activeSelf)
				result.Add (gridBalls [ball.row] [ball.column + 1]);
		}
 
		//left
		if (ball.column - 1 >= 0) {
			if (!gridBalls [ball.row] [ball.column - 1].gameObject.activeSelf)
				result.Add (gridBalls [ball.row] [ball.column - 1]);
		}
		//top
		if (ball.row - 1 >= 0) {
			if (!gridBalls [ball.row - 1] [ball.column].gameObject.activeSelf)
				result.Add (gridBalls [ball.row - 1] [ball.column]);
		}
 
		//bottom
		if (ball.row + 1 < ROWS) {
			if (!gridBalls [ball.row + 1] [ball.column].gameObject.activeSelf)
				result.Add (gridBalls [ball.row + 1] [ball.column]);
		}
 
		if (ball.column % 2 == 0) {
			//bottom-left
			if (ball.row + 1 < ROWS && ball.column - 1 >= 0) {
				if (!gridBalls [ball.row + 1] [ball.column - 1].gameObject.activeSelf)
					result.Add (gridBalls [ball.row + 1] [ball.column - 1]);
			}
 
			//bottom-right
			if (ball.row + 1 < ROWS && ball.column + 1 < COLUMNS) {
				if (!gridBalls [ball.row + 1] [ball.column + 1].gameObject.activeSelf)
					result.Add (gridBalls [ball.row + 1] [ball.column + 1]);
			}
		} else {
			//top-left
			if (ball.row - 1 >= 0 && ball.column - 1 >= 0) {
				if (!gridBalls [ball.row - 1] [ball.column - 1].gameObject.activeSelf)
					result.Add (gridBalls [ball.row - 1] [ball.column - 1]);
			}
 
			//top-right
			if (ball.row - 1 >= 0 && ball.column + 1 < COLUMNS) {
				if (!gridBalls [ball.row - 1] [ball.column + 1].gameObject.activeSelf)
					result.Add (gridBalls [ball.row - 1] [ball.column + 1]);
			}
		}
 
 
		return result;
	}
 
	List<Ball> BallActiveNeighbors (Ball ball) {
		var result = new List<Ball> ();
		if (ball.column + 1 < COLUMNS) {
			if (gridBalls [ball.row] [ball.column + 1].gameObject.activeSelf)
				result.Add (gridBalls [ball.row] [ball.column + 1]);
		}
 
		//left
		if (ball.column - 1 >= 0) {
			if (gridBalls [ball.row] [ball.column - 1].gameObject.activeSelf)
				result.Add (gridBalls [ball.row] [ball.column - 1]);
		}
		//top
		if (ball.row - 1 >= 0) {
			if (gridBalls [ball.row - 1] [ball.column].gameObject.activeSelf)
				result.Add (gridBalls [ball.row - 1] [ball.column]);
		}
 
		//bottom
		if (ball.row + 1 < ROWS) {
			if (gridBalls [ball.row + 1] [ball.column].gameObject.activeSelf)
				result.Add (gridBalls [ball.row + 1] [ball.column]);
		}
 
 
		if (ball.column % 2 == 0) {
			//bottom-left
			if (ball.row + 1 < ROWS && ball.column - 1 >= 0) {
				if (gridBalls [ball.row + 1] [ball.column - 1].gameObject.activeSelf)
					result.Add (gridBalls [ball.row + 1] [ball.column - 1]);
			}
 
			//bottom-right
			if (ball.row + 1 < ROWS && ball.column + 1 < COLUMNS) {
				if (gridBalls [ball.row + 1] [ball.column + 1].gameObject.activeSelf)
					result.Add (gridBalls [ball.row + 1] [ball.column + 1]);
			}
		} else {
			//top-left
			if (ball.row - 1 >= 0 && ball.column - 1 >= 0) {
				if (gridBalls [ball.row - 1] [ball.column - 1].gameObject.activeSelf)
					result.Add (gridBalls [ball.row - 1] [ball.column - 1]);
			}
 
			//top-right
			if (ball.row - 1 >= 0 && ball.column + 1 < COLUMNS) {
				if (gridBalls [ball.row - 1] [ball.column + 1].gameObject.activeSelf)
					result.Add (gridBalls [ball.row - 1] [ball.column + 1]);
			}
		}
 
		return result;
	}
 
	public Ball BallCloseToPoint (Vector2 point)
	{
 
		point.y -= transform.position.y;
 
		int c = Mathf.FloorToInt ((point.x + GRID_OFFSET_X + ( TILE_SIZE * 0.5f )) / TILE_SIZE);
		if (c < 0)
			c = 0;
		if (c >= COLUMNS)
			c = COLUMNS - 1;
 
		int r =  Mathf.FloorToInt ((GRID_OFFSET_Y + ( TILE_SIZE * 0.5f ) - point.y )/  TILE_SIZE);
		if (r < 0) r = 0;
		if (r >= ROWS) r = ROWS - 1;
 
 
 
		return gridBalls [r] [c];
 
	}
 
	void HandleShootBall () {
		bullets++;
 
		if (bullets > 10) {
			bullets = 0;
			AddLine ();
		}
	}
 
	void Update () {
 
		var p = transform.position;
		p.y -= Time.deltaTime * GRID_SPEED;
		transform.position = p;
 
	}
 
	private static System.Random rng = new System.Random(); 
	public static void Shuffle<T>(IList<T> list)  {  
		int n = list.Count;  
		while (n > 1) {  
			n--;  
			int k = rng.Next(n + 1);  
			T value = list[k];  
			list[k] = list[n];  
			list[n] = value;  
		}  
	}
}

First of all, we change the Grid Y offset, because this grid needs to align perfectly with the bottom of the screen (the last row appears at the bottom of the screen.)

So the BuildGrid method looks like this now:

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
void BuildGrid (){
	gridBalls = new List<List<Ball>> ();
 
 
	GRID_OFFSET_X = (COLUMNS * TILE_SIZE) * 0.5f;
	GRID_OFFSET_Y = (ROWS * TILE_SIZE);
 
	GRID_OFFSET_X -= TILE_SIZE * 0.5f;
	GRID_OFFSET_Y -= TILE_SIZE * 0.5f;
 
 
	for (int row = 0; row < ROWS; row++) {
 
		var rowBalls = new List<Ball>();
 
		for (int column = 0; column < COLUMNS; column++) {
 
			var item = Instantiate (gridBallGO) as GameObject;
			var ball = item.GetComponent<Ball>();
 
			ball.SetBallPosition(this, column, row);
			ball.SetType (typePool[0]);
			typePool.RemoveAt (0);
 
			ball.transform.parent = gameObject.transform;
			rowBalls.Add (ball);
 
			if (gridBalls.Count > ROWS - emptyLines) {
				ball.gameObject.SetActive (false);
			}
		}
 
		gridBalls.Add(rowBalls);
	}
 
	var p = transform.position;
	p.y -= 4.7f;
	transform.position = p;
 
}

First GRID_OFFSET_Y is the full height of the grid, and at the end of the method I shift the grid container, which is what I scroll down the screen.

The logic identifying the closest cell to a given point changes too (although I have not been using this logic, it’s good to know how to handle it.)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public Ball BallCloseToPoint (Vector2 point){
 
	point.y -= transform.position.y;
 
	int c = Mathf.FloorToInt ((point.x + GRID_OFFSET_X + ( TILE_SIZE * 0.5f )) / TILE_SIZE);
	if (c < 0)
		c = 0;
	if (c >= COLUMNS)
		c = COLUMNS - 1;
 
	int r =  Mathf.FloorToInt ((GRID_OFFSET_Y + ( TILE_SIZE * 0.5f ) - point.y )/  TILE_SIZE);
	if (r < 0) r = 0;
	if (r >= ROWS) r = ROWS - 1;
 
 
 
	return gridBalls [r] [c];
 
}

All I need to do is offset the current Y position of the grid container. I do that in the very first line of the method.

And then finally, the update method that scrolls the grid.

1
2
3
4
5
6
7
void Update () {
 
	var p = transform.position;
	p.y -= Time.deltaTime * GRID_SPEED;
	transform.position = p;
 
}

And that’s it.

I didn’t add any checks for game over conditions, like the balls hitting the bottom of the screen. But that’s simple to do, if you want.

Here is the project with the logic to scroll down the grid.