<!doctype html> <html> <head> <meta charset="utf-8"> <title> </title> <link rel="stylesheet" type="text/css" href="css/style.css"> </head> <body> <div class="chess"> <canvas id="chess">Updet!</canvas> </div> <div class="info"> <p> : 1<p> <p> : 0<p> </div> </body> <script src="js/jQuery.js"></script> <script src="js/script.js"></script> </html>
* { margin: 0px; padding: 0px; } body, html { height: 100%; } .chess { margin: auto; position: relative; width: 600px; height: 600px; } .chess canvas { margin: 28px 0 0 58px; background: url(../images/chess-block.png) no-repeat; } .kon { position: absolute; width: 52px; height: 98px; background: url(../images/kon.png) no-repeat; }
var ctx; var arr=[];var v1=[]; var error=0; function rect(color, x, y, width, height,opecity) { this.color = color; // this.x = x; // this.y = y; // this.width = width; // this.height = height; // this.draw = function() // { ctx.beginPath(); ctx.globalAlpha = opecity; ctx.fillStyle = this.color; ctx.fillRect(this.x, this.y, this.width, this.height); } this.clear = function(){ ctx.clearRect(this.x, this.y, this.width, this.height); } } function init(p,h){ (p===undefined) ? x=60 : x=p; (h===undefined) ? y=480 : y=h; // 1 - . if (x < 60 ) x = 60;if (x > 480 ) x = 480; if (y < 60 ) y = 60;if (y > 480 ) y = 480; // arr.push([x,y]); //, var chess = document.getElementById("chess"); var width = chess.width = 600; var height = chess.height = 600; ctx = chess.getContext('2d'); ctx.clearRect(0,0,width,height); // ( ) 8 v1 = [ [x+60*2, y+60], [x+60*2, y-60], [x+60, y+60*2], [x-60, y+60*2], [x+60, y-60*2], [x-60, y-60*2], [x-60*2, y-60], [x-60*2, y+60] ]; // for(var v=0;v<v1.length;v++){ if(v1[v][0]>59 && v1[v][0]<481 && v1[v][1]>59 && v1[v][1]<481) { var f = new rect('#ffe800',v1[v][0],v1[v][1],60,60,0.4); f.draw(); } } // , for (var i=0;i<arr.length;i++) { var f = new rect('#f00',arr[i][0],arr[i][1],60,60,1); f.draw(); } // imkago(x,y); } init();
function imkago(x,y) { var kon = new Image(); kon.src = 'images/kon.png'; kon.onload = function() { ctx.beginPath(); ctx.globalAlpha = 1; ctx.drawImage(kon, x+3, y-45); } }
$('#chess').click(function(event){ var x = event.pageX - this.offsetLeft-$('.chess').offset().left; var y = event.pageY - this.offsetTop; x = (Math.floor(x/60))*60; y = (Math.floor(y/60))*60; //, . , for (var j=0;j<arr.length;j++) { if (arr[j][0]==x && arr[j][1]==y){ error++; $(".info").html("<p> : "+arr.length+"<p><p> : "+error+"<p>"); return false; } } for(var v=0;v<v1.length;v++){ if(v1[v][0]==x && v1[v][1]==y) { init(x,y); } } $(".info").html("<p> : "+arr.length+"<p><p> : "+error+"<p>"); })
var ctx; var arr=[];var v1=[]; var error=0; function rect(color, x, y, width, height,opecity) { this.color = color; // this.x = x; // this.y = y; // this.width = width; // this.height = height; // this.draw = function() // { ctx.beginPath(); ctx.globalAlpha = opecity; ctx.fillStyle = this.color; ctx.fillRect(this.x, this.y, this.width, this.height); } this.clear = function(){ ctx.clearRect(this.x, this.y, this.width, this.height); } } function init(p,h){ (p===undefined) ? x=60 : x=p; (h===undefined) ? y=480 : y=h; // 1 - . if (x < 60 ) x = 60;if (x > 480 ) x = 480; if (y < 60 ) y = 60;if (y > 480 ) y = 480; // arr.push([x,y]); //, var chess = document.getElementById("chess"); var width = chess.width = 600; var height = chess.height = 600; ctx = chess.getContext('2d'); ctx.clearRect(0,0,width,height); // ( ) 8 v1 = [ [x+60*2, y+60], [x+60*2, y-60], [x+60, y+60*2], [x-60, y+60*2], [x+60, y-60*2], [x-60, y-60*2], [x-60*2, y-60], [x-60*2, y+60] ]; // for(var v=0;v<v1.length;v++){ if(v1[v][0]>59 && v1[v][0]<481 && v1[v][1]>59 && v1[v][1]<481) { var f = new rect('#ffe800',v1[v][0],v1[v][1],60,60,0.4); f.draw(); } } // , for (var i=0;i<arr.length;i++) { var f = new rect('#f00',arr[i][0],arr[i][1],60,60,1); f.draw(); } // imkago(x,y); } init(); function imkago(x,y) { var kon = new Image(); kon.src = 'images/kon.png'; kon.onload = function() { ctx.beginPath(); ctx.globalAlpha = 1; ctx.drawImage(kon, x+3, y-45); } } $('#chess').click(function(event){ var x = event.pageX - this.offsetLeft-$('.chess').offset().left; var y = event.pageY - this.offsetTop; x = (Math.floor(x/60))*60; y = (Math.floor(y/60))*60; //, . , for (var j=0;j<arr.length;j++) { if (arr[j][0]==x && arr[j][1]==y){ error++; $(".info").html("<p> : "+arr.length+"<p><p> : "+error+"<p>"); return false; } } for(var v=0;v<v1.length;v++){ if(v1[v][0]==x && v1[v][1]==y) { init(x,y); } } $(".info").html("<p> : "+arr.length+"<p><p> : "+error+"<p>"); })
Source: https://habr.com/ru/post/205894/
All Articles