xxxxxxxxxx
173
// today we went to:
// https://www.spinderihallerne.dk/arrangementer/retro-game-days-2024/
const OFFSETS = [0,90,180,270,385,475,565,665]
const SIZES = [[80,80],[80,80],[80,80],[100,100],[80,80],[80,80],[80,80],[90,90]];
class Block{
constructor(col,row,type){
this.col = col;
this.row = row;
this.type = type;
}
}
function drawBlock(b,large=false){
stroke("white");
fill("darkblue");
let [w,h] = SIZES[b.type];
if (!large)
image(blocks,
b.col*20+20,b.row*20+85,20,20,
OFFSETS[b.type],0,w,h);
else // large version
image(blocks,
b.col*20+20,b.row*20+85,40,40,
OFFSETS[b.type],0,w,h);
}
let level = ['..........'.split(''),
'..........'.split(''),
'..........'.split(''),
'..........'.split(''),
'..........'.split(''),
'..........'.split(''),
'..........'.split(''),
'..........'.split(''),
'..........'.split(''),
'..........'.split(''),
'..........'.split(''),
'..........'.split(''),
'..........'.split(''),
'.......1..'.split(''),
'.......1..'.split(''),
'.......21.'.split(''),
'.......32.'.split(''),
'.......11.'.split(''),
'1411113111'.split('')
]
let currentBlock;
let next;
let bk;
let blocks;
let fallingTimer;
let score = 0;
function preload(){
bk = loadImage('background.png');
// 10 blocks width x 10
blocks = loadImage('blocks.png');
}
function setup() {
createCanvas(400, 500);
next = new Block(13,9,~~random(8));
currentBlock = new Block(5,0,~~random(8));
fallingTimer = 30*4;
}
let justCollided;
function draw() {
justCollided = false;
background(0);
image(bk,0,0);
noStroke();
fill("white");
textSize(24);
text(score,150,45);
for (let row=0;row<level.length;row++){
for (let col=0;col<level[row].length;col++){
if (level[row][col]!='.'){
drawBlock(new Block(col,row,
parseInt(level[row][col])));
} else {
noFill();
stroke("white");
point(col*20+20 +10,row*20+85 +10);
}
}
}
drawBlock(next,true); // large
drawBlock(currentBlock);
if (fallingTimer>0){
fallingTimer-=1;
}
if (fallingTimer==0){
fallingTimer = 30*4;
currentBlock.row+=1;
}
// collisions
if ((currentBlock.row>18) ||
(level[currentBlock.row][currentBlock.col]!='.')){
currentBlock.row -=1; // roll back
level[currentBlock.row][currentBlock.col] = currentBlock.type;
currentBlock = new Block(5,0,~~random(8));
currentBlock.type = next.type;
next = new Block(13,9,~~random(8));
justCollided = true;
}
if (justCollided){
// if 3 same in a row -> score!
for (let row=level.length-3;row>0;row--){
for (let col=0;col<level[row].length;col++){
if ((level[row][col]!='.') &&
(level[row][col]==level[row+1][col]) &&
(level[row+1][col]==level[row+2][col]) ){
score += 100;
level[row][col] = '.';
level[row+1][col] = '.';
level[row+2][col] = '.';
}
}
}
// console.table(level);
}
}
function keyPressed(){
if (keyCode === LEFT_ARROW) {
currentBlock.col -=1;
} else if (keyCode === RIGHT_ARROW) {
currentBlock.col +=1;
} else if (keyCode === DOWN_ARROW) {
fallingTimer=1;
}
// collisions
if (currentBlock.col<0)
currentBlock.col = 0;
if (currentBlock.col>=10)
currentBlock.col = 9;
}
function mouseClicked() {
if (mouseY > height*3/4) {
fallingTimer=1;
} else {
if (mouseX < width/2) {
currentBlock.col -=1;
}
if (mouseX > width/2) {
currentBlock.col +=1;
}
}
// collisions
if (currentBlock.col<0)
currentBlock.col = 0;
if (currentBlock.col>=10)
currentBlock.col = 9;
}