Guest User

Untitled

a guest
Jul 4th, 2016
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Created by Christian Hofmann on 28.06.16.
  3.  */
  4. // Create the state that will contain the whole game
  5. var buttonLeftPress = 0;
  6. var buttonRightPress = 0;
  7.  
  8. var mainState = {
  9.     preload: function() {
  10.         // Here we preload the assets
  11.         game.load.image('paddle', 'media/paddle.png');
  12.         game.load.image('brick', 'media/brick.png');
  13.         game.load.image('ball', 'media/ball.png');
  14.         game.load.image('arrow-left', 'media/arrow-left.png');
  15.         game.load.image('arrow-right', 'media/arrow-right.png');
  16.  
  17.         game.load.audio('music', 'media/music.mp3');
  18.         game.load.audio('sound1', 'media/sound1.mp3');
  19.  
  20.         // Fullscreen setup
  21.         game.scale.scaleMode = Phaser.ScaleManager.SHOW_ALL;
  22.         game.scale.fullScreenScaleMode = Phaser.ScaleManager.EXACT_FIT;
  23.     },
  24.  
  25.     create: function() {
  26.         if (!game.device.desktop){ game.input.onDown.add(gofull, this);}
  27.  
  28.         // Set the background color to blue
  29.         game.stage.backgroundColor = '#3598db';
  30.  
  31.         // Start the Arcade physics system (for movements and collisions)
  32.         game.physics.startSystem(Phaser.Physics.ARCADE);
  33.  
  34.         // Add the physics engine to all the game objetcs
  35.         game.world.enableBody = true;
  36.  
  37.         // Start our great music and load sound
  38.         this.music = game.add.audio('music');
  39.         this.sound1 = game.add.audio('sound1');
  40.         this.music.volume -= 0.5;
  41.         this.music.play();
  42.  
  43.         // Create the left/right arrow keys
  44.         this.left = game.input.keyboard.addKey(Phaser.Keyboard.LEFT);
  45.         this.right = game.input.keyboard.addKey(Phaser.Keyboard.RIGHT);
  46.  
  47.         // Create a group that will contain all the bricks
  48.         this.bricks = game.add.group();
  49.  
  50.         // Add 25 bricks to the group (5 columns an 5 lines)
  51.         for (var i = 0; i < 5; i++) {
  52.             for (var j = 0; j < 5; j++) {
  53.                 // Create the brick at the correct position
  54.                 var brick = game.add.sprite(55+i*60, 55+j*35, 'brick');
  55.  
  56.                 // Make sure the brick won't move when the ball hits it
  57.                 brick.body.immovable = true;
  58.  
  59.                 // Add the brick to the group
  60.                 this.bricks.add(brick);
  61.             }
  62.         }
  63.  
  64.         // Add the paddle at the bottom of the screen
  65.         this.paddle = game.add.sprite(200, 400, 'paddle');
  66.  
  67.         // Make sure the paddle won't move when it hits the ball
  68.         this.paddle.body.immovable = true;
  69.  
  70.         // Make sure the paddle not leave the field
  71.         this.paddle.body.bounce.setTo(1);
  72.         this.paddle.body.collideWorldBounds = true;
  73.  
  74.         // Add the ball
  75.         this.ball = game.add.sprite(200, 300, 'ball');
  76.  
  77.         // Give the ball some initial speed
  78.         this.ball.body.velocity.x = 185;
  79.         this.ball.body.velocity.y = 200;
  80.  
  81.         // Make sure the ball will bounce when hitting something
  82.         this.ball.body.bounce.setTo(1);
  83.         this.ball.body.collideWorldBounds = true;
  84.  
  85.         // Create touch buttons
  86.         this.buttonLeft = game.add.button(0, 500, 'arrow-left', null, this, 0, 1, 0, 1);
  87.         this.buttonLeft.fixedToCamera = true;
  88.         this.buttonLeft.events.onInputOver.add(function () {buttonLeftPress=1;});
  89.         this.buttonLeft.events.onInputOut.add(function(){buttonLeftPress=0;});
  90.         this.buttonLeft.events.onInputDown.add(function(){buttonLeftPress=1;});
  91.         this.buttonLeft.events.onInputUp.add(function(){buttonLeftPress=0;});
  92.  
  93.         this.buttonRight = game.add.button(288, 500, 'arrow-right', null, this, 0, 1, 0, 1);
  94.         this.buttonRight.fixedToCamera = true;
  95.         this.buttonRight.events.onInputOver.add(function () {buttonRightPress=1;});
  96.         this.buttonRight.events.onInputOut.add(function(){buttonRightPress=0;});
  97.         this.buttonRight.events.onInputDown.add(function(){buttonRightPress=1;});
  98.         this.buttonRight.events.onInputUp.add(function(){buttonRightPress=0;});
  99.  
  100.     },
  101.  
  102.     update: function() {
  103.         // Here we update the game 60 times per second
  104.  
  105.         // Move the paddle left/right when an arrow key is pressed
  106.         if (this.left.isDown || buttonLeftPress == 1) this.movePaddle(-300);
  107.         else if (this.right.isDown || buttonRightPress == 1) this.movePaddle(300);
  108.  
  109.         // Stop the paddle when no key is pressed
  110.         else this.paddle.body.velocity.x = 0;
  111.  
  112.         // Add collisions between the paddle and the ball
  113.         game.physics.arcade.collide(this.paddle, this.ball);
  114.  
  115.         // call the 'hit' function when the ball hits a brick
  116.         game.physics.arcade.collide(this.ball, this.bricks, this.hit, null, this);
  117.  
  118.         // Restart the game if the ball is below the paddle
  119.         if (this.ball.y > this.paddle.y) {
  120.             this.music.stop();
  121.             game.state.start('main');
  122.         }
  123.     },
  124.  
  125.     hit: function(ball, brick) {
  126.         // Here we removes a brick from the game
  127.         brick.kill();
  128.         // And play a sound
  129.         this.sound1.play();
  130.     },
  131.  
  132.     movePaddle: function(direction){
  133.         this.paddle.body.velocity.x = direction;
  134.     },
  135. };
  136.  
  137. function gofull() { game.scale.startFullScreen(false);}
  138.  
  139. // Initialize the game and start our state
  140. var game = new Phaser.Game(400,600);
  141. game.state.add('main', mainState);
  142. game.state.start('main');
Advertisement
Add Comment
Please, Sign In to add comment