Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const PIPES = 8;
- const mainState = {
- preload: function () {
- game.load.image('bird', 'assets/bird.png');
- game.load.image('pipe', 'assets/pipe.png');
- game.load.audio('jump', 'assets/jump.wav');
- },
- create: function () {
- this.jumpSound = game.add.audio('jump');
- this.score = 0;
- this.pipes = game.add.group();
- game.stage.backgroundColor = '#71c5cf';
- this.bird = game.add.sprite(100, 245, 'bird');
- this.bird.anchor.setTo(-0.2, 0.5);
- game.physics.startSystem(game.physics.arcade);
- game.physics.arcade.enable(this.bird);
- this.bird.body.gravity.y = 1000;
- const spaceKey = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);
- spaceKey.onDown.add(this.jump, this);
- this.timer = game.time.events.loop(1500, this.addPipes, this);
- this.labelScore = game.add.text(20, 20, "0", {
- font: "30px Arial",
- fill: "#ffffff"
- });
- },
- update: function () {
- if (this.bird.y < 0 || this.bird.y > 490) {
- this.restartGame();
- }
- game.physics.arcade.overlap(this.bird, this.pipes, this.hitPipe, null, this);
- if (this.bird.angle < 20) {
- this.bird.angle += 1;
- }
- },
- jump: function () {
- if (!this.bird.alive) {
- return;
- }
- this.bird.body.velocity.y = -350;
- const animation = game.add.tween(this.bird);
- animation.to({angle: -20}, 100);
- animation.start();
- this.jumpSound.play();
- },
- restartGame: function () {
- game.state.start('main');
- },
- addPipe: function (x, y) {
- const pipe = game.add.sprite(x, y, 'pipe');
- this.pipes.add(pipe);
- game.physics.arcade.enable(pipe);
- pipe.body.velocity.x = -200;
- pipe.checkWorldBounds = true;
- pipe.outOfBoundsKill = true;
- },
- addPipes: function () {
- const holeIndex = Math.floor(Math.random() * 5) + 1;
- for (let i = 0; i < PIPES; i++) {
- if (i !== holeIndex && i !== holeIndex + 1) {
- this.addPipe(400, i * 60 + 10);
- }
- }
- this.score += 1;
- this.labelScore.text = this.score;
- },
- hitPipe: function () {
- if (!this.bird.alive) {
- return;
- }
- this.bird.alive = false;
- game.time.events.remove(this.timer);
- this.pipes.forEach(function(pipe) {
- pipe.body.velocity.x = 0;
- }, this);
- }
- };
- const game = new Phaser.Game(400, 490);
- game.state.add('main', mainState);
- game.state.start('main');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement