3D Game Programming

(C. Jardin) #1

A1.11Code: Fruit Hunt


This is the avatar code after we added it to the fruit-hunt game in Chapter
11, Project: Fruit Hunt, on page 99. This code uses WebGLRenderer to make the
trees a little prettier, but the CanvasRenderer should work nearly as well.

<body></body>
<script src="http://gamingJS.com/Three.js"></script>
<script src="http://gamingJS.com/Tween.js"></script>
<script src="http://gamingJS.com/ChromeFixes.js"></script>
<script src="http://gamingJS.com/Scoreboard.js"></script>
<script src="http://gamingJS.com/Sounds.js"></script>
<script>
// This is where stuff in our game will happen:
varscene =newTHREE.Scene();

// This is what sees the stuff:
varaspect_ratio = window.innerWidth / window.innerHeight;
varcamera =newTHREE.PerspectiveCamera(75, aspect_ratio, 1, 10000);
camera.position.z = 500;
//scene.add(camera);

// This will draw what the camera sees onto the screen:
varrenderer =newTHREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);

// ******** START CODING ON THE NEXT LINE ********
varnot_allowed = [];

varscoreboard =newScoreboard();
scoreboard.countdown(45);
scoreboard.score();
scoreboard.help(
'Arrow keys to move; '+
'Space bar to jump for fruit; '+
'Watch for shaking trees with fruit.'+
'Get near the tree and jump before the fruit is gone!'
);
vargame_over = false;
scoreboard.onTimeExpired(function() {
scoreboard.message("Game Over!");
game_over = true;
});

varmarker =newTHREE.Object3D();
scene.add(marker);

varcover =newTHREE.MeshNormalMaterial();
varbody =newTHREE.SphereGeometry(100);

Appendix 1. Project Code • 234


Prepared exclusively for Michael Powell report erratum • discuss

Free download pdf