GithubHelp home page GithubHelp logo

ractive / pxt-spritewrapper Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 825 KB

pxt-arcade extension to easily wrap a sprite in a class

Home Page: https://ractive.github.io/pxt-spritewrapper/

Ruby 3.12% Makefile 2.99% TypeScript 93.90%
makecode makecode-arcade makecode-arcade-extensions makecode-extension

pxt-spritewrapper's Introduction

pxt-spritewrapper Build Status Abzeichen

This extension allows you to easily wrap a sprite in a class and get the corresponding class instance when given a given sprite. Your class needs to extend the SpriteWrapper.Support base class. Your object's onDestroyed method is set as a callback to the sprite's onDestroyed method. Whenever the sprite gets destroyed, the onDestroyed method of your object is called, so that you can do some clanup. To destroy an object yourself, call the destroy method of its sprite or call the destroy method directly on the object.

Here's a very simple space shooter game that demonstrates how to use it:

class EnemySpaceShip extends SpriteWrapper.Support {
    private static readonly image: Image = img`
        5 5 5 5 5 5 d
        . 5 5 5 5 d .
        . . 5 5 d . .
        . . . 5 . . .
    `;

    constructor(xPos: number) {
        super(sprites.create(EnemySpaceShip.image, SpriteKind.Enemy));
        this.sprite.setFlag(SpriteFlag.AutoDestroy, true);
        this.sprite.y = 0;
        this.sprite.x = xPos;
        this.sprite.vy = 50;
    }

    public gotHit() {
        info.changeScoreBy(10);
        this.destroy();
    }
}

class SpaceShip extends SpriteWrapper.Support {
    private static readonly image: Image = img`
        . . . d . . .
        . . 1 1 d . .
        . 1 1 1 1 d .
        1 1 1 1 1 1 d
    `;

    constructor() {
        super(sprites.create(SpaceShip.image, SpriteKind.Player));
        this.sprite.setFlag(SpriteFlag.AutoDestroy, true);
        this.sprite.y = scene.screenHeight() - 10;
        this.sprite.setFlag(SpriteFlag.StayInScreen, true);

        controller.left.onEvent(ControllerButtonEvent.Pressed, function () {
            this.flyLeft();
        });
        controller.right.onEvent(ControllerButtonEvent.Pressed, function () {
            this.flyRight();
        });
        controller.A.onEvent(ControllerButtonEvent.Pressed, function () {
            this.shoot();
        });
    }

    public onDestroyed() {
        music.powerDown.play();
        game.over(false);
    }

    public flyLeft() {
        this.sprite.vy = 0;
        this.sprite.vx = -50;
    }

    public flyRight() {
        this.sprite.vy = 0;
        this.sprite.vx = 50;
    }

    public shoot() {
        sprites.createProjectileFromSprite(img`
            1
        `, this.sprite, 0, -70);
    }
}

const player: SpaceShip = new SpaceShip();

game.onUpdateInterval(1000, () => {
   new EnemySpaceShip(randint(0, scene.screenWidth()));
});

sprites.onOverlap(SpriteKind.Player, SpriteKind.Enemy, (playerSprite: Sprite, enemySprite: Sprite) => {
    SpriteWrapper.fromSprite(playerSprite).destroy();
    SpriteWrapper.fromSprite(enemySprite).destroy();
});

sprites.onOverlap(SpriteKind.Projectile, SpriteKind.Enemy, (projectileSprite: Sprite, enemySprite: Sprite) => {
    (SpriteWrapper.fromSprite(enemySprite) as EnemySpaceShip).gotHit();
    projectileSprite.destroy();
});

Using it in your project

To use this extension in your project, choose "Advanced > Extensions..." and enter https://github.com/ractive/pxt-spritewrapper in the search box.

Metadata (used for search, rendering)

  • for PXT/arcade
<script src="https://makecode.com/gh-pages-embed.js"></script><script>makeCodeRender("{{ site.makecode.home_url }}", "{{ site.github.owner_name }}/{{ site.github.repository_name }}");</script>

pxt-spritewrapper's People

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.