アプリ版:「スタンプのみでお礼する」機能のリリースについて

javascriptでスロットゲームを作ろうとしています。
途中まではなんとかできたんですけど、出来ないところがあるんで質問したいと思います。

<html>
<head>
<title>スロットゲーム</title>
</head>
<body>
<h3>スロットゲーム</h3>
<hr>
<form name="slot">
<table border="2">
<tr>

</tr>
<tr bgcolor="#CCCCCC">
<td><div id="dram0">☆</div></td>
<td><div id="dram1">☆</div></td>
<td><div id="dram2">☆</div></td>
</tr>
<tr>
<td><input type="button" value="ストップ" onClick="dramstop(0)"></td>
<td><input type="button" value="ストップ" onClick="dramstop(1)"></td>
<td><input type="button" value="ストップ" onClick="dramstop(2)"></td>
</tr>
<tr>
<td colspan="3"><input type="button" value="スタート" onClick="dramreset()"></td>
</tr>
</table>
</form>

<hr>
<div id="rireki"></div>

<script language="JavaScript">
img = new Array("<img src='0.png'>" ,"<img src='1.png'>","<img src='2.png'>"
,"<img src='3.png'>" ,"<img src='4.png'>","<img src='5.png'>","<img src='6.png'>"
,"<img src='7.png'>","<img src='8.png'>","<img src='9.png'>");
kiroku = new Array();
rrk = "";
rrk_num = 0;

dramreset();

dramstart();

function dramreset() {
var s = "";
for (i=0; i<3; i++) {
s += img[kiroku[i]];

document.slot.elements[i].disabled = false;
}

rrk_num++;

}

function dramstart() {
for (i=0; i<3; i++) {
if (!document.slot.elements[i].disabled) {
r = Math.floor(Math.random() * 10);
document.getElementById("dram" + i).innerHTML = img[r];
}
}
setTimeout("dramstart()",200);
}

function dramstop(btn) {
r = Math.floor(Math.random() * 10);
document.getElementById("dram"+btn).innerHTML = img[r];
document.slot.elements[btn].disabled = true;
kiroku[btn] = r;
}

</script>
</body>
</html>

★絵柄がランダムじゃなくて順番に回転させる。
★各絵柄がすべてそろうと、大当たりとしてページの背景色が変化する。
★大当たり後、再度スタートボタンを押すと、背景色は元の色にもどる。
★すべての絵柄が止まってないのに、大当たりの背景色変化が起こらないようになっている。

A 回答 (15件中1~10件)

作ると長くなりそうなので、とりあえず似ているものを探してみました。



このあたり(↓)が参考になるのでは?
http://jimita.com/etc/javascript/slot/slot.html
http://www.tac-net.ne.jp/~imekat/senkousha/slot/ …

比較的読み易く書いてあるみたい。
    • good
    • 0

ゲームですか?がんばってください


えっと。
document.getElementById("dram" + i).innerHTML = img[r];
で、上書きしてますが、

<img src=""alt="" id="abc">
だとしたら
document.getElementById('abc').src='2.png';
みたいにできますよ

ランダムな部分を、順列にするなら
r = Math.floor(Math.random() * 10);


if( ++r == img.length ) r = 0;
//もしくは、 r=++r%img.length;
(変数 r は、グローバルですよね)

背景色は、
document.body.style.backgroundColor = "red";
みたいにかえられます


話は変わりますが、ちがうところで、オブジェクト指向てきなのやってますね。
普段からのサンプルも、それ方式でやってほしいなぁ~と思ってます。
    • good
    • 0

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">


<title>スロットゲーム</title>

<table border="2">
<tr>
<td><img src='./img/0.gif' alt="no0" id="d0"></td>
<td><img src='./img/0.gif' alt="no1" id="d1"></td>
<td><img src='./img/0.gif' alt="no2" id="d2"></td>
</tr>
<tr>
<td><input type="button" value="ストップ" id="sw0"></td>
<td><input type="button" value="ストップ" id="sw1"></td>
<td><input type="button" value="ストップ" id="sw2"></td>
</tr>
<tr>
<td colspan="3"><input type="button" value="スタート" id="swStart"></td>
</tr>
</table>

