Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Created by Christian Hofmann on 28.06.16.
- */
- // Create the state that will contain the whole game
- var buttonLeftPress = 0;
- var buttonRightPress = 0;
- var mainState = {
- preload: function() {
- // Here we preload the assets
- game.load.image('paddle', 'media/paddle.png');
- game.load.image('brick', 'media/brick.png');
- game.load.image('ball', 'media/ball.png');
- game.load.image('arrow-left', 'media/arrow-left.png');
- game.load.image('arrow-right', 'media/arrow-right.png');
- game.load.audio('music', 'media/music.mp3');
- game.load.audio('sound1', 'media/sound1.mp3');
- // Fullscreen setup
- game.scale.scaleMode = Phaser.ScaleManager.SHOW_ALL;
- game.scale.fullScreenScaleMode = Phaser.ScaleManager.EXACT_FIT;
- },
- create: function() {
- if (!game.device.desktop){ game.input.onDown.add(gofull, this);}
- // Set the background color to blue
- game.stage.backgroundColor = '#3598db';
- // Start the Arcade physics system (for movements and collisions)
- game.physics.startSystem(Phaser.Physics.ARCADE);
- // Add the physics engine to all the game objetcs
- game.world.enableBody = true;
- // Start our great music and load sound
- this.music = game.add.audio('music');
- this.sound1 = game.add.audio('sound1');
- this.music.volume -= 0.5;
- this.music.play();
- // Create the left/right arrow keys
- this.left = game.input.keyboard.addKey(Phaser.Keyboard.LEFT);
- this.right = game.input.keyboard.addKey(Phaser.Keyboard.RIGHT);
- // Create a group that will contain all the bricks
- this.bricks = game.add.group();
- // Add 25 bricks to the group (5 columns an 5 lines)
- for (var i = 0; i < 5; i++) {
- for (var j = 0; j < 5; j++) {
- // Create the brick at the correct position
- var brick = game.add.sprite(55+i*60, 55+j*35, 'brick');
- // Make sure the brick won't move when the ball hits it
- brick.body.immovable = true;
- // Add the brick to the group
- this.bricks.add(brick);
- }
- }
- // Add the paddle at the bottom of the screen
- this.paddle = game.add.sprite(200, 400, 'paddle');
- // Make sure the paddle won't move when it hits the ball
- this.paddle.body.immovable = true;
- // Make sure the paddle not leave the field
- this.paddle.body.bounce.setTo(1);
- this.paddle.body.collideWorldBounds = true;
- // Add the ball
- this.ball = game.add.sprite(200, 300, 'ball');
- // Give the ball some initial speed
- this.ball.body.velocity.x = 185;
- this.ball.body.velocity.y = 200;
- // Make sure the ball will bounce when hitting something
- this.ball.body.bounce.setTo(1);
- this.ball.body.collideWorldBounds = true;
- // Create touch buttons
- this.buttonLeft = game.add.button(0, 500, 'arrow-left', null, this, 0, 1, 0, 1);
- this.buttonLeft.fixedToCamera = true;
- this.buttonLeft.events.onInputOver.add(function () {buttonLeftPress=1;});
- this.buttonLeft.events.onInputOut.add(function(){buttonLeftPress=0;});
- this.buttonLeft.events.onInputDown.add(function(){buttonLeftPress=1;});
- this.buttonLeft.events.onInputUp.add(function(){buttonLeftPress=0;});
- this.buttonRight = game.add.button(288, 500, 'arrow-right', null, this, 0, 1, 0, 1);
- this.buttonRight.fixedToCamera = true;
- this.buttonRight.events.onInputOver.add(function () {buttonRightPress=1;});
- this.buttonRight.events.onInputOut.add(function(){buttonRightPress=0;});
- this.buttonRight.events.onInputDown.add(function(){buttonRightPress=1;});
- this.buttonRight.events.onInputUp.add(function(){buttonRightPress=0;});
- },
- update: function() {
- // Here we update the game 60 times per second
- // Move the paddle left/right when an arrow key is pressed
- if (this.left.isDown || buttonLeftPress == 1) this.movePaddle(-300);
- else if (this.right.isDown || buttonRightPress == 1) this.movePaddle(300);
- // Stop the paddle when no key is pressed
- else this.paddle.body.velocity.x = 0;
- // Add collisions between the paddle and the ball
- game.physics.arcade.collide(this.paddle, this.ball);
- // call the 'hit' function when the ball hits a brick
- game.physics.arcade.collide(this.ball, this.bricks, this.hit, null, this);
- // Restart the game if the ball is below the paddle
- if (this.ball.y > this.paddle.y) {
- this.music.stop();
- game.state.start('main');
- }
- },
- hit: function(ball, brick) {
- // Here we removes a brick from the game
- brick.kill();
- // And play a sound
- this.sound1.play();
- },
- movePaddle: function(direction){
- this.paddle.body.velocity.x = direction;
- },
- };
- function gofull() { game.scale.startFullScreen(false);}
- // Initialize the game and start our state
- var game = new Phaser.Game(400,600);
- game.state.add('main', mainState);
- game.state.start('main');
Advertisement
Add Comment
Please, Sign In to add comment