3D PROGRAMMING: Change the world with JavaScript by Elijah Cosmos
Author:Elijah, Cosmos [Elijah, Cosmos]
Language: eng
Format: epub
Publisher: UNKNOWN
Published: 2021-01-11T00:00:00+00:00
⢠Stop the game if too much fruit gets past the purple fruit monster
⢠Reset if the game ends
⢠Incorporate graphics
Although we can add lots more to make the game even better, the core of the game is done. We can control the avatar. We can tell when the player earns points. We can tell when the game is over. We can show the score. Thatâs a lot of stuff.
Adding Simple Graphics
Of course, we have graphics available for the purple fruit monster, so letâs add them. First, in the addAvatar() function, make the MeshBasicMaterial invisible and add the purple fruit monster image:
function addAvatar() {
avatar = new Physijs.BoxMesh(
new THREE.CubeGeometry(40, 50, 1) ,
ⶠnew THREE.MeshBasicMaterial({visible: false}) ) ;
â· var avatar_material = new THREE.MeshBasicMaterial({ map:
THREE.ImageUtils.loadTexture('/images/purple_fruit_monster.png' ) , transparent: true }) ;
⸠var avatar_picture = new THREE.Mesh( new THREE.PlaneGeometry(40, 50), avatar_material ) ;
â¹ avatar.add(avatar_picture);
// Everything else stays the same in this function, starting with this: avatar.position.set(-50, 50, 0) ;
scene.add(avatar);
ⶠRemove the purple color and make this box invisible.
â· Create a new kind of material: an image material.
⸠Build a simple mesh with this material.
â¹ Attach the image mesh to the avatar.
Do the same for the launchFruit() function.
function launchFruit() {
var fruit = new Physijs.ConvexMesh(
new THREE.CylinderGeometry(20, 20, 1, 24) ,
ⶠnew THREE.MeshBasicMaterial({visible: false})
) ;
â· var material = new THREE.MeshBasicMaterial({ map: THREE.ImageUtils.loadTexture('/images/fruit.png' ) , transparent: true
}) ;
⸠var picture = new THREE.Mesh(
new THREE.PlaneGeometry(40, 40), material
) ;
â¹ picture.rotation.x = - Math.PI/2;
⺠fruit.add(picture);
Prepared
ⶠRemove the red color and make this cylinder invisible.
â· Create a new kind of material: an image material.
⸠Build a simple mesh with this material.
â¹ Rotate the image mesh to align with the cylinder.
⺠Attach the image mesh to the fruit. With that, we should have a purple fruit monster on the prowl!
⢠143
The Code So Far
Challenge: Game Reset
Right now, the only way to restart a game is to show the code, press the Update button, and then hide the code again. Try adding a keyboard handler so that when the R key (computer code 82) , is pressed, the game resets.
Some things to keep in mind:
⢠The avatar should go back to the starting position.
⢠The score should reset.
⢠The game is no longer over.
⢠Both animate() and gameStep() need to restart.
Good luck! This may prove quite a challengeâyou may even want to give it a try now, and then return after a few more chapters of experience with physics.
15.4 The Code So Far
If you would like to double-check the code in this chapter, turn to Section A1.15, Code: The Purple Fruit Monster Game , on page 245 .
15.5 Whatâs Next
This was an impressive game to make. In the upcoming chapters weâll practice the physics skills that we developed here. Weâll also build on the concept of a gameStep() function, which was fairly simple in this game.
Prepared
When youâre done with this chapter, you will
⢠Know how to build a full 3D game
⢠Know how to build complex 3D shapes
⢠Begin to understand how interesting shapes, materials, lights, and physics work together in a game
CHAPTER16
Project: Tilt-a-Board
In this chapter weâll build a 3D game in which a ball lands on a game board in space.
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Hello! Python by Anthony Briggs(9365)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9336)
The Mikado Method by Ola Ellnestam Daniel Brolund(9300)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(7845)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(7397)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7304)
Grails in Action by Glen Smith Peter Ledbrook(7291)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7111)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(5950)
Kotlin in Action by Dmitry Jemerov(4633)
Hands-On Full-Stack Web Development with GraphQL and React by Sebastian Grebe(3844)
Functional Programming in JavaScript by Mantyla Dan(3718)
Learning Java by Patrick Niemeyer & Daniel Leuck(2866)
Learning React: Functional Web Development with React and Redux by Banks Alex & Porcello Eve(2830)
Mastering Python for Finance by Weiming James Ma;(2822)
WordPress Plugin Development Cookbook by Yannick Lefebvre(2574)
Mastering Java 9 by Dr. Edward Lavieri(2569)
Architecting Modern Java EE Applications: Designing lightweight, business-oriented enterprise applications in the age of cloud, containers, and Java EE 8 by Daschner Sebastian(2555)
Learning Concurrency in Python by Elliot Forbes(2547)