Advertisement
Guest User

Zombie Game

a guest
Jan 3rd, 2014
91
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 1</title>
  6.     <script type="text/javascript" src="js/phaser.min.js"></script>
  7.     <style type="text/css">
  8.         body {
  9.             margin: 0;
  10.         }
  11.     </style>
  12. </head>
  13. <body>
  14. <script type="text/javascript">
  15.  
  16. var game = new Phaser.Game(320, 480, Phaser.AUTO, '', { preload: preload, create: create, update: update, render: render });
  17.  
  18. function preload() {
  19.     game.load.spritesheet('zombie', 'assets/zombies32.png', 29, 35)
  20.     game.load.image('bkg', 'assets/grass.png');
  21.     game.load.image('player', 'assets/diamond.png');
  22.     game.load.image('bullet', 'assets/star.png');
  23. }
  24.    
  25. var seconds = 0;
  26. var b = 0;
  27. var can_shoot = true;
  28. var bullets;
  29. var zombies;
  30. var score = 0;
  31. var txt;
  32.  
  33. function create() {
  34. if (game.device.desktop==false) {
  35.     //scaling start
  36. game.stage.scale.startFullScreen();
  37. game.stage.scaleMode = Phaser.StageScaleMode.SHOW_ALL; //resize your window to see the stage resize too
  38. game.stage.scale.setShowAll();
  39. game.stage.scale.pageAlignHorizontally = true;
  40. game.stage.scale.pageAlignVertically = true;
  41. game.stage.scale.refresh();
  42.     //scaling end
  43. }
  44.     background = game.add.tileSprite(0, 0, 320, 480, 'bkg');
  45.     player = game.add.sprite(160, 440, 'player');
  46.     txt = game.add.group();
  47.     timer = game.add.text(4, 48, 'Time: 0s', { fontSize: '32px', fill: 'white', stroke: "black", strokeThickness: 5}, txt);
  48.     scoreText = game.add.text(4, 4, 'Score: 0', { fontSize: '32px', fill: 'white', stroke: "black", strokeThickness: 5 }, txt);
  49.    
  50.     //  Enable Input detection. Sprites have this disabled by default,
  51.     //  so you have to start it if you want to interact with them.
  52.     player.input.start(0,true);
  53.  
  54.     //  This allows you to drag the sprite. The parameter controls if you drag from the position you touched it (false)
  55.     //  or if it will snap to the center (true)
  56.     player.input.enableDrag();
  57.  
  58.     //  This will lock the sprite so it can only be dragged horizontally, not vertically
  59.     player.input.allowVerticalDrag = false;
  60.     player.body.immovable = true;
  61.  
  62.     player.events.onDragStop.add(dragStop, this);
  63.     player.events.onDragStart.add(dragStart, this);
  64.    
  65.     zombies = game.add.group();
  66.     bullets = game.add.group();
  67. }
  68.    
  69. function update() {
  70.     game.physics.collide(bullets, zombies, collisionHandler, null, this);
  71.     game.physics.overlap(player, zombies, death_touch, null, this);
  72.     //Calling a different function to update the timer just cleans up the update loop if you have other code.
  73.     updateTimer();
  74.     shoot();
  75.  
  76. }
  77.  
  78. function render() {
  79.  
  80.     game.debug.renderSpriteBody(player);
  81.  
  82. }
  83.    
  84. function shoot(){
  85.    
  86.     b += 1;
  87.    
  88.     if(b % 30 == 0 && can_shoot == true){
  89.     var bullet = bullets.create(player.x, player.y, 'bullet');
  90.    
  91.     player.bringToTop();
  92.     bullet.body.velocity.y = -150;
  93.     bullet.events.onOutOfBounds.add(destroy, this);
  94.     b = 0;
  95.     }
  96. }
  97.    
  98. function updateTimer() {
  99.  
  100.     seconds += 1;
  101.     timer.content = "Time: " +  Math.round(seconds/100) + 's';
  102.    
  103.     if(seconds/10 % 20 == 0){
  104.         spawn()
  105.     }
  106.  
  107. }
  108.    
  109. function spawn() {
  110.     for (var i = 0; i < 5; i++)
  111.     {
  112.         //  Create a zombie inside of the 'stars' group
  113.        
  114.         var z = zombies.create(i * 70, i - game.rnd.integerInRange(0, 32), 'zombie');
  115.        
  116.         var rand = game.rnd.integerInRange(0, 6)
  117.        
  118.         //console.log("Random: "+rand);
  119.        
  120.         if (rand == 1){
  121.             z.animations.add('1', [0, 1, 2], 5, true);
  122.             z.animations.play('1')
  123.         }
  124.         else if (rand == 2){
  125.             z.animations.add('2', [3, 4, 5], 5, true);
  126.             z.animations.play('2')
  127.         }
  128.         else if (rand == 3){
  129.             z.animations.add('3', [6, 7, 8], 5, true);
  130.             z.animations.play('3')
  131.         }
  132.         else if (rand == 4){
  133.             z.animations.add('4', [9, 10, 11], 5, true);
  134.             z.animations.play('4')
  135.         }
  136.        
  137.         z.name = 'zomb' + i;
  138.         z.events.onOutOfBounds.add(destroy, this);
  139.         z.body.velocity.y = 50;
  140.  
  141.     }
  142. }
  143.    
  144. function dragStart() {
  145. can_shoot = false;
  146. }
  147.    
  148. function dragStop() {
  149. can_shoot = true;        
  150. }
  151.    
  152. function collisionHandler (bullet, zomb) {
  153.     bullet.kill();
  154.     score+=25;
  155.     scoreText.content = "Score: " +  score;
  156.     zomb.kill();
  157. }    
  158.    
  159. function death_touch (obj1, obj2) {
  160.  
  161.     //  If the player collides with the chillis then they get eaten :)
  162.     //  The chilli frame ID is 17
  163.  
  164.     console.log('Hit', obj2.name);
  165.     score-=50;
  166.     scoreText.content = "Score: " +  score;
  167.     obj2.kill();
  168.  
  169. }
  170.    
  171. function destroy (obj){
  172.     if (obj.group==zombies){
  173.         score-=10;
  174.         scoreText.content = "Score: " +  score;
  175.     }
  176.     obj.kill();
  177. }
  178.    
  179. </script>
  180. </body>
  181. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement