小豬佩奇之小雞下蛋JS小遊戲

2020年春節正值冠狀病毒施虐,在家閒來無事,通過JS實現了一個小雞下蛋的小遊戲,按下任何按鍵小雞都會下一個蛋,小雞自動移動一個位置。上js程序:


<!DOCTYPE html>
<html lang="en">
    <head>
        <meta  http-equiv="Content-Type" content="text/html; charset=gb2312">
        <title>Title</title>
        <audio src="F:/chichk.mp3"  autoplay="autoplay"> </audio>
    </head>
    <body>
        <center id="myScan">
            <img id="img_chick" src="F:/chick.png" style="width: 150px;height: 200px;position: absolute;left: 10px;top: 10px">
            <img id="img_egg" src="F:/egg1.jpg" style="width: 80px;height: 100px;position: absolute;left: 40px;top: 40px">
            <script type="text/javascript"      src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
            <script type="text/javascript">
            
            var tmpEggTop;
            var tmpEggLeft;
            $(document).ready(
               function(){
                   debugger;
                document.onkeydown = function(){
                var oEvent = window.event;
                var obj_chick = document.getElementById('img_chick');
                obj_chick.left += 100;

                var tooWidth = 0, tooTop = 0;
                if(obj_chick.left > document.documentElement.clientWidth )
                {
                    obj_chick.left = 10;
                    obj_chick.top += 100;
                    tooWidth = 1;
                }

                if(obj_chick.top > document.documentElement.clientHeight)
                {
                    obj_chick.top = 10;
                    tooTop = 1;
                }
                obj_chick.style.left = obj_chick.left + "px", obj_chick.style.top = obj_chick.top + "px";

                var myParent=document.getElementById('myScan');

                var myImage = document.createElement("img");
                 myImage.src = 'F:/egg1.jpg';
                 myImage.style.position = "absolute";

                tmpEggLeft +=80;
                if(tooWidth == 1)
                {
                    tmpEggLeft = 0;
                    tmpEggTop = obj_chick.top;
                    tmpEggTop += 200;
                }

                if(tooTop == 1)
                {
                    tmpEggTop.top = 10;
                }
                 myImage.style.top = tmpEggTop + "px" ;
                 myImage.style.left = tmpEggLeft + "px";
                 myImage.style.width = "80px";
                 myImage.style.height = "100px";
                 myParent.appendChild(myImage);
              }
            });
            </script>
            
            <script type="text/javascript">
                var obj = document.getElementById('img_chick');
                obj.style.zIndex = 1;
                obj.left = 10;
                obj.top = 10;
                var obj_egg = document.getElementById('img_egg');
                obj_egg.style.display="none";
                tmpEggTop = 100;
                tmpEggLeft = 0;
            </script>
    </body>
</html>

發佈了4 篇原創文章 · 獲贊 7 · 訪問量 8056
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章