亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

原生JS面向?qū)ο髮?shí)現(xiàn)打磚塊小游戲

 更新時(shí)間:2022年09月01日 16:39:04   作者:weixin_44447804  
這篇文章主要為大家詳細(xì)介紹了原生JS面向?qū)ο髮?shí)現(xiàn)打磚塊小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了JS實(shí)現(xiàn)打磚塊小游戲的具體代碼,供大家參考,具體內(nèi)容如下

通過面向?qū)ο?通過修改對(duì)JS的調(diào)用次數(shù)可直接設(shè)置打磚塊游戲的個(gè)數(shù)

小球的反彈速度以及反彈方向都設(shè)置了隨機(jī)值,
當(dāng)小球與磚塊發(fā)生碰撞時(shí),會(huì)使磚塊display屬性設(shè)置為none,從而達(dá)到消失的效果.

HTML代碼:

<!DOCTYPE html>
<html lang="en">
<head>
? ? <meta charset="UTF-8">
? ? <meta name="viewport" content="width=device-width, initial-scale=1.0">
? ? <title>Document</title>
? ? <style>
? ? ? ? *{
? ? ? ? ? ? margin: 0;
? ? ? ? ? ? padding: 0;
? ? ? ? }
? ? </style>
</head>
<body>
? ? <script type="module">
? ? ? ? import Brick from "./js/Brick.js";
? ? ? ? //可以通過循環(huán)創(chuàng)建多個(gè)打磚塊游戲模塊
? ? ? ? var brick = new Brick();
? ? ? ? brick.appendTo("body");
? ? </script>
</body>
</html>

JS代碼1:

export default class Component extends EventTarget{
? ? elem;
? ? constructor(){
? ? ? ? super();
? ? ? ? this.elem=this.createElem();
? ? }
? ? createElem(){
? ? ? ? if(this.elem) return this.elem;
? ? ? ? let div=document.createElement("div");
? ? ? ? return div;
? ? }
? ? appendTo(parent){
? ? ? ? if(typeof parent==="string")parent=document.querySelector(parent);
? ? ? ? parent.appendChild(this.elem);
? ? }
}

JS代碼2:

import Component from "./Component.js";

