xxxxxxxxxx
240
var cols = 18;
var rows = 14;
var scl = 35;
var totalBombs = Math.floor(rows * cols * 0.16);
var particles = [];
var started = false;
var sounds = true;
let c
function preload() {
let barH = scl * 1.72;
createCanvas(cols * scl, rows * scl + barH);
bar = new Bar(width, barH);
breakSound = [];
for (let i = 0; i <= 5; i++) breakSound[i] = loadSound('sounds/break' + i + '.wav');
flagPlace = loadSound('sounds/flagplace.wav');
flagTake = loadSound('sounds/flagtake.wav');
defColors();
flagImg = loadImage('images/game/flag.png');
scores = loadJSON('scores.json');
numFont = loadFont('fonts/Roboto-Bold.ttf');
barFont = loadFont('fonts/Roboto-Regular.ttf');
}
function setup() {
canvas=createCanvas(cols * scl, rows * scl + bar.h).mousePressed(() => canvasMousePressed());
let btns = document.getElementById('bar-buttons');
let btn = [];
let __btn = document.getElementsByClassName('bar-button');
for (let i = 0; i < __btn.length; i++) btn[i] = __btn[i];
let maxh = btn.reduce((keep, b) => max(keep, b.style.height.slice(0, -2) - 0), 0);
btns.style.left = width - btn.length * scl + 'px';
btns.style.top = ((bar.h - maxh) / 2) + 'px';
btn.forEach((b, i) => {
let h = val(b.style.height);
b.style.left = i * scl + 'px';
b.style.top = ((maxh - h) / 2) + 'px';
});
btns.style.display = 'block';
select('#sound').mousePressed(() => {
sounds = !sounds;
document.getElementById('sound').src = "images/bar/sound" + (sounds ? 'On' : 'Off') + ".png";
});
select('#share').mousePressed(() => {
let s=document.getElementById('share-window');
s.style.display='block';
});
select('#share-link-button').mouseOver(() => {
document.getElementById('share-link-button').src='images/bar/share/link-hover.png';
});
select('#share-link-button').mouseOut(() => {
document.getElementById('share-link-button').src='images/bar/share/link.png';
});
grid = [];
for (let x = 0; x < cols; x++) {
grid[x] = [];
for (let y = 0; y < rows; y++) {
grid[x][y] = new Field(x, y);
}
}
document.addEventListener("contextmenu", e => {
if(!c) e.preventDefault();
});
document.onselectstart = () => false;
}
function draw() {
bar.update();
bar.show();
for (let col of grid) {
for (let f of col) {
f.show();
}
}
for (let p of particles) {
p.show();
p.update();
}
particles = particles.filter(p => p.pos.y < height + 10 && p.scl > 0);
}
function canvasMousePressed() {
c=true
for (let col of grid) {
for (let f of col) {
if (f.mouseOver()) {
if (mouseButton == LEFT && f.grass && !f.flag) {
if (!started) {
begin(f.x, f.y);
}
if (f.bomb) {
failed = true;
}
f.removeGrass(!f.bomb);
if (sounds) f.playBreakSound();
} else if (mouseButton == RIGHT) {
if (f.flag) {
f.removeFlag();
} else if (f.grass) {
f.placeFlag();
}
}
}
}
}
}
function mouseReleased() {
c=false;
}
function begin(x, y) {
started = true;
bar.time.start = millis();
for (let col of grid) {
for (let f of col) {
f.number = 0;
f.bomb = false;
f.grass = true;
if (f.flag) f.removeFlag(false);
}
}
totalBombs = floor(rows * cols * 0.16);
let bombsPlaced = 0;
bar.flags.left = totalBombs;
while (bombsPlaced < totalBombs) {
let f = random(random(grid));
if (!f.bomb) {
if (!near(x, y, f.x, f.y) && (x != f.x - 1 && y != f.y)) {
f.placeBomb();
bombsPlaced++;
}
}
if (bombsPlaced >= totalBombs) {
for (let col of grid) {
for (let f of col) {
if (f.bomb) f.number = 0;
}
}
return;
}
}
print(totalBombs);
}
function near(x1, y1, x2, y2) {
return ((x1 == x2 - 1 && y1 == y2 - 1) ||
(x1 == x2 && y1 == y2 - 1) ||
(x1 == x2 + 1 && y1 == y2 - 1) ||
(x1 == x2 - 1 && y1 == y2) ||
(x1 == x2 && y1 == y2) ||
(x1 == x2 - 1 && y1 == y2 + 1) ||
(x1 == x2 && y1 == y2 + 1) ||
(x1 == x2 + 1 && y1 == y2 + 1));
}
function leave() {
window.close();
alert("The p5.js editor doesn't allow closing itself.");
}
function val(s) {
for (let i = 0; i < s.length; i++) {
if (!isNaN(s.substring(0, s.length - i))) return s.substring(0, s.length - i) - 0;
}
}
// function createFlagImg() {
// let h = scl * 0.73;
// let baseW = h * 0.318;
// let baseH = h * 0.18;
// let staffW = h * 0.16;
// let staffX = baseW / 2 - staffW / 2;
// let flyH = h * 0.7;
// let img = createGraphics(h, h);
// img.height = img.width = h;
// img.noStroke();
// img.fill(colors.lightFlag);
// img.triangle(staffX, 0, h, flyH / 2 - 0.5, staffX + staffW / 3, flyH);
// img.fill(colors.darkFlag);
// img.quad(staffX, 0, staffX + staffW, h * 0.054, staffX + staffW, h, staffX, h); //staff
// img.rect(0, h - baseH, baseW, baseH, scl / 15, scl / 15, 0, 0);
// return img;
// }
function createFlagImg() {
let w = floor(scl * 0.74);
let h = floor(scl * 0.74);
flagImg.resize(w, h);
let img = createGraphics(w, h);
for (let x = 0; x < w; x++) {
for (let y = 0; y < h; y++) {
img.stroke(flagImg.get(x, y));
img.point(x, y);
}
}
return img;
}
function createFieldImg(options = {}) {
let img = createGraphics(scl, scl);
img.height = img.width = scl;
img.noStroke();
img.fill(options.dark ? colors.darkField : colors.lightField);
img.rect(0, 0, scl, scl);
return img;
}