jQuery小游戏

打印 上一主题 下一主题

主题 1043|帖子 1043|积分 3129

jQuery小游戏(一)

嘻嘻,本日我们来写个jquery小游戏吧


  • 起首,我们预备一下写小游戏需要预备的佩饰,如果:图片、音乐、搞怪的小心情
   这里我预备了一些游戏中需要涉及到的图片

  

  • 游戏中使用到的方法


  • eval() 函数:用于计算传入字符串形式的 JavaScript 代码并执行它。当调用时不带任何对象前缀时,默认是在全局作用域下运行;而通过 window.eval() 显式指定,则可以在当前窗口上下文中评估表达式
  • window.requestAnimationFrame:这里夸大一下为什么使用这个办法,其实setInterval也有定时的作用,但是不同之处在于setInterval需指定时间再执行,window.requestAnimationFrame()则推迟到浏览器下一次重绘时就执行回调。重绘通常是 16ms 执行一次,不过浏览器会主动调治这个速率,比如网页切换到后台 Tab 页时,requestAnimationFrame()会暂停执行。需要留意的是:若是某个函数会影响到网页的布局调整,一般就放在window.requestAnimationFrame()内里执行。 取消操作使用cancelAnimationFrame()方法,下面代码为详细实操:
  1. //requestAnimationFrame效果
  2.     (function zhouqi(time) {
  3.        console.log(time,Date.now())
  4.        render();
  5.        zhou = requestAnimationFrame(zhouqi);
  6.        //如果left等于50 停止动画
  7.        if(left == 50){
  8.            cancelAnimationFrame(zhou)
  9.        }
  10.    })
