Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head>
- <title>blocksweeper</title>
- </head>
- <body>
- <canvas id='gameCanvas' width='800' height='800'></canvas>
- <script type="text/javascript">
- var firstClick;
- var blockWidth = 32;
- var blocks;
- var showEndScreen = false;
- window.onload = function() {
- canvas = document.getElementById('gameCanvas');
- ctx = canvas.getContext('2d');
- canvas.addEventListener('mousedown', mouseDown, false);
- createGrid();
- var fps = 30;
- setInterval(draw, 1000/fps);
- }
- function draw() {
- drawBackgroundGrid();
- for(var i = 0; i < blocks.length; i++)
- for(var j = 0; j < blocks[0].length; j++)
- blocks[i][j].show();
- }
- function mouseDown (evt) {
- var mousePos = calculateMousePos(evt);
- for(var i = 0; i < blocks.length; i++)
- for(var j = 0; j < blocks.length; j++)
- if(mousePos.x >= blocks[i][j].x+1 && mousePos.x <= blocks[i][j].x + blocks[i][j].width-1)
- if(mousePos.y >= blocks[i][j].y+1 && mousePos.y <= blocks[i][j].y + blocks[i][j].height-1)
- if(evt.which == 1 && !blocks[i][j].isFlagged) {
- blocks[i][j].hidden = false;
- if(firstClick)
- createMines(i, j);
- console.log(blocks[i][j]);
- showAdjacent(blocks[i][j]);
- } else if(evt.which == 3) {
- if(blocks[i][j].hidden)
- blocks[i][j].isFlagged = !blocks[i][j].isFlagged;
- }
- }
- function calculateMousePos (evt) {
- var canvasBounds = canvas.getBoundingClientRect();
- var root = document.documentElement;
- var mouseX = evt.clientX - canvasBounds.left - root.scrollLeft;
- var mouseY = evt.clientY - canvasBounds.top - root.scrollTop;
- return {
- x: mouseX,
- y: mouseY
- }
- }
- function showAdjacent(block) {
- if(block.minesNextTo != 0)
- return;
- console.log(block.row+','+block.col);
- block.uncovered = true;
- block.hidden = false;
- console.log(block.adjacentBlocks.length);
- for(i = 0; i < block.adjacentBlocks.length; i++)
- block.adjacentBlocks[i].hidden = false;
- for(i = 0; i < block.adjacentBlocks.length; i++) {
- if(!block.adjacentBlocks[i].uncovered)
- showAdjacent(block.adjacentBlocks[i]);
- }
- }
- function Block(x, y, row, col) {
- this.x = x;
- this.y = y;
- this.width = blockWidth;
- this.height = blockWidth;
- this.isMine = false;
- this.hidden = true;
- this.isFlagged = false;
- this.minesNextTo = 0;
- this.adjacentBlocks = [];
- this.uncovered = false;
- this.row = row;
- this.col = col;
- this.show = function() {
- if(!this.hidden) {
- if(this.isMine) {
- ctx.fillStyle = 'rgb(75, 75, 75)';
- ctx.beginPath();
- ctx.arc(this.x+this.width/2+.5, this.y+this.height/2, this.width/2-3, 0, 2*Math.PI);
- ctx.closePath();
- ctx.fill();
- } else {
- if(this.minesNextTo > 0) {
- ctx.fillStyle = 'blue';
- ctx.textAlign = 'center';
- ctx.font = 'bold 25px Courier';
- ctx.fillText(this.minesNextTo, this.x+this.width/2, this.y+this.width/1.35);
- }
- }
- } else {
- ctx.fillStyle = 'rgb(150, 150, 150)';
- ctx.fillRect(this.x, this.y, this.width, this.height);
- ctx.fillStyle = 'rgb(235, 235, 235)';
- ctx.fillRect(this.x+3, this.y+3, this.width-6, this.height-6);
- if(this.isFlagged) {
- ctx.fillStyle = 'black';
- ctx.fillRect(this.x+10, this.y+this.height-16, 3, 10);
- ctx.fillStyle = 'red';
- ctx.beginPath();
- ctx.moveTo(this.x+10, this.y+6)
- ctx.lineTo(this.x+10, this.y+this.height-14)
- ctx.lineTo(this.x+this.width-8, this.y+this.height/2-5)
- ctx.closePath();
- ctx.fill();
- }
- }
- }
- }
- function createGrid() {
- firstClick = true;
- blocks = create2DArray(canvas.width/blockWidth, canvas.height/blockWidth)
- for(var i = 0; i < blocks.length; i++)
- for(var j = 0; j < blocks[0].length; j++){
- blocks[i][j] = new Block(i*blockWidth, j*blockWidth, i, j);
- console.log('('+i+','+j+')('+(i*blockWidth)+','+(j*blockWidth)+')')
- }
- }
- function create2DArray(rows, cols) {
- var ray = new Array(rows);
- for(var i = 0; i < ray.length; i++)
- ray[i] = new Array(cols)
- return ray;
- }
- function createMines(firstClickX, firstClickY) {
- firstClick = false;
- var numberOfMines = 15;//Math.floor(blocks.length*blocks[0].length*.15);
- console.log(numberOfMines);
- var minesCreated = 0;
- while(minesCreated <= numberOfMines) {
- var row = Math.floor(Math.random()*(blocks.length-1));
- var col = Math.floor(Math.random()*(blocks[0].length-1));
- if(!blocks[row][col].isMine && row != firstClickX && col != firstClickY) {
- blocks[row][col].isMine = true;
- minesCreated++;
- }
- }
- for(var i = 0; i < blocks.length; i++)
- for(var j = 0; j < blocks[0].length; j++) {
- var totalNextTo = 0;
- if(i+1 in blocks && j in blocks[i]) {
- blocks[i][j].adjacentBlocks.unshift(blocks[i+1][j]);
- if(blocks[i+1][j].isMine)
- totalNextTo++;
- }
- if(i-1 in blocks && j in blocks[i]) {
- blocks[i][j].adjacentBlocks.unshift(blocks[i-1][j]);
- if(blocks[i-1][j].isMine)
- totalNextTo++;
- }
- if(i in blocks && j+1 in blocks[i]) {
- blocks[i][j].adjacentBlocks.unshift(blocks[i][j+1]);
- if(blocks[i][j+1].isMine)
- totalNextTo++;
- }
- if(i in blocks && j-1 in blocks[i]) {
- blocks[i][j].adjacentBlocks.unshift(blocks[i][j-1]);
- if(blocks[i][j-1].isMine)
- totalNextTo++;
- }
- if(i+1 in blocks && j-1 in blocks[i]) {
- blocks[i][j].adjacentBlocks.unshift(blocks[i+1][j-1]);
- if(blocks[i+1][j-1].isMine)
- totalNextTo++;
- }
- if(i-1 in blocks && j+1 in blocks[i]) {
- blocks[i][j].adjacentBlocks.unshift(blocks[i-1][j+1]);
- if(blocks[i-1][j+1].isMine)
- totalNextTo++;
- }
- if(i-1 in blocks && j-1 in blocks[i]) {
- blocks[i][j].adjacentBlocks.unshift(blocks[i-1][j-1]);
- if(blocks[i-1][j-1].isMine)
- totalNextTo++;
- }
- if(i+1 in blocks && j+1 in blocks[i]) {
- blocks[i][j].adjacentBlocks.unshift(blocks[i+1][j+1]);
- if(blocks[i+1][j+1].isMine)
- totalNextTo++;
- }
- blocks[i][j].minesNextTo = totalNextTo;
- }
- }
- document.oncontextmenu = function() {
- return false;
- }
- function drawBackgroundGrid() {
- ctx.fillStyle = 'rgb(200, 200, 200)';
- ctx.fillRect(0, 0, canvas.width, canvas.height);
- ctx.fillStyle = 'black'
- for(var i = 0; i < canvas.width; i+=blockWidth) {
- ctx.fillRect(i-0.5, 0, 1, canvas.height);
- ctx.fillRect(0, i-0.5, canvas.width, 1);
- }
- }
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement