Skip to content

Instantly share code, notes, and snippets.

@MarkGamed7794
Last active June 8, 2021 12:55
Show Gist options
  • Save MarkGamed7794/b9627a117988bde2742e5e82e5a9fda1 to your computer and use it in GitHub Desktop.
Save MarkGamed7794/b9627a117988bde2742e5e82e5a9fda1 to your computer and use it in GitHub Desktop.
SCRIPT-8
{
"0": {
"0": {
"1": 0
},
"1": {
"1": 1
},
"2": {
"1": 2
},
"3": {
"1": 3
},
"4": {
"1": 4
},
"5": {
"1": 5
},
"6": {
"1": 4
},
"7": {
"1": 6
}
}
}
//title: Tetras
var board = [
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
]
var nextArr = shuffle([0,1,2,3,4,5,6]);
var bagArr = shuffle([0,1,2,3,4,5,6]);
var blockX = 3
var blockY = 0
var blockRotation = 0
var currentBlock = 0
var next = 0
var aah = false;
currentBlock = next;
next = nextArr.shift();
if(nextArr.length < 7){nextArr.push(bagArr.shift())}
currentBlock = next;
next = nextArr.shift();
if(nextArr.length < 7){nextArr.push(bagArr.shift())}
var downTimer = 0
var dropTimer = 0
var linesCleared = 0
var gameover = false;
var message = "";
var messageTimer = 0;
var combo = -1;
var score = 0
var lines = 0
var level = 0
var blocks = [
["0100010001000100", "0000111100000000", "0010001000100010", "0000000011110000"], // I
["0200020022000000", "2000222000000000", "0220020002000000", "0000222000200000"], // J
["0100110001000000", "0100111000000000", "0100011001000000", "0000111001000000"], // T
["0200020002200000", "0000222020000000", "2200020002000000", "0020222000000000"], // L
["0330330000000000", "0300033000300000", "0000033033000000", "3000330003000000"], // S
["3300033000000000", "0030033003000000", "0000330003300000", "0300330030000000"], // Z
["0000011001100000", "0000011001100000", "0000011001100000", "0000011001100000"] // O
]
function checkCollision(){
for(var y = 0;y < 4;y++){
for(var x = 0;x < 4;x++){
if(blockY+y < 20){if(board[blockY+y][blockX+x] != 0 && blocks[currentBlock][blockRotation][y*4+x] != 0){return true;}}
else{if(blocks[currentBlock][blockRotation][y*4+x] != 0){return true;}}
}
}
return false;
}
function shuffle(a) {
var j, x, i;
for (i = a.length - 1; i > 0; i--) {
j = Math.floor(Math.random() * (i + 1));
x = a[i];
a[i] = a[j];
a[j] = x;
}
return a;
}
function drop(){
var hardDropFlag = false;
blockY++;
if(checkCollision()){
blockY--;
hardDropFlag = true;
for(var y = 0;y < 4;y++){
for(var x = 0;x < 4;x++){
if(blockX+x < 10 && blockX+x >= 0 && blockY+y < 20 && blocks[currentBlock][blockRotation][y*4+x] != 0){board[blockY+y][blockX+x] = blocks[currentBlock][blockRotation][y*4+x]}
}
}
blockX = 3;
blockY = 0;
currentBlock = next;
next = nextArr.shift();
if(nextArr.length < 7){nextArr.push(bagArr.shift())}
if(bagArr.length == 0){bagArr = shuffle([0,1,2,3,4,5,6]);}
blockRotation = 0;
linesCleared = 0
if(checkCollision()){gameover = true;}
for(var l = 0;l < board.length;l++){
if(!board[l].includes(0)){
board.splice(l,1)
board.unshift([0,0,0,0,0,0,0,0,0,0])
lines++;
linesCleared++;
level = Math.floor(lines/10)+1
}
}
if(linesCleared > 0){
combo++;
score += [40,200,500,1200][linesCleared-1]*level;
if(combo > 0){
score += [40,200,500,1200][linesCleared-1]*level*(0.2*combo);
message = combo + " combo! (x" + (1+(0.2*combo)) + ")"
messageTimer = 120;
}
}else{
combo = -1;
}
return true;
}
return false;
}
update = (dum,input) => {
if(!gameover){
if(input.rightPressed){
blockX++;
if(checkCollision()){blockX--;}
}
if(input.leftPressed){
blockX--;
if(checkCollision()){blockX++;}
}
if(input.down){
downTimer++;
if((downTimer > 25 && downTimer%5 == 0) || downTimer == 1){drop();score++;}
}else{
downTimer = 0
}
if(input.bPressed){
blockRotation = (blockRotation+1)%4;
if(checkCollision()){blockRotation = (blockRotation+3)%4;}
}
if(input.aPressed){
blockRotation = (blockRotation+3)%4;
if(checkCollision()){blockRotation = (blockRotation+1)%4;}
}
if(input.upPressed){
while(!drop()){score+=2}
}
}
}
draw = () => {
if(!gameover){
clear()
for(var y = 0;y < board.length;y++){
for(var x = 0;x < board[0].length;x++){
rectFill(x*6,y*6,6,6,board[y][x])
}
}
for(var y = 0;y < 4;y++){
for(var x = 0;x < 4;x++){
if(blockX+x < 10 && blockX+x >= 0 && blockY+y < 20 && blocks[currentBlock][blockRotation][y*4+x] != 0){rectFill((blockX+x)*6,(blockY+y)*6,6,6,blocks[currentBlock][blockRotation][y*4+x])}
}
}
print(65,1,"NEXT",4);
print(64,0,"NEXT",1);
for(var y = 0;y < 4;y++){
for(var x = 0;x < 4;x++){
if(blocks[next][0][y*4+x] != 0){rectFill(70+x*6,10+y*6,6,6,blocks[next][0][y*4+x])}
}
}
if(messageTimer > 0){print(65,40,message,4);}
print(65,51,"SCORE",4);
print(64,50,"SCORE",1);
print(123-(score.toString().length*4),59,score,4);
print(122-(score.toString().length*4),58,score,1);
print(65,76,"LINES",4);
print(64,75,"LINES",1);
print(123-(lines.toString().length*4),84,lines,4);
print(122-(lines.toString().length*4),83,lines,1);
print(65,101,"LEVEL",4);
print(64,100,"LEVEL",1);
print(123-(level.toString().length*4),109,level,4);
print(122-(level.toString().length*4),108,level,1);
//I'd put music and SFX here, but they don't work...
dropTimer++;
if(dropTimer > 70 - clamp(level*5,0,65)){drop();dropTimer = 0}
messageTimer--;
}else{
rectFill(22,38,84,42,2);
rectFill(23,39,82,40,3);
rectFill(24,40,80,38,2);
print(46,44,"GAME OVER",4);
print(45,43,"GAME OVER",1);
print(30,56,"SCORE:",4);
print(29,55,"SCORE:",1);
print(98-(score.toString().length*4),56,score,4);
print(97-(score.toString().length*4),55,score,1);
print(30,63,"LINES:",4);
print(29,62,"LINES:",1);
print(98-(lines.toString().length*4),63,lines,4);
print(97-(lines.toString().length*4),62,lines,1);
print(30,70,"LEVEL:",4);
print(29,69,"LEVEL:",1);
print(98-(level.toString().length*4),70,level,4);
print(97-(level.toString().length*4),69,level,1);
}
}
{
"iframeVersion": "0.1.255",
"lines": [
223,
0,
0,
0,
0,
0,
0,
0
]
}
{
"0": {
"notes": [
"0e37",
"2b27",
"3c37",
"4d37",
"6c37",
"7b27",
"8a27",
"10a27",
"11c37",
"12e37",
"14d37",
"15c37"
],
"tempo": "1"
},
"1": {
"notes": [
"0b27",
"3c37",
"4d37",
"6e37",
"8c37",
"10a27",
"12a27"
],
"tempo": "1"
},
"2": {
"notes": [
"0d37",
"2f37",
"3a37",
"5g37",
"6f37",
"7e37",
"10c37",
"11e37",
"13d37",
"14c37",
"15b27"
],
"tempo": "1"
},
"3": {
"notes": [
"2b27",
"4c37",
"5d37",
"7e37",
"9c37",
"11a27",
"13a27"
],
"tempo": "1"
},
"4": {
"notes": [
"1e37",
"5c37",
"9d37",
"13b27"
],
"tempo": "1"
},
"5": {
"notes": [
"1c37",
"5a27",
"9g#27",
"13b27"
],
"tempo": "1"
},
"6": {
"notes": [
"1c37",
"3d37",
"5f37",
"9e37"
],
"tempo": "1"
},
"7": {
"notes": [
"0g37"
],
"tempo": "3"
},
"8": {
"notes": [
"0e37",
"1f37",
"2e37",
"3f37",
"4f#37",
"5e37",
"6f37",
"7f#37",
"8g37"
],
"tempo": "3"
}
}
{
"0": {
"0": 0
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment