본문 바로가기
ETC/연습

주사위게임_ 랜덤 이미지/숫자 + If구문

by 율✌️ 2022. 5. 26.
두개의 주사위 숫자가 랜덤으로 보여짐.
같은숫자가 나오면 'DRAW'
player1 숫자가 높으면 'player 1 WIN'
player1 숫자가 높으면 'player 2 WIN'

 

 

 

 

HTML: 

<!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>Document</title>
    <link rel="stylesheet" href="style.css">
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Pacifico&display=swap" rel="stylesheet">
</head>

<body>
    <div class="main">
        <h1>Refresh Me</h1>
        
        <div class="player">
            <h3 class=>player1</h3>
            <img src="images/dice6.png" alt="dice image">
        </div>

        <div class="player">
            <h3>player2</h3>
            <img src="images/dice6.png" alt="dice image">
        </div>
    </div>
    <script src="index.js" charset="utf-8"></script>
</body>

</html>

 

CSS:

body{
    font-family: 'Pacifico', cursive; 
background-color: rgba(70, 59, 59, 0.842);
    
}

h1{
    color:rgb(67, 176, 140);
    font-size: 10rem;
    text-shadow: 5px 0 #232931;
}
.player{
   
    text-align: center;
  display: inline-block;
margin-right: 10px;
}
.main{
    text-align: center;
    width:70%;
    margin: auto;

}

 

Javs Script:

var randomNumber1 = 
    Math.floor(Math.random() * 6) + 1;

var randomImage ="images/" + "dice"+randomNumber1+".png";

document.querySelectorAll("img")[0].setAttribute("src",randomImage)



var randomNumber2 = 
    Math.floor(Math.random() * 6) + 1;

var randomImage2 ="images/" + "dice"+randomNumber2+".png";

document.querySelectorAll("img")[1].setAttribute("src",randomImage2)


if (randomNumber1>randomNumber2) {

    document.querySelector('h1').innerHTML = "Player 1 Win!✌️"
   
}
else if (randomNumber1<randomNumber2) {
    document.querySelector('h1').innerHTML = "Player 2 Win!✌️"

}
else{
    document.querySelector('h1').innerHTML = "DRAW"  
}