• Stars
    star
    954
  • Rank 47,702 (Top 1.0 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 9 years ago
  • Updated about 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

HTML5 Tetris Game for jQuery

Blockrain.js

A tetris game in HTML5 + Javascript (with autoplay!)

Check out the demo

Blockrain Screenshot

Setup

Create any element (a div, article, figure, whatever you want) and make sure it has a width and a height set via CSS . You can use any class, but we are using .game for this example.

<div class="game" style="width:250px; height:500px;"></div>

Then you just include jquery and blockrain and setup the game with $('.game').blockrain(). Adding the CSS file is strongly recommended, as it provides some important styles for the UI (but you can customize them as needed).

<!-- The stylesheet should go in the <head>, or be included in your CSS -->
<link rel="stylesheet" href="blockrain.css">

<!-- jQuery and Blockrain.js -->
<script src="jquery.js"></script>
<script src="blockrain.js"></script>
<script>
    $('.game').blockrain();
</script>

For extra fun, you can enable continuous autoplay, so the game plays itself continuously:

$('.game').blockrain({ autoplay: true, autoplayRestart: true });

Themes

Blockrain comes with many themes out-of-the-box, but you can create custom ones by adding them to BlockrainThemes. You have multiple settings and can even use custom textures (base64-encoded).

{
  background: '#000000', // The main background color.
  backgroundGrid: '#101010', // You can draw a small background grid as well.
  primary: null, // Color of the falling blocks. This overrides the standard block color.
  secondary: null, // Color of the placed blocks. This overrides the standard block color.
  stroke: null, // Border color for the blocks.
  innerStroke: null, // A small border inside the blocks to give some texture.

  // The following are the colors of each piece
  blocks: {
    line:     '#fa1e1e',
    square:   '#f1fa1e',
    arrow:    '#d838cb',
    rightHook:'#f5821f',
    leftHook: '#42c6f0',
    rightZag: '#4bd838',
    leftZag:  '#fa1e1e'
  }
}

Here's an example of a retro theme (vim) with a custom texture:

{
  background: '#000000',
  backgroundGrid: 'data:image/png;base64,iVBORw0KGgoAAA{AND SO ON}',
  primary: '#C2FFAE',
  secondary: '#C2FFAE',
  stroke: '#000000',
  strokeWidth: 3,
  innerStroke: null
}

Creating custom designs

You can now use completely custom designs for each block! You just link to one image for each block when creating your theme and the plugin takes care of the rest. You can even use lists of images if you want BlockRain to pick a random design for each block.

Keep in mind that the images need to follow the exact same format (rotation and width/height ratio) as the blocks that are bundled with the custom theme.

Fun fact: Now you can rotate the square!

'custom': {
  background: '#040304',
  backgroundGrid: '#000',
  complexBlocks: {
    line:     ['assets/blocks/custom/line.png', 'assets/blocks/custom/line.png'],
    square:   'assets/blocks/custom/square.png',
    arrow:    'assets/blocks/custom/arrow.png',
    rightHook:'assets/blocks/custom/rightHook.png',
    leftHook: 'assets/blocks/custom/leftHook.png',
    rightZag: 'assets/blocks/custom/rightZag.png',
    leftZag:  'assets/blocks/custom/leftZag.png'
  }
}

Available themes:

  • custom NEW!
  • candy
  • modern
  • retro
  • vim
  • monochrome
  • gameboy
  • aerolab

Remember you can create custom themes or modify these to better suit your design needs.

Options

Blockrain comes with many options to help customize the game:

{
  autoplay: false, // Let a bot play the game
  autoplayRestart: true, // Restart the game automatically once a bot loses
  showFieldOnStart: true, // Show a bunch of random blocks on the start screen (it looks nice)
  theme: null, // The theme name or a theme object
  blockWidth: 10, // How many blocks wide the field is (The standard is 10 blocks)
  autoBlockWidth: false, // The blockWidth is dinamically calculated based on the autoBlockSize. Disabled blockWidth. Useful for responsive backgrounds
  autoBlockSize: 24, // The max size of a block for autowidth mode
  difficulty: 'normal', // Difficulty (normal|nice|evil).
  speed: 20, // The speed of the game. The higher, the faster the pieces go.

  // Copy
  playText: 'Let\'s play some Tetris',
  playButtonText: 'Play',
  gameOverText: 'Game Over',
  restartButtonText: 'Play Again',
  scoreText: 'Score',

  // Basic Callbacks
  onStart: function(){},
  onRestart: function(){},
  onGameOver: function(score){},

  // When a line is made. Returns the number of lines, score assigned and total score
  onLine: function(lines, scoreIncrement, score){}
}

Methods

There are a few utility methods available to control the game. $game represents your game selector (like $('.game'), for example).

// Start the game
$game.blockrain('start');

// Restart the game
$game.blockrain('restart');

// Trigger a game over
$game.blockrain('gameover');
// Pause
$game.blockrain('pause');

// Resume
$game.blockrain('resume');
// Enable or Disable Autoplay (true|false)
$game.blockrain('autoplay', true);
// Enable or Disable Controls (true|false)
$game.blockrain('controls', true);
// Change the theme. 

// You can provide a theme name...
$game.blockrain('theme', 'vim');

// Or a theme object. **Check out src/blockrain.jquery.themes.js** for examples.
$game.blockrain('theme', {
  background: '#ffffff',
  primary: '#ff7b00',
  secondary: '#000000'
});
// Return the current score
var score = $game.blockrain('score');

Building Blockrain.js

This will generate the full minified source under /build . It's basically License + libs + themes + src, concatenated and minified.

gulp build

Credits

Blockrain.js is based on the work by @mrcoles, which is one of the best HTML5 tetris versions out there. The code was then modified and refactored to build a jQuery Plugin and added quite a few methods to easily add themes to it and make it simpler to implement.

More Repositories

1

midnight.js

Switch your nav's design on the fly
JavaScript
3,669
star
2

readremaining.js

How long you'll need to read an article
JavaScript
553
star
3

nextjs-pwa

Build a PWA with Next.JS
JavaScript
142
star
4

setup

Make your Mac Aerowesome
Python
134
star
5

frontend-developer-coding-challenge

Are your looking for a remote developer job? Solve this frontend developer challenge and show us what you can do and what you are an expert at!
125
star
6

rooms

A simple Display for Meeting Rooms
JavaScript
102
star
7

pixelcounterapp

How many pixels have you crafted?
HTML
68
star
8

booom

Dribbble Enhancement Suite
JavaScript
51
star
9

lullaby

A treatment for tinnitus
JavaScript
44
star
10

youtube-tv

Build a desktop app with Node Webkit
JavaScript
42
star
11

subtle-animations

How to add simple UI details
CSS
35
star
12

coding-challenges

Show your abilities as a developer
HTML
35
star
13

welcome

A macOS app to onboard new Aerolabers
JavaScript
34
star
14

platzi-pwa

Material de PWA en Platzi
JavaScript
28
star
15

acanohayluz

Reporte de cortes de luz en Argentina
PHP
20
star
16

workshop-nextjs

Aprendiendo Next.JS
JavaScript
16
star
17

pixelcounter

Count the pixels in all the images in a folder
JavaScript
15
star
18

svg

The SVG Workflow
HTML
15
star
19

aerolab.github.io

Open Source Projects by Aerolab
JavaScript
13
star
20

activate-mac

Activate Windows. For Mac.
JavaScript
12
star
21

criterios

Criterios de Calidad de Aerolab
HTML
11
star
22

fullstack-developer-coding-challenge

HTML
11
star
23

aerolab-cli

A tool for automating dev tasks
JavaScript
10
star
24

perfect

The only perfect piece of software
9
star
25

platzi-nextjs

Material de Next.JS en Platzi
JavaScript
9
star
26

iojs-colorpicker

A color picker for our IO.JS logo
JavaScript
8
star
27

nokia-psd

The long-awaited PSD for Nokia 1100
SCSS
7
star
28

type

Workshop de tipografía
CSS
7
star
29

jsconf2014-workshop

Node-Webkit Workshop for JSConf
JavaScript
7
star
30

meeting-quantifier

How much did you spend on that meeting?
CSS
6
star
31

goldenimage.js

Fix camera rotation of an image
JavaScript
6
star
32

iojs-logo

IO.JS Logo Proposal
6
star
33

nextjs-routing

Routing y Layout avanzado con Next.JS
JavaScript
5
star
34

rosarigasino

Egasel mejogasor lenguagasaje degasel mugasundo
JavaScript
5
star
35

zeplin-renamer

Rename Zeplin images for React Native
Python
5
star
36

nextjs-intro

Introducción a Next.JS y Styled JSX
JavaScript
5
star
37

origami

Workshop de Origami
3
star
38

startupmap

Mapping Startups in Latin America
CSS
3
star
39

tutorial-navigator

A tutorial for Auth0
JavaScript
1
star
40

meeetup

Buenos Aires Dribbble Meetup 2014
1
star
41

next-sanity-starter

Next.js starter with Santy
TypeScript
1
star