xxxxxxxxxx
574
// change this to calibrate the camera's range for detecting black blobs.
// lower numbers exclude more blobs, higher numbers include more.
let rangeVal = 100;
let filterRadius = 6;
//background image
let img, startImg;
//boolean to turn walker movement on and off
let isMoving = false;
//change the speed to make the game faster - default 7.5
let speed = 12.5;
//walker variable for movement and size
let walkX, walkY, wlakR, stepX, stepY;
//boolean for turning diceCount text on
let textFlag = false;
//camera variables
let capture;
let tracker;
//turn counter
let playerCount = -1;
let player = [];
let playerColor = [];
let playerPhoto = [];
let turnOver = false;
//dice and weights
let diceCount;
let currentDiceCount;
//change to make dice worth more or less, 1 is max
let weightVal = 0.3;
//toggle value for aligning and showing camera
let showGrid = false;
let rhi, ghi, bhi;
let rlo, glo, blo;
function preload() {
img = loadImage("assets/players.jpg");
startImg = loadImage("assets/cover1.jpg");
imgSheree = loadImage("assets/Sheree.png");
imgNoble = loadImage("assets/Noble.png");
imgClarence = loadImage("assets/Clarence.png");
imgWalker = loadImage("assets/Walker.png");
}
//camera function to set target color
function setTarget(r, g, b, range) {
range = range || 32;
rhi = r + range;
rlo = r - range;
ghi = g + range;
glo = g - range;
bhi = b + range;
blo = b - range;
}
function setup() {
//camera calibration stuff
// lightRangeSlider = createSlider(10, 150, 80);
// lightRangeSlider.position(width / 5, height / 5);
// lightRangeSlider.hide();
// rangeVal = lightRangeSlider.value();
//camera stuff
var w = 640,
h = 480;
capture = createCapture(VIDEO);
capture.size(w, h);
capture.parent('camera');
cnv = createCanvas(w, h);
cnv.parent('container');
// capture.hide(); // tracking.js can't track the video when it's hidden
setTarget(0, 0, 0, rangeVal);
tracking.ColorTracker.registerColor('match', function(r, g, b) {
if (r <= rhi && r >= rlo &&
g <= ghi && g >= glo &&
b <= bhi && b >= blo) {
return true;
}
return false;
});
tracker = new tracking.ColorTracker(['match']);
tracker.minDimension = 0.10; // make this smaller to track smaller objects
capture.elt.id = 'p5video'; //element
tracking.track('#p5video', tracker, {
camera: true
});
tracker.on('track', function(event) {
camCal = event.data;
diceCount = event.data.length;
// print(diceCount);
diceCount = event.data.filter(d => {
if (d.width < 1 || d.height < 1 || d.width > filterRadius || d.height > filterRadius) return false
else return true
}).length;
camCal = event.data.filter(c => {
if (c.width < 1 || c.height < 1 || c.width > filterRadius || c.height > filterRadius) return false
else return true
});
//to trouble shoot the camera settings
// strokeWeight(2);
// stroke(255, 0, 0);
// noFill();
// console.log(diceCount);
// camCal.forEach(function(r) {
// rect(r.x, r.y, r.width, r.height);
// console.log('r.width: ', r.width);
// console.log('r.height: ', r.height);
// });
});
//game stuff
var gameCanvas = createCanvas(700, 700);
gameCanvas.parent('game');
//Walker variables
walkX = -20;
walkY = -20;
walkR = width / 40;
//player stuff
player[0] = "Sheree";
player[1] = "Noble";
player[2] = "Clarence";
player[3] = "Walker";
player[4] = "Sheree";
playerColor[0] = color(255, 0, 0);
playerColor[1] = color(0, 255, 0);
playerColor[2] = color(0, 0, 255);
playerColor[3] = color(255, 255, 0);
playerColor[4] = color(255, 0, 0);
playerPhoto[0] = imgSheree;
playerPhoto[1] = imgNoble;
playerPhoto[2] = imgClarence;
playerPhoto[3] = imgWalker;
playerPhoto[4] = imgSheree;
//start screen
push();
imageMode(CENTER);
image(startImg, width / 2, height / 2, 640, 480);
textAlign(CENTER);
fill(255);
strokeWeight(3);
stroke(0);
textSize(32);
text("This is...", width * 0.2, height * 0.45);
textSize(50);
strokeWeight(3);
stroke(0);
text("Walker, Texas Gamer!!!", width / 2, height * 0.60);
textSize(24);
push();
fill(255,0,0);
text("Ladies", width * 0.62, height * 0.73);
pop();
text("roll first..", width * 0.675 + 50, height * 0.73);
textSize(18);
text("then hit spacebar ", width * 0.72, height * 0.76);
text("to run the Walker!!", width * 0.77, height * 0.79);
textSize(16);
textAlign(LEFT);
text("And if you haven't already done so, press 'a' to calibrate your camera.", width * 0.15, height * 0.83);
pop();
}
function draw() {
// if the 'a' key is pressed we enter calibration mode
if (showGrid == true) {
calibration();
} else if (showGrid == false) {
step();
display();
checkWinner();
showDiceCount();
}
}
function showDiceCount() {
if (isMoving) {
push();
textSize(18);
stroke(255);
strokeWeight(1);
fill(255);
text("You rolled:",
width * 0.12, height * 0.7);
textSize(64);
text(currentDiceCount, width * 0.15, height * 0.78);
textSize(18);
if (diceCount == 2) {
text("SNAKE EYES!!!! ... anything goes now...", width * 0.12, height * 0.82);
}
// text("...right????", width * 0.20, height * 0.81);
// text("(if not, press spacebar again and/or move the dice)",
// width * 0.12, height * 0.86);
pop();
}
}
function keyPressed() {
if (keyCode == 32) {
if (!isMoving) {
playerCount++;
if (playerCount > 3) {
playerCount = 0;
}
}
isMoving = true;
fill(0);
image(img, 0, height / 20, width, height);
borders();
walkX = width / 2;
walkY = height / 2;
// textFlag = !textFlag;
currentDiceCount = diceCount;
// console.log('space is pressed')
// console.log('Dice Count: ', diceCount)
}
if (keyCode === 65) {
showGrid = !showGrid;
}
}
function calibration () {
push();
imageMode(CORNER);
image(capture, 0, 0);
pop();
// lightRangeSlider.show();
push();
noStroke();
stroke(255, 0, 0);
noFill();
camCal.forEach(function(r) {
rect(r.x, r.y, r.width, r.height);
});
pop();
push();
noStroke();
fill(255, 0, 0);
textSize(16);
text("Place the dice in the center of the board.", cnv.width * 0.05, cnv.height * 0.50);
text("Make sure the board is clear and well lit with few shadows.", cnv.width * 0.05, cnv.height * 0.53);
text("You should see one red rectangle for each blob only.", cnv.width * 0.05, cnv.height * 0.56);
text("If not, you can change the rangeVal at the top of the code.", cnv.width * 0.05, cnv.height * 0.59);
text("Higher numbers pick up more blobs; lower numbers exclude any errant blobs.", cnv.width * 0.05, cnv.height * 0.62);
text("Once finished, save the sketch and re-enter calibration to see the results.", cnv.width * 0.05, cnv.height * 0.65);
pop();
}
//display the walker
function display() {
strokeWeight(1);
// stroke(255);
fill(150, 0, 255);
ellipse(walkX, walkY, walkR, walkR);
}
//move the walker by these values
function step() {
let ran = random(1 + (diceCount / 12 - 1/6) * weightVal);
// let ran = random(1) + (diceCount / 12) * weightVal;
//if isMoving is true then move the walker like so
if (isMoving == true) {
if (playerCount === 0 || playerCount === 5) {
if (ran < 0.25) {
walkY -= speed;
} else if (ran < 0.5) {
walkX += speed;
} else if (ran < 0.75) {
walkY += speed;
} else if (ran > 0.75) {
walkX -= speed;
}
} else if (playerCount == 1) {
if (ran < 0.25) {
walkX += speed;
} else if (ran < 0.5) {
walkY += speed;
} else if (ran < 0.75) {
walkX -= speed;
} else if (ran > 0.75) {
walkY -= speed;
}
} else if (playerCount == 2) {
if (ran < 0.25) {
walkY += speed;
} else if (ran < 0.5) {
walkX -= speed;
} else if (ran < 0.75) {
walkY -= speed;
} else if (ran > 0.75) {
walkX += speed;
}
} else if (playerCount == 3) {
if (ran < 0.25) {
walkX -= speed;
} else if (ran < 0.5) {
walkY -= speed;
} else if (ran < 0.75) {
walkX += speed;
} else if (ran > 0.75) {
walkY += speed;
}
} else if (isMoving == false) {
// otherwise, stop the walker where it is;
stepX = 1;
stepY = 1;
walkX += 0;
walkY += 0;
}
}
}
function checkWinner() {
let textX = width * 0.2;
let offset1 = 125;
let offset2 = 330;
//Sheree player 1 red - left
if (walkX <= width / 10 && walkX > 0) {
turnOver = true;
isMoving = false;
textFlag = false;
background(255, 0, 0);
push();
imageMode(CENTER);
image(imgSheree, width * 0.5, height * 0.2, imgSheree.width * 0.3, imgSheree.height * 0.3);
textAlign(CENTER);
textSize(64);
fill(150, 0, 255);
text("Sheree Wins!!!", width * 0.5, height * 0.4);
pop();
showNextPlayer();
}
//Noble player 2 green - top
if (walkY <= height / 10 && walkY > 0) {
turnOver = true;
isMoving = false;
textFlag = false;
background(0, 255, 0);
push();
imageMode(CENTER);
image(imgNoble, width * 0.5, height * 0.2, imgNoble.width * 0.3, imgNoble.height * 0.3);
textAlign(CENTER);
textSize(64);
fill(150, 0, 255);
text("Noble Wins!!!", width * 0.5, height * 0.4);
pop();
showNextPlayer();
}
//Clarence player 3 blue - right
if (walkX >= (width - (width / 10)) && walkX > 0) {
turnOver = true;
isMoving = false;
textFlag = false;
background(0, 0, 255);
push();
imageMode(CENTER);
image(imgClarence, width * 0.5, height * 0.2, imgClarence.width * 0.3, imgClarence.height * 0.3);
textAlign(CENTER);
textSize(64);
fill(150, 0, 255);
text("Clarence Wins!!!", width * 0.5, height * 0.4);
pop();
showNextPlayer();
}
//Walker player 4 yellow - bottom
if (walkY >= (height - (height / 10)) && walkY > 0) {
turnOver = true;
isMoving = false;
textFlag = false;
background(255, 255, 0);
push();
imageMode(CENTER);
image(imgWalker, width * 0.5, height * 0.2, imgWalker.width * 0.3, imgWalker.height * 0.3);
textAlign(CENTER);
textSize(64);
fill(150, 0, 255);
text("WALKER Wins!!!", width * 0.5, height * 0.4);
pop();
showNextPlayer();
}
function showNextPlayer() {
push();
imageMode(CENTER);
tint(playerColor[playerCount + 1]);
image(playerPhoto[playerCount + 1], textX + offset1, height * 0.63,
playerPhoto[playerCount + 1].width * 0.20, playerPhoto[playerCount + 1].height * 0.20);
tint(255,150);
image(playerPhoto[playerCount + 1], textX + offset1, height * 0.63,
playerPhoto[playerCount + 1].width * 0.20, playerPhoto[playerCount + 1].height * 0.20);
textAlign(CENTER);
stroke(0);
strokeWeight(2);
textSize(32);
text("And it's ", textX, 3 * height / 4);
push();
fill(playerColor[playerCount + 1]);
text(player[playerCount + 1] + "'s", textX + offset1, 3 * height / 4);
pop();
text(" turn to roll now...", textX + offset2, 3 * height / 4);
text("Then press SPACEBAR to run the Walker!", width / 2, height * 0.80);
pop();
}
}
//draw all the borders
function borders() {
// Sheree , left
if (playerCount == 0) {
push();
stroke(255);
strokeWeight(4);
fill(255, 0, 0);
quad(0, 0, width / 10, height / 10, width / 10, height - (height / 10), 0, height);
pop();
} else {
fill(255, 0, 0);
quad(0, 0, width / 10, height / 10, width / 10, height - (height / 10), 0, height);
}
// Noble , top
if (playerCount == 1) {
push();
stroke(255);
strokeWeight(4);
fill(0, 255, 0);
quad(0, 0, width / 10, height / 10, width - (width / 10), height / 10, width, 0);
pop();
} else {
fill(0, 255, 0);
quad(0, 0, width / 10, height / 10, width - (width / 10), height / 10, width, 0);
}
// Clarence , right
if (playerCount == 2) {
push();
stroke(255);
strokeWeight(4);
fill(0, 0, 255);
quad(width - (width / 10), (height / 10), width, 0, width, height, width - (width / 10), height - (height / 10));
pop();
} else {
fill(0, 0, 255);
quad(width - (width / 10), (height / 10), width, 0, width, height, width - (width / 10), height - (height / 10));
}
// Walker , bottom
if (playerCount == 3) {
push();
stroke(255);
strokeWeight(4);
fill(255, 255, 0);
quad(width - (width / 10), height - (height / 10), width, height, 0, height, width / 10, height - (height / 10));
pop();
} else {
fill(255, 255, 0);
quad(width - (width / 10), height - (height / 10), width, height, 0, height, width / 10, height - (height / 10));
}
}
/*
want to build a timer with visible counter to auto restart after x seconds
scoreboard
percentages of likelihood walker
alignment and camera procedures
*/
// still needs tweaking, goes x--, y--, y++, but not x++ evenly...
// cheat = function () {
// let mouselocX = x >= mouseX;
// let mouselocY = y >= mouseY;
// let mouselocXX = x < mouseX;
// let mouselocYY = y < mouseX;
// let fifty;
// fifty = (random(1));
// let chance = 0.8;
// if (mouselocX & fifty <= chance ) {
// x--;
// } else if (mouselocY & fifty <= chance) {
// y--;
// } else if (mouselocYY & fifty <= chance) {
// y++;
// } else if (mouselocXX & fifty <= chance) {
// x++;
// }
// }
// function for cheating the walker equally
// this.step = function() {
// let choice = int(random(4));
// if (choice == 0) {
// this.x+=this.speed;
// } else if (choice == 1) {
// this.y+=this.speed;
// } else if (choice == 2) {
// this.y-=this.speed;
// } else {
// this.x-=this.speed;
// }
// }
/*
let r = random(1);
//to player 1
if (playerCount = 1) {
r * diceCount/12
if (r < 0.25) {
x--;
// to player 2
} else if (r < 0.5) {
y--;
//to player 3
} else if (r < 0.75) {
x++;
//to player 4
}else if (r < 1) {
y++;
}
*/