xxxxxxxxxx
//credit to: https://openprocessing.org/sketch/1359930
var squares = [];
var size = 80;
var zerosquarex, zerosquarey;
var isplaying = false;
function preload(){
theFont = loadFont('Righteous-Regular.ttf');
theSound = loadSound('coin.mp3');
}
function setup() {
createCanvas(windowWidth/4, windowHeight/2);
background(0);
textSize(size/3);
textFont (theFont);
textAlign(CENTER);
leftWall= width*0.15/2
rightWall=height*0.8
topWall=width*0.1
bottomWall=height*0.8
}
function draw() {
background(0);
if(!isplaying)
{
background(0);
fill(255);
textAlign(CENTER);
textSize(size/5);
if(frameCount%80>30) text('Press any key to START!!!', width/2, height/3);
if(frameCount%80>30) text('The goal of the game is to put all', width/2, height/3+20);
if(frameCount%80>30) text('numbers in order from 1-15', width/2, height/3+40);
if(frameCount%80>30) text('To restart the game press any key', width/2, height/3+70);
}
if(isplaying) // MAIN GAME LOOP
{
// drawWalls();
for(let i = 0;i<4;i++)
{
for(let j = 0;j<4;j++)
{
strokeWeight(4);
stroke('blue')
textSize(size/3);
fill(squares[i][j].r, squares[i][j].g, squares[i][j].b);
//rect(i*size + leftWall, j*size + topWall, size, size);
rect(i*size, j*size, size, size);
fill(0);
strokeWeight(0);
//text(squares[i][j].v, i*size + size/2 + leftWall, j*size + size/2 + topWall);
text(squares[i][j].v, i*size + size/2, j*size + size/2);
}
}
}
if(squares.v== [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]){
isplaying = false;
background(0);
fill(255);
textAlign(CENTER);
textSize(size/3);
if(frameCount%80>30) text('YOU WON!!!', width/2, height/3);
}
}
function keyTyped() {
newGame();
isplaying = true;
}
function newGame() {
var shuffle = new Array(16);
for(let i = 0;i<16;i++)
{
shuffle[i] = 0;
}
var sptr = 0;
while(sptr<16) // THIS IS GONNA GO FOREVER
{
let r = floor(random(16));
let alreadythere = 0;
for(let i =0;i<sptr;i++)
{
if(shuffle[i]==r)
{
alreadythere= 1;
}
}
if(alreadythere==0)
{
shuffle[sptr] = r;
sptr++;
}
}
//console.log(shuffle);
for(let i = 0;i<4;i++)
{
squares[i] = [];
for(let j = 0;j<4;j++)
{
squares[i][j] = {};
squares[i][j].v = shuffle[i*4 + j];
if(squares[i][j].v==0) {
squares[i][j].r = 0;
squares[i][j].g = 0;
squares[i][j].b = 0;
zerosquarex = i;
zerosquarey = j;
}
else {
squares[i][j].r = random(80, 255);
squares[i][j].g = random(96, 192);
squares[i][j].b = random(120, 200);
}
}
}
//console.log(squares);
//isplaying = true;
}
function mouseClicked()
{
var whichx = floor(mouseX/size);
var whichy = floor(mouseY/size);
var nexttozero = 0;
var distx = abs(zerosquarex-whichx);
var disty = abs(zerosquarey-whichy);
if(distx==1 && disty==0)
{
nexttozero = 1;
theSound.play();
theSound.interrupt = true;
}
if(distx==0 && disty==1)
{
nexttozero = 1;
theSound.play();
theSound.interrupt = true;
}
// console.log(squares[whichx][whichy].v + ": " + distx + " " + disty + " " + nexttozero);
if(nexttozero==1)
{
let temp = squares[zerosquarex][zerosquarey];
squares[zerosquarex][zerosquarey] = squares[whichx][whichy];
squares[whichx][whichy] = temp;
zerosquarex = whichx;
zerosquarey = whichy;
}
}