HTML練習之吃豆豆小遊戲
阿新 • • 發佈:2020-12-17
一、概述
閒來無聊,稍微學一點HTML的知識,動手寫一個吃豆豆的小遊戲,感覺蠻好玩的。這裡把程式碼放在這,以防以後用的上,可以參考。
二、程式碼
已經將重點部分註釋了,用的animation很有意思,方便,快捷。
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>吃豆豆</title>
<style>
.mouth{
width: 200px;
height: 200px;
border-radius: 50%; /* 畫個圓 */
animation: peas 1s linear infinite; /* 動畫播放 繫結keyframes: peas */
}
.mouthTop{ /* 上半個嘴巴*/
width: 200px;
height: 100px;
background-color: pink;
margin-left: 200px;
margin-top: 100px;
border-radius: 100px 100px 0 0;
animation : mouthTopK 1s linear infinite;
}
.mouthBottom{/* 下半個嘴巴*/
width: 200px;
height: 100px;
background-color: pink;
margin-left: 200px;
margin-top: 0px;
border-radius: 0 0 100px 100px;
animation: mouthBottomK 1s linear infinite;
}
@keyframes mouthTopK{ /* 上半個嘴巴關鍵幀 定義三個關鍵幀 */
0% {
transform: rotate(-30deg) ;
}
50%{
transform: rotate(0deg) ;
}
100%{
transform: rotate(-30deg) ;
}
}
@keyframes mouthBottomK{
0%{
transform: rotate(30deg) ;
}
50%{
transform: rotate(0deg) ;
}
100%{
transform: rotate(30deg) ;
}
}
@keyframes peas{ /* 定義豆豆的動畫關鍵幀 */
0%{
box-shadow:
300px 0 0 -80px red,
450px 0 0 -80px red,
600px 0 0 -80px red,
750px 0 0 -80px red;
}
100%{
box-shadow:
150px 0 0 -80px red,
300px 0 0 -80px red,
450px 0 0 -80px red,
600px 0 0 -80px red;
}
}
</style>
</head>
<body>
<div class="mouth">
<div class="mouthTop">
</div>
<div class="mouthBottom">
</div>
</div>
</body>
</html>