File tree Expand file tree Collapse file tree 4 files changed +104
-0
lines changed
Javascript/Dice Rolling Simulator Expand file tree Collapse file tree 4 files changed +104
-0
lines changed Original file line number Diff line number Diff line change 1+ # Project Title
2+
3+ ## Introduction
4+ This is a Dice Rolling Simulator which you can use while playing board games when physical dice is unavailable.
5+
6+ ## Steps to run the code:
7+ 1 . Download the given code.
8+ 2 . If you are in Visual Studio Code, you can use the Go-live extension to deploy this webpage locally.
9+ 3 . Now, you are ready to use this on that local device.
Original file line number Diff line number Diff line change 1+ <!DOCTYPE html>
2+ < html lang ="en ">
3+ < head >
4+ < meta charset ="UTF-8 ">
5+ < meta name ="viewport " content ="width=device-width, initial-scale=1.0 ">
6+ < title > Document</ title >
7+ < style >
8+ .dice {
9+ text-align : center;
10+ margin : auto;
11+ }
12+ .main {
13+ max-width : 400px ;
14+ }
15+ # rolling-dice {
16+ width : 200px ;
17+ }
18+ # rolling-dice video {
19+ width : 200px ;
20+ }
21+ # button {
22+ width : 200px ;
23+ cursor : pointer;
24+ }
25+ # output-text {
26+ width : 200px ;
27+ color : red;
28+ }
29+ footer {
30+ position : fixed;
31+ bottom : 0 ;
32+ }
33+ </ style >
34+
35+ </ head >
36+ < body >
37+ < div >
38+ < h1 > Dice Rolling Simulator</ h1 >
39+ < div class ="dice ">
40+
41+ </ div >
42+ < div class ="main ">
43+ < input type ="button " value ="Roll " id ="button ">
44+ < br >
45+ < div id ="rolling-dice ">
46+ < video
47+ src ="./Dice.mp4 " loop
48+ type ="video/mp4 " muted autoplay
49+ > </ video >
50+ </ div >
51+
52+ < h2 > Output: < span id ="output-text "> </ span > </ h2 >
53+ </ div >
54+
55+ </ div >
56+
57+ < footer > Developed by Contributor Kaustubh</ footer >
58+
59+ < script src ="script.js "> </ script >
60+ </ body >
61+
62+ </ html >
Original file line number Diff line number Diff line change 1+ // input element displaying button is referenced in the 'button' variable.
2+ const button = document . querySelector ( '#button' ) ;
3+ // video element displaying animation is referenced in the 'animation' variable.
4+ const animation = document . getElementById ( 'rolling-dice' ) ;
5+ // span element displaying the output is referenced in the 'output' variable.
6+ const output = document . getElementById ( 'output-text' ) ;
7+
8+ // function 'getRandomIntegerInclusive' generates a number between 1 to 6.
9+ function getRandomIntegerInclusive ( min , max ) {
10+ min = Math . ceil ( min ) ;
11+ max = Math . floor ( max ) ;
12+ return Math . floor ( Math . random ( ) * ( max - min + 1 ) ) + min ;
13+ }
14+
15+ // When 'Roll' button is clicked then below action is carried out.
16+ button . addEventListener ( 'click' , function ( ) {
17+ // video containing rolling-dice animation is hidden.
18+ animation . style . visibility = 'hidden' ;
19+ // adding a slight delay for effective visual effect.
20+ setTimeout ( ( ) => {
21+ // we will generate a random number by calling 'getRandomIntegerInclusive' function.
22+ let randomInteger = getRandomIntegerInclusive ( 1 , 6 ) ;
23+ console . log ( randomInteger ) ;
24+ // Now, displaying the output number in the span element
25+ output . textContent = randomInteger ;
26+
27+ // Now, displaying the dice rolling animation again.
28+ if ( output . textContent != "" ) {
29+ animation . style . visibility = 'visible' ;
30+ }
31+ } , 500 ) ;
32+
33+ } ) ;
You can’t perform that action at this time.
0 commit comments