使用html+css+js實現彈球游戲

来源:https://www.cnblogs.com/yidaixiaohui/archive/2018/08/18/8447170.html
-Advertisement-
Play Games

使用html+css+js實現彈球游戲 效果圖: 代碼如下,複製即可使用: 如果您有更好的方法或更多的功能,可以和我們大家一起分享哦,如有錯誤,歡迎聯繫我改正,非常感謝!!! ...


 使用html+css+js實現彈球游戲

 效果圖:

 代碼如下,複製即可使用:

<!doctype html>
<head>
    <style type="text/css">
        .panel{
            position: relative;
            z-index: 0;
            top:0px;
            left: 400px;
            width: 300px;
            height: 500px;
        }
        .console{
            position: absolute;
            z-index: 1;
            top:0;
            left:0;
            width:100%;
            height: 40px;
            background-color: #bbb;
        }
        .message{
            position: absolute;
            z-index: 1;
            top:40px;
            left:0;
            width:100%;
            height: 460px;
            color: white;
            font-size: 50px;
            text-align: center;
            line-height: 460px;
            background-color: #999;
        }
        .start,.score,.pause{
            position: absolute;
            z-index: 2;
            top: 0;
            width: 100px;
            height: 100%;
            font-size: large;
            color: white;
            text-align: center;
            line-height: 40px;
            background: -webkit-linear-gradient(top,#4ca8ff,yellow);
        }
        .start{
            left: 0px;
        }
        .score{
            left:100px;
            background-color: red;
        }
        .pause{
            left:200px;
        }
        .start:after,.pause:before{
            content: "";
            position: absolute;
            z-index: 2;
            top: 0;
            width: 3px;
            height: 100%;
            background: -webkit-linear-gradient(top,#666,#999);
        }
        .start:after{
            left: 97px;
        }
        .pause:before{
            left: 0px;
        }
        .start:hover,.pause:hover{
            cursor: pointer;
            background: -webkit-linear-gradient(top,#4ca8ff,red);
        }
        .panel span{
            position: absolute;
            z-index: 0;
            top:50%;
            left: 50%;
            font-size: 50px;
            color: blue;
        }
        .ball,.secondBall{
            position:absolute;
            z-index: 2;
            border-radius:50%;
            width: 20px;
            height: 20px;
        }
        .ball{
            top: 460px;
            left:140px;
            background-color: red;
        }
        .secondBall{
            top: 40px;
            left:140px;
            background-color: red;
        }
        .plate{
            position: absolute;
            top:480px;
            left: 100px;
            z-index: 2;
            width: 100px;
            height: 20px;
            background-color: #e5e5e5;
        }
        .promte{
            margin-top: 20px;
            text-align: center;
        }
    </style>
</head>
<body>
    <div id="panel" class="panel">
        <div class="console">
            <div id="start" class="start">開始</div>
            <div id="score" class="score">0</div>
            <div id="pause" class="pause">暫停</div>
        </div>
        <div id="message" class="message"></div>
        <div id="ball" class="ball"></div>
        <div id="plate" class="plate"></div>
    </div>
    <div class="promte">提示:鍵盤左右箭頭控制滑板</div>
    <script     type="text/javascript">
        (function(){
            document.onkeydown = function(e){
                var e = e || window.event;
                if(e.keyCode == 37){
                    //鍵盤向左鍵
                    plateMove("left");
                }else if(e.keyCode == 39){
                    //鍵盤向右鍵
                    plateMove("right");
                }
            }
        })();
        var panel = document.getElementById("panel"),
            message = document.getElementById("message"),
            plate = document.getElementById("plate"),
            ball = document.getElementById("ball"),
            start = document.getElementById("start"),
            score = document.getElementById("score"),
            pause = document.getElementById("pause"),
            secondBall;
        var startGame, x = x2 = -1, y = y2 = -1, speed = 1, positionArr = [], pauseActive = false,
            //一個標誌:表示難度是否還能增加
            flag = true,
            //球的起始位置
            ballX, ballY, secondBallX, secondBallY,
            //邊界
            minX = 0,
            maxX = panel.offsetWidth - ball.offsetWidth,
            minY = 40;
            maxY = panel.offsetHeight - ball.offsetHeight - plate.offsetHeight;
        window.onload = function(){
            if(window.addEventListener){
                start.addEventListener("click",startClick,false);
                pause.addEventListener("click",pauseClick,false);
            }else if(window.attachEvent){
                start.attachEvent("onclik",startClick);
                pause.attachEvent("onclik",pauseClick);
            }else{
                start.onclik = startClick;
                pause.onclik = pauseClick;
            }
        }
        
        function plateMove(direction){
            if(direction == "left"){
                if(plate.offsetLeft > 0){
                    plate.style.left = (plate.offsetLeft-30 < 0? 0 : plate.offsetLeft-30)+"px";
                }
            }
            if(direction == "right"){
                if(plate.offsetLeft < 200){
                    plate.style.left = (plate.offsetLeft+30 > 200? 200 : plate.offsetLeft+30)+"px";
                }
            }
        }
        function startClick(){
            if(!pauseActive){
                resetGame();
            }else{
                pauseActive = !pauseActive;
            }
            startGame = setInterval(function(){
                //console.log(ballX+"======"+ballY);
                positionArr = setPosition(ballX,ballY,true);
                if(positionArr == "GAMEOVER"){
                    return;
                }
                ballX = positionArr[0];
                ballY = positionArr[1];
                //設置球的位置
                ball.style.left = ballX+"px";
                ball.style.top = ballY+"px";
                if(!flag){
                    positionArr = setPosition(secondBallX,secondBallY,false);
                    secondBallX = positionArr[0];
                    secondBallY = positionArr[1];
                    secondBall.style.left = secondBallX+"px";
                    secondBall.style.top = secondBallY+"px";
                }else{
                    addDifficulty();
                }
            },30);
        }
        function pauseClick(){
            pauseActive = true;
            clearInterval(startGame);
        }

        function resetGame(){
            clearInterval(startGame);
            message.innerHTML="";
            score.innerHTML="0";
            ball.style.left = "140px";
            ball.style.top = "460px";
            plate.style.left = "100px";
            plate.style.top = "480px";
            ballX = ball.offsetLeft;
            ballY = ball.offsetTop;
            speed = 1;
            flag = true;
            //第二個球設置隱藏
            if(secondBall){
                secondBall.style.display="none";
                secondBall.style.left = "140px";
                secondBall.style.top = "40px";
            }
        }

        function addDifficulty(){
            if(parseInt(score.innerHTML) > 500 && parseInt(score.innerHTML) < 2000){
                speed = 1.2;
            }else if(parseInt(score.innerHTML) > 2000 && parseInt(score.innerHTML) < 5000){
                speed = 1.5;
            }else if(parseInt(score.innerHTML) > 5000){
                if(typeof secondBall != "undefined"){
                    secondBall.style.display="";
                }else{
                    secondBall = document.createElement('div');
                }
                
                secondBall.className = 'secondBall';
                panel.appendChild(secondBall);
                secondBallX = secondBall.offsetLeft;
                secondBallY = secondBall.offsetTop;
                flag = false;
            }
        }
        function setPosition(_x,_y,firstball){
            if(_x == minX || _x == maxX){
                //x*=-1;
                firstball? x*=-1 : x2*=-1;
            }
            if(_y == minY || _y == maxY){
                //y*=-1;
                firstball? y*=-1 : y2*=-1;
            }
            if(_y 	   

您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 總結一下近期學習的大數據知識, 學習之前沒搞清楚的知識 傳統的web應用(LAMP、JavaEE、NODE系等)與大數據什麼關係? 之前一直以為大數據的東西就是來取代傳統的Web應用的,其實並不是這樣;即使是大數據的架構,應用層依然會是傳統的web應用,但是會根據數據特點對數據存儲(結構化數據依然會 ...
  • 註:此文章所寫內容完全在虛擬機配置,系統:centos,jdk和hadoop已經安裝完成所配集群包括hadoop01,hadoop02,hadoop03,hadoop04四台,ip分別為:192.168.80.101,192.168.80.102,192.168.80.103,192.168.80. ...
  • redis五大數據類型:string(字元串),hash(哈希,類似java的Map),list(列表),set(集合),zset(有序集合) 1、redis的鍵(key) keys 查詢資料庫中的key值,keys * 查出所有,keys rt* 查出匹配的key exists key 判斷key ...
  • weight詳解 是用來等比例劃分區域的屬性。 案例代碼 為什麼出現2:1的案例呢?three怎麼就不見了呢? 1. 每個寬度為 ,屏幕為1,那麼屏幕就是1 3= 2個 2. 計算方法, ,結果是one占了兩份,two占了一份,three什麼都沒有 ,`padding`詳解 代表是偏移,表示組件離容 ...
  • UI的描述 對於 應用程式中,所有用戶界面元素都是由 和`ViewGroup View ViewGroup View ViewGroup`對象的佈局容器! 為我們提供了 和`ViewGroup`的兩個子類的集合,提供常用的一些輸入控制項(比如按鈕,圖片和文本域等)和各種各樣的佈局模式(比如線程佈局,相 ...
  • 前言 lodash受歡迎的一個原因,是其優異的計算性能。而其性能能有這麼突出的表現,很大部分就來源於其使用的演算法——惰性求值。 本文將講述lodash源碼中,惰性求值的原理和實現。 一、惰性求值的原理分析 惰性求值(Lazy Evaluation),又譯為惰性計算、懶惰求值,也稱為傳需求調用(cal ...
  • 五層網路模型 簡介 互聯網的實現,依托於網路協議。網路協議又分為好幾層,關於如何分層有過很多爭論,比較受人認可的有五層模型、七層模型、四層模型。今天我們就來講講五層網路模型。 從名字就可以看出來,五層網路模型將網路協議分為五層,每層都有對應的一些網路協議。從上到下分別是: 應用層 傳輸層 網路層 數 ...
  • 1 2 3 4 5 6 wangEditor上傳圖片到伺服器 7 8 9 10 11 12 13 14 15 16 119 120 121 ...
一周排行
    -Advertisement-
    Play Games
  • 前言 本文介紹一款使用 C# 與 WPF 開發的音頻播放器,其界面簡潔大方,操作體驗流暢。該播放器支持多種音頻格式(如 MP4、WMA、OGG、FLAC 等),並具備標記、實時歌詞顯示等功能。 另外,還支持換膚及多語言(中英文)切換。核心音頻處理採用 FFmpeg 組件,獲得了廣泛認可,目前 Git ...
  • OAuth2.0授權驗證-gitee授權碼模式 本文主要介紹如何筆者自己是如何使用gitee提供的OAuth2.0協議完成授權驗證並登錄到自己的系統,完整模式如圖 1、創建應用 打開gitee個人中心->第三方應用->創建應用 創建應用後在我的應用界面,查看已創建應用的Client ID和Clien ...
  • 解決了這個問題:《winForm下,fastReport.net 從.net framework 升級到.net5遇到的錯誤“Operation is not supported on this platform.”》 本文內容轉載自:https://www.fcnsoft.com/Home/Sho ...
  • 國內文章 WPF 從裸 Win 32 的 WM_Pointer 消息獲取觸摸點繪製筆跡 https://www.cnblogs.com/lindexi/p/18390983 本文將告訴大家如何在 WPF 裡面,接收裸 Win 32 的 WM_Pointer 消息,從消息裡面獲取觸摸點信息,使用觸摸點 ...
  • 前言 給大家推薦一個專為新零售快消行業打造了一套高效的進銷存管理系統。 系統不僅具備強大的庫存管理功能,還集成了高性能的輕量級 POS 解決方案,確保頁面載入速度極快,提供良好的用戶體驗。 項目介紹 Dorisoy.POS 是一款基於 .NET 7 和 Angular 4 開發的新零售快消進銷存管理 ...
  • ABP CLI常用的代碼分享 一、確保環境配置正確 安裝.NET CLI: ABP CLI是基於.NET Core或.NET 5/6/7等更高版本構建的,因此首先需要在你的開發環境中安裝.NET CLI。這可以通過訪問Microsoft官網下載並安裝相應版本的.NET SDK來實現。 安裝ABP ...
  • 問題 問題是這樣的:第三方的webapi,需要先調用登陸介面獲取Cookie,訪問其它介面時攜帶Cookie信息。 但使用HttpClient類調用登陸介面,返回的Headers中沒有找到Cookie信息。 分析 首先,使用Postman測試該登陸介面,正常返回Cookie信息,說明是HttpCli ...
  • 國內文章 關於.NET在中國為什麼工資低的分析 https://www.cnblogs.com/thinkingmore/p/18406244 .NET在中國開發者的薪資偏低,主要因市場需求、技術棧選擇和企業文化等因素所致。歷史上,.NET曾因微軟的閉源策略發展受限,儘管後來推出了跨平臺的.NET ...
  • 在WPF開發應用中,動畫不僅可以引起用戶的註意與興趣,而且還使軟體更加便於使用。前面幾篇文章講解了畫筆(Brush),形狀(Shape),幾何圖形(Geometry),變換(Transform)等相關內容,今天繼續講解動畫相關內容和知識點,僅供學習分享使用,如有不足之處,還請指正。 ...
  • 什麼是委托? 委托可以說是把一個方法代入另一個方法執行,相當於指向函數的指針;事件就相當於保存委托的數組; 1.實例化委托的方式: 方式1:通過new創建實例: public delegate void ShowDelegate(); 或者 public delegate string ShowDe ...