Appendix 6: Input Chapter Code

Below is the code for the game.js file as created on the Input Chapter.

  1 /**
  2  * This is the Game game state.
  3  * Its our game loop responsible for the game itself.
  4  *
  5  * In the create() function we setup the display.
  6  * In the update() function we do the gameloop.
  7  *
  8  * To learn more about states refer to:
  9  * Refer to: http://docs.phaser.io/Phaser.State.html
 10  */
 11 
 12 GameStates.Game = {
 13     resetBall: function() {
 14         this.ball.reset(160, 240);
 15         this.ball.body.velocity.x = this.ballSpeed;
 16         this.ball.body.velocity.y = this.ballSpeed;
 17     },
 18 
 19     initWorld: function() {
 20         // Some constants
 21         this.playerSpeed = 250;
 22         this.ballSpeed = 220;
 23         this.blocksPerRow = 5;
 24         this.blockRows = 4;
 25         this.playerLives = 13;
 26 
 27         // Add the background
 28         this.add.sprite(0, 0, 'background');
 29 
 30         // Add keyboard input
 31         this.cursors = this.input.keyboard.createCursorKeys();
 32     },
 33 
 34 
 35     addPlayer: function () {
 36         // Add the player
 37         this.player = this.add.sprite(160, 440, 'player');
 38         this.physics.arcade.enable(this.player);
 39         this.player.anchor.setTo(0.5, 0);
 40         this.player.enableBody = true;
 41         this.player.body.immovable = true;
 42         this.player.body.collideWorldBounds = true;
 43 
 44 
 45         // Add the display of player lives
 46         this.livesDisplay = this.add.text(10, 8, "Lives: " +
 47             this.playerLives, {
 48                 fill: "white",
 49                 fontSize: 12
 50             });
 51     },
 52 
 53     addBall: function () {
 54         // Add ball
 55         this.ball = this.add.sprite(160, 240, 'ball');
 56         this.physics.arcade.enable(this.ball);
 57         this.ball.anchor.setTo(0.5, null);
 58         this.ball.enableBody = true;
 59         this.ball.body.bounce.setTo(1, 1);
 60         this.ball.body.velocity.x = this.ballSpeed;
 61         this.ball.body.velocity.y = this.ballSpeed;
 62         this.ball.body.collideWorldBounds = true;
 63     },
 64 
 65     addBlocks: function () {
 66         // Blocks
 67         this.blocks = this.game.add.group();
 68         for (var line = 0; line <= this.blockRows - 1; line++) {
 69             for (var row = 0; row <= this.blocksPerRow - 1; row++) {
 70                 var posY = (line * 30) + 40;
 71                 var posX = (row * 50) + 40;
 72                 console.log("Adding block at: " + posX + "," + posY)
 73                 var temp = this.add.sprite(posX, posY, 'block');
 74                 this.physics.arcade.enable(temp);
 75                 temp.enableBody = true;
 76                 temp.body.immovable = true;
 77 
 78                 this.blocks.add(temp);
 79             }
 80         }
 81     },
 82 
 83     create: function() {
 84         this.initWorld();
 85         this.addPlayer();
 86         this.addBall();
 87         this.addBlocks();
 88     },
 89 
 90     checkHitWithBlocks: function () {
 91         this.game.physics.arcade.collide(this.ball, this.blocks, this.ballCollidesWi\
 92 thBlock);
 93     },
 94 
 95     checkHitWithPlayer: function () {
 96         this.game.physics.arcade.collide(this.ball, this.player);
 97     },
 98 
 99     handleTouchInput: function () {
100       if (this.input.pointer1.isDown) {
101           if (this.input.pointer1.worldX > 160) {
102               this.player.body.velocity.x = this.playerSpeed;
103           }
104 
105           if (this.input.pointer1.worldX <= 160) {
106               this.player.body.velocity.x = -1 * this.playerSpeed;
107           }
108        }
109     },
110 
111     handleKeyboardInput: function () {
112         if (this.cursors.right.isDown) {
113             this.player.body.velocity.x = this.playerSpeed;
114         }
115 
116         if (this.cursors.left.isDown) {
117             this.player.body.velocity.x = -1 * this.playerSpeed;
118         }
119     },
120 
121     update: function() {
122         this.handleTouchInput();
123         this.handleKeyboardInput();
124         this.checkHitWithBlocks();
125         this.checkHitWithPlayer();
126         this.ballCollidesWithGround();
127     },
128 
129     ballCollidesWithBlock: function(sprite, block) {
130         console.log("Collided with block!");
131         block.kill();
132     },
133 
134     ballCollidesWithGround: function() {
135         if (this.ball.y >= 470) {
136             this.playerLives -= 1;
137             this.resetBall();
138         }
139 
140         /*
141          Update lives display
142          */
143 
144         this.livesDisplay.setText("Lives: " + this.playerLives);
145 
146         if (this.playerLives === 0) {
147             // Soon there will be a game over state switch here
148         }
149 
150     }
151 
152 };