Advertisement
Guest User

Untitled

a guest
Mar 19th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. function rnd(nMax) {
  2. return Math.floor(Math.random() * nMax);
  3. }
  4.  
  5. const RENDERER_WIDTH = 800;
  6. const RENDERER_HEIGHT = 600;
  7.  
  8. const app = new PIXI.Application(
  9. RENDERER_WIDTH,
  10. RENDERER_HEIGHT,
  11. {backgroundColor : 0x1099bb}
  12. );
  13. document.body.appendChild(app.view);
  14.  
  15. const INITIAL_BUNNIES_AMOUNT = 10;
  16. const bunnies = [];
  17.  
  18. function makeBunny(width = 20, height = 32) {
  19. const bunny = PIXI.Sprite.fromImage('examples/assets/bunny.png');
  20.  
  21. bunny.anchor.set(0.5);
  22. bunny.width = width;
  23. bunny.height = height;
  24.  
  25.  
  26. bunny.body = new PIXI.Rectangle(0, 0, width, height);
  27. bunny.velocity = new PIXI.Point(0, 0);
  28.  
  29. bunnies.push(bunny);
  30.  
  31. return bunny;
  32. }
  33.  
  34.  
  35. function initialize() {
  36. for (let i = 0; i < INITIAL_BUNNIES_AMOUNT; i++) {
  37. const bunny = makeBunny();
  38.  
  39. bunny.body.x = rnd(app.view.width);
  40. bunny.body.y = rnd(app.view.height);
  41. bunny.velocity = new PIXI.Point(rnd(10), rnd(10));
  42.  
  43. app.stage.addChild(bunny);
  44. }
  45. }
  46.  
  47. initialize();
  48.  
  49. app.ticker.add(function(delta) {
  50. bunnies.forEach((b, i) => {
  51. b.body.x = += b.velocity.x);
  52. b.body.y = += b.velocity.y);
  53.  
  54.  
  55. if (b.body.x >= RENDERER_WIDTH || b.body.x <= 0) {
  56. b.velocity.x *= -1;
  57. }
  58. if (b.body.y >= RENDERER_HEIGHT || b.body.y <= 0) {
  59. b.velocity.y *= -1;
  60. }
  61.  
  62. b.x = b.body.x;
  63. b.y = b.body.y;
  64. });
  65. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement