Advertisement
Guest User

Untitled

a guest
Mar 27th, 2016
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8" />
  5.     <title>Phaser - Making your first game, part 7</title>
  6.     <script src="//cdn.jsdelivr.net/phaser/2.2.2/phaser.min.js"></script>
  7.    <script src='https://code.responsivevoice.org/responsivevoice.js'></script>
  8.     <style type="text/css">
  9.         body {
  10.             margin: 0;
  11.         }
  12.     </style>
  13. </head>
  14. <body>
  15.  
  16. <script type="text/javascript">
  17.  
  18. var game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: preload, create: create, update: update });
  19.  
  20. var bgtile;
  21. var player;
  22. var stars;
  23. var droplets;
  24. var count = 0;
  25. var start = new Date();
  26. var scoreText;
  27. var totalDropletsText;
  28. var totalPoisonText;
  29. var percentageText;
  30. var score = 0;
  31. var dropletCount = 0;
  32. var poisonCount = 0;
  33. var percentage = 0;
  34. var totalCount = 0;
  35. var procTime = 0;
  36. var totalTimeStart = new Date();
  37. var done = 0;
  38.  
  39. function preload() {
  40. game.load.image('bgtile', 'riverTest.png');
  41. game.load.spritesheet('dude', 'assets/dude.png', 32, 48);
  42. game.load.image('star', 'assets/star.png');
  43. game.load.image('droplet', 'assets/firstaid.png')
  44. }
  45.  
  46.  
  47. function create() {
  48. bgtile = game.add.tileSprite(0, 0, game.width, game.cache.getImage('bgtile').height, 'bgtile');
  49.  
  50. //  We're going to be using physics, so enable the Arcade Physics system
  51.     game.physics.startSystem(Phaser.Physics.ARCADE);
  52.  
  53.     // The player and its settings
  54.     player = game.add.sprite(32, game.world.height - 150, 'dude');
  55.  
  56.     //  We need to enable physics on the player
  57.     game.physics.arcade.enable(player);
  58.  
  59.     //  Player physics properties. Give the little guy a slight bounce.
  60.     player.body.bounce.y = 0.2;
  61.     player.body.gravity.y = 0;
  62.     player.body.collideWorldBounds = true;
  63.  
  64.     //  Our two animations, walking left and right.
  65.     player.animations.add('left', [0, 1, 2, 3], 10, true);
  66.     player.animations.add('right', [5, 6, 7, 8], 10, true);
  67.  
  68.     //  Our controls.
  69.     cursors = game.input.keyboard.createCursorKeys();
  70.  
  71.     stars = game.add.group();
  72.  
  73.     stars.enableBody = true;
  74.  
  75.     droplets = game.add.group();
  76.  
  77.     droplets.enableBody = true;
  78.  
  79.     //setting up the scoring text
  80.     scoreText = game.add.text(16, 16, 'Score: 0', { fontSize: '32px', fill: '#000'});
  81.    
  82.  
  83. }
  84.  
  85. function update() {
  86. var randomnumber = Math.floor(Math.random()*11);
  87.  
  88. bgtile.tilePosition.x -= 1;
  89.  
  90. if(count < 75){
  91.     if(randomnumber % 2 == 0){  
  92.         createPoison();
  93.     }else{
  94.         createDroplets();
  95.     }
  96.  
  97. }
  98.  
  99.  
  100.     if(count == 75 && done == 0){
  101.         responsiveVoice.speak("Congratulations you have made it through the river relatively pollutant free!  You are now ready to continue on in the water cycle to evaporation!");
  102.         done +=1;
  103.     }
  104.  
  105. //checking to see if the player overlaps with the poison to pick it up
  106.     game.physics.arcade.overlap(player, stars, collectStar, null, this);
  107.  
  108. //checking to see if the player overlaps with the droplet to pick it up
  109.     game.physics.arcade.overlap(player, droplets, collectDroplet, null, this);
  110.  
  111.  
  112. player.body.velocity.y = 0;
  113.     if (cursors.left.isDown && player.body.y > 300)
  114.     {
  115.         //  Move to the left
  116.         player.body.velocity.y = -150;
  117.  
  118.         player.animations.play('left');
  119.     }
  120.     else if (cursors.right.isDown)
  121.     {
  122.         //  Move to the right
  123.         player.body.velocity.y = 150;
  124.  
  125.         player.animations.play('right');
  126.     }
  127.     else
  128.     {
  129.         //  Stand still
  130.         player.animations.stop();
  131.  
  132.         player.frame = 4;
  133.     }
  134.  
  135.  
  136.  
  137. }
  138.  
  139. /**
  140.  * Returns a random number between min (inclusive) and max (exclusive)
  141.  */
  142. function getRandomArbitrary() {
  143.     return Math.random() * (600 - 325) + 325;
  144. }
  145.  
  146.  
  147. function binaryChoice() {
  148.     return Math.random() * (2 - 1) + 1;
  149. }
  150.  
  151.  
  152.  
  153. //randomly generates poison
  154. function createPoison(){
  155.     var elapsed = new Date() - start;
  156.  
  157.         if(elapsed > 1000){
  158.         var star = stars.create(800, getRandomArbitrary(), 'star');
  159.         moveIndividual(star);
  160.         count += 1;
  161.         start = new Date();
  162.         poisonCount += 1;
  163.         totalCount += 1;
  164.     }
  165. }
  166.  
  167. //randomly generates good water dudes
  168. function createDroplets(){
  169. var elapsed2 = new Date() - start;
  170. //had to make this jank counter because JavaScript sucks
  171.         if(elapsed2 > 1000){
  172.         var droplet = droplets.create(800, getRandomArbitrary(), 'droplet');
  173.         moveIndividual(droplet);
  174.         count += 1;
  175.         start = new Date();
  176.         dropletCount += 1;
  177.         totalCount += 1;
  178.     }
  179. }
  180.  
  181.  
  182. function moveIndividual(moved){
  183.     moved.body.velocity.x = -100;
  184. }
  185.  
  186.  
  187. function collectStar(player, star){
  188.     //removes the star
  189.     star.kill();
  190.  
  191.     //add and update the score
  192.     score -= 1;
  193.     scoreText.text = 'Score: ' + score;
  194. }
  195.  
  196. function collectDroplet(player, droplet){
  197.     //removes the droplet
  198.     droplet.kill();
  199.  
  200.     //add and update the score
  201.     score += 1;
  202.     scoreText.text = 'Score: ' + score;
  203. }
  204.  
  205.  
  206. </script>
  207.  
  208. </body>
  209. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement