Randomizers

In this lesson, you’ll discover the history behind randomizers and choose one for the game.

To have more colors than only a blue tetromino in our game, we need to add more pieces to our code.

Following the Super Rotation System, we can take the first position of the pieces and add them to the constants and their colors:

Press + to interact
const COLORS = [
'cyan',
'blue',
'orange',
'yellow',
'green',
'purple',
'red'
];
const SHAPES = [
[
[0, 0, 0, 0],
[1, 1, 1, 1],
[0, 0, 0, 0],
[0, 0, 0, 0]
],
[
[2, 0, 0],
[2, 2, 2],
[0, 0, 0]
],
// And so on
];

Basic randomizer

To randomly pick one of the tetrominoes, we can use Math, a built-in object in JavaScript that includes a couple of static methods to help us with our calculations:

...

Create a free account to view this lesson.

By signing up, you agree to Educative's Terms of Service and Privacy Policy