Skip to content
Advertisements

JavaScript Rock Paper Scissors

Rock Paper Scissors

github: https://github.com/guldus98/HTML-CSS/tree/master/taskagıtmakas

(images can be downloaded from github.)


HTML CODE:

div id="container">
        <div id="topBox">
            <div id="tas">
                <p>TAŞ</p>
                <div id="image1"></div>
            </div>
            <div id="kagit">
                <p>KAĞIT</p>
                <div id="image2"></div>
            </div>
            <div id="makas">
                <p>MAKAS</p>
                <div id="image3"></div>
            </div>
        </div>
        <div id="botBox">
            <div id="left">
                <p class="player">OYUNCU 1</p>
                <div id="secim1"></div>
                <div id="leftPhoto"></div>
            </div>
            <div id="right">
                <p class="player">OYUNCU 2</p>
                <div id="secim2"></div>
                <div id="rightPhoto"></div>
            </div>
        </div>
        <div class="result">
            <div class="py1">
                <div id="score1"></div>
            </div>
            <div class="py2">
                <div id="score2"></div>
            </div>
        </div>
    </div>

CSS CODE:


body {
    padding: 0;
    margin: 0;
    background: #ededed;
    font-family: Verdana;
    width:100%;
}
#container {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%,-50%);
}
#topBox{
    width:425px;
    margin:0 auto;
}
#tas, #kagit, #makas {
    display: block;
    float: left;
    width: 120px;
    height: 150px;
    border: 2px solid #333;
    margin: 5px;
}
#image1 {
    width: 100%;
    height: 81%;
    background: url(tas.png);
    background-position: center;
    background-repeat: no-repeat;
    background-size: cover;
}
#image2 {
    width: 100%;
    height: 81%;
    background: url(kagit.png);
    background-position:center;
    background-repeat:no-repeat;
    background-size:cover;
}
#image3 {
    width: 100%;
    height: 81%;
    background: url(makas.png);
    background-position: center;
    background-repeat: no-repeat;
    background-size: cover;
}
#tas p, #kagit p, #makas p{
    text-align:center;
    padding:0;
    margin:5px;
}
#botBox {
    width:800px;
    height: 300px;
    border: 2px solid #333;
    margin-top:170px;
}
#left {
    display: block;
    width: 395px;
    float: left;
    height: 100%;
}
#right {
    display: block;
    float: right;
    width: 395px;
    height: 100%;
}
.player{
    font-family: Verdana;
    font-weight:bold;
    color:#ff7c2b;
    text-shadow:0px 0px 1px #000;
    margin-left:100px;
}
#leftPhoto {
    width: 200px;
    height: 200px;
    position:absolute;
    top:250px;
    background-position: center;
    background-repeat: no-repeat;
    background-size: cover;
    margin-left:50px;
    border:2px solid #262626;
    border-radius:20px;
}
#rightPhoto {
    width: 200px;
    height: 200px;
    position: absolute;
    top: 250px;
    background-position: center;
    background-repeat: no-repeat;
    background-size: cover;
    margin-left: 50px;
    border: 2px solid #262626;
    border-radius: 20px;
}
.result{
    width:200px;
    height:50px;
    margin:0 auto;
    border:2px solid #262626;
}
.py1 {
    float: left;
    width: 50%;
    height: 100%;
    background: #116900;
    color: #fff;
    line-height: 45px;
    text-align: center;
    font-size:24px;
}
.py2 {
    float: right;
    width: 50%;
    height: 100%;
    background: #7c1200;
    color: #fff;
    line-height: 45px;
    text-align: center;
    font-size: 24px;
}
#secim1, #secim2{
    margin-left:125px;
    font-family:sans-serif
}

JAVASCRIPT CODE:

 var tas = document.getElementById("tas"),
            kagit = document.getElementById("kagit"),
            makas = document.getElementById("makas"),
            secim1 = document.getElementById("secim1"),
            secim2 = document.getElementById("secim2"),
            score1 = document.getElementById("score1"),
            score2 = document.getElementById("score2"),
            leftPhoto = document.getElementById("leftPhoto");
        var player1 = "";
        var player2 = "";
        var deger = ["tas", "kağıt", "makas"];
        function skor() {
            score1.innerHTML = player1.length;
            score2.innerHTML = player2.length;
        }
        function right(a) {
            rightPhoto.style.background = "url("+a+")";
            rightPhoto.style.backgroundSize = "cover"
        }
        tas.onclick = function () {
            x = Math.floor(Math.random() * 3);
            secim1.innerHTML = "TAŞ";
            leftPhoto.style.background = "url(tas.png)";
            leftPhoto.style.backgroundSize = "cover"
            if (x == 0) {
                secim2.innerHTML = "TAŞ";
                right("tas.png")
            } else if (x == 1) {
                player2 += "1";
                secim2.innerHTML = "KAĞIT";
                right("kagit.png")
            } else if (x == 2) {
                secim2.innerHTML = "MAKAS";
                player1 += "1";
                right("makas.png")
            }
            skor();
        }
        kagit.onclick = function () {
            x = Math.floor(Math.random() * 3);
            secim1.innerHTML = "KAĞIT";
            leftPhoto.style.background = "url(kagit.png)";
            leftPhoto.style.backgroundSize = "cover";
            if (x == 0) {
                secim2.innerHTML = "TAŞ";
                player1 += "1";
                right("tas.png")
            } else if (x == 1) {
                secim2.innerHTML = "KAĞIT"
                right("kagit.png")
            } else if (x == 2) {
                secim2.innerHTML = "MAKAS"
                player2 += 1;
                right("makas.png")
            }
            skor();
        }
        makas.onclick = function () {
            x = Math.floor(Math.random() * 3);
            secim1.innerHTML = "MAKAS";
            leftPhoto.style.background = "url(makas.png)";
            leftPhoto.style.backgroundSize = "cover"
            if (x == 0) {
                secim2.innerHTML = "TAŞ";
                player2 += "1";
                right("tas.png")
            } else if (x == 1) {
                secim2.innerHTML = "KAĞIT"
                player1 += "1";
                right("kagit.png")
            } else if (x == 2) {
                secim2.innerHTML = "MAKAS";
                right("makas.png")
            }
            skor();
        }
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: