Bonus #1: Add a Background

We can give this mini-game a space-age feel with the starry background from Chapter 13, ​Project: Phases of the Moon​. Below the win function definition and the collision event listener, add the following code:

​ ​function​ addBackground() {
​  ​var​ cover = ​new​ THREE.PointsMaterial({color: ​'white'​, size: 2});
​  ​var​ shape = ​new​ THREE.Geometry();
​ 
​  ​var​ distance = 500;
​  ​for​ (​var​ i = 0; i < 2000; i++) {
​  ​var​ ra = 2 * Math.PI * Math.random();
​  ​var​ dec = 2 * Math.PI * Math.random();
​ 
​  ​var​ point = ​new​ THREE.Vector3();
​  point.x = distance * Math.cos(dec) * Math.cos(ra);
​  point.y = distance * Math.sin(dec); ...

Get 3D Game Programming for Kids, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.