Giter Club home page Giter Club logo

blockrain.js's Introduction

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.

blockrain.js's People

Contributors

heyimjuani avatar lsei avatar mikirobles avatar mrcoles avatar robertogonzalez avatar sbehrends avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

blockrain.js's Issues

Show upcoming/next block

The original Tetris show the upcoming/next block somewhere. Didn't see it immediately in the demo or options.

The game speed won't restart as default

Once I call $game.blockrain('restart'); the game speed is supposed to reset to default too, but the speed become faster.

Is the setTimeout function has some problem that it won't be cleared after restart, so the old function used in setTimeout still running after a new game is started?

demo.html is error

I want to play it on my mobile phone. Then I noticed that demo.html has buttons to support mobile.
But when I run it, it reports an error:

Uncaught TypeError: Cannot read properties of undefined (reading 'square')
    at Object.getBlockColor (blockrain.jquery.src.js:1102)
    at Object.drawBlock (blockrain.jquery.src.js:957)
    at Object.draw (blockrain.jquery.src.js:663)
    at Object.render (blockrain.jquery.src.js:935)
    at Object.createRandomBoard (blockrain.jquery.src.js:926)
    at Object.init (blockrain.jquery.src.js:729)
    at e.<computed>.<computed>._create (blockrain.jquery.src.js:228)
    at e.<computed>.<computed>._create (blockrain.jquery.libs.js:2)
    at e.<computed>.<computed>._createWidget (blockrain.jquery.libs.js:2)
    at new e.<computed>.<computed> (blockrain.jquery.libs.js:2)

Responsiv and Speed

Hi, great script, i like it a lot.

Question 1: how to use it on mobile? is it possible now?
Question 2: will there be a possibility to change the speed during the game?

We want to use it on our 404 Site, can we do it? Is it enough we link to your page as author of the script?

Asset licenses?

Are your assets licensed the same way as your code, or differently?

Where are the controls?

I can't figure out how to activate the touch controls. I tried with
$ game.blockrain ('controls', true);
$ game.blockrain ('touchcontrols', true);
But no buttons appear next to the playing field. Swiping or typing doesn't work either. I can't control the game on touch screens.

module

Your lib is awesome but it's a pain to use in any modern setup. Can you consider using JS module (es6 native + babel or simple UMD header) so it can be easily import and not just limited to script node.

Assistance with triggering controls

Hi there! I'm not hopeful I'll get a reply seeing as the last issue was over a year ago, but I recently have been attempting to integrate blockrain into a programming project concerning a sandbox VR game called Anyland. Anyland has in-game webscreens that allow you to pass in-game triggers through to the website using the JavaScript function AnylandTold. Proper usage looks like this:
function AnylandTold(tell, isAuthority) { //function goes here, usually if...else... with tell }

I can not for the life of me figure out how to trigger the controls for blockrain using this system! Can anyone offer me some assistance? I'd like to just do a 'if(tell == 'right') {moveRight(true)}' but I get the feeling that won't work. Please let me know!

Speeding up the game

Is there a way to speed up the game after a certain number of lines have been reached?

Using a game controller

Hi,

I would like to use Blockrain using a USB game controller. It will be in some arcade-like environment, in a full screen browser, and using a controller somewhat like this: Arcade Zero Delay Interface Board (but that is not so important as it registers as any generic USB gamepad).

Luckily, there is a Gamepad API in Javascript: Using the Gamepad API /
Implementing controls using the Gamepad API
.

How could we make it work with Blockrain? Any directions to go? Any first step or specific file to look at?

Thanks!

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.