Facebook
From Sole Octupus, 4 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 231
  1. // define variables
  2. var game;
  3. var player;
  4. var platforms;
  5. var badges;
  6. var items;
  7. var cursors;
  8. var jumpButton;
  9. var text;
  10. var winningMessage;
  11. var won = false;
  12. var currentScore = 0;
  13. var winningScore = 100;
  14.  
  15. // add collectable items to the game
  16. function addItems() {
  17.   items = game.add.physicsGroup();
  18.   createItem(375, 400, 'coin');
  19.   createItem(575, 500, 'coin');
  20.   createItem(225, 500, 'coin');
  21.   createItem(100, 250, 'coin');
  22.   createItem(575, 150, 'coin');
  23.   createItem(525, 300, 'coin');
  24.   createItem(650, 250, 'coin');
  25.   createItem(225, 200, 'coin');
  26.   createItem(375, 100, 'coin');
  27. }
  28.  
  29. // add platforms to the game
  30. function addPlatforms() {
  31.   platforms = game.add.physicsGroup();
  32.   platforms.create(450, 550, 'platform');
  33.   platforms.create(100, 550, 'platform');
  34.   platforms.create(300, 450, 'platform');
  35.   platforms.create(250, 150, 'platform');
  36.   platforms.create(50, 300, 'platform');
  37.   platforms.create(150, 250, 'platform');
  38.   platforms.create(650, 300, 'platform');
  39.   platforms.create(550, 200, 'platform2');
  40.   platforms.create(300, 450, 'platform2');
  41.   platforms.create(400, 350, 'platform2');                                                             platforms.setAll('body.immovable', true);
  42. }
  43.  
  44. // create a single animated item and add to screen
  45. function createItem(left, top, image) {
  46.   var item = items.create(left, top, image);
  47.   item.animations.add('spin');
  48.   item.animations.play('spin', 10, true);
  49. }
  50.  
  51. // create the winning badge and add to screen
  52. function createBadge() {
  53.   badges = game.add.physicsGroup();
  54.   var badge = badges.create(750, 400, 'badge');
  55.   badge.animations.add('spin');
  56.   badge.animations.play('spin', 10, true);
  57. }
  58.  
  59. // when the player collects an item on the screen
  60. function itemHandler(player, item) {
  61.   item.kill();
  62.   currentScore = currentScore + 10;
  63.   if (currentScore === winningScore) {
  64.       createBadge();
  65.   }
  66. }
  67.  
  68. // when the player collects the badge at the end of the game
  69. function badgeHandler(player, badge) {
  70.   badge.kill();
  71.   won = true;
  72. }
  73.  
  74. // setup game when the web page loads
  75. window.onload = function () {
  76.   game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: preload, create: create, update: update, render: render });
  77.  
  78.   // before the game begins
  79.   function preload() {
  80.     game.stage.backgroundColor = '#5db1ad';
  81.    
  82.     //Load images
  83.     game.load.image('platform', 'platform_1.png');
  84.     game.load.image('platform2, 'platform_.png');
  85.    
  86.    
  87.     //Load spritesheets
  88.     game.load.spritesheet('player', 'chalkers.png', 48, 62);
  89.     game.load.spritesheet('coin', 'coin.png', 36, 44);
  90.     game.load.spritesheet('badge', 'badge.png', 42, 54);
  91.   }
  92.  
  93.   // initial game set up
  94.   function create() {
  95.     player = game.add.sprite(50, 600, 'player');
  96.     player.animations.add('walk');
  97.     player.anchor.setTo(0.5, 1);
  98.     game.physics.arcade.enable(player);
  99.     player.body.collideWorldBounds = true;
  100.     player.body.gravity.y = 500;
  101.  
  102.     addItems();
  103.     addPlatforms();
  104.  
  105.     cursors = game.input.keyboard.createCursorKeys();
  106.     jumpButton = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);
  107.     text = game.add.text(16, 16, "SCORE: " + currentScore, { font: "bold 24px Arial", fill: "white" });
  108.     winningMessage = game.add.text(game.world.centerX, 275, "", { font: "bold 48px Arial", fill: "white" });
  109.     winningMessage.anchor.setTo(0.5, 1);
  110.   }
  111.  
  112.   // while the game is running
  113.   function update() {
  114.     text.text = "SCORE: " + currentScore;
  115.     game.physics.arcade.collide(player, platforms);
  116.     game.physics.arcade.overlap(player, items, itemHandler);
  117.     game.physics.arcade.overlap(player, badges, badgeHandler);
  118.     player.body.velocity.x = 0;
  119.  
  120.     // is the left cursor key presssed?
  121.     if (cursors.left.isDown) {
  122.       player.animations.play('walk', 10, true);
  123.       player.body.velocity.x = -300;
  124.       player.scale.x = - 1;
  125.     }
  126.     // is the right cursor key pressed?
  127.     else if (cursors.right.isDown) {
  128.       player.animations.play('walk', 10, true);
  129.       player.body.velocity.x = 300;
  130.       player.scale.x = 1;
  131.     }
  132.     // player doesn't move
  133.     else {
  134.       player.animations.stop();
  135.     }
  136.    
  137.     if (jumpButton.isDown && (player.body.onFloor() || player.body.touching.down)) {
  138.       player.body.velocity.y = -400;
  139.     }
  140.     // when the player winw the game
  141.     if (won) {
  142.       winningMessage.text = "YOU WIN!!!";
  143.     }
  144.   }
  145.  
  146.   function render() {
  147.  
  148.   }
  149.  
  150. };
  151.