<script type="text/javascript">

//cc_on
//________________________
var imageList = [
'./img/0.gif', './img/1.gif', './img/2.gif', './img/3.gif', './img/4.gif',
'./img/5.gif', './img/6.gif', './img/7.gif', './img/8.gif', './img/9.gif'
];

//画像の先読み
function ImageLoader ( srcList ) {
var rst = [ ];
var cnt = 0;
var max = srcList.length;

while( cnt < max ) {
rst[ cnt ] = new Image;
rst[ cnt ].src = srcList[ cnt ];
cnt++;
}
return rst;
};

//ドラムを1つを定義する
var Dram = function ( ) {
this.init.apply( this, arguments );
};

//初期化
Dram.prototype.init = function ( targetId, images, wait, stratNo ) {
this.tgt = document.getElementById( targetId );
this.img = images;
this.max = images.length;
this.wait = wait;
this.no = 'number' === typeof startNo ? startNo % this.max: Math.random() * this.max |0;
this.tmid = null;
this.name = 'Dram';
this.setNo();
};

//指定した画像番号にする機能
Dram.prototype.setNo = function ( n ) {
if( 'number' === typeof n ) this.no = n % this.max;
this.tgt.src = this.img[ this.no ].src;
};

//次の画像番号に画像を変える
Dram.prototype.next = function ( ) {
this.no = ( this.no + 1 ) % this.max;
this.setNo();
};

//現在のドラムの番号を返す
Dram.prototype.getNoValue = function ( ) {
return this.tmid ? false: this.no;
};
    • good
    • 0

つづき。


//ドラムを回転させる
Dram.prototype.start = function () {
if( this.tmid ) return;
var that = this;
this.tmid = setInterval(
(function( that ) {
return function() {
that.next( );
};
})( this ), this.wait );
};

//ドラムを停止させる
Dram.prototype.stop = function () {
clearInterval( this.tmid );
this.tmid = null;
};

//スロットを定義
var Slot = function ( ) {
this.init.apply( this, arguments );
};

Slot.buffer = [ ];

Slot.prototype.init = function ( tgsw /*, [ swid, dramObj ], [ ] */ ) {
this.name = 'Slot';
this.buf = [ ];
var cnt = 1;
var ary;

Slot.buffer[ tgsw ] = this;
while( ary = arguments[ cnt++ ] ) {
this.buf[ ary[0] ] = ary[1];
Slot.buffer[ ary[0] ] = this;
}
};


Slot.prototype.start = function ( ) {
var cnt = 0, k, o;
for( k in this.buf ) {
o = this.buf[ k ];
if( 'Dram' === o.name ) o.start();
}
};

Slot.prototype.check = function ( id ) {
var obj = this.buf[ id ];
var k, o, n, num = '';

if( obj ) {
obj.stop();
for( k in this.buf ) {
o = this.buf[ k ];
if( 'Dram' === o.name ) {
n = o.getNoValue();
if( false === n ) return;
num += n + '';
}
}
alert( num );
}
else
this.start();
};

//何が押されたか
Slot.handler = function ( evt ) {
var e = evt./*@if( @_jscript ) srcElement @else@*/ target /*@end@*/;
var obj;

if( e.id && ( obj = Slot.buffer[ e.id ] ) )
obj.check( e.id );
};

//クリックされたら
(function ( ) {
document./*@if( @_jscript ) attachEvent( 'on' + @else@*/ addEventListener( /*@end@*/
'click', Slot.handler, false );
})();

var images = ImageLoader( imageList );//画像の先読みっぽい?
var dram0 = new Dram( 'd0', images, 100, 0 );
var dram1 = new Dram( 'd1', images, 100, 0 );
var dram2 = new Dram( 'd2', images, 100, 0 );//回転ドラムを3個つくる

