JSによるゲーム制作-(神経衰弱)

JavaScript


トランプ画像を使って神経衰弱を作ってみよう。
2023作成の最新版があります

[準備編]
1.まずは以下をダウンロードし、デスクトップに展開する。

2.以下のようなフォルダ構成になっている。imagesフォルダにはトランプの画像が入っている。

3.index.htmlはcssファイルとjsファイルの読み込み設定とメインとなるテーブルタグがある。

01<!DOCTYPE html>
02<html>
03<head>
04<meta charset="UTF-8">
05<link rel="stylesheet" href="css/main.css">
06<script src="js/main.js"></script>
07<title>神経衰弱</title>
08</head>
09<body>
10  <!--メインとなるテーブル要素-->
11  <table id="table" border="1"></table>
12</body>
13</html>

4.main.jsはドキュメントの読み込みが完了したら処理を開始する記述をしてある。main.cssは現状空白の状態だ。

1'use strict';
2window.onload=function(){
3 
4};

[作成]
1.main.jsを以下のように更新する。

01'use strict';
02window.onload=function(){
03  //コンストラクタ作成
04  function Card(suit,num){
05    this.suit=suit;
06    this.num=num;
07  }
08  //カード配列作成
09  const cards=[];
10  const suits=['s','d','h','c'];
11  for(let i=0;i<suits.length;i++){
12    for(let j=1;j<=13;j++){
13      let card=new Card(suits[i],j);
14      cards.push(card);
15    }
16  }
17  //テーブル作成
18  const table=document.getElementById('table');
19  for(let i=0;i<suits.length;i++){
20    let tr=document.createElement('tr');
21    for(let j=0;j<13;j++){
22      let td=document.createElement('td');
23      let tempCard=cards[i*13+j];
24      td.textContent=`${tempCard.suit}:${tempCard.num}`;
25      tr.appendChild(td);
26    }
27    table.appendChild(tr);
28  }
29}

2.ブラウザから実行してみよう。以下のように表示されれば成功だ。

[スタイルをあてる]
それでは画像とスタイルを用いてカードの見た目を表現していこう。
1.まずはindex.htmlのtableタグについているborder属性を外す

01<!DOCTYPE html>
02<html>
03<head>
04<meta charset="UTF-8">
05<link rel="stylesheet" href="css/main.css">
06<script src="js/main.js"></script>
07<title>神経衰弱</title>
08</head>
09<body>
10  <!--メインとなるテーブル要素-->
11  <!--border属性を外す-->
12  <table id="table"></table>
13</body>
14</html>

2.main.jsを以下のように編集

01'use strict';
02window.onload=function(){
03  function Card(suit,num){
04    this.suit=suit;
05    this.num=num;
06    //以下を追記
07    this.front;
08    this.setFront=function(){
09      this.front=`${this.suit}${this.num<10?'0':''}${this.num}.gif`;
10    };
11  }
12 
13  const cards=[];
14  const suits=['s','d','h','c'];
15  for(let i=0;i<suits.length;i++){
16    for(let j=1;j<=13;j++){
17      let card=new Card(suits[i],j);
18      //以下を追加
19      card.setFront();
20      cards.push(card);
21    }
22  }
23  const table=document.getElementById('table');
24  for(let i=0;i<suits.length;i++){
25    let tr=document.createElement('tr');
26    for(let j=0;j<13;j++){
27      let td=document.createElement('td');
28      let tempCard=cards[i*13+j];
29      //以下を追記
30      td.classList.add('card');
31      td.style.backgroundImage=`url(images/${tempCard.front})`;
32      //以下をコメントアウト(または削除)
33      //td.textContent=`${tempCard.suit}:${tempCard.num}`;
34      tr.appendChild(td);
35    }
36    table.appendChild(tr);
37  }
38}

3.main.cssを以下のように編集

01body{
02  background-color:lightgreen;
03}
04table{
05  margin:10px auto;
06}
07td.card{
08  width:80px;
09  height:120px;
10  margin:2px;
11  background-size:cover;
12}

