Giter Club home page Giter Club logo

ngx-tetris's Introduction

ngx-tetris

Tetris game as an angular component

ngx-tetris is actually only the core of the game... YOU need to add everything around it (controls, score...) yourself :)

Check the demo here

Using it:

Install:

npm install ngx-tetris

Import

import {BrowserModule} from '@angular/platform-browser';
import {NgModule} from '@angular/core';

import {AppComponent} from './app.component';
import {TetrisCoreModule} from 'ngx-tetris';

@NgModule({
    declarations: [
        AppComponent,
    ],
    imports: [
        BrowserModule,
        TetrisCoreModule
    ],
    providers: [],
    bootstrap: [AppComponent]
})
export class AppModule {
}

Add/Connect to your fancy control interface

    <tetris-core #game
        (lineCleared)="onLineCleared()"></tetris-core>

        <button (click)="game.actionStart()">Start</button>
        <button (click)="game.actionStop()">Stop</button>
        <button (click)="game.actionReset()">Reset</button>
        <button (click)="game.actionRotate()">Rotate</button>
        <button (click)="game.actionLeft()">Left</button>
        <button (click)="game.actionRight()">Right</button>
        <button (click)="game.actionDown()">Down</button>
        <button (click)="game.actionDrop()">Drop</button>

Inputs

Name Default Type Description
tileSize 25px css unit Size to use when rendering single tile. Can be in 'px' or in 'em' or any(probably) other css unit
initialSpeed 500 integer Miliseconds (ms) time between falling cycles (lower number -> faster game)
start boolean Flag that toggled from false to true starts the game
stop boolean Flag that toggled from false to true stops the game
reset boolean Flag that toggled from false to true resets the game
moveLeft boolean Flag that toggled from false to true moves current piece to the left
moveRight boolean Flag that toggled from false to true moves current piece to the right
moveDown boolean Flag that toggled from false to true moves piece down (falls faster)
drop boolean Flag that toggled from false to true moves piece down (instantly drops)
rotate boolean Flag that toggled from false to true rotates current piece

Outputs

Name Description
lineCleared Called whenever a line was completely filled and got cleared
gameOver :( player was flooded with pieces and didn't make it (remember about reset button)

Public methods

  • actionStart
  • actionStop
  • actionReset
  • actionLeft
  • actionDown
  • actionRight
  • actionRotate
  • actionDrop

which can be used like:

<button (click)="onRotateButtonPressed()">Rotate</button>
...
export class TetrisContainingComponent {
    @ViewChild(TetrisCoreComponent)
    private _tetris: TetrisCoreComponent;

    public onRotateButtonPressed() {
        this._tetris.actionRotate();
    }
}

OR

<tetris-core #game>
    </tetris-core>
<button (click)="game.actionRotate()">Rotate</button>

Styling

To change colors and tiles (to black and white for example) define styles with colors like

tetris-core {
    tile {
        background: white;
        &.color-box {
          background: #000000;
        }
    }
}

for full example (and all class names) go here

Development

Run ng serve for a dev server. Navigate to http://localhost:4200/. The app will automatically reload if you change any of the source files.

Authors

Chrystian Ruminowicz

Licence

This project is licensed under the MIT license. See the LICENSE file for more info.

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.