HTML5 games. It uses Pixi.js internally for fast 2D Canvas and WebGL rendering.
<!DOCTYPE html> <head> <meta charset="utf-8"> <title>Flappy Bird</title> <link rel="shortcut icon" href="/favicon.ico" /> <style type="text/css"> * { margin: 0; padding: 0; } </style> </head> <body> <script type="text/javascript"> var Clay = Clay || {}; Clay.gameKey = "gflappybird"; Clay.readyFunctions = []; Clay.ready = function(fn) { Clay.readyFunctions.push(fn); }; (function() { var clay = document.createElement("script"); clay.async = true; clay.src = ("https:" == document.location.protocol ? "https://" : "http://") + "clay.io/api/api-leaderboard-achievement.js"; var tag = document.getElementsByTagName("script")[0]; tag.parentNode.insertBefore(clay, tag); })(); </script> <script src="//ajax.googleapis.com/ajax/libs/webfont/1.4.7/webfont.js"></script> <script src="//cdnjs.cloudflare.com/ajax/libs/phaser/1.1.4/phaser.min.js"></script> <script src="js/Game.js"></script> </body> </html>
WebFont.load({ google: { families: ['Press+Start+2P'] }, active: function() { GameInitialize(); } });
var DEBUG_MODE = true, // SPEED = 180, // GRAVITY = 1800, // BIRD_FLAP = 550, // "" PIPE_SPAWN_MIN_INTERVAL = 1200, // PIPE_SPAWN_MAX_INTERVAL = 3000, // AVAILABLE_SPACE_BETWEEN_PIPES = 130, // ( ) CLOUDS_SHOW_MIN_TIME = 3000, // CLOUDS_SHOW_MAX_TIME = 5000, // MAX_DIFFICULT = 100, // SCENE = '', // , . ( body) TITLE_TEXT = "FLAPPY BIRD", // HIGHSCORE_TITLE = "HIGHSCORES", // HIGHSCORE_SUBMIT = "POST SCORE", // INSTRUCTIONS_TEXT = "TOUCH\nTO\nFLY", // DEVELOPER_TEXT = "Developer\nEugene Obrezkov\nghaiklor@gmail.com", // :) GRAPHIC_TEXT = "Graphic\nDmitry Lezhenko\ndima.lezhenko@gmail.com", LOADING_TEXT = "LOADING...", // WINDOW_WIDTH = window.innerWidth || document.documentElement.clientWidth || document.getElementsByTagName('body')[0].clientWidth, WINDOW_HEIGHT = window.innerHeight || document.documentElement.clientHeight || document.getElementsByTagName('body')[0].clientHeight;
var Background, // Clouds, CloudsTimer, // Pipes, PipesTimer, FreeSpacesInPipes, // , "" , "" Bird, // Town, //TileSprite FlapSound, ScoreSound, HurtSound, // , SoundEnabledIcon, SoundDisabledIcon, // \ TitleText, DeveloperText, GraphicText, ScoreText, InstructionsText, HighScoreTitleText, HighScoreText, PostScoreText, LoadingText, // PostScoreClickArea, // isScorePosted = false, // , "" isSoundEnabled = true, // , Leaderboard; // Leaderboard Clay.io
new Game (width, height, renderer, parent, state, transparent, antialias)We are interested in width , height , renderer , parent . It is enough to specify the size of the canvas, the rendering method and the empty container so that Phaser starts rendering the game scene in the body.
var Game = new Phaser.Game(WINDOW_WIDTH, WINDOW_HEIGHT, Phaser.CANVAS, SCENE);
add (key, state, autoStart)key is the string for identifying the State (its ID), state is the Phaser.State object, and autoStart - whether to start the State immediately after its initialization. In this case, we do not need autoStart, so that we can determine the call of the states at the right moments of the game.
Game.state.add('Boot', BootGameState, false); Game.state.add('Preloader', PreloaderGameState, false); Game.state.add('MainMenu', MainMenuState, false); Game.state.add('Game', GameState, false); Game.state.add('GameOver', GameOverState, false);
Game.state.start('Boot');
// instance Canvas var Game = new Phaser.Game(WINDOW_WIDTH, WINDOW_HEIGHT, Phaser.CANVAS, SCENE); // RequestAnimationFrame Game.raf = new Phaser.RequestAnimationFrame(Game); Game.antialias = false; Game.raf.start(); // State Game // State' Game.state.add('Boot', BootGameState, false); Game.state.add('Preloader', PreloaderGameState, false); Game.state.add('MainMenu', MainMenuState, false); Game.state.add('Game', GameState, false); Game.state.add('GameOver', GameOverState, false); // Boot State' Game.state.start('Boot'); // Clay Leaderboard Clay.ready(function() { Leaderboard = new Clay.Leaderboard({ id: 'your-leaderboard-id' }); });
var BootGameState = new Phaser.State();
var BootGameState = new Phaser.State(); BootGameState.create = function() { LoadingText = Game.add.text(Game.world.width / 2, Game.world.height / 2, LOADING_TEXT, { font: '32px "Press Start 2P"', fill: '#FFFFFF', stroke: '#000000', strokeThickness: 3, align: 'center' }); LoadingText.anchor.setTo(0.5, 0.5); Game.state.start('Preloader', false, false); };
Phaser.Loader.spritesheet(key, url, frameWidth, frameHeight, frameMax, margin, spacing) Phaser.Loader.image(key, url, overwrite) Phaser.Loader.audio(key, urls, autoDecode)
var loadAssets = function loadAssets() { Game.load.spritesheet('bird', 'img/bird.png', 48, 35); Game.load.spritesheet('clouds', 'img/clouds.png', 64, 34); Game.load.image('town', 'img/town.png'); Game.load.image('pipe', 'img/pipe.png'); Game.load.image('soundOn', 'img/soundOn.png'); Game.load.image('soundOff', 'img/soundOff.png'); Game.load.audio('flap', 'wav/flap.wav'); Game.load.audio('hurt', 'wav/hurt.wav'); Game.load.audio('score', 'wav/score.wav'); };
var PreloaderGameState = new Phaser.State();
PreloaderGameState.preload = function() { loadAssets(); };
PreloaderGameState.create = function() { var tween = Game.add.tween(LoadingText).to({ alpha: 0 }, 1000, Phaser.Easing.Linear.None, true); tween.onComplete.add(function() { Game.state.start('MainMenu', false, false); }, this); };
var PreloaderGameState = new Phaser.State(); PreloaderGameState.preload = function() { loadAssets(); }; PreloaderGameState.create = function() { var tween = Game.add.tween(LoadingText).to({ alpha: 0 }, 1000, Phaser.Easing.Linear.None, true); tween.onComplete.add(function() { Game.state.start('MainMenu', false, false); }, this); };
Source: https://habr.com/ru/post/214013/