new Slot( 'swStart', [ 'sw0', dram0 ], [ 'sw1', dram1 ], [ 'sw2', dram2 ] );//スイッチとセットでスロットを作る


</script>
    • good
    • 0

まちがえた


//cc_on

//@cc_on


あぁ~今年こそ無駄な回答をしないようにと、思ってたのに・・・。
    • good
    • 0

#1です。



#2に回答がでているので、#3様にならってオブジェクト的になもの(?)を練習で作ってみました。(prototype使うの慣れてないので、要領悪いかも…)

<div class="dram">となっている要素の中に、ドラムが作成されます。
とりあえず、imgではなくtextで代用していますが、ドラムを生成する時にTextNodeの代わりにimg要素を入れるようにすれば、表示をそのまま画像に替えられるはず。

stopボタンを押してから空回りして止まるようにしています。その変化をintervalとstepを変えることで行なっているので、数値を変えると止まるまでの時間や回り方も変わります。
最後に止まるときに、どちら側で止まるのか逆転させたりして、ややアナログ的な雰囲気にしたつもり。

ご愛嬌ですが、ご参考まで…

<html>
<head>
<title>test</title>
<style type="text/css">
.dram {
margin:0; padding:0;
text-align:center;
float:left;
width:50px;
}
.dram div {
height:48px; _height:52px;
overflow:hidden;
background-color:#fff;
border:4px ridge #fff;
margin-bottom:8px;
}
.dram div span {
font-size:30px;
font-weight:bold;
line-height:46px;
position:relative;
}
.clr { clear:both; }
</style>

</head>
<body>
<div class="dram">dram1</div>
<div class="dram">dram2</div>
<div class="dram">dram3</div>
<div class="dram">dram4</div>
<div class="dram">dram5</div>
<div class="dram">dram6</div>
<div class="dram">dram7</div>
<hr class="clr">
<input type="button" value="start" id="starter">

<script type="text/javascript">
window.onload = function(){ slot.init(); };

// *** スロット全体 ***
var slot = {
item : [],

//スロットの初期設定
init : function() {
var param = {
height : 46, // 1コマの高さ(px)
interval : [30, 30, 40, 36, 40, 30, 50, 80], // 速度(msec)
step : [10, 8, 6, 4, 2, 1, 1, 1], // 1回の送り量(px)
point : [10, 30, 70, 100, 140, 160, 180, 'end'] //速度切替えポイント
};
var letter = '9876543210'; // 表示用文字列(仮設定)

var i = 0, e, tmp;
var m = letter.split(''), n = m.length;
var div = document.getElementsByTagName('DIV');
while (e = div[i++]) if (e.className == 'dram') {
tmp = new dram(param);
tmp.create(e, m, n);
this.item[this.item.length] = tmp;
}
document.getElementById('starter').onclick =
function() { slot.starter(); };
},

//スタートボタンを押したとき
starter : function() {
var i = 0, e;
while (e = this.item[i++]) e.start();
},

//ドラムが止まったときの結果判定
result : function() {
var i = 0, e, f = true, result = '';
while (e = this.item[i++]) {
if (e.status.tid) { f = false; break; }
result += (e.status.number - e.status.value - 1) + ' ';
}
if (f) alert(result);
}
}

// *** ドラムの定義 ***
var dram = function (p) {
//this.type = 'dram';
this.status = {tid:null, counter:0, pointer:0, value:0};
this.param = p;
}

//ドラムを停止
dram.prototype.stop = function() {
var s = this.status;
s.counter = 1;
s.element.parentNode.style.backgroundColor = '';
}

//ドラムをスタート
dram.prototype.start = function() {
var p = this.param, s = this.status;
if (s.tid) clearTimeout(s.tid);
s.interval = p.interval[0];
s.step = p.step[0];
s.counter = 0, s.pointer = 0;
this.roll();
s.element.parentNode.style.backgroundColor = '#ffd';
}

//ドラムを回転させる(速度調整含む)
dram.prototype.roll = function() {
var f = true, p = this.param, s = this.status;
var tmp = s.top + s.step;
tmp -= (tmp<0)?0:s.max;
s.element.style.top = tmp + 'px';
s.top = tmp, s.value = Math.round(-tmp / p.height) % s.number;
if (s.counter) {
tmp = p.point[s.pointer];
if (tmp == 'end') {
tmp = s.top / p.height;
s.step = tmp - Math.round(tmp)>0?-1:1;
f = !!(s.top % p.height);
} else {
if (++s.counter > tmp) {
s.interval = p.interval[++s.pointer];
s.step = p.step[s.pointer];
}
}
}
if (f) {
s.tid = setTimeout((function(obj) {
return function(){obj.roll();}
})(this), s.interval);
} else {
s.tid = null;
slot.result();
}
}

//ドラムを作成する
dram.prototype.create = function(e, m, n) {
var d, sp, j, h = this.param.height, s = this.status;
sp = document.createElement('span');
d = document.createElement('div');
while (e.firstChild) e.removeChild(e.firstChild);
for (j=0; j<m.length; j++) {
sp.appendChild(document.createTextNode(m[j]));
sp.appendChild(document.createElement('br'));
}
sp.appendChild(document.createTextNode(m[0]));
d.appendChild(sp);
e.appendChild(d);
s.element = sp;
s.number = n;
s.max = h * n;
j = -Math.floor(Math.random() * n) * h;
s.top = j, sp.style.top = j + 'px';
sp = document.createElement('input');
sp.type = 'button';
sp.value = 'stop';
e.appendChild(sp);
sp.onclick = (function(obj) {
return function(){obj.stop();}
})(this);
}
</script>

</body>
</html>
    • good
    • 0

おぅ~がっつり書きましたね~。

まってました!>fujillin さん

ここでは、がっつんと指摘する人が少ないので、やんわりと・・・。
最後の方の
 sp.onclick = (function(obj) {
  return function(){obj.stop();}
 })(this);
とかって、メモリーリークだと思ってる。
回避策とかありますか?

いまだに、自分も悩みます。

OKWaveからだと、文字数制限があって、一度にアップできなくて、分割するのが面倒です。^^;
    • good
    • 0

長いので分割してアップします。

全角空白は、全て半角空白になおしてください
その1
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<title>スロットゲーム</title>

<style type="text/css">

#slot div {
 height: 240px;
 width : 60px;
 overflow: hidden;
 position: relative;
 border: 1px #888 solid;
 float:left;
 z-index: 0;
 margin:1px;
}

#slot div img {
 height: 80px;
 width: 60px;
}

#button {
 clear:both;
}

#button input {
 width: 60px;
}

</style>

<div id="slot">
 <div id="s0"></div>
 <div id="s1"></div>
 <div id="s2"></div>
 <div id="s3"></div>
 <div id="s4"></div>
</div>

<p id="button">
 <input type="button" value="Stop" id="sw0">
 <input type="button" value="Stop" id="sw1">
 <input type="button" value="Stop" id="sw2">
 <input type="button" value="Stop" id="sw3">
 <input type="button" value="Stop" id="sw4">
 <input type="button" value="Start" id="swStart">
</p>

<script type="text/javascript">
//@cc_on

//画像の先読み
var ImageLoader = function ( srcList, header ) {
 var rst = [ ];
 var cnt = 0;
 var max = srcList.length;

 while( cnt < max ) {
  rst[ cnt ] = new Image;
  rst[ cnt ].src = ( header ? header + ',': '' ) + srcList[ cnt ];
  rst[ cnt ].alt = cnt + '';
  cnt++;
 }
 return rst;
};
    • good
    • 0

//その2




var Drum = function ( ) {
 this.init.apply( this, arguments );
};

//ドラムの初期化
Drum.prototype.init = function ( id, images, setNo, ac ) {
 var cnt = 0;
 var pnt = document.getElementById( id );
 var img, newImg;
 
 this.imgs = [ ];
 
 while( img = images[ cnt++ ] ) {
  newImg = document.createElement( 'img' );
  newImg.src = img.src;
  newImg.alt = img.alt;

  with( newImg.style )
   position = 'absolute',top = left = '0px';

  this.imgs.push( pnt.appendChild( newImg ) );
 }

 this.name = 'Drum';
 this.max = --cnt;
 this.radius = newImg.offsetHeight / (2 * Math.tan( Math.PI / cnt ) );//半径
 this.center = Math.floor( pnt.offsetHeight / 2 );//中心
 this.n = 360 / cnt; //n角形
 this.angle = setNo || Math.floor( Math.random( ) * cnt ) * this.n; //ドラムの回転角度
 this.step = 0; //回転量(加速量)
 this.ac = ac || .2;//加速度
 this.f = false;//true:加速 false:減速
 this.tmid = null;
 this.view();
};


//ドラムの絵柄表示
Drum.prototype.view = (function ( int, deg, sin ) {
 return function ( ) {
  var cnt = 0;
  var sa = this.angle + this.n / 2;
  var py0, py1, img, style;

  py0 = int( this.center + sin( sa * deg ) * this.radius );

  while( img = this.imgs[ cnt++ ] ) {
   style = img.style;
   sa -= this.n;
   py1 = int( this.center + sin( sa * deg ) * this.radius );

   if( py0 > py1 ) {
    style.top = py1 + 'px';
    style.height = py0 - py1 + 'px';
    style.display = 'inline';
   } else {
    style.display = 'none';
   }
   py0 = py1;
  }
 };
})( Math.floor, Math.PI / 180, Math.sin );
    • good
    • 1

//その3


//呼ばれるたびドラムを回転
Drum.prototype.roll = (function ( int ) {
 return function ( ) {
  if( this.f ) {
   //加速
   this.step = Math.min( this.step + this.ac, 15 );
  } else {
   //減速
   this.step = Math.max( this.step - this.ac, 1 );
   if( 1 == this.step && int( this.angle % this.n ) == 0) {
    clearInterval( this.tmid );
    this.tmid = null;
    if( 'function' === typeof this.cbFunc ) {
     this.cbFunc( this.getNumber() );
    }
   }
  }
  this.angle += this.step;
  this.view();
 };
})( Math.floor );


Drum.prototype.start = (function ( ) {
 return function ( ) {
  if( !this.tmid ) {
   this.f = true;
   this.step = 0;
   this.tmid = setInterval( (function ( that ) {
    return function ( ) { that.roll( ); }; })( this ), 30);
  }
 };})();


Drum.prototype.stop = (function ( ) {
 return function ( cbFunc ) {
  this.cbFunc = cbFunc;
  this.f = false;
 };})();


Drum.prototype.getNumber = (function ( ) {
 return function ( ) {
  return this.tmid ? false: Math.floor( ( this.angle % 360 ) / this.n );
 };})();


//スロットを定義
var Slot = function ( ) {
 this.init.apply( this, arguments );
};

Slot.buffer = [ ];//clickでidをキーとして、オブジェクトを調べるため

Slot.prototype.init = (function ( ) {
 return function ( tgsw /*, [ swid, dramObj ], [ ] */ ) {
  this.name = 'Slot';
  this.buf = [ ];
  this.nums = [ ];
  var cnt = 1;
  var ary, id, drum;

  Slot.buffer[ tgsw ] = this;
  
  while( ary = arguments[ cnt++ ] ) {
   id = ary[0], drum = ary[1];
   this.buf[ id ] = drum;
   Slot.buffer[ id ] = this;
  }
 };})();


Slot.prototype.start = (function ( ) {
 return function ( ) {
  for( var k in this.buf )
   if( this.buf.hasOwnProperty( k ) ) {
    this.nums[ k ] = false;
    this.buf[ k ].start();
   }
 };})();
    • good
    • 0

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!