export default class Brick extends Component{
? ? box;
? ? ul;
? ? li;
? ? fra;//碎片容器
? ? bubble;//球
? ? board;//木板
? ? start;
? ? lis=[];
? ? boxWidth=680;
? ? liWidth=66;
? ? liHeight=15;
? ? fy=-1;
? ? fx=1;
? ? static NUM=130;
? ? static ZHENG=1;
? ? static FU=-1;
? ? constructor(){
? ? ? ? super();
? ? ? ? Object.assign(this.elem.style,{
? ? ? ? ? ? width:"800px",
? ? ? ? ? ? height:"500px",
? ? ? ? ? ? border:"orange solid 1px",
? ? ? ? ? ? margin:"40px 0 0 200px",
? ? ? ? ? ? background:"url('./img/2.jpg')",
? ? ? ? ? ? // backgroundSize:"contain",
? ? ? ? });
? ? ? ? this.creatCon();
? ? ? ? // this.sortLi();
? ? ? ? this.creatButton();
? ? ? ? this.creatBoardAndBubble();
? ? ? ? document.body.appendChild(this.elem);
? ? ? ? this.sortLi();
? ? ? ? // this.move();
? ? ? ? this.start.addEventListener("click",e=>this.clickHandler(e))
? ? ? ? // document.addEventListener("keydown",e=>this.keyHandler(e));
? ? }
? ? //游戲區(qū)域box盒子
? ? creatCon(){
? ? ? ? this.box=document.createElement("div");
? ? ? ? Object.assign(this.box.style,{
? ? ? ? ? ? width:"680px",
? ? ? ? ? ? fontSize:"55px",
? ? ? ? ? ? textAlign:"center",
? ? ? ? ? ? lineHeight:"400px",
? ? ? ? ? ? height:"400px",
? ? ? ? ? ? position:"relative",
? ? ? ? ? ? border:"orangered solid 1px",
? ? ? ? ? ? margin:"20px 60px",
? ? ? ? ? ? // background:"url('./img/0.jpg') ",
? ? ? ? ? ? // backgroundSize:"contain",
? ? ? ? })
? ? ? ? this.creatUl();
? ? ? ? this.creatLi();
? ? ? ? this.elem.appendChild(this.box);
? ? }
? ? creatUl(){
? ? ? ? this.ul=document.createElement("ul");
? ? ? ? Object.assign(this.ul.style,{
? ? ? ? ? ? listStyle:"none",
? ? ? ? })
? ? }
? ? //創(chuàng)建所有l(wèi)i
? ? creatLi(){
? ? ? ? this.fra=document.createDocumentFragment("div");//碎片容器
? ? ? ? for(var i=0;i<Brick.NUM;i++){
? ? ? ? ? ? this.li=document.createElement("li");
? ? ? ? ? ? Object.assign(this.li.style,{
? ? ? ? ? ? ? ? width:"66px",
? ? ? ? ? ? ? ? height:"15px",
? ? ? ? ? ? ? ? border:"solid 1px #ccc",
? ? ? ? ? ? ? ? position:"absolute"
? ? ? ? ? ? })
? ? ? ? ? ? var r=Math.floor(Math.random()*255);
? ? ? ? ? ? var g=Math.floor(Math.random()*255);
? ? ? ? ? ? var b=Math.floor(Math.random()*255);
? ? ? ? ? ? this.li.style.backgroundColor="rgb("+r+","+g+","+b+")";
? ? ? ? ? ? this.fra.appendChild(this.li);
? ? ? ? ? ? this.lis.push(this.li)
? ? ? ? }
? ? ? ? this.ul.appendChild(this.fra);
? ? ? ? this.box.appendChild(this.ul);
? ? }
? ? //創(chuàng)建滑板和小球
? ? creatBoardAndBubble(){
? ? ? ? this.bubble=document.createElement("div");
? ? ? ? this.board=document.createElement("div");
? ? ? ? Object.assign(this.bubble.style,{
? ? ? ? ? ? width:"15px",
? ? ? ? ? ? height:"15px",
? ? ? ? ? ? backgroundColor: "red",
? ? ? ? ? ? borderRadius:"50%",
? ? ? ? ? ? position:"absolute",
? ? ? ? ? ? bottom: "10px",
? ? ? ? ? ? left:"180px",
? ? ? ? })
? ? ? ? Object.assign(this.board.style,{
? ? ? ? ? ? width:"150px",
? ? ? ? ? ? height:"10px",
? ? ? ? ? ? backgroundColor: "orange",
? ? ? ? ? ? borderRadius:"5px",
? ? ? ? ? ? position:"absolute",
? ? ? ? ? ? bottom:"0",
? ? ? ? ? ? left:"160px",
? ? ? ? })
? ? ? ? this.box.appendChild(this.bubble);
? ? ? ? this.box.appendChild(this.board);
? ? ? ? // document.addEventListener("keydown",e=>this.keyHandler(e));
? ? }
? ? //創(chuàng)建游戲開始按鈕
? ? creatButton(){
? ? ? ? this.start=document.createElement("button");
? ? ? ? Object.assign(this.start.style,{
? ? ? ? ? ? marginLeft:"50px",
? ? ? ? ? ? width:"100px",
? ? ? ? })
? ? ? ? this.start.textContent="開始游戲";
? ? ? ? this.elem.appendChild(this.start);
? ? }
? ? //擺放li
? ? sortLi(){
? ? ? ? var leftInit = 0;?
? ? ? ? var topInit = 0;?
? ? ? ? this.cols = parseInt(this.boxWidth / (this.liWidth+2));
? ? ? ? for(var i = 0 ; i < Brick.NUM ; i++){
? ? ? ? ? ? var x = leftInit * (this.liWidth+2);
? ? ? ? ? ? var y = topInit;
? ? ? ? ? ? this.lis[i].style.left = x + "px";
? ? ? ? ? ? this.lis[i].style.top = y + "px";
? ? ? ? ? ? leftInit++;
? ? ? ? ? ? //換行
? ? ? ? ? ? if ((i+1)%this.cols==0) {
? ? ? ? ? ? ? ? leftInit = 0;
? ? ? ? ? ? ? ? topInit = topInit + this.liHeight;
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ? clickHandler(e){
? ? ? ? document.addEventListener("keydown",e=>this.keyHandler(e));
? ? ? ? this.move();
? ? }
? ? keyHandler(e){
? ? ? ? ? ? //左鍵
? ? ? ? ? ? if( e.keyCode === 37 ){
? ? ? ? ? ? ? ? this.board.style.left = this.board.offsetLeft - 15 + "px";
? ? ? ? ? ? ? ? if (this.board.offsetLeft<=0) {
? ? ? ? ? ? ? ? ? ? this.board.style.left = 0;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? //右鍵
? ? ? ? ? ? if( e.keyCode === 39 ){
? ? ? ? ? ? ? ? this.board.style.left = this.board.offsetLeft + 15 + "px";
? ? ? ? ? ? ? ? if(this.board.offsetLeft>=this.box.offsetWidth-this.board.offsetWidth){
? ? ? ? ? ? ? ? ? ? this.board.style.left = this.box.offsetWidth-this.board.offsetWidth +"px";
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? }
? ? move(){
? ? ? ? var timer = setInterval(()=>{
? ? ? ? ? ? this.bubble.style.left = this.bubble.offsetLeft + this.fx + "px";
? ? ? ? ? ? this.bubble.style.top = this.bubble.offsetTop + this.fy + "px";
? ? ? ? ? ? //上邊界
? ? ? ? ? ? if(this.bubble.offsetTop<=0){
? ? ? ? ? ? ? ? this.fy = 1;
? ? ? ? ? ? }
? ? ? ? ? ? //左邊界
? ? ? ? ? ? if(this.bubble.offsetLeft<=0){
? ? ? ? ? ? ? ? this.fx = 1;
? ? ? ? ? ? }
? ? ? ? ? ? //右邊界
? ? ? ? ? ? if(this.bubble.offsetLeft>=this.box.offsetWidth-this.bubble.offsetWidth){
? ? ? ? ? ? ? ? this.fx = -1;
? ? ? ? ? ? }
? ? ? ? ? ? //小球 反彈
? ? ? ? ? ? if( this.bubble.offsetTop+this.bubble.offsetHeight >= this.board.offsetTop&&this.bubble.offsetLeft>=this.board.offsetLeft ){
? ? ? ? ? ? ? ? if(this.bubble.offsetLeft+this.bubble.offsetWidth<=this.board.offsetLeft+this.board.offsetWidth){
? ? ? ? ? ? ? ? ? ? this.fy = -1;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? if(this.bubble.offsetTop >= this.box.offsetHeight-this.bubble.offsetHeight){
? ? ? ? ? ? ? ? this.box.appendChild(document.createTextNode("GAME OVER!"));
? ? ? ? ? ? ? ? clearInterval(timer);
? ? ? ? ? ? }
? ? ? ? ? ? //小球和磚塊的碰撞 ? 以小球?yàn)榛鶞?zhǔn) ?遍歷所有磚塊
? ? ? ? ? ? for( var i =0; i < Brick.NUM ; i++){
? ? ? ? ? ? ? ? if(this.bubble.offsetTop<=this.lis[i].offsetTop+this.lis[i].offsetHeight&&this.bubble.offsetLeft>=this.lis[i].offsetLeft&&this.bubble.offsetLeft+this.bubble.offsetWidth<=this.lis[i].offsetLeft+this.lis[i].offsetWidth){
? ? ? ? ? ? ? ? ? ? ? ? ? ? // this.fy = 3;
? ? ? ? ? ? ? ? ? ? ? ? ? ? this.fx=Math.floor(Math.random()*6-3);//
? ? ? ? ? ? ? ? ? ? ? ? ? ? this.fy=Math.floor(Math.random()*5+1);
? ? ? ? ? ? ? ? ? ? ? ? ? ? console.log(this.fy)
? ? ? ? ? ? ? ? ? ? ? ? ? ? this.lis[i].style.display = "none";
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? },8);
? ? }
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • layui實(shí)現(xiàn)顯示數(shù)據(jù)表格、搜索和修改功能示例

    layui實(shí)現(xiàn)顯示數(shù)據(jù)表格、搜索和修改功能示例

    這篇文章主要介紹了layui實(shí)現(xiàn)顯示數(shù)據(jù)表格、搜索和修改功能,結(jié)合實(shí)例形式分析了layui顯示數(shù)據(jù)表格、搜索和修改功能具體界面布局、功能實(shí)現(xiàn)相關(guān)操作技巧,需要的朋友可以參考下
    2020-06-06
  • 別了 JavaScript中的isXX系列

    別了 JavaScript中的isXX系列

    我們很容易被漂亮的代碼吸引,也不知不覺的在自己的代碼庫(kù)中加入這些。卻沒有冷靜的想過它們的優(yōu)劣。這不,我就收集了一系列形如 是否為……? 的判斷的boolean函數(shù)
    2012-08-08
  • 原生js實(shí)現(xiàn)自定義滾動(dòng)條

    原生js實(shí)現(xiàn)自定義滾動(dòng)條

    這篇文章主要為大家詳細(xì)介紹了原生js實(shí)現(xiàn)自定義滾動(dòng)條,可點(diǎn)擊、拖動(dòng)到達(dá),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-01-01
  • JavaScript和JQuery獲取DIV值的方法示例

    JavaScript和JQuery獲取DIV值的方法示例

    這篇文章主要介紹了JavaScript和JQuery獲取DIV值的方法,結(jié)合具體實(shí)例形式對(duì)比分析了javascript與jQuery獲取頁(yè)面div元素值的實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2017-03-03
  • js控制href內(nèi)容的連接內(nèi)容的變化示例

    js控制href內(nèi)容的連接內(nèi)容的變化示例

    這篇文章主要介紹了使用js控制href內(nèi)容的連接內(nèi)容的變化,需要的朋友可以參考下
    2014-04-04
  • javascript 系統(tǒng)文件夾文件操作及參數(shù)介紹

    javascript 系統(tǒng)文件夾文件操作及參數(shù)介紹

    javascript文件操作包括寫入文件、當(dāng)前目錄文件、讀文件、刪除文件、批量刪除,未刪除文件夾,刪除不了當(dāng)前目錄文件等等,感興趣的朋友可以參考下
    2013-01-01
  • JS圖片預(yù)加載 JS實(shí)現(xiàn)圖片預(yù)加載應(yīng)用

    JS圖片預(yù)加載 JS實(shí)現(xiàn)圖片預(yù)加載應(yīng)用

    由于圖片加載慢,導(dǎo)致用戶體驗(yàn)特別差,本文將介紹一種圖片預(yù)加載技術(shù),需要了解的朋友可以參考下
    2012-12-12
  • JavaScript實(shí)現(xiàn)音樂自動(dòng)切換和輪播

    JavaScript實(shí)現(xiàn)音樂自動(dòng)切換和輪播

    這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)音樂自動(dòng)切換和輪播效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-11-11
  • 微信小程序?qū)崿F(xiàn)搖篩子效果

    微信小程序?qū)崿F(xiàn)搖篩子效果

    這篇文章主要為大家詳細(xì)介紹了微信小程序?qū)崿F(xiàn)搖篩子效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-05-05
  • 基于JS實(shí)現(xiàn)一個(gè)小型編譯器

    基于JS實(shí)現(xiàn)一個(gè)小型編譯器

    這篇文章主要為大家介紹了如何利用JS實(shí)現(xiàn)一個(gè)小型編譯器,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)或工作有一定的參考價(jià)值,需要的可以參考一下
    2022-04-04

最新評(píng)論