Tuesday, January 26, 2021

Creating Google logo by using just HTML and CSS.

 

Step 1:

Create a div element and give it id as google..

  <div id="google"></div>


Step 2 :

Style the div element by selecting its id ..code is given below:

 #google{
            positionrelative;
            border-top100px solid #ea4335;
            border-right100px solid #4285f4;
            border-bottom100px solid #34a853;
            border-left100px solid #fbbc05;
            border-radius50%;
            background-color#fff;
            width300px;
            height300px;
            padding0;
            margin10vh auto 0;
        }


Step 3 :

Use before pseudo selector on the #google element

  #google::before{
            content"";
            z-index100;
            positionabsolute;
            top50%;
            right-95px;
            transformtranslateY(-50%);
            width245px;
            height100px;
            background-color#4285f4;
        }


Step 4 :

Use after pseudo selector on the #google element


        #google::after{
            content"";
            z-index101;
            positionabsolute;
            border-top200px solid transparent;
            border-right200px solid white;
            top-100px;
            right-100px;
            width0;
            height0;
            
        }


Rock Paper and Scissors game tutorial

 


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Rock paper and scissors</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <section class="game">
        <div class="score">
            <div class="player-score">
                <h2>Player</h2>
                <p>0</p>
            </div>
            <div class="computer-score">
                <h2>Computer</h2>
                <p>0</p>
            </div>
        </div>

        <div class="intro">
            <h3>Rock Paper and Scissors</h3>
            <button>Lets Play</button>
        </div>

        <div class="match fadeOut">
            <h2 class="winner">Choose an option</h2>
            <div class="hands">
                <img class="player-hand" src="Assets/rock.png" alt="">
                <img class="computer-hand" src="Assets/rock.png" alt="">
            </div>
            <div class="options">
                <button class="rock">rock</button>
                <button class="paper">paper</button>
                <button class="scissors">scissors</button>
            </div>
        </div>
    </section>
    <script src="index.js"></script>
</body>
</html>



CSS


/* Css reset */
*{
    margin0;
    padding0;
    box-sizingborder-box;
}

section{
    height100vh;
    background-colorpurple;
    font-familysans-serif;
}

.score{
    coloryellow;
    height20vh;
    displayflex;
    justify-contentspace-around;
    align-itemscenter;
}

.score h2{
    font-size30px;
}

.score p{
    text-aligncenter;
    padding10px;
    font-size25px;
}

.intro{
    colorgreenyellow;
    height50vh;
    displayflex;
    flex-directioncolumn;
    justify-contentspace-around;
    align-itemscenter;
    transition: opacity 0.5s ease;
    /* opacity: 0; */
}

.intro h3{
    font-size50px;
}

.intro button,.match button{
    width150px;
    height50px;
    backgroundnone;
    bordernone;
    colorblack;
    font-size20px;
    font-weightbold;
    background-colorturquoise;
    outlinenone;
    border2px solid white;
    border-radius10px;
    cursorpointer;
}

.match{
    positionabsolute;
    top50%;
    left50%;
    transformtranslate(-50%,-50%);
    transition: opacity 0.5s ease 0.5s;
    /* opacity: 0; */
}

.winner{
    colorwhite;
    text-aligncenter;
    font-size35px;
}

.hands,.options{
    displayflex;
    justify-contentspace-around;
    align-itemscenter;
    margin20px;

}

/* need to rotate players hand */
.player-hand{
    transformrotateY(180deg);
}

/* creating the utility classes
fadein and fadeout */

.fadeIn{
    opacity1;
    pointer-eventsall;
}

.fadeOut{
    opacity0;
    pointer-eventsnone;
}

/* animation here
 */

@keyframes shakePlayer{
    0%{
        transformrotateY(180degtranslateY(0px);
    }
    15%{
        transformrotateY(180degtranslateY(-50px);
    }
    25%{
        transformrotateY(180degtranslateY(0px);
    }
    35%{
        transformrotateY(180degtranslateY(-50px);
    }
    50%{
        transformrotateY(180degtranslateY(0px);
    }
    65%{
        transformrotateY(180degtranslateY(-50px);
    }
    75%{
        transformrotateY(180degtranslateY(0px);
    }
    85%{
        transformrotateY(180degtranslateY(-50px);
    }
    100%{
        transformrotateY(180degtranslateY(0px);
    }
}

@keyframes shakeComputer{
    0%{
        transform:translateY(0px);
    }
    15%{
        transformtranslateY(-50px);
    }
    25%{
        transformtranslateY(0px);
    }
    35%{
        transformtranslateY(-50px);
    }
    50%{
        transformtranslateY(0px);
    }
    65%{
        transformtranslateY(-50px);
    }
    75%{
        transformtranslateY(0px);
    }
    85%{
        transformtranslateY(-50px);
    }
    100%{
        transformtranslateY(0px);
    }
}



Javascript....................


// We are creating the different functions in the js
//Game function
const game = () => {
    // scores of player and computer
    let pScore = 0;
    let cScore = 0;

    // creating start game function
    const startGame = () => {
        // taking play button, intro screen and match screen
        const playButton = document.querySelector('.intro button')
        const introScreen = document.querySelector('.intro')
        const matchScreen = document.querySelector('.match')

        // When someone clicks on play then do the following
        playButton.addEventListener('click', () => {
            introScreen.classList.add('fadeOut');
            matchScreen.classList.add('fadeIn');
            matchScreen.classList.remove('fadeOut')
        });
    }
    //play match funtion
    const playMatch = () => {
        // taking option buttons , player hand and computer hand
        const options = document.querySelectorAll('.options button');
        const playerHand = document.querySelector('.player-hand');
        const computerHand = document.querySelector('.computer-hand');

        // getting computer options by generating random numbers
        const computerOptions = ['rock''paper''scissors'];
        const hands = document.querySelectorAll('.hands img');

        hands.forEach(hand => {
            //for shaking the hands
            hand.addEventListener('animationend'function () {
                this.style.animation = '';
            })
        })

        //taking the options and doing accordingly
        options.forEach(option => {
            option.addEventListener('click'function () {
                //computer's choice
                // generating random number between 1 and 3
                const computerNumber = Math.floor(Math.random() * 3);
                // selecting rock paper scissor based on the number generated
                const computerChoice = computerOptions[computerNumber];

                setTimeout(() => {
                    compare(this.textContentcomputerChoice);

                    // update the images
                    playerHand.src = `Assets/${this.textContent}.png`
                    computerHand.src = `Assets/${computerChoice}.png`
                }, 2000)

                // animating the hands up and down
                playerHand.style.animation = 'shakePlayer 2s ease';
                computerHand.style.animation = 'shakeComputer 2s ease';

            })
        })
    }

    
    //updating the score
    const updateScore = ()=>{
        const playerScore = document.querySelector('.player-score p');
        const computerScore = document.querySelector('.computer-score p');
        playerScore.textContent = pScore;
        computerScore.textContent = cScore;
    }

    const compare = (playerChoice,computerChoice)=>{
        const winner = document.querySelector('.winner')

        //tie
        if(playerChoice === computerChoice){
            winner.textContent = "It is a tie"
            return;
        }

        //rock
        if(playerChoice === 'rock'){
            if(computerChoice === 'scissors'){
                winner.textContent = "Player wins";
                pScore++;
                updateScore();
                return;
            }
            else{
                winner.textContent = "Computer wins";
                cScore++
                updateScore();
                return;
            }
        }

        //paper
        if(playerChoice === 'paper'){
            if(computerChoice === 'scissors'){
                winner.textContent = "Computer wins";
                cScore++
                updateScore();
                return;
            }
            else{
                winner.textContent = "Player wins";
                pScore++
                updateScore();
                return;
            }
        }

        //scissors
        if(playerChoice === 'scissors'){
            if(computerChoice === 'paper'){
                winner.textContent = "Player wins";
                pScore++
                updateScore();
                return;
            }
            else{
                winner.textContent = "Computer wins";
                cScore++
                updateScore();
                return;
            }
        }
    }


    startGame();
    playMatch();
}

game();