Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function rnd(nMax) {
- return Math.floor(Math.random() * nMax);
- }
- const RENDERER_WIDTH = 800;
- const RENDERER_HEIGHT = 600;
- const app = new PIXI.Application(
- RENDERER_WIDTH,
- RENDERER_HEIGHT,
- {backgroundColor : 0x1099bb}
- );
- document.body.appendChild(app.view);
- const INITIAL_BUNNIES_AMOUNT = 10;
- const bunnies = [];
- function makeBunny(width = 20, height = 32) {
- const bunny = PIXI.Sprite.fromImage('examples/assets/bunny.png');
- bunny.anchor.set(0.5);
- bunny.width = width;
- bunny.height = height;
- bunny.body = new PIXI.Rectangle(0, 0, width, height);
- bunny.velocity = new PIXI.Point(0, 0);
- bunnies.push(bunny);
- return bunny;
- }
- function initialize() {
- for (let i = 0; i < INITIAL_BUNNIES_AMOUNT; i++) {
- const bunny = makeBunny();
- bunny.body.x = rnd(app.view.width);
- bunny.body.y = rnd(app.view.height);
- bunny.velocity = new PIXI.Point(rnd(10), rnd(10));
- app.stage.addChild(bunny);
- }
- }
- initialize();
- app.ticker.add(function(delta) {
- bunnies.forEach((b, i) => {
- b.body.x = += b.velocity.x);
- b.body.y = += b.velocity.y);
- if (b.body.x >= RENDERER_WIDTH || b.body.x <= 0) {
- b.velocity.x *= -1;
- }
- if (b.body.y >= RENDERER_HEIGHT || b.body.y <= 0) {
- b.velocity.y *= -1;
- }
- b.x = b.body.x;
- b.y = b.body.y;
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement