Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Create our 'main' state that will contain the game
- var mainState = {
- preload: function() {
- // This function will be executed at the beginning
- // That's where we load the images and sounds
- game.load.image('bird', 'assets/bird.png');
- game.load.image('pipe', 'assets/pipe.png');
- },
- create: function() {
- // This function is called after the preload function
- // Here we set up the game, display sprites, etc.
- // If this is not a desktop (so it's a mobile device)
- if (game.device.desktop == false) {
- // Set the scaling mode to SHOW_ALL to show all the game
- game.scale.scaleMode = Phaser.ScaleManager.SHOW_ALL;
- // Set a minimum and maximum size for the game
- // Here the minimum is half the game size
- // And the maximum is the original game size
- game.scale.setMinMax(game.width/2, game.height/2, game.width, game.height);
- }
- // Center the game horizontally and vertically
- game.stage.backgroundColor = '#71c5cf';
- game.physics.startSystem(Phaser.Physics.ARCADE);
- this.bird = game.add.sprite(100, 245, 'bird');
- this.pipes = game.add.group();
- game.physics.arcade.enable(this.bird);
- this.bird.body.gravity.y = 1000;
- var spaceKey = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);
- spaceKey.onDown.add(this.jump, this);
- game.input.onDown.add(this.jump, this);
- this.timer = game.time.events.loop(1500, this.addRowOfPipes, this);
- this.score = 0;
- this.labelScore = game.add.text(20, 20, "0",
- { font: "30px Arial", fill: "#ffffff" });
- setTimeout(test, 100);
- },
- update: function() {
- // This function is called 60 times per second
- // It contains the game's logic
- if (this.bird.y < 0 || this.bird.y > 490)
- this.restartGame();
- game.physics.arcade.overlap(this.bird, this.pipes, this.restartGame, null, this);
- },
- jump: function() {
- // Add a vertical velocity to the bird
- this.bird.body.velocity.y = -350;
- },
- addOnePipe: function(x, y) {
- // Create a pipe at the position x and y
- var pipe = game.add.sprite(x, y, 'pipe');
- // Add the pipe to our previously created group
- this.pipes.add(pipe);
- // Enable physics on the pipe
- game.physics.arcade.enable(pipe);
- // Add velocity to the pipe to make it move left
- pipe.body.velocity.x = -200;
- // Automatically kill the pipe when it's no longer visible
- pipe.checkWorldBounds = true;
- pipe.outOfBoundsKill = true;
- },
- addRowOfPipes: function() {
- // Randomly pick a number between 1 and 5
- // This will be the hole position
- var hole = Math.floor(Math.random() * 5) + 1;
- // Add the 6 pipes
- // With one big hole at position 'hole' and 'hole + 1'
- for (var i = 0; i < 8; i++)
- if (i != hole && i != hole + 1)
- this.addOnePipe(400, i * 60 + 10);
- this.score += 1;
- this.labelScore.text = this.score;
- },
- // Restart the game
- restartGame: function() {
- // Start the 'main' state, which restarts the game
- var buildProgress = 1;
- var houseid = 1;
- if(this.score > 0){
- $.ajax({
- type: "POST",
- url: 'updatehighscore.php',
- data: {test: buildProgress, test2: this.score },
- });
- }
- game.state.start('main');
- },
- };
- // Initialize Phaser, and create a 400px by 490px game
- var game = new Phaser.Game(400, 490);
- // Add and start the 'main' state to start the game
- game.state.add('main', mainState, true);
- function test(){
- window.dispatchEvent(new Event('resize'));
- }
- function resizeGame() {var height = $(window).height();var width = $(window).width(); game.width = width;game.height = height; }
- $( document ).ready(function() {
- $(window).resize(function() { window.resizeGame(); } );
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement