Guest User

spacegame.js

a guest
Feb 16th, 2014
30
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'spacegame', { preload: preload, create: create, update: update, render: render } );
  2.  
  3.  
  4.    
  5.     function preload(){
  6.        
  7.         game.load.image('background', 'assets/bg.png');
  8.         game.load.image('pBlaster', 'assets/pblaster.png');
  9.         game.load.image('eBlaster', 'assets/eblaster.png');
  10.         game.load.spritesheet('ship', 'assets/ship.png', 50, 40);
  11.         game.load.spritesheet('drone1', 'assets/enemydrone1.png', 32, 50);
  12.         game.load.spritesheet('drone2', 'assets/enemydrone2.png', 60, 38);
  13.         game.load.spritesheet('fighter', 'assets/enemyheavyfighter.png', 80, 70);
  14.         game.load.spritesheet('boss1', 'assets/enemyboss1.png', 150, 191);
  15.         game.load.spritesheet('medic', 'assets/enemymedic.png', 60, 50);
  16.         game.load.audio('gunshot', 'sounds/gunshot.wav');
  17.         game.load.audio('engine', 'sounds/engine.wav');
  18.         game.load.audio('hit', 'sounds/explode.wav');
  19.        
  20.     }
  21.    
  22.     var background;
  23.     //Player
  24.     var cursors;
  25.     var player;
  26.    
  27.     //Enemy Sprites
  28.     var Drone1;
  29.     var Drone2;
  30.     var HFighter;
  31.     var Medic;
  32.     var Boss1;
  33.    
  34.     //Enemy Groups
  35.     var livingDrone1 = [];
  36.     var livingDrone2 = [];
  37.     var livingHFighter = [];
  38.     var livingMedic = [];
  39.  
  40.     var shooter;
  41.    
  42.    
  43.        
  44.     //Sound
  45.     var shot;
  46.     //var engine;
  47.     var hit;
  48.    
  49.     //Blaster
  50.     var pBlasters;
  51.     var pBlasterTime = 0;
  52.     var fireButton;
  53.     var firingTimer = 0;
  54.     var eBlasters;
  55.    
  56.     //Score+Status
  57.     var score = 0;
  58.     var scoreString = '';
  59.     var scoreText;
  60.     var shotsfired = 0;
  61.     var shotsfiredString = '';
  62.     var shotsfiredText;
  63.     var eshotsfired = 0;
  64.     var eshotsfiredText;
  65.     var eshotsString = '';
  66.  
  67.    
  68.     //var scoreStyle = { font: "34px Arial", fill = "#fff000" };
  69.    
  70.    
  71.     function create()
  72.     {
  73.         //game.add.sprite(0, 0, 'background');
  74.         background = game.add.tileSprite(0, 0, 800, 600, 'background');
  75.         game.physics.setBoundsToWorld();
  76.         //game.setWorldBounds(0, 0, 800, 0, 800, 600, 0, 600);
  77.        
  78.         player = game.add.sprite(300, game.world.height - 100, 'ship');
  79.         player.anchor.setTo(0, 0);
  80.         player.body.setPolygon(1, 24, 5, 14, 11, 1, 11, 22, 18, 25, 18, 13, 25, 8, 31, 13, 31, 25, 39, 22, 39, 1, 45, 14, 49, 24, 33, 40, 17, 40);
  81.  
  82.         shot = game.add.audio('gunshot');
  83.         //engine = game.add.audio('engine');
  84.         hit = game.add.audio('hit');
  85.        
  86.         //Controls
  87.         cursors = game.input.keyboard.createCursorKeys();
  88.         fireButton = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);
  89.        
  90.         //Player Blaster Bullet Group
  91.         pBlasters = game.add.group();
  92.         pBlasters.createMultiple(30, 'pBlaster');
  93.         pBlasters.setAll('anchor.x', -3.6);
  94.         pBlasters.setAll('anchor.y', 1);
  95.         pBlasters.setAll('outOfBoundsKill', true);
  96.        
  97.         //Enemy Blaster Bullet Group
  98.         eBlasters = game.add.group();
  99.         eBlasters.createMultiple(30, 'eBlaster');
  100.         eBlasters.setAll('anchor.x', 0);
  101.         eBlasters.setAll('anchor.y', 0);
  102.         eBlasters.setAll('outOfBoundsKill', true);
  103.        
  104.         //Enemy Groups
  105.         //Enemies = game.add.group();
  106.         Drone1 = game.add.group();
  107.         Drone2 = game.add.group();
  108.         HFighter = game.add.group();
  109.         Medic = game.add.group();
  110.        
  111.         createDrone1();
  112.         createDrone2();
  113.         createFighter();
  114.         createMedic();
  115.    
  116.         //Info Text
  117.         scoreString = 'Score : ';
  118.         scoreText = game.add.text(10, 10, scoreString + score, { font: '34px' , fill: '#fff000' });
  119.         shotsfiredString = 'Shots Fired : ';
  120.         shotsfiredText = game.add.text(10, 20, shotsfiredString + shotsfired, { font: '34px' , fill: '#fff000' });
  121.         eshotsfiredString = 'Enemy Shots Fired : ';
  122.         eshotsfiredText = game.add.text(10, 30, eshotsfiredString + eshotsfired, { font: '34px' , fill: '#fff000' });
  123.    
  124.     }
  125.    
  126.    
  127.     //First Drone Group
  128.     function createDrone1()
  129.     {
  130.         for (var y = 0; y < 1; y++)
  131.         {
  132.             for (var x = 0; x < 10; x++)
  133.             {
  134.                 var drones1 = Drone1.create(x * 32, y * 50, 'drone1');
  135.                 drones1.anchor.setTo(0, 0);
  136.                 drones1.body.setPolygon(1, 14, 11, 17, 11, 2, 21, 2, 21, 17, 31, 14, 31, 42, 29, 49, 27, 35, 22, 30, 16, 36, 10, 30, 5, 35, 3, 50, 1, 39);
  137.             }
  138.         }
  139.     }
  140.    
  141.     //Second Drone Group
  142.     function createDrone2()
  143.     {
  144.         for (var y = 1; y < 2; y++)
  145.         {
  146.             for (var x = 0; x < 10; x++)
  147.             {
  148.                 var drones2 = Drone2.create(x * 60, y * 38, 'drone2');
  149.                 drones2.anchor.setTo(0, 0);
  150.                 drones2.body.setPolygon(18, 0, 26, 8, 30, 2, 34, 8, 42, 0, 59, 7, 49, 7, 45, 13, 41, 13, 41, 10, 34, 16, 34, 37, 31, 34, 31, 17, 29, 17, 29, 34, 26, 37, 26, 16, 19, 10, 19, 13, 15, 13, 12, 7, 1, 7);
  151.             }
  152.         }
  153.     }
  154.    
  155.     //Heavy Fighter Group
  156.     function createFighter()
  157.     {
  158.         for (var y = 2; y < 3; y++)
  159.         {
  160.             for (var x = 0; x < 10; x++)
  161.             {
  162.                 var fighter = HFighter.create(x * 80, y * 70, 'fighter');
  163.                 fighter.anchor.setTo(0, 0);
  164.                 fighter.body.setPolygon(30, 2, 49, 2, 47, 10, 52, 13, 52, 21, 60, 24, 80, 16, 80, 20, 60, 38, 53, 33, 51, 39, 46, 35, 46, 60, 40, 69, 34, 60, 34, 35, 29, 38, 27, 32, 20, 38, 0, 20, 0, 15, 21, 23, 28, 22, 28, 13, 33, 10);
  165.             }
  166.         }
  167.     }
  168.    
  169.     //Health Dropping Enemy Group
  170.     function createMedic()
  171.     {
  172.         for (var y = 3; y < 4; y++)
  173.         {
  174.             for (var x = 0; x < 10; x++)
  175.             {
  176.                 var healer = Medic.create(x * 60, y * 50, 'medic');
  177.                 healer.anchor.setTo(0, 0);
  178.                 healer.body.setPolygon(0, 3, 3, 2, 16, 8, 19, 13, 42, 13, 45, 7, 56, 2, 60, 3, 45, 18, 44, 15, 39, 20, 41, 24, 41, 39, 38, 33, 30, 50, 22, 33, 19, 39, 19, 24, 22, 21);
  179.             }
  180.         }
  181.     }
  182.    
  183.    
  184.     function update()
  185.     {
  186.    
  187.         //Stop Player Movement
  188.         player.body.velocity.x = 0;
  189.         player.body.velocity.y = 0;
  190.         //engine.play();
  191.        
  192.         //Player Controls
  193.         if (cursors.up.isDown)
  194.         {
  195.             player.body.velocity.y = -200;
  196.         }
  197.         if (cursors.down.isDown)
  198.         {
  199.             player.body.velocity.y = 200;
  200.         }
  201.         if (cursors.left.isDown)
  202.         {
  203.             player.body.velocity.x = -250;
  204.         }
  205.         if (cursors.right.isDown)
  206.         {
  207.             player.body.velocity.x = 250;
  208.         }
  209.  
  210.         //Player Shooting
  211.         if (fireButton.isDown)
  212.         {
  213.             firepBlaster();
  214.         }
  215.        
  216.         //Enemy Shooting
  217.         if (game.time.now > firingTimer)
  218.         {
  219.             fireeBlaster();
  220.         }
  221.        
  222.         game.physics.overlap(pBlasters, Drone1, collisionHandler, null, this);
  223.         game.physics.overlap(eBlasters, player, Drone1HitsPlayer, null, this);
  224.     }
  225.    
  226.    
  227.     //Player Blaster Function
  228.     function firepBlaster ()
  229.     {
  230.        
  231.         if (game.time.now > pBlasterTime)
  232.         {
  233.             pBlaster = pBlasters.getFirstExists(false);
  234.  
  235.             if (pBlaster)
  236.             {
  237.                 pBlaster.reset(player.x, player.y + 8);
  238.                 pBlaster.body.velocity.y = -400;
  239.                 pBlaster.body.setRectangle = [0, 0, 6, 0, 6, 36, 0, 36];
  240.                 pBlasterTime = game.time.now + 200;
  241.                 shotsfired += 1;
  242.                 shotsfiredText.content = shotsfiredString + shotsfired;
  243.                 shot.play();
  244.             }
  245.         }
  246.     }
  247.    
  248.     //Enemy Drone1 Blaster Function
  249.     function fireeBlaster()
  250.     {
  251.         eBlaster = eBlasters.getFirstExists(false);
  252.         livingDrone1.length = 0;
  253.         Drone1.forEachAlive(function(Drone1)
  254.         {
  255.             livingDrone1.push(Drone1);
  256.         });
  257.        
  258.         if (eBlaster && Drone1.length > 0)
  259.         {
  260.             var random=game.rnd.integerInRange(0,livingDrone1.length);
  261.             var shooter=livingDrone1[random];
  262.             eBlaster.body.setRectangle = [0, 0, 6, 0, 6, 36, 0, 36];
  263.             eBlaster.reset(shooter.body.x, shooter.body.y);
  264.             eBlaster.body.velocity.y = 400;
  265.             firingTimer = game.time.now + 800;
  266.             eshotsfired +=1;
  267.             eshotsfiredText.content = eshotsfiredString + eshotsfired;
  268.             shot.play();
  269.         }
  270.     }
  271.    
  272.     //enemies.x = 100;
  273.     //enemies.y = 50;
  274.        
  275.     function collisionHandler (pBlasters, Drone1)
  276.     {
  277.         pBlaster.kill();
  278.         //Drone1.kill()
  279.         hit.play();
  280.         score += 100;
  281.         scoreText.content = scoreString + score;
  282.     }
  283.    
  284.     function Drone1HitsPlayer (pBlasters, Drone1)
  285.     {
  286.         eBlaster.kill();
  287.         hit.play();
  288.     }
  289.    
  290.     //Kill bullets when off screen
  291.     function resetpBlaster(pBlaster)
  292.     {
  293.         pBlaster.kill();
  294.     }
  295.     function reseteBlaster(eBlaster)
  296.     {
  297.         eBlaster.kill();
  298.     }
  299.  
  300.     //Collision Debugging Information
  301.     function render()
  302.     {
  303.         game.debug.renderPhysicsBody(player.body);
  304.         //game.debug.renderPhysicsBody(drones1.body);
  305.         //game.debug.renderPhysicsBody(pBlasters.body);
  306.         //game.debug.renderPhysicsBody(eBlaster.body);
  307.     }
RAW Paste Data