-
Notifications
You must be signed in to change notification settings - Fork 0
/
javascript-file-1.js
96 lines (92 loc) · 3.5 KB
/
javascript-file-1.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
var isPlaying=true,value,user_score=0,rival_score=0,rival_value;
//-----------------------------------BEFORE GAME STARTS--------------------------------------
document.getElementById('roll').style.display='none';
document.getElementById('dice-pic1').style.display='none';
document.getElementById('dice-pic2').style.display='none';
document.getElementById('score-0').textContent='0';
document.getElementById('score-1').textContent='0';
//--------------------------------NEW GAME MOUSE-OVER CODE----------------------------------
document.getElementById('new-game').addEventListener('mouseover',function(){
document.getElementById('new-game').style.backgroundColor='green';
});
document.getElementById('new-game').addEventListener('mouseout',function(){
document.getElementById('new-game').style.backgroundColor='white';
});
//----------------------------------------AFTER GAME STARTS--------------------------------
document.getElementById('new-game').addEventListener('click',function(){
//---------------------------------BACKGROUND MUSIC CODE -----------------------------------
if(isPlaying==true){
document.getElementById('gaana').play();
document.getElementById('new-game').textContent='Stop Game.';
document.getElementById('new-game').onclick=function(){
location.reload();
};
//---------------------------------ROLL DICE ONCLICK CODE------------------------------------
var rollbtn = document.getElementById('roll');
rollbtn.onmouseover=function(){
rollbtn.style.backgroundColor='darkcyan';
};
rollbtn.onmouseout=function(){
rollbtn.style.backgroundColor='beige';
};
rollbtn.addEventListener('click',user);
rollbtn.addEventListener('click',rival);
//--------------------------------------------------------------------------------------------
document.getElementById('roll').style.display='block';
document.getElementById('dice-pic1').style.display='block';
document.getElementById('dice-pic2').style.display='block';
//--------------------------------------------------------------------------------------------
function user(){
value = Math.floor(Math.random()*6)+1;
document.getElementById('dice-img1').src='game-images/dice-'+value+'.png';
user_score=user_score + value;
document.getElementById('score-0').innerHTML=user_score;
if(user_score>=40){
if(user_score>rival_score){
user_won();
}
else if(rival_score>user_score){
rival_won();
}
else if(rival_score>=40){
draw();
}
}
};
function rival(){
rival_value = Math.floor(Math.random()*6)+1;
document.getElementById('dice-img2').src='game-images/dice-'+rival_value+'.png';
rival_score=rival_score + rival_value;
document.getElementById('score-1').innerHTML=rival_score;
if(rival_score>=40){
if(rival_score>user_score){
rival_won();
}
else if(user_score>rival_score){
user_won();
}
else if(user_score>=40){
draw();
}
}
};
function endGame(){
document.getElementById('roll').style.display='none';
document.getElementById('gaana').pause();
};
function user_won(){
document.getElementById('score-0').style.fontSize='30px';
document.getElementById('score-0').textContent='YOU WON';
endGame();
};
function rival_won(){
document.getElementById('score-1').style.fontSize='30px';
document.getElementById('score-1').textContent='RIVAL WON';
endGame();
};
function draw(){
document.getElementById('score-0').textContent='MATCH DRAWN';
document.getElementById('score-1').textContent='MATCH DRAWN';
};
}
});