├── .gitmodules ├── README.md ├── examples ├── 01_helloWorld.html ├── 02_displayingTheCanvas.html ├── 03_spriteFromImage.html ├── 04_aliases.html ├── 05_loadingProgress.html ├── 06_positionAndRotation.html ├── 07_spriteFromTileset.html ├── 08_spriteFromTextureAtlas.html ├── 09_movingSprites.html ├── 10_velocityVariables.html ├── 11_gameStates.html ├── 12_keyboardMovement.html ├── 13_groupingSprites.html ├── 14_graphicPrimitives.html ├── 15_displayingText.html ├── 16_collisionDetection.html ├── 17_treasureHunter.html ├── images │ ├── animals.json │ ├── animals.png │ ├── blob.png │ ├── cat.png │ ├── door.png │ ├── dungeon.png │ ├── explorer.png │ ├── screenshots │ │ ├── 01.png │ │ ├── 02.png │ │ ├── 03.png │ │ ├── 04.png │ │ ├── 05.png │ │ ├── 06.png │ │ ├── 07.png │ │ ├── 08.png │ │ ├── 09.png │ │ ├── 10.png │ │ ├── 11.png │ │ ├── 12.png │ │ ├── 13.png │ │ ├── 14.png │ │ ├── 15.png │ │ ├── 16.png │ │ ├── 17.png │ │ ├── 18.png │ │ ├── 19.png │ │ ├── 20.png │ │ ├── 21.png │ │ ├── 22.png │ │ ├── 23.png │ │ ├── 24.5.png │ │ ├── 24.png │ │ ├── 25.png │ │ ├── 26.ai │ │ ├── 26.png │ │ ├── 27.png │ │ ├── 28.png │ │ └── 29.png │ ├── tileset.png │ ├── treasure.png │ ├── treasureHunter.json │ └── treasureHunter.png └── texturePackerFiles │ └── treasureHunter.tps └── pixi └── pixi.min.js /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "pixi.js"] 2 | path = pixi.js 3 | url = git@github.com:GoodBoyDigital/pixi.js.git 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Learning Pixi 2 | ============= 3 | 4 | A step-by-step introduction to making games and interactive media with 5 | the [Pixi rendering engine](https://github.com/pixijs/pixi.js). **[Updated for Pixi v5.3.10](https://github.com/pixijs/pixi.js/releases/tag/v5.3.10)**. [Chinese version here: Pixi官方教程中文版](https://github.com/Zainking/learningPixi). If you like this 6 | tutorial, [you'll love the book, which contains 80% more content!](http://www.springer.com/us/book/9781484210956). 7 | 8 | ### Table of contents 9 | 1. [Introduction](#introduction) 10 | 2. [Setting up](#settingup) 11 | 1. [Installing Pixi](#installingpixi) 12 | 3. [Creating the stage and renderer](#application) 13 | 4. [Pixi sprites](#sprites) 14 | 5. [Loading images into the texture cache](#loading) 15 | 6. [Displaying sprites](#displaying) 16 | 1. [Using Aliases](#usingaliases) 17 | 1. [A little more about loading things](#alittlemoreaboutloadingthings) 18 | 1. [Make a sprite from an ordinary JavaScript Image object or Canvas](#makeaspritefromanordinaryjavascriptimageobject) 19 | 2. [Assigning a name to a loaded file](#assigninganametoaloadingfile) 20 | 3. [Monitoring load progress](#monitoringloadprogress) 21 | 4. [More about Pixi's loader](#moreaboutpixisloader) 22 | 7. [Positioning sprites](#positioning) 23 | 8. [Size and scale](#sizenscale) 24 | 9. [Rotation](#rotation) 25 | 10. [Make a sprite from a tileset sub-image](#tileset) 26 | 11. [Using a texture atlas](#textureatlas) 27 | 12. [Loading the texture atlas](#loadingatlas) 28 | 13. [Creating sprites from a loaded texture atlas](#creating-sprites-from-a-loaded-texture-atlas) 29 | 14. [Moving Sprites](#movingsprites) 30 | 15. [Using velocity properties](#velocity) 31 | 16. [Game states](#gamestates) 32 | 17. [Keyboard Movement](#keyboard) 33 | 18. [Grouping Sprites](#grouping) 34 | 1. [Local and global positions](#localnglobal) 35 | 2. [Using a ParticleContainer to group sprites](#spritebatch) 36 | 19. [Pixi's Graphic Primitives](#graphic) 37 | 1. [Rectangle](#rectangles) 38 | 2. [Circles](#circles) 39 | 3. [Ellipses](#ellipses) 40 | 4. [Rounded rectangles](#rounded-rectangles) 41 | 5. [Lines](#lines) 42 | 6. [Polygons](#polygons) 43 | 20. [Displaying text](#text) 44 | 21. [Collision detection](#collision) 45 | 1. [The hitTestRectangle function](#the-hittestrectangle-function) 46 | 22. [Case study: Treasure Hunter](#casestudy) 47 | 1. [Initialize the game in the setup function](#initialize) 48 | 1. [Creating the game scenes](#gamescene) 49 | 2. [Making the dungeon, door, explorer and treasure](#makingdungon) 50 | 3. [Making the blob monsters](#makingblob) 51 | 4. [Making health bar](#healthbar) 52 | 5. [Making message text](#message) 53 | 2. [Playing the game](#playing) 54 | 3. [Moving the explorer](#movingexplorer) 55 | 1. [Containing movement](#containingmovement) 56 | 4. [Moving the monsters](#movingmonsters) 57 | 5. [Checking for collisions](#checkingcollisions) 58 | 6. [Reaching the exit door and ending game](#reachingexit) 59 | 23. [More about sprites](#spriteproperties) 60 | 24. [Taking it further](#takingitfurther) 61 | 1.[Hexi](#hexi) 62 | 2.[BabylonJS](#babylonjs) 63 | 25. [Supporting this project](#supportingthisproject) 64 | 65 | 66 | Introduction 67 | ------------ 68 | 69 | Pixi is an extremely fast 2D sprite rendering engine. What does that 70 | mean? It means that it helps you to display, animate and manage 71 | interactive graphics so that it's easy for you to make games and 72 | applications using 73 | JavaScript and other HTML5 technologies. It has a sensible, 74 | uncluttered API and includes many useful features, like supporting 75 | texture atlases and providing a streamlined system for animating 76 | sprites (interactive images). It also gives you a complete scene graph so that you can 77 | create hierarchies of nested sprites (sprites inside sprites), as well 78 | as letting you attach mouse and touch events directly to sprites. And, 79 | most 80 | importantly, Pixi gets out of your way so that you can use as much or 81 | as little of it as you want to, adapt it to your personal coding 82 | style, and integrate it seamlessly with other useful frameworks. 83 | 84 | Pixi’s API is actually a refinement of a well-worn and battle-tested 85 | API pioneered by Macromedia/Adobe Flash. Old-skool Flash developers 86 | will feel right at home. Other current sprite rendering frameworks use 87 | a similar API: CreateJS, Starling, Sparrow and Apple’s SpriteKit. The 88 | strength of Pixi’s API is that it’s general-purpose: it’s not a game 89 | engine. That’s good because it gives you total expressive freedom to make anything you like, and wrap your own custom game engine around it. 90 | 91 | In this tutorial you’re going to find out how to combine Pixi’s 92 | powerful image rendering features and scene graph to start making 93 | games. But Pixi isn't just for games - you can use these same 94 | techniques to create any interactive media applications. That means 95 | apps for phones! 96 | 97 | What do you need to know before you get started with this tutorial? 98 | 99 | You should have a reasonable understanding of HTML and 100 | JavaScript. You don't have to be an expert, just an ambitious beginner 101 | with an eagerness to learn. If you don't know HTML and JavaScript, the 102 | best place to start learning it is this book: 103 | 104 | [Foundation Game Design with HTML5 and JavaScript](http://www.apress.com/9781430247166) 105 | 106 | I know for a fact that it's the best book, because I wrote it! 107 | 108 | There are also some good internet resources to help get you started: 109 | 110 | [Khan Academy: Computer 111 | Programming](http://www.khanacademy.org/computing/cs) 112 | 113 | [Code Academy: 114 | JavaScript](http://www.codecademy.com/tracks/javascript) 115 | 116 | Choose whichever best suits your learning style. 117 | 118 | Ok, got it? 119 | Do you know what JavaScript variables, functions, arrays and objects are and how to 120 | use them? Do you know what [JSON data 121 | files](http://www.copterlabs.com/blog/json-what-it-is-how-it-works-how-to-use-it/) 122 | are? Have you used the [Canvas Drawing API](https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API/Drawing_graphics_with_canvas)? 123 | 124 | To use Pixi, you'll also need to run a webserver in your root project 125 | directory. Do you know what a webserver is and 126 | how to launch one in your project folder? The best way is to use 127 | [node.js](http://nodejs.org) and then to install the extremely easy to use 128 | [http-server](https://github.com/nodeapps/http-server). However, you need to be comfortable working with the Unix 129 | command line if you want to do that. You can learn how to use 130 | Unix [in this 131 | video](https://www.youtube.com/watch?feature=player_embedded&v=cX9ASUE3YAQ) 132 | and, when you're finished, follow it with [this 133 | video](https://www.youtube.com/watch?v=INk0ATBbclc). You should learn 134 | how to use Unix - it only takes a couple of hours to learn and is a 135 | really fun and easy way to interact with your computer. 136 | 137 | But if you don't want to mess around with the command line just yet, try the [Mongoose](http://cesanta.com/) 138 | webserver: 139 | 140 | Or, just write all your code using the excellent [Brackets text 141 | editor](http://brackets.io). Brackets automatically launches a webserver 142 | and browser for you when you click the lightning bolt button in its 143 | main workspace. 144 | 145 | Now if you think you're ready, read on! 146 | 147 | (Request to readers: this is a *living document*. If you have any 148 | questions about specific details or need any of the content clarified, please 149 | create an **issue** in this GitHub repository and I'll update the text 150 | with more information.) 151 | 152 | 153 | Setting up 154 | ---------- 155 | 156 | Before you start writing any code, create a folder for your project, and launch a 157 | webserver in the project's root directory. If you aren't running a 158 | webserver, Pixi won't work. 159 | 160 | Next, you need to install Pixi. 161 | 162 | 163 | ### Installing Pixi 164 | 165 | The version used for this introduction is **v5.3.10** 166 | and you can find the `pixi.min.js` file either in this repository's `pixi` folder or on [Pixi's release page for v5.3.10](https://github.com/pixijs/pixi.js/releases/tag/v5.3.10). 167 | Or, you can get the latest version from [Pixi's main release page](https://github.com/pixijs/pixi.js/releases). 168 | 169 | This one file is all you need to use Pixi. You can ignore all the 170 | other files in the repository: **you don't need them.** 171 | 172 | Next, create a basic HTML page, and use a 173 | ` 179 | ``` 180 | Here's a basic HTML page that you could use to link Pixi and test that 181 | it's working. (This assumes that the `pixi.min.js` is in a subfolder called `pixi`): 182 | 183 | ```html 184 | 185 | 186 |
187 | 188 |