function CARD(img, sp_w, sp_h, img_w, img_h) { this.img = img; //Image File path this.sw = sp_w; //Sprite の幅(51) this.sh = sp_h; //Sprite の高さ(75) this.wn = img_w / this.sw; //Sprite の横分割数(10) this.hn = img_h / this.sh; //Sprite の縦分割数(5) //「上, 右, 下, 左」の順に指定 this.Clip = function(num, x, y) { var xp = (num%this.wn)*this.sw; var yp = Math.floor(num/this.wn)*this.sh; var s1 = 'clip:rect(' + yp + 'px,' + (xp+this.sw) + 'px,' + (yp+this.sh) + 'px,' + xp + 'px);'; return s1; } // 描画座標の計算 this.Position = function(num, x, y) { var xp = (num%this.wn)*this.sw; var yp = Math.floor(num/this.wn)*this.sh; var s2 = 'position:absolute;left:' + (x-xp) + 'px;top:' + (y-yp) + 'px;"'; return s2; } // Sprite View this.View = function(num, x, y) { var s = '<img src="' + this.img + '" style="' + this.Clip(num, x, y) + this.Position(num, x, y) + '>'; document.write(s); } // Click Function this.Func = function(num, x, y, id, func) { var s3 = ' id="' + id + '" onclick=' + func + '("' + id + '")'; var s = '<img src="' + this.img + '"' + s3 + ' style="' + this.Clip(num, x, y) + this.Position(num, x, y) + '>'; document.write(s); } // 左上座標を求める(描画基点(xp,yp) 描画間隔(xs,xs) 1行のカード数(xn)) this.PosXY = function(num, xp, yp, xs, ys, xn) { var s = 'position:absolute;left:' + (((num%xn)*xs)+xp) + 'px;top:' + ((Math.floor(num/xn)*ys)+yp) + 'px;'; return s; } //カードをシャッフルする this.Suflle = function(n) { var t = Array(); var i, j; for(i=0; i<n; i++) t[i]= -1; for(i=0; i<n; i++) { j= Math.floor(Math.random()*n); for(; t[j]!=-1; j=(j+1)%n); t[j]= i; } return t; } } |
this.img = img; //Image File path this.sw = sp_w; //Sprite の幅(51) this.sh = sp_h; //Sprite の高さ(75) this.wn = img_w / this.sw; //Sprite の横分割数(10) this.hn = img_h / this.sh; //Sprite の縦分割数(5) |
<script src="card.js"> </script> </head> |
<script type="text/javascript"> var card= new CARD("img/hanafuda.gif", 51, 75, 510, 375); var i; var t = Array(); t = card.Suflle(48); for(i=0; i<48; i++) { card.View(t[i], i%12*53+10, Math.floor(i/12)*77+60); } </script> </body> |
<script type="text/javascript"> var card= new CARD("img/hanafuda.gif", 51, 75, 510, 375); var i; var t = Array(); t = card.Suflle(48); for(i=0; i<7; i++) { card.View(t[i], i*25+100, 120); } </script> </body> |
<script type="text/javascript"> document.onmousedown = function(e) { if (!e) e= window.event; var xp= e.clientX; var yp= e.clientY; var num = Math.floor((yp-60)/77)*12+Math.floor((xp-10)/53); window.alert("XP:" + xp + " YP:" + yp + " NUM:" + num); } var card= new CARD("img/hanafuda.gif", 51, 75, 510, 375); var i; var t = Array(); t = card.Suflle(48); for(i=0; i<48; i++) { card.View(t[i], i%12*53+10, Math.floor(i/12)*77+60); } </script> |
var num = Math.floor((yp-60)/77)*12+Math.floor((xp-10)/53); |
<script type="text/javascript"> document.onmousedown = function(e) { if (!e) e= window.event; var xp= e.clientX; var yp= e.clientY; var num = Math.floor((yp-60)/77)*12+Math.floor((xp-10)/53); card.View(48, num%12*53+10, Math.floor(num/12)*77+60); } var card= new CARD("img/hanafuda.gif", 51, 75, 510, 375); var i; var t = Array(); t = card.Suflle(48); for(i=0; i<48; i++) { card.View(t[i], i%12*53+10, Math.floor(i/12)*77+60); } </script> |
var num = Math.floor((yp-60)/77)*12+Math.floor((xp-10)/53); card.View(48, num%12*53+10, Math.floor(num/12)*77+60); |
<script type="text/javascript"> function ImgClick(num) { document.getElementById(num).src= "img/hana_ura.gif"; var s2 = card.PosXY(num, 10, 60, 53, 77, 12); document.getElementById(num).style= s2; } card= new CARD("img/hanafuda.gif", 51, 75, 510, 375); var t = Array(); t = card.Suflle(48); for(var num=0; num<48; num++) { card.Func(t[num], num%12*53+10, Math.floor(num/12)*77+60, num, "ImgClick"); } </script> |
// Click Function this.Func = function(num, x, y, id, func) { var s3 = ' id="' + id + '" onclick=' + func + '("' + id + '")'; var s = '<img src="' + this.img + '"' + s3 + ' style="' + this.Clip(num, x, y) + this.Position(num, x, y) + '>'; document.write(s); } |
for(var num=0; num<48; num++) { card.Func(t[num], num%12*53+10, Math.floor(num/12)*77+60, num, "ImgClick"); } |
function ImgClick(num) { document.getElementById(num).src= "img/hana_ura.gif"; var s2 = card.PosXY(num, 10, 60, 53, 77, 12) document.getElementById(num).style= s2; } |
this.PosXY = function(num, xp, yp, xs, ys, xn) { var s = 'position:absolute;left:' + (((num%xn)*xs)+xp) + 'px;top:' + ((Math.floor(num/xn)*ys)+yp) + 'px;"'; return s; } |
<script type="text/javascript"> function ImgClick(num) { document.getElementById(num).src= "img/hanafuda.gif"; var cad= t[num]; if (num==n1 || th[cad]>=47) return; if (c1==-1 || c2==-1) { if (c1==-1) { c1 = cad; n1 = num; } else { c2 = cad; n2 = num; } var s1 = card.Clip(cad, cad%12*53+10, Math.floor(cad/12)*77+60); var s2 = card.Position(cad, num%12*53+10, Math.floor(num/12)*77+60); var s = s1 + ' ' + s2; document.getElementById(num).style= s; return; } // Pair 判定 if (th[c1]!=th[c2]) { s1 = card.Clip(48, n1%12*53+10, Math.floor(n1/12)*77+60); s2 = card.Position(48, n1%12*53+10, Math.floor(n1/12)*77+60); s = s1 + ' ' + s2; document.getElementById(n1).style= s; s1 = card.Clip(48, n2%12*53+10, Math.floor(n2/12)*77+60); s2 = card.Position(48, n2%12*53+10, Math.floor(n2/12)*77+60); s = s1 + ' ' + s2; document.getElementById(n2).style= s; c1 = -1; c2 = -1; n1 = -1; n2 = -1; return; } // Pair の札を削除 s1 = card.Clip(49, n1%12*53+10, Math.floor(n1/12)*77+60); s2 = card.Position(49, n1%12*53+10, Math.floor(n1/12)*77+60); s = s1 + ' ' + s2; document.getElementById(n1).style= s; s1 = card.Clip(49, n2%12*53+10, Math.floor(n2/12)*77+60); s2 = card.Position(49, n2%12*53+10, Math.floor(n2/12)*77+60); s = s1 + ' ' + s2; document.getElementById(n2).style= s; th[c1] = 49; th[c2] = 49; c1 = -1; c2 = -1; n1 = -1; n2 = -1; } th = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 11, 11, 12, 12, 12, 12, 0, 0 ]; c1 = -1; c2 = -1; n1 = -1; n2 = -1; card= new CARD("img/hanafuda.gif", 51, 75, 510, 375); var num; t = Array(); t = card.Suflle(48); for(num=0; num<48; num++) { card.Func(48, num%12*53+10, Math.floor(num/12)*77+60, num, "ImgClick"); } </script> |
th = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 11, 11, 12, 12, 12, 12, 0, 0 ]; |
th[c1] = 49; th[c2] = 49; |
if (th[c1]!=th[c2]) { s1 = card.Clip(48, n1%12*53+10, Math.floor(n1/12)*77+60); s2 = card.Position(48, n1%12*53+10, Math.floor(n1/12)*77+60); s = s1 + ' ' + s2; document.getElementById(n1).style= s; s1 = card.Clip(48, n2%12*53+10, Math.floor(n2/12)*77+60); s2 = card.Position(48, n2%12*53+10, Math.floor(n2/12)*77+60); s = s1 + ' ' + s2; document.getElementById(n2).style= s; |
s1 = card.Clip(49, n1%12*53+10, Math.floor(n1/12)*77+60); s2 = card.Position(49, n1%12*53+10, Math.floor(n1/12)*77+60); s = s1 + ' ' + s2; document.getElementById(n1).style= s; s1 = card.Clip(49, n2%12*53+10, Math.floor(n2/12)*77+60); s2 = card.Position(49, n2%12*53+10, Math.floor(n2/12)*77+60); s = s1 + ' ' + s2; document.getElementById(n2).style= s; th[c1] = 49; th[c2] = 49; |