I have created images that I would like to have placed where the message is revealed? What is the best way to make it work. I tried to make it work and have not had much success. My images are saved as png's. Here is my original code.
<!Doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Dice Game</title>
<link rel="stylesheet" type="text/css" href="demo.css">
</head>
<body>
<div class="row" align="center">
<div class="col-4">
<h3>Your Dice</h3>
<img src="images/d1.png" width="100" height="100" alt="roll: 1"
id="mydice1">
<img src="images/d1.png" width="100" height="100" alt="roll: 1"
id="mydice2">
</div>
<div class="col-4">
<h3>Enemy's Dice</h3>
<img src="images/e1.png" width="100" height="100" alt="roll: 1"
id="hisdice1">
<img src="images/e1.png" width="100" height="100" alt="roll: 1"
id="hisdice2">
<div id="enemy_score"></div>
</div>
<div class="col-4" align="center">
<p id="message"> Good Luck!</p>
<button class="greenBut"
onClick="throwdice()">Play!</button>
</div>
</div>
<script>
// var sides = ["d1.png", "d2.png", "d3.png", "d4.png", "d5.png",
"d6.png"];
var side_alt = ["roll: 1", "roll: 2", "roll: 3", "roll: 4", "roll:
5", "roll: 6"];
function throwdice(){
//create a random integer between 1 and 6
var rand1 = Math.round(Math.random()*5) + 1;
var rand2 = Math.round(Math.random()*5) + 1;
var rand3 = Math.round(Math.random()*5) + 1;
var rand4 = Math.round(Math.random()*5) + 1;
// Set Image src
document.getElementById("mydice1").src = "images/d" + rand1 +
".png";
document.getElementById("mydice2").src = "images/d" + rand2 +
".png";
document.getElementById("hisdice1").src = "images/e" + rand3 +
".png";
document.getElementById("hisdice2").src = "images/e" + rand4 +
".png";
// Set Image alt
document.getElementById("mydice1").alt = side_alt[rand1];
document.getElementById("mydice2").alt = side_alt[rand2];
document.getElementById("hisdice1").alt = side_alt[rand3];
document.getElementById("hisdice2").alt = side_alt[rand4];
who_won(rand1,rand2,rand3,rand4);
}
function who_won(rand1,rand2,rand3,rand4){
let player_points = rand1 + rand2 + 2;
let enemy_points = rand3 + rand4 + 2;
let result = winner(player_points,enemy_points);
document.getElementById("message").innerHTML = result;
}
function winner(player, enemy) {
if (player < enemy) {
return "looser";
}
if (enemy < player) {
return "winner"
}
if (player == enemy) {
return "equal"
}
}
</script>
</body>
</html>
Here is the solution for the above problem , images beside each player message
<!Doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Dice Game</title>
<link rel="stylesheet" type="text/css" href="demo.css">
<style>
.col-4{
display:flex;
}
.row,.button1{
width:55%;
margin-left:auto;
margin-right:auto;
}
</style>
</head>
<body>
<div class="row">
<div class="col-4">
<h3>Your Dice</h3>
<img src="images/d1.png" width="100" height="100"
id="mydice1">
<img src="images/d1.png" width="100" height="100"
id="mydice2">
</div>
<div class="col-4">
<h3>Enemy's Dice</h3>
<img src="images/e1.png" width="100" height="100"
id="hisdice1">
<img src="images/e1.png" width="100" height="100"
id="hisdice2">
<div id="enemy_score"></div>
</div>
<div class="button1" >
<p id="message"> Good Luck!</p>
<button class="greenBut"
onClick="throwdice()">Play!</button>
</div>
</div>
</div>
<script>
// var sides = ["d1.png", "d2.png", "d3.png", "d4.png", "d5.png",
"d6.png"];
var side_alt = ["roll: 1", "roll: 2", "roll: 3", "roll: 4", "roll:
5", "roll: 6"];
function throwdice(){
//create a random integer between 1 and 6
var rand1 = Math.round(Math.random()*5) + 1;
var rand2 = Math.round(Math.random()*5) + 1;
var rand3 = Math.round(Math.random()*5) + 1;
var rand4 = Math.round(Math.random()*5) + 1;
// Set Image src
document.getElementById("mydice1").src = "images/d" + rand1 +
".png";
document.getElementById("mydice2").src = "images/d" + rand2 +
".png";
document.getElementById("hisdice1").src = "images/e" + rand3 +
".png";
document.getElementById("hisdice2").src = "images/e" + rand4 +
".png";
// Set Image alt
document.getElementById("mydice1").alt = side_alt[rand1];
document.getElementById("mydice2").alt = side_alt[rand2];
document.getElementById("hisdice1").alt = side_alt[rand3];
document.getElementById("hisdice2").alt = side_alt[rand4];
who_won(rand1,rand2,rand3,rand4);
}
function who_won(rand1,rand2,rand3,rand4){
let player_points = rand1 + rand2 + 2;
let enemy_points = rand3 + rand4 + 2;
let result = winner(player_points,enemy_points);
document.getElementById("message").innerHTML = result;
}
function winner(player, enemy) {
if (player < enemy) {
return "looser";
}
if (enemy < player) {
return "winner"
}
if (player == enemy) {
return "equal"
}
}
</script>
</body>
Get Answers For Free
Most questions answered within 1 hours.