本文主要包含Html5 ,Canvas,斗地主游戏等相关知识,匿名希望在学习及工作中可以帮助到您
现在我看了html5以及canvas相关知识和斗地主的demo后,自己用demo上的素材试着写了个斗地主,代码没重构好,欢迎赐教。
话不多说,下面就一步一步解释下吧
只有一个common.js文件
1、资源类
var Resource = Class.create(); $.extend(Resource.prototype, { initialize: function () { }, Images: [ { path: 'img/bg1.png', x: 0, y: 0, w: 800, h: 480, data: null, type: 61, visible: true }, { path: 'img/BeiMian.jpg', x: 320, y: 5, w: 100, h: 121, data: null, type: 62, visible: true }, { path: 'img/btn.jpg', x: 300, y: 281, w: 140, h: 50, data: null, type: 63, visible: true, text: '开始', textX: 366, textY: 310 }, { x: 0, y: 0, type: 66, isText: true, visible: false }, { path: 'img/1.jpg', data: null, type: 16, visible: false }, { path: 'img/2.jpg', data: null, type: 17, visible: false }, { path: 'img/3.jpg', data: null, type: 3, visible: false, se: 1 }, { path: 'img/4.jpg', data: null, type: 4, visible: false, se: 1 }, { path: 'img/5.jpg', data: null, type: 5, visible: false, se: 1 }, { path: 'img/6.jpg', data: null, type: 6, visible: false, se: 1 }, { path: 'img/7.jpg', data: null, type: 7, visible: false, se: 1 }, { path: 'img/8.jpg', data: null, type: 8, visible: false, se: 1 }, { path: 'img/9.jpg', data: null, type: 9, visible: false, se: 1 }, { path: 'img/10.jpg', data: null, type: 10, visible: false, se: 1 }, { path: 'img/11.jpg', data: null, type: 11, visible: false, se: 1 }, { path: 'img/12.jpg', data: null, type: 12, visible: false, se: 1 }, { path: 'img/13.jpg', data: null, type: 13, visible: false, se: 1 }, { path: 'img/14.jpg', data: null, type: 14, visible: false, se: 1 }, { path: 'img/15.jpg', data: null, type: 15, visible: false, se: 1 }, { path: 'img/16.jpg', data: null, type: 3, visible: false, se: 4 }, { path: 'img/17.jpg', data: null, type: 4, visible: false, se: 4 }, { path: 'img/18.jpg', data: null, type: 5, visible: false, se: 4 }, { path: 'img/19.jpg', data: null, type: 6, visible: false, se: 4 }, { path: 'img/20.jpg', data: null, type: 7, visible: false, se: 4 }, { path: 'img/21.jpg', data: null, type: 8, visible: false, se: 4 }, { path: 'img/22.jpg', data: null, type: 9, visible: false, se: 4 }, { path: 'img/23.jpg', data: null, type: 10, visible: false, se: 4 }, { path: 'img/24.jpg', data: null, type: 11, visible: false, se: 4 }, { path: 'img/25.jpg', data: null, type: 12, visible: false, se: 4 }, { path: 'img/26.jpg', data: null, type: 13, visible: false, se: 4 }, { path: 'img/27.jpg', data: null, type: 14, visible: false, se: 4 }, { path: 'img/28.jpg', data: null, type: 15, visible: false, se: 4 }, { path: 'img/29.jpg', data: null, type: 3, visible: false, se: 3 }, { path: 'img/30.jpg', data: null, type: 4, visible: false, se: 3 }, { path: 'img/31.jpg', data: null, type: 5, visible: false, se: 3 }, { path: 'img/32.jpg', data: null, type: 6, visible: false, se: 3 }, { path: 'img/33.jpg', data: null, type: 7, visible: false, se: 3 }, { path: 'img/34.jpg', data: null, type: 8, visible: false, se: 3 }, { path: 'img/35.jpg', data: null, type: 9, visible: false, se: 3 }, { path: 'img/36.jpg', data: null, type: 10, visible: false, se: 3 }, { path: 'img/37.jpg', data: null, type: 11, visible: false, se: 3 }, { path: 'img/38.jpg', data: null, type: 12, visible: false, se: 3 }, { path: 'img/39.jpg', data: null, type: 13, visible: false, se: 3 }, { path: 'img/40.jpg', data: null, type: 14, visible: false, se: 3 }, { path: 'img/41.jpg', data: null, type: 15, visible: false, se: 3 }, { path: 'img/42.jpg', data: null, type: 3, visible: false, se: 2 }, { path: 'img/43.jpg', data: null, type: 4, visible: false, se: 2 }, { path: 'img/44.jpg', data: null, type: 5, visible: false, se: 2 }, { path: 'img/45.jpg', data: null, type: 6, visible: false, se: 2 }, { path: 'img/46.jpg', data: null, type: 7, visible: false, se: 2 }, { path: 'img/47.jpg', data: null, type: 8, visible: false, se: 2 }, { path: 'img/48.jpg', data: null, type: 9, visible: false, se: 2 }, { path: 'img/49.jpg', data: null, type: 10, visible: false, se: 2 }, { path: 'img/50.jpg', data: null, type: 11, visible: false, se: 2 }, { path: 'img/51.jpg', data: null, type: 12, visible: false, se: 2 }, { path: 'img/52.jpg', data: null, type: 13, visible: false, se: 2 }, { path: 'img/53.jpg', data: null, type: 14, visible: false, se: 2 }, { path: 'img/54.jpg', data: null, type: 15, visible: false, se: 2 } ] });
Resource.Images是素材数组(几个按钮,文本,54张牌,背景图片等),大家可以下载demo看看
2、Lables类,在Canvas画布上画文本的,比如按钮文字,相关知识请看canvas教程
var Labels = Class.create(); $.extend(Labels.prototype, { initialize: function (cxt) { this.cxt = cxt; }, setText: function (text, postion) { this.cxt.font = 'bold 20px serif'; this.cxt.fillStyle = '#000000'; this.cxt.textAlign = 'center'; this.cxt.fillText(text, postion.x, postion.y); } });
这个类的方法setText主要是根据设置的字体,字体大小,字体颜色,在Canvas上画文本的,this.cxt这个是canvas上下文(每个教程的叫法不一样),首先this.cxt.font = 'bold 20px serif';这个是设置字体大小,样式等,this.cxt.fillStyle = '#000000';这个
是设置字体颜色,this.cxt.textAlign = 'center';这个是设置字体对齐方式,this.cxt.fillText(text, postion.x, postion.y);这个是开始在canvas上画文本,postion.x, postion.y分别是x坐标和y坐标。
3、DdZGame游戏类,主要功能就是初始化斗地主,发牌,抢地主等,出牌未完待续,后续更新
var DdZGame = Class.create(); DdZGame.Statics = { DealedNums: 0, isLeftFirstDeal: true }; $.extend(DdZGame.prototype, { initialize: function () { DdZGame.Statics.IsGetLander = false; DdZGame.Statics.DealTime = 66; this.leftPokers = []; this.rightPokers = []; this.myPokers = []; this.LastPokers = [];//最后3张牌 this.leftPlays = []; this.rightPlays = []; this.myPlays = []; this.myBtnPostion = { y: 245, x: 162 }; this.isStart = false; this.Res = new Resource(); this.allPokers = new Array(); this.Lander = 0;//地主,1右边,2My,3左边 this.isGetLander = {}; this.GmCanvas = document.getElementById('gmCanvas'); this.cxt = this.GmCanvas.getContext('2d'); this.Lbl = new Labels(this.cxt); this.init(); this.initEvt(); }, initEvt: function () { this.GmCanvas.onclick = $.proxy(function (e) { var box = this.GmCanvas.getBoundingClientRect(); DdZGame.Statics.MousePostion = { x: e.pageX - box.left, y: e.pageY - box.top }; this.onControlClick(); }, this); }, onControlClick: function () { var isClick = false; for (var i = 0; i <