1.1.0 • Published 1 year ago

ngx-tetris v1.1.0

Weekly downloads
31
License
MIT
Repository
github
Last release
1 year ago

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

NameDefaultTypeDescription
tileSize25pxcss unitSize to use when rendering single tile. Can be in 'px' or in 'em' or any(probably) other css unit
initialSpeed500integerMiliseconds (ms) time between falling cycles (lower number -> faster game)
startbooleanFlag that toggled from false to true starts the game
stopbooleanFlag that toggled from false to true stops the game
resetbooleanFlag that toggled from false to true resets the game
moveLeftbooleanFlag that toggled from false to true moves current piece to the left
moveRightbooleanFlag that toggled from false to true moves current piece to the right
moveDownbooleanFlag that toggled from false to true moves piece down (falls faster)
dropbooleanFlag that toggled from false to true moves piece down (instantly drops)
rotatebooleanFlag that toggled from false to true rotates current piece

Outputs

NameDescription
lineClearedCalled 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.

1.1.0

1 year ago

1.0.0

2 years ago

0.2.1

5 years ago

0.2.0

5 years ago

0.1.3

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago

0.0.1

6 years ago