4.実行してみよう以下のように表示されれば成功だ。

[シャッフル]
トランプをランダムに並べてみよう。
1.main.jsに以下を追記

01'use strict';
02window.onload=function(){
03  function Card(suit,num){
04    this.suit=suit;
05    this.num=num;
06    this.front;
07    this.setFront=function(){
08      this.front=`${this.suit}${this.num<10?'0':''}${this.num}.gif`;
09    };
10  }
11 
12  const cards=[];
13  const suits=['s','d','h','c'];
14  for(let i=0;i<suits.length;i++){
15    for(let j=1;j<=13;j++){
16      let card=new Card(suits[i],j);
17      card.setFront();
18      cards.push(card);
19    }
20  }
21  //以下を追記
22  function shuffle(){
23    let i=cards.length;
24    while(i){
25      let index=Math.floor(Math.random()*i--);
26      var temp=cards[index];
27      cards[index]=cards[i];
28      cards[i]=temp;
29    }
30  }
31  shuffle();//シャッフル実行
32  const table=document.getElementById('table');
33  for(let i=0;i<suits.length;i++){
34    let tr=document.createElement('tr');
35    for(let j=0;j<13;j++){
36      let td=document.createElement('td');
37      let tempCard=cards[i*13+j];
38      td.classList.add('card');
39      td.style.backgroundImage=`url(images/${tempCard.front})`;
40      tr.appendChild(td);
41    }
42    table.appendChild(tr);
43  }
44}

2.実行してみよう。シャッフルのアルゴリズムによりカードがランダムに並ぶようになった。

[最初は裏面表示にする]
1.まずはmain.cssを以下のように変更

01body{
02  background-color:lightgreen;
03}
04table{
05  margin:10px auto;
06}
07td.card{
08  width:80px;
09  height:120px;
10  margin:2px;
11  background-size:cover;
12}
13/*以下を追記*/
14td.card.back{
15  background-image:url(../images/z01.gif) !important;
16}

2.次にmain.jsを変更する。といってもclassListにaddしている部分にbackを追加するだけだ。

01'use strict';
02window.onload=function(){
03  function Card(suit,num){
04    this.suit=suit;
05    this.num=num;
06    this.front;
07    this.setFront=function(){
08      this.front=`${this.suit}${this.num<10?'0':''}${this.num}.gif`;
09    };
10  }
11 
12  const cards=[];
13  const suits=['s','d','h','c'];
14  for(let i=0;i<suits.length;i++){
15    for(let j=1;j<=13;j++){
16      let card=new Card(suits[i],j);
17      card.setFront();
18      cards.push(card);
19    }
20  }
21  function shuffle(){
22    let i=cards.length;
23    while(i){
24      let index=Math.floor(Math.random()*i--);
25      var temp=cards[index];
26      cards[index]=cards[i];
27      cards[i]=temp;
28    }
29  }
30  shuffle();
31  const table=document.getElementById('table');
32  for(let i=0;i<suits.length;i++){
33    let tr=document.createElement('tr');
34    for(let j=0;j<13;j++){
35      let td=document.createElement('td');
36      let tempCard=cards[i*13+j];
37      //backを追加
38      td.classList.add('card','back');
39      td.style.backgroundImage=`url(images/${tempCard.front})`;
40      tr.appendChild(td);
41    }
42    table.appendChild(tr);
43  }
44}

3.実行してみよう。すべて裏面が表示されるはずだ。jsによって設定したstyleはインラインに記述されるため、それを覆すにはimportant設定が必要となる。

[クリックしたら表になる挙動の作成]
1.現状classListにbackが入っているときは裏、そうでないときは表が表示される。
まずはclickイベントを作成し、backをtoggleしてみよう。
main.jsを以下のように変更

