Skip to content

Instantly share code, notes, and snippets.

@albizures
Last active December 31, 2019 13:00
Show Gist options
  • Save albizures/dca90c7202f4d12f9873205ed0166a9e to your computer and use it in GitHub Desktop.
Save albizures/dca90c7202f4d12f9873205ed0166a9e to your computer and use it in GitHub Desktop.
SCRIPT-8
const size = 8;
const center = 128 / 2;
const colors = [0, 1, 2, 3, 4, 5, 7, 8, 9];
const getRandomElement = items => {
const index = random(0, items.length -1)
return items[index];
};
const paintBlockGroup = figure => {
const { color, offset, items } = figure;
items.forEach(item => {
const x = item.x + offset.x;
const y = item.y + offset.y;
rectFill(x, y, size, size, color);
rectStroke(x, y, size, size, 6);
});
};
const createItem = (x, y) => {
return {
x,
y
};
};
const createSquare = () => {
return {
color: getRandomElement(colors),
offset: {
x: center,
y: 0
},
items: [
createItem(0, 0),
createItem(size, 0),
createItem(size, size),
createItem(0, size)
]
};
};
const createLine = () => {
return {
color: getRandomElement(colors),
offset: {
x: center,
y: 0
},
items: [
createItem(0, 0),
createItem(0, size),
createItem(0, size * 2),
createItem(0, size * 3)
]
};
};
const createL = () => {
const lastBlock = getRandomElement([
createItem(size, size * 2),
createItem(-size, size * 2)
]);
return {
color: getRandomElement(colors),
offset: {
x: center,
y: 0
},
items: [
createItem(0, 0),
createItem(0, size),
createItem(0, size * 2),
lastBlock
]
};
};
const createZ = () => {
const { firstBlock, lastBlock } = getRandomElement([
{
firstBlock: createItem(-size, 0),
lastBlock: createItem(size, size)
},
{
firstBlock: createItem(size, 0),
lastBlock: createItem(-size, size)
}
]);
return {
color: getRandomElement(colors),
offset: {
x: center,
y: 0
},
items: [
firstBlock,
createItem(0, 0),
createItem(0, size),
lastBlock
]
};
};
const createT = () => {
return {
color: getRandomElement(colors),
offset: {
x: center,
y: 0
},
items: [
createItem(0, 0),
createItem(0, size),
createItem(-size, size),
createItem(size, size )
]
};
}
const shapes = [createSquare, createLine, createL, createZ, createT];
const createRandomGroup = () => {};
init = state => {
state.blocks = [];
state.blockGroup = getRandomElement(shapes)();
};
update = (state, input, elapsed) => {};
draw = state => {
clear();
paintBlockGroup(state.blockGroup);
};
{
"iframeVersion": "0.1.273",
"lines": [
138,
0,
0,
0,
0,
0,
0,
0
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment