-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgame.js
111 lines (99 loc) · 3.91 KB
/
game.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
遊戲JavaScript文件
*/
//Math.floor() 函式會回傳 無條件捨去 後的最大整數
var currentPlayer =1; //現在的玩家
var sumPlay1Score=0; //玩家1總分
var sumPlay2Score=0;//玩家2總分
var counter =1;//搖骰子總次數
var isPlayingGame=true;//是否可以繼續玩
//隱藏骰子圖案與冠軍獎杯的圖案
document.querySelector('.dice').style ='display:none';
document.querySelector('.winner1').style ='display:none';
document.querySelector('.winner2').style ='display:none';
//even監聽 新增摇骰子ONCLICK事件
document.querySelector('.roll').addEventListener('click',function(){
if(isPlayingGame){
var dice=Math.floor(Math.random()*6)+1;
console.log(dice);
document.querySelector('.dice').style ='display:block';
document.querySelector('.dice').src ='dice'+ dice +'.png';
if(currentPlayer===1)
{
sumPlay1Score+= dice;
//修改玩家1分數
//document.querySelector('#player'+ currentPlayer +'Score').innerHTML='<h1>'+ dice +'</h1>'; //#為ID .為CLASS 修改值
document.getElementById('player'+ currentPlayer +'Score').textContent=dice; //#為ID .為CLASS 修改值
//修改玩家1總分分數
document.getElementById('total'+ currentPlayer +'Score').textContent=sumPlay1Score;
//移除玩家active的class
document.querySelector('.panel-'+ currentPlayer).classList.remove('active');
currentPlayer=2;
//add玩家active的class
document.querySelector('.panel-'+ currentPlayer).classList.add('active');
document.getElementById('player'+ currentPlayer +'Score').textContent=0; //#為ID .為CLASS 修改值
}
else {
sumPlay2Score+= dice;
//修改玩家1分數
document.getElementById('player'+ currentPlayer +'Score').textContent=dice; //#為ID .為CLASS 修改值
//修改玩家1總分分數
document.getElementById('total'+ currentPlayer +'Score').textContent=sumPlay2Score;
document.querySelector('.panel-'+ currentPlayer).classList.remove('active');
currentPlayer=1;
//add玩家active的class
document.querySelector('.panel-'+ currentPlayer).classList.add('active');
document.getElementById('player'+ currentPlayer +'Score').textContent=0; //#為ID .為CLASS 修改值
}
counter +=1;
if(counter===7)
{
if(sumPlay1Score > sumPlay2Score)
{
document.querySelector('.winner1').style ='display:block';
document.getElementById('player1Score').style='margin-top:0px';
}
else if (sumPlay2Score > sumPlay1Score) {
document.querySelector('.winner2').style ='display:block';
document.getElementById('player2Score').style='margin-top:0px';
}
else {
document.querySelector('.roll').textCotent='平手';
}
isPlayingGame=false;
}
}
else {
init();
isPlayingGame=true;
}
});
//even監聽 新增重設遊戲ONCLICK事件
document.querySelector('.newGame').addEventListener('click',function(){
init();
isPlayingGame=true;
});
function init()
{
currentPlayer =1; //現在的玩家
sumPlay1Score=0; //玩家1總分
sumPlay2Score=0;//玩家2總分
counter =1;//搖骰子總次數
//隱藏骰子圖案與冠軍獎杯的圖案
document.querySelector('.dice').style ='display:none';
document.querySelector('.winner1').style ='display:none';
document.querySelector('.winner2').style ='display:none';
//修改玩家1分數為0
document.getElementById('player1Score').textContent=0; //#為ID .為CLASS 修改值
//修改玩家1總分分數為0
document.getElementById('total1Score').textContent=0;
//修改玩家2分數為0
document.getElementById('player2Score').textContent=0; //#為ID .為CLASS 修改值
//修改玩家2總分分數為0
document.getElementById('total2Score').textContent=0;
document.querySelector('.panel-1').classList.add('active');
document.querySelector('.panel-2').classList.remove('active');
document.querySelector('.roll').textCotent='搖骰子';
document.getElementById('player1Score').style='margin-top:55px';
document.getElementById('player2Score').style='margin-top:55px';
}