01'use strict';
02window.onload=function(){
03  function Card(suit,num){
04    this.suit=suit;
05    this.num=num;
06    this.front;
07    this.setFront=function(){
08      this.front=`${this.suit}${this.num<10?'0':''}${this.num}.gif`;
09    };
10  }
11  const cards=[];
12  const suits=['s','d','h','c'];
13  for(let i=0;i<suits.length;i++){
14    for(let j=1;j<=13;j++){
15      let card=new Card(suits[i],j);
16      card.setFront();
17      cards.push(card);
18    }
19  }
20  function shuffle(){
21    let i=cards.length;
22    while(i){
23      let index=Math.floor(Math.random()*i--);
24      var temp=cards[index];
25      cards[index]=cards[i];
26      cards[i]=temp;
27    }
28  }
29  shuffle();
30  const table=document.getElementById('table');
31  for(let i=0;i<suits.length;i++){
32    let tr=document.createElement('tr');
33    for(let j=0;j<13;j++){
34      let td=document.createElement('td');
35      let tempCard=cards[i*13+j];
36      td.classList.add('card','back');
37      //以下を追加
38      td.onclick=flip;
39      td.style.backgroundImage=`url(images/${tempCard.front})`;
40      tr.appendChild(td);
41    }
42    table.appendChild(tr);
43  }
44  //flipの作成
45  function flip(e){
46    let td=e.target;
47    td.classList.toggle('back');
48  }
49 
50}

2.カードをクリックしてみよう。backクラスがついたり外れたりして表示が切り替わるはずだ。

[1枚目のカードと2枚目のカードが同じ数字だったら表のままにする]
いよいよ神経衰弱をつくっていこう。
クリックされたカードが1枚目なのか2枚目なのかをどう判定するか・・・
今回はfirstCardという変数を用意し、それがnullかどうかで判定することとする。

1.main.jsを以下のように変更。

01'use strict';
02window.onload=function(){
03  function Card(suit,num){
04    this.suit=suit;
05    this.num=num;
06    this.front;
07    this.setFront=function(){
08      this.front=`${this.suit}${('0'+this.num).slice(-2)}.gif`;
09    };
10  }
11  const cards=[];
12  const suits=['s','d','h','c'];
13  for(let i=0;i<suits.length;i++){
14    for(let j=1;j<=13;j++){
15      let card=new Card(suits[i],j);
16      card.setFront();
17      cards.push(card);
18    }
19  }
20  function shuffle(){
21    let i=cards.length;
22    while(i){
23      let index=Math.floor(Math.random()*i--);
24      var temp=cards[index];
25      cards[index]=cards[i];
26      cards[i]=temp;
27    }
28  }
29  shuffle();
30  const table=document.getElementById('table');
31  for(let i=0;i<suits.length;i++){
32    let tr=document.createElement('tr');
33    for(let j=0;j<13;j++){
34      let td=document.createElement('td');
35      let tempCard=cards[i*13+j];
36      td.classList.add('card','back');
37      td.onclick=flip;
38      //以下を追加
39      td.num=tempCard.num;
40      td.style.backgroundImage=`url(images/${tempCard.front})`;
41      tr.appendChild(td);
42    }
43    table.appendChild(tr);
44  }
45  //以下の変数を追加
46  let firstCard=null;
47  let flipTimerId=NaN;
48  function flip(e){
49    let td=e.target;
50    //下の一行をコメントアウト
51    //td.classList.toggle('back');
52    //以下を追記
53    if(!td.classList.contains('back') || flipTimerId){
54      return;//表のカードをクリックしても何もしない。
55    }
56    td.classList.remove('back');//カードを表にする。
57    if(firstCard===null){
58      firstCard=td;//1枚目だったら今めくったカードをfirstCardに設定
59    }else{
60      //2枚目だったら1枚目と比較して結果を判定する。
61      if(firstCard.num===td.num){
62        //2枚が同じだったときの処理
63        firstCard=null;
64      }else{
65        flipTimerId=setTimeout(function(){
66          firstCard.classList.add('back');
67          td.classList.add('back');
68          flipTimerId=NaN;
69          firstCard=null;
70        },1200);
71      }
72 
73 
74    }
75  }
76}

コメント

タイトルとURLをコピーしました