Guest User

Untitled

a guest
Oct 20th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <script type="text/javascript">
  2.     //Configurations for the physics engine
  3.     var physicsConfig = {
  4.         default: 'arcade',
  5.         arcade: {
  6.             debug: false //CHANGE THIS TO TRUE TO SEE LINES
  7.         }
  8.     }
  9.     //Configurations for the game itself
  10.     var config = {
  11.         type: Phaser.AUTO,
  12.         width: 800,
  13.         height: 600,
  14.         physics: physicsConfig,
  15.         scene: {
  16.             preload: preload,
  17.             create: create,
  18.             update: update,
  19.             render: render
  20.         }
  21.     };
  22.     //Declare variables for the score
  23.     let score = 0;
  24.     let scoreBoard;
  25.  
  26.     //Start the game
  27.     var game = new Phaser.Game(config);
  28.  
  29.     function preload ()
  30.     {  
  31.         //Images
  32.         this.load.image('sky', 'assets/images/sky.png');
  33.         this.load.image('target', 'assets/images/target.png');
  34.         this.load.image('ground', 'assets/images/ground.png');
  35.         this.load.image('arrow', 'assets/images/arrow.png');
  36.         this.load.image('gold_medal', 'assets/images/goldmedal.png');
  37.         this.load.image('silver_medal', 'assets/images/silvermedal.png');
  38.         this.load.image('bronze_medal', 'assets/images/bronzemedal.png');
  39.         //Spritesheets
  40.         this.load.spritesheet('archer', 'assets/spritesheets/archer_sprites.png', {frameWidth: 128, frameHeight: 128});
  41.         this.load.spritesheet('rings', 'assets/spritesheets/rings_sprite.png', {frameWidth: 320, frameHeight: 320});
  42.         //Audio
  43.         this.load.audio('arrow_shot', 'assets/sounds/arrow_shooting.mp3');
  44.     }
  45.     function create ()
  46.     {  
  47.         //Load all the images that won't move
  48.         this.add.image(400, 300, 'sky');
  49.         this.add.image(210, 200, 'ground');
  50.  
  51.         //Create the archer/player
  52.         this.player = this.physics.add.sprite(100, 410, 'archer');
  53.         this.player.setBounce(0.2);
  54.         this.player.setCollideWorldBounds(true);
  55.  
  56.         //Shooting animation
  57.         this.anims.create({
  58.             key: 'shoot',
  59.             frames: this.anims.generateFrameNumbers('archer', {start : 0, end: 4}),
  60.             frameRate: 20,
  61.             repeat: 0
  62.         });
  63.  
  64.         //Rings animation
  65.         this.anims.create({
  66.             key: 'rings_anim',
  67.             frames: this.anims.generateFrameNumbers('rings', {start : 0, end : 69}),
  68.             frameRate: 10,
  69.             repeat: 0
  70.         })
  71.         //Play the animation on start
  72.         this.rings = this.physics.add.sprite(300, 40, 'rings');
  73.         this.rings.anims.play('rings_anim', true);
  74.  
  75.         //Create the target
  76.         this.target = this.physics.add.sprite(530, 365, 'target');
  77.         this.target.setSize(115, 95).setOffset(70, 130); //TARGET HITBOX
  78.         this.target.enableBody = true;
  79.         this.target.setImmovable();
  80.  
  81.         //Create an array for arrows for later
  82.         this.arrows = [];
  83.  
  84.         //Get keypresses
  85.         this.cursors = this.input.keyboard.createCursorKeys();
  86.         //Assign input for spacebar
  87.         this.spacebar = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.SPACE);
  88.        
  89.         //Play sound when the arrow is shot
  90.         this.arrowSound = this.sound.add('arrow_shot');
  91.  
  92.         //Make the arrows collide with the target
  93.         this.physics.add.collider(this.arrows, this.target)
  94.  
  95.         //Add the scoreboard in
  96.         scoreBoard = this.add.text(440, 40, "SCORE: 0", {fontSize: '32px', fill: '#fff'});
  97.     }
  98.     function update ()
  99.     {  
  100.         //Declare constants for movement
  101.         const playerMoveAmt = 200;
  102.         const arrowMoveAmt = 1500;
  103.         this.player.setDrag(2000);
  104.        
  105.         //Rotation of the player
  106.         if (this.cursors.up.isDown && this.player.angle > -45) {
  107.             this.player.angle -= 1;}
  108.  
  109.         if (this.cursors.down.isDown && this.player.angle < 0) {
  110.             this.player.angle += 1;}
  111.  
  112.         //Shooting with the spacebar
  113.         if (Phaser.Input.Keyboard.JustDown(this.spacebar)) {
  114.            
  115.             //Animate the shooting
  116.             this.player.anims.play('shoot', true);
  117.  
  118.             //Arrow shooting
  119.             let arrow = this.physics.add.sprite(this.player.x, (this.player.y + 20), 'arrow');
  120.             arrow.enableBody = true;
  121.             arrow.body.immovable = false;
  122.  
  123.             //Edit arrow hitbox
  124.             arrow.setSize(50, 15).setOffset(5, 50);
  125.  
  126.             arrow.setGravityY(3600); //Gravity will affect the arrows
  127.  
  128.             //Arrow speeds
  129.             arrow.setVelocityX(arrowMoveAmt);
  130.             arrow.setVelocityY((this.player.angle * 50));
  131.  
  132.             this.arrows.push(arrow); //Add arrow to the arrow created earlier
  133.             this.arrowSound.play(); //Play the sound
  134.  
  135.         }
  136.  
  137.         else if( this.target.body.touching.left) {
  138.  
  139.             let i = 0;
  140.  
  141.             //Set initial position of new medals
  142.             let arrowOnTargetPositionX = 200;
  143.  
  144.             //Loop to create multiple arrows
  145.             while (i < this.arrows.length) {
  146.                 newArrows = this.arrows[i];
  147.                 newArrows.setGravityY(0);
  148.                 newArrows.setVelocityX(0);
  149.                 newArrows.setVelocityY(0);
  150.  
  151.                 //Add 30 to the new medal's x position
  152.                 arrowOnTargetPositionX = arrowOnTargetPositionX + 40;
  153.  
  154.                 i++;
  155.             }
  156.  
  157.             //Call the function to determine medal and pass the variable
  158.             if(this.arrows.length <= 5) {
  159.                 getMedal(arrowOnTargetPositionX);
  160.             }
  161.         }
  162.  
  163.         getMedal = (value) => {
  164.             //Gold medal
  165.             if (410 < newArrows.y && newArrows.y < 435) {
  166.                 this.add.image(value, 170, 'gold_medal');
  167.                 score += 5;
  168.             }
  169.             //Silver medal
  170.             else if (395 < newArrows.y && newArrows.y < 450) {
  171.                 this.add.image(value, 170, 'silver_medal');
  172.                 score += 3;
  173.             }
  174.             //Bronze medal
  175.             else if (380 < newArrows.y && newArrows.y < 460) {
  176.                 this.add.image(value, 173, 'bronze_medal');
  177.                 score += 1;
  178.             }
  179.             scoreBoard.setText('Score: ' + score);
  180.         }
  181.     }
  182.    
  183.     function render() {
  184.     }
  185. </script>
Advertisement
Add Comment
Please, Sign In to add comment