xxxxxxxxxx
86
let diceImages = [];
let diceImage;
let currentDie;
let currentRoll = 0;
let numRolls = 0;
let rollTotal = 0;
let start = true;
let winLose = false;
function preload() {
// load all 6 images w/a for loop
for (i = 0; i < 6; i++) {
//diceImages[i] = loadImage("die-" + i + ".png");
diceImage = loadImage("die-" + i + ".png");
diceImages.push(diceImage);
}
}
function setup() {
createCanvas(600, 200);
background(220);
// draw text
//textSize(32);
//text("Click to roll.", 5, 160);
displayUI();
}
function mousePressed() {
if(start){
background(220);
displayUI;
start = false;
}
// get random number, assign it to image
if (numRolls >= 6) {
numRolls = 0;
background(220);
displayWinLose();
} else {
currentDie = floor(random(0, 6));
print(currentDie);
currentRoll = currentDie + 1;
print(currentRoll);
rollTotal = rollTotal + currentRoll;
print(rollTotal);
image(diceImages[currentDie], numRolls * 100, 0);
numRolls++;
displayUI();
}
}
function displayUI() {
// draw text
textSize(32);
text("Click to roll.", 5, 160);
}
function displayWinLose() {
background(220);
text("Your total is: ", 5, 100);
text(rollTotal, 185, 100);
if(rollTotal == 21){
winLose = true;
} else {
winLose = false;
}
if (winLose){
text("YOU WIN! ", 400, 100);
} else {
text("YOU LOSE! ", 400, 100);
}
text("Press any key to roll again", 5, 160);
currentRoll = 0;
numRolls = 0;
rollTotal = 0;
start = true;
winLose = false;
}
function keyPressed() {
currentRoll = 0;
numRolls = 0;
rollTotal = 0;
setup();
}