复制代码


  • window.cancelAnimationFrame():用于取消先前通过的 window.requestAnimationFrame()方法请求的动画帧回调函数的方法,上面代码也有提及到,在使用 window.requestAnimationFrame()方法创建动画时,通常会把返回的ID存储起来,以便在需要时使用window.cancelAnimationFrame()方法取消动画
  • String.format : Java 中用于格式化字符串的一个方法, String.format 允许你通过占位符来创建一个格式化的字符串。使用 String.format 可以将各种类型的数据(如整数、小数、字符串等)以指定的格式组合成一个字符串
  1. var link, jsGame; (function() {
  2.         var e = window.eval,
  3.                 t = function(e, t, n) {
  4.                         var r = n || {};
  5.                         if (t) {
  6.                                 var i = function() {};
  7.                                 i.prototype = t.prototype,
  8.                                         e.prototype = new i,
  9.                                         e.prototype.constructor = e,
  10.                                         e.prototype.superClass = t.prototype,//javascript的superclass继承
  11.                                         i = null
  12.                         }
  13.                         for (var s in r) e.prototype[s] = r[s];
  14.                         return r = null, e};
  15.             window.requestAnimationFrame = function() {
  16.                 return window.requestAnimationFrame || window.webkitRequestAnimationFrame ||   
  17.                         window.mozRequestAnimationFrame || window.oRequestAnimationFrame ||
  18.                         window.msRequestAnimationFrame || window.setTimeout} (),//这段代码是用来兼容不同浏览器的requestAnimationFrame 方法,requestAnimationFrame 用于在下一次浏览器重绘之前执行指定的函数,以实现流畅的动画效果。首先会尝试获取浏览器原生的requestAnimationFrame 方法,如果浏览器不支持,则依次尝试获取其他浏览器的前缀版本,若依旧不支持,则使用setTimeout模拟实现,这样可以确认在不同情况下都能正常运行requestAnimationFrame 方法
  19.                     window.cancelAnimationFrame = function() {//同上面解释类似
  20.                         return window.cancelAnimationFrame || window.webkitCancelAnimationFrame ||
  21.                                 window.mozCancelAnimationFrame || window.oCancelAnimationFrame ||
  22.                                 window.msCancelAnimationFrame || window.clearTimeout} (),
  23.         String || (String = {}),
  24.             String.format || (String.format = function() {
  25.                 if (arguments.length == 0) return null;//当参数个数为0即空数组         //arguments类数组对象
  26.                 var e = arguments[0] || "", t;
  27.                 for (var n = 1, r = arguments.length; n < r; n++) t = new RegExp("\\{" + (n - 1)+"\\}","gm"),
  28.                         e = e.replace(t, arguments[n]);
  29.                  return t = null, e
  30.         }),
  31.           
复制代码


  • Array.prototype : 所有的数组实例都继续于 Array.prototype,和其他构造函数一样,可以通过Array和prototype属性上的方法来给所有数组实例增加方法
  1. String.getByteLength || (String.getByteLength = function(e) {
  2.                 var t, n = 0, r = e || "", i = r.length;
  3.                 for (t = 0; t < i; t++) r.charCodeAt(t) >= 0 & r.charCodeAt(t) <= 255 ? n += 1 : n += 2;
  4.                    return t = r = i = null, n});
  5.             if (!Array || !Array.prototype) Array.prototype = {};
  6.             Array.prototype.indexOfAttr = function(e, t) {
  7.                 var n = (typeof e).toLowerCase(), r = -1;
  8.                 for (var i = 0, s = this.length; i < s; i++) if (n == "string" && this[i][e] == t || n =="number"
  9.                         && this[i] == e) {
  10.                         r = i;
  11.                         break
  12.                 }
复制代码


  • canvas: HTML5的新增元素
  1. <canvas id="zhouqi"></canvas> // 如果没有设置宽高,canvas会自动创建一个 300 * 150 的画布(单位默认为px)
  2. canvas.width = 300    // 设置画布宽度
  3. canvas.height = 300   // 设置画布高度
  4. const canvas = document.getElementById('zhouqi')  // 获取画布节点
  5. const ctx = canvas.getContext('2D') // 获取 2D 绘图上下文对象
复制代码
  1. return n = null, r};
  2.            var n = {
  3.                         canvas: {
  4.                                 id: "linkScreen",
  5.                                 defaultId: "linkScreen",
  6.                                 defaultFont: "12px Arial",
  7.                                 defaultWidth: 240,
  8.                                 defaultHeight: 320,
  9.                                 defaultColor: "rgb(0, 0, 0)",
  10.                                 bgColor: "#000",
  11.                                 cavansDoms: [],
  12.                                 ctxs: [],
  13.                                 device: "",
  14.                                 fps: 1,
  15.                                 touch: !1,
  16.                                 zoom: 1
  17.                         },
  18.                         system: {
  19.                                 loadRes: null,
  20.                                 pageLoad: null,
  21.                                 menu: null,
  22.                                 run: null,
  23.                                 runFn: function() {},
  24.                                 rafRun: null,
  25.                                 stop: null,
  26.                                 over: null,
  27.                                 zone: null,
  28.                                 active: null,
  29.                                 lastDate: Date.now(),
  30.                                 timeout: 30,
  31.                                 isPause: !1,
  32.                                 gameFlow: -1,
  33.                                 loadedImageToGameFlow: -1,
  34.                                 zoneArgs: null,
  35.                                 activeArgs: null,
  36.                                 spendTime: 0,
  37.                                 loadResTimer: null,
  38.                                 playTimer: null
  39.                         },
  40.                         event: {
  41.                                 key: 0,
  42.                                 keys: {
  43.                                         up: !1,
  44.                                         down: !1,
  45.                                         left: !1,
  46.                                         right: !1,
  47.                                         a: !1,
  48.                                         b: !1,
  49.                                         c: !1,
  50.                                         menu: !1,
  51.                                         quit: !1
  52.                                 },
  53.                                 lastKey: {
  54.                                         up: !1,
  55.                                         down: !1,
  56.                                         left: !1,
  57.                                         right: !1,
  58.                                         a: !1,
  59.                                         b: !1,
  60.                                         c: !1,
  61.                                         menu: !1,
  62.                                         quit: !1
  63.                                 },
  64.                                 pressedKey: {
  65.                                         up: !1,
  66.                                         down: !1,
  67.                                         left: !1,
  68.                                         right: !1,
  69.                                         a: !1,
  70.                                         b: !1,
  71.                                         c: !1,
  72.                                         menu: !1,
  73.                                         quit: !1
  74.                                 },
  75.                                 keyPressCtrl: {
  76.                                         up: !0,
  77.                                         down: !0,
  78.                                         left: !0,
  79.                                         right: !0,
  80.                                         a: !0,
  81.                                         b: !0,
  82.                                         c: !0,
  83.                                         menu: !0,
  84.                                         quit: !0
  85.                                 },
  86.                                 keyDownGo: !1,
  87.                                 keyUpGo: !1,
  88.                                 keyPressedGo: !1,
  89.                                 keyDownCallBack: null,
  90.                                 keyUpCallBack: null,
  91.                                 orientationChange: null,
  92.                                 touchStart: null,
  93.                                 touchEnd: null,
  94.                                 touchMove: null,
  95.                                 touchCancel: null,
  96.                                 clickCallBack: null,
  97.                                 mouseDownCallBack: null,
  98.                                 mouseUpCallBack: null,
  99.                                 mouseMoveCallBack: null,
  100.                                 focused: !1,
  101.                                 pageFocusCallBack: null,
  102.                                 pageUnFocusCallBack: null,
  103.                                 swipeCallBack: null,
  104.                                 pageOffX: 0,
  105.                                 pageOffY: 0,
  106.                                 pageStarOffX: 0,
  107.                                 pageStarOffY: 0,
  108.                                 swipeDate: null,
  109.                                 swipeTimeout: 200,
  110.                                 swipeRange: 50
  111.                         },
  112.                         image: {
  113.                                 imgs: {},
  114.                                 imgObjs: [],
  115.                                 initImgs: {},
  116.                                 asyncImgObjs: {},
  117.                                 imgCount: 0,
  118.                                 countLoaded: 0,
  119.                                 version: "",
  120.                                 inited: !1
  121.                         },
  122.                         audio: {
  123.                                 audios: {}
  124.                         },
  125.                         ajax: {
  126.                                 xhrObj: null,
  127.                                 pool: [],
  128.                                 poolLength: 5,
  129.                                 date: null,
  130.                                 isTimeout: !1,
  131.                                 param: {
  132.                                         type: "get",
  133.                                         data: null,
  134.                                         dataType: "json",
  135.                                         url: "",
  136.                                         xhr: null,
  137.                                         timeout: 5e3,
  138.                                         before: function(e) {},
  139.                                         success: function(e, t) {},
  140.                                         error: function(e, t) {},
  141.                                         complete: function(e) {}
  142.                                 }
  143.                         },
  144.                 }
复制代码


  • fillStyle: 用于图形内部的颜色
  • fillRect: 用于绘制一个填充的矩形
  • strokeStyle: 用于设置图形表面的颜色
  • drawImage: 用于绘制图像
  1. request: {
  2.                                 gets: []
  3.                         },
  4.                         buttonLayout: {
  5.                                 buttons: [],
  6.                                 Button: t(function(e) {
  7.                                                 this.id = e.id,
  8.                                                         this.value = e.value,
  9.                                                         this.x = e.x,
  10.                                                         this.y = e.y,
  11.                                                         this.width = e.width,
  12.                                                         this.height = e.height,
  13.                                                         this.bgColor = e.bgColor,
  14.                                                         this.bgStroke = e.bgStroke,
  15.                                                         this.stroke = e.stroke,
  16.                                                         this.font = e.font,
  17.                                                         this.imageId = e.imageId,
  18.                                                         this.sx = e.sx,
  19.                                                         this.sy = e.sy,
  20.                                                         this.color = e.color,
  21.                                                         this.hx = e.hx,
  22.                                                         this.hy = e.hy,
  23.                                                         this.hColor = e.hColor,
  24.                                                         this.dex = e.dex,
  25.                                                         this.dey = e.dey,
  26.                                                         this.deColor = e.deColor,
  27.                                                         this.hided = e.hided,
  28.                                                         this.disabled = e.disabled,
  29.                                                         this.path = e.path,
  30.                                                         this.hovered = !1,
  31.                                                         this.repeated = !1,
  32.                                                         this.pressed = !1,
  33.                                                         this.released = !1,
  34.                                                         this.goned = !1,
  35.                                                         this.cacheId = "buttonLayoutCache_" + this.id,
  36.                                                         this.setDelay(e.delay).refresh()
  37.                                         },
  38.                                         null, {
  39.                                                 refresh: function() {
  40.                                                         m.canvas.pass(this.cacheId, this.width * 3, this.height),
  41.                                                                 this.imageId == "" ? (this.bgColor != "" && m.canvas.fillStyle(this.bgColor).fillRect(0, 0, this.width, this.height).fillRect(this.width, 0, this.width, this.height).fillRect(this.width * 2, 0, this.width, this.height), this.bgStroke != "" && m.canvas.strokeStyle(this.bgStroke).strokeRect(1, 1, this.width - 2, this.height - 2).strokeRect(this.width + 1, 1, this.width - 2, this.height - 2).strokeRect(this.width * 2 + 1, 1, this.width - 2, this.height - 2)) : m.canvas.drawImage(this.imageId, this.sx, this.sy, this.width, this.height, 0, 0, this.width, this.height).drawImage(this.imageId, this.hx, this.hy, this.width, this.height, this.width, 0, this.width, this.height).drawImage(this.imageId, this.dex, this.dey, this.width * 2, this.height, this.width * 2, 0, this.width, this.height);
  42.                                                         if (this.value != "") {
  43.                                                                 var e = m.canvas.font(this.font).measureText(this.value),
  44.                                                                         t = this.width - e.width >> 1,
  45.                                                                         n = (this.height - e.height >> 1) + parseInt(this.font) - 2;
  46.                                                                 this.stroke != "" && m.canvas.fillStyle(this.stroke).fillText(this.value, t - 1, n).fillText(this.value, t, n - 1).fillText(this.value, t + 1, n).fillText(this.value, t, n + 1).fillText(this.value, t + this.width - 1, n).fillText(this.value, t + this.width, n - 1).fillText(this.value, t + this.width + 1, n).fillText(this.value, t + this.width, n + 1).fillText(this.value, t + this.width * 2 - 1, n).fillText(this.value, t + this.width * 2, n - 1).fillText(this.value, t + this.width * 2 + 1, n).fillText(this.value, t + this.width * 2, n + 1),
  47.                                                                         m.canvas.fillStyle(this.color).fillText(this.value, t, n).fillStyle(this.hColor).fillText(this.value, t + this.width, n).fillStyle(this.deColor).fillText(this.value, t + this.width * 2, n),
  48.                                                                         e = t = n = null
  49.                                                         }
  50.                                                         return m.canvas.pass(),
  51.                                                                 this
  52.                                                 },
  53.                                                 show: function() {
  54.                                                         return this.hided = !1,
  55.                                                                 this
  56.                                                 },
  57.                                                 hide: function() {
  58.                                                         return this.hided = !0,
  59.                                                                 this
  60.                                                 },
  61.                                                 disable: function(e) {
  62.                                                         return this.disabled = e,
  63.                                                                 this
  64.                                                 },
  65.                                                 setPath: function(e, t) {
  66.                                                         return this.setDelay(t).path = e || [],
  67.                                                                 this
  68.                                                 },
  69.                                                 endPath: function() {
  70.                                                         return this.path.length == 0
  71.                                                 },
  72.                                                 gone: function(e, t) {
  73.                                                         return this.setPath(e, t).goned = !0,
  74.                                                                 this
  75.                                                 },
  76.                                                 setDelay: function(e) {
  77.                                                         return this.delay = e || 0,
  78.                                                                 this.delayDate = null,
  79.                                                         this.delay > 0 && (this.delayDate = Date.now()),
  80.                                                                 this
  81.                                                 },
  82.                                                 action: function() {
  83.                                                         if (this.hided) return this;
  84.                                                         this.delayDate && Date.now() - this.delayDate >= this.delay && (this.delayDate = null);
  85.                                                         if (!this.delayDate && this.path.length > 0) {
  86.                                                                 var e = this.path.shift();
  87.                                                                 this.x += e[0],
  88.                                                                         this.y += e[1],
  89.                                                                         e = null
  90.                                                         }
  91.                                                         return this
  92.                                                 },
  93.                                                 render: function() {
  94.                                                         return this.hided ? this: (m.canvas.drawCache(this.cacheId, this.hovered ? this.width: this.disabled ? this.width * 2 : 0, 0, this.width, this.height, this.x, this.y, this.width, this.height), this)
  95.                                                 },
  96.                                                 disposed: function() {
  97.                                                         return this
  98.                                                 }
  99.                                         })
  100.                         }
  101.                 },
复制代码
  1. r = {
  2.                         canvas: {
  3.                                 context: {
  4.                                         base: 0
  5.                                 },
  6.                                 graphics: {
  7.                                         HCENTER: 1,
  8.                                         VCENTER: 2,
  9.                                         LEFT: 4,
  10.                                         RIGHT: 8,
  11.                                         TOP: 16,
  12.                                         BOTTOM: 32,
  13.                                         ANCHOR_LT: 20,
  14.                                         ANCHOR_LV: 6,
  15.                                         ANCHOR_LB: 36,
  16.                                         ANCHOR_HT: 17,
  17.                                         ANCHOR_HV: 3,
  18.                                         ANCHOR_HB: 33,
  19.                                         ANCHOR_RT: 24,
  20.                                         ANCHOR_RV: 10,
  21.                                         ANCHOR_RB: 40
  22.                                 },
  23.                                 trans: {
  24.                                         TRANS_MIRROR: 2,
  25.                                         TRANS_NONE: 0,
  26.                                         TRANS_ROT90: 5,
  27.                                         TRANS_ROT180: 3,
  28.                                         TRANS_ROT270: 6,
  29.                                         TRANS_MIRROR_ROT90: 7,
  30.                                         TRANS_MIRROR_ROT180: 1,
  31.                                         TRANS_MIRROR_ROT270: 4
  32.                                 }
  33.                         },
  34.                         event: {
  35.                                 key: {
  36.                                         up: 38,
  37.                                         down: 40,
  38.                                         left: 37,
  39.                                         right: 39,
  40.                                         a: 90,
  41.                                         b: 88,
  42.                                         c: 67,
  43.                                         menu: 49,
  44.                                         quit: 50
  45.                                 }
  46.                         },
  47.                         system: {
  48.                                 gameFlowType: {
  49.                                         menu: 0,
  50.                                         run: 1,
  51.                                         stop: 2,
  52.                                         over: 3,
  53.                                         zone: 4,
  54.                                         active: 5,
  55.                                         loadImage: 6,
  56.                                         loadedImage: 7
  57.                                 }
  58.                         }
  59.                 },
复制代码


  • getOffsetX:指 offsetX 属性,是一个只读属性,表示触发事件的位置相对于目标元素内边距边缘 (padding edge) 的 X 坐标位置。如果事件发生在子元素上,则计算的是相对于最近的有定位(position 不为 static)祖先元素的偏移量;该属性属于鼠标事件对象的一部分
  • changedTouches:说到changedTouches,那需要提及的就是移动端滑屏touch事件,刚开始触摸事件touchstart、touchmove和touchend是苹果操作系统Safari浏览器新添加的事件;重要是IOS大多设备没有鼠标及键盘,以是在移动Safari浏览器开发交互性网页时,PC端的鼠标及键盘事件是远远不够的;接下来看看代码中应用到的touch事件吧
   

  • touches:当前位于屏幕上的所有手指触摸点的一个列表
  • targetTouches:当前元素对象上所有触摸点的列表
  • changedTouches:涉及当前事件的触摸点的列表
    每个触摸点对应的Touch都有三对紧张的属性:(clientX/clientY)、(pageX/pageY)、(screenX/screenY)
    e.changedTouches.length表示获取手指的个数
    e.changedTouches[0].pageX表示获取坐标
  1. i = {
  2.                         getCanvasDom: function() {
  3.                                 var e;
  4.                                 return function() {
  5.                                         return e || (e = m.getDom(n.canvas.defaultId)),
  6.                                                 e
  7.                                 }
  8.                         } (),
  9.                         getOffsetX: function(e) {
  10.                                 return e.offsetX || (e.changedTouches && e.changedTouches[0] ? e.changedTouches[0].clientX - i.getCanvasDom().offsetLeft: e.clientX - i.getCanvasDom().offsetLeft) || 0
  11.                         },
  12.                         getOffsetY: function(e) {
  13.                                 return e.offsetY || (e.changedTouches && e.changedTouches[0] ? e.changedTouches[0].clientY - i.getCanvasDom().offsetTop: e.clientY - i.getCanvasDom().offsetTop) || 0
  14.                         },
复制代码
本日的内容有些多,我们先消化这些吧,后面的下一次继续

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

麻花痒

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表