-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdice_game.html
More file actions
138 lines (114 loc) · 3.64 KB
/
dice_game.html
File metadata and controls
138 lines (114 loc) · 3.64 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Dice Game</title>
<style>
.container{
width: 70%;
margin: auto;
text-align: center;
}
.dice{
text-align: center;
display:inline-block;
margin: 10px;
}
body{
background-color: #042f4b;
margin: 0;
}
h1{
margin: 30px;
font-family: "Lobster", cursive;
text-shadow: 5px 0 #232931;
font-size: 4.5rem;
color: #4ecca3;
text-align: center;
}
p{
font-size: 2rem;
color: #4ecca3;
font-family: "Indie Flower", cursive;
}
img{
width: 100%;
}
.bottom{
padding-top: 30px;
}
.butn{
background: #4ecca3;
font-family: "Indie Flower", cursive;
border-radius: 7px;
color: #ffff;
font-size: 30px;
padding: 16px 25px 16px 25px;
text-decoration: none;
}
.butn:hover {
background: #232931;
text-decoration: none;
}
</style>
</head>
<body>
<div class="container">
<h1>Let's Play</h1>
<div class="dice">
<p class="Player1">Player 1</p>
<img class="img1" src="dice6.png">
</div>
<div class="dice">
<p class="Player2">Player 2</p>
<img class="img2" src="dice6.png">
</div>
</div>
<div class="container bottom">
<button type="button" class="butn"
onclick="rollTheDice()">
Roll the Dice
</button>
</div>
<div class="container bottom">
<button type="button" class="butn"
onclick="editNames()">
Edit Names
</button>
</div>
<script>
// player name
var Player1 = "Player 1";
var Player2 = "Player 2";
// function to change the player name
function editNames(){
player1 =prompt("Change player1 name");
player2 =prompt("Change player2 name");
document.querySelector("p.Player1").innerHTML = player1;
document.querySelector("p.Player2").innerHTML = player2;
}
// Function to roll the dice
function rollTheDice(){
setTimeout(function () {
var randomNumber1 = Math.floor(Math.random() * 6) + 1;
var randomNumber2 = Math.floor(Math.random() * 6) + 1;
document.querySelector(".img1").setAttribute("src", "dice" + randomNumber1 + ".png");
document.querySelector(".img2").setAttribute("src", "dice" + randomNumber2 + ".png");
if(randomNumber1 ===randomNumber2) {
document.querySelector("h1").innerHTML = "Draw!";
}
else if (randomNumber1 < randomNumber2) {
document.querySelector("h1").innerHTML
= (player2 + " WINS!");
}
else {
document.querySelector("h1").innerHTML
= (player1 + " WINS!");
}
} , 2500);
}
</script>
</body>
</html>