图片加载的js:

(function (window) {

    'use strict';
//定义缓存的Map对象
var cacheMap = new Map();
//资源的总数量
var resourceTotalCount = 1;
//当前加载资源的数量
var currentLoaded = 0; //判断是否都是加载成功
var isAddLoaded = function () {
//当前的数量 +1
currentLoaded += 1;
//当前的数量是否等于我们的总量 判断是否为函数
if (currentLoaded === resourceTotalCount && typeof window.ResourceManager.onResourceLoaded === 'function') {
window.ResourceManager.onResourceLoaded();
}
};
//加载资源的方法
var init = function () {
//定义图片的对象
var image = new Image();
//加载时的回调函数
image.onload = function () {
//把对象进行存储
cacheMap.set('blocks', image);
isAddLoaded();
};
//指定加载的路径
image.src = 'images/blocks.png';
}; var getResource = function (key) {
//返回资源
return cacheMap.get(key);
};
//ResourceManager 挂在到window上
window.ResourceManager = {
getResource: getResource,
init: init,
onResourceLoaded: null // 资源加载完成回调
};
})(window);

当前得分的js:

(function (window) {
'use strict';
//得分的面板
function Score() {
this.canvas = new Canvas('score', 100, 70);
//得分
this.score = 0; this._init();
} Score.prototype = {
constructor: Score,
_init: function () {
this._render();
},
//绘制得分
_render: function () {
this.canvas.drawText(this.score);
},
//分数的变化
addScore:function(value){
this.score+=value;
//渲染
this._render();
//返回当前的得分
return this.score;
}
}; window.Score=Score;
})(window);

方块的调整js:

(function (window) {
'use strict';
//布局
var shapeLayouts=[
[[0,1,0],[1,1,1]],
[[1,1,1,1]],
[[1,1],[1,1]],
[[0,1],[1,1],[1,0]],
[[1,0],[1,1],[0,1]],
[[1,0,1],[1,1,1]],
[[0,1],[1,1]],
[[1,1]],
[[1,1],[1,0],[1,0]],
[[1,1],[0,1],[0,1]],
]; //生成随机数的方法(通过最小值和最大值随机生成数据)
var random=function(minValue,maxValue){
return minValue+ Math.floor(Math.random()*maxValue);//产生随机数 从0到1 (包含0,不包含1)
};
//定义颜色总类
var styleCount=7; function Shape() {
this.x = 0;
this.y = 0;
//随机生成颜色
this.blockType=random(1,styleCount);
this.block = new Block(this.blockType); this.layout=shapeLayouts[random(0,shapeLayouts.length)];
} Shape.prototype = {
constructor: Shape,
draw: function (context,size) {
for (var i = 0; i < this.layout.length; i++) {
for (var j = 0; j < this.layout[i].length; j++) {
if (this.layout[i][j]) {
this.block.draw(context, j + this.x, i + this.y,undefined,size);
}
}
}
}, //反转算法
rotate:function () {
var newLayout=[];
//交换行和列
for (var y = 0; y < this.layout[0].length; y++) {
newLayout[y] = [];
for (var x = 0; x < this.layout.length; x++) {
newLayout[y][x] = this.layout[this.layout.length - 1 - x][y];
}
}
//覆盖
this.layout=newLayout;
//不在外部反问 旋转时超过边界的处理
this._setLayout();
},
_setLayout: function () {
if (this.x < 0) {
this.x = 0;
}
if (this.y < 0) {
this.y = 0;
}
if (this.x + this.layout[0].length > TetrisConfig.cols) {
this.x = TetrisConfig.cols - this.layout[0].length;
}
if (this.y + this.layout.length > TetrisConfig.rows) {
this.y = TetrisConfig.rows - this.layout.length;
}
}, //算出最大的cols
_getMaxCols:function(){
var max=0;
for(var y=0;y<this.layout.length;y++)
{
max=Math.max(max,this.layout[y].length);
}
return max;
},
_getMaxRos:function(){
return this.layout.length;
},
//ignoreRows 主面板和小面板显示的效果
setPosition:function(cols,rows,ignoreRows){
this.x=Math.floor((cols- this._getMaxCols()) /2);
if(!ignoreRows){
this.y=Math.floor((rows-this._getMaxRos())/2);
}
}
}; window.Shape = Shape;
})(window);

整个文件加载js:

(function (window) {
'use strict'; //计时器的id
/** var intervalId;
var speed=200;*/ function Tetris() { this.board = new Board(this);
//成绩
this.score=new Score();
//时间
this.timer = new Timer();
this.level=new Level();
//下一方块
this.nextshape=new NextShape();
//最高分
this.highscore = new HighScore(); this._sound;
this._state='playing'; //启动键盘事件
(new keyboard()).init(this.board);
} Tetris.prototype ={ constructor: Tetris,
_initAudio:function(){
this._sound =new Howl({
src:['audio/bg.wav'],
loop:true,
//音量
volume:0.3,
}); this._playSound();
}, _playSound:function(){
if(window.TetrisConfig.config.enableSound){
this._sound.play();
}
}, //重复利用的方法进行封装
_startTick(){
var self=this;
var self=this;
window.TetrisConfig.intervalId = window.setInterval(function(){
//每次调用他的tick
self.board.tick();
}, TetrisConfig.speed);
},
//取消tick的公用方法
_stopTick:function(){
window.clearInterval(window.TetrisConfig.intervalId);
}, //开始
startGame: function(){
//初始化音频
this._initAudio();
//开始tick方法
this._startTick();
}, //结束
endGame:function(){
//停止声音播放 找到声音实例 停止
this._sound.stop();
//停止Tick
this._stopTick();
//停止计时
this.Timer.stop(); },
pause:function(){
if(this._state==='over'){
return;
}
//暂停播放音乐
this._sound.pause();
//暂停事件响应
this._state='pause'; //取消tick
this._stopTick();
//暂停计时器
this.timer.pause();
},
resume:function(){
//避免再次生效
if(this._state==='over'){
return;
}
//this._sound.play();
this._playSound();
this._state='playing';
//恢复tick方法
this._startTick();
//重新激活
this.timer.resume(); } }; window.Tetris = Tetris; })(window);

计时器的js:

(function (window) {
'use strict'
function Timer() {
this.canvas=new Canvas('timer',100,70);
//毫秒数
this.time=0;
//存储时间的Inver
this.timerId; this._init();
}; Timer.prototype = {
constructor: Timer,
_init: function () {
var self=this;
this._render();
//时间的累加
this.resume();
},
//处理time的值
_format:function(seconds){
//取出毫秒数 小时
var hours=Math.floor(seconds/(3600));
seconds=seconds-hours*3600;
//分钟
var minutes=Math.floor(seconds/ 60);
seconds=seconds-minutes*60;
if(hours<10){
//补零
hours='0'+hours;
}
if(minutes<10){
minutes='0'+minutes;
}
if(seconds<10){
seconds='0'+seconds;
}
return hours+':'+minutes+':'+seconds;
},
_render: function () {
this.canvas.drawText(this._format(this.time));
},
pause:function(){
window.clearInterval(this.timerId)
},
resume:function(){
//恢复时激活
var self=this;
this.timerId=window.setInterval(function(){
self.time += 1;
self._render();
},1000);
},
stop:function(){
this.pause();
}
}; window.Timer = Timer;
})(window);

开发工具vscode

H5实现俄罗斯方块(四)的更多相关文章

  1. H5版俄罗斯方块(2)---游戏的基本框架和实现

    前言: 上文中谈到了H5版俄罗斯方块的需求和目标, 这次要实现一个可玩的版本. 但饭要一口一口吃, 很多东西并非一蹴而就. 本文将简单实现一个可玩的俄罗斯方块版本. 下一步会引入AI, 最终采用coc ...

  2. H5版俄罗斯方块(3)---游戏的AI算法

    前言: 算是"long long ago"的事了, 某著名互联网公司在我校举行了一次"lengend code"的比赛, 其中有一题就是"智能俄罗斯方 ...

  3. H5版俄罗斯方块(1)---需求分析和目标创新

    前言: 俄罗斯方块和五子棋一样, 规则简单, 上手容易. 几乎每个开发者, 都会在其青春年华时, 签下"xx到此一游". 犹记得大一老师在布置大程作业的时候提过: "什么 ...

  4. H5版俄罗斯方块(4)---火拼对战的雏形

    前言: 勿忘初心, 本系列的目标是实现一款类似QQ"火拼系列"的人机对战版俄罗斯方块. 在完成了基本游戏框架和AI的算法探索后, 让我们来尝试一下人机大战雏形编写. 本系列的文章链 ...

  5. H5版俄罗斯方块(5)---需求演进和产品迭代

    前言: 产品的形态是不断迭代的, 从粗糙到精致, 从简易到立体. 有了最初的技术积累和时间思考后, 终于明确了该游戏的方向. 我想说的是: 技术不是重点, 产品/用户体验才是核心议题. 结合朋友的游戏 ...

  6. H5实现俄罗斯方块(一)

    这几天一直忙于公司的项目,涉及到流程问题,(有时间会写成博客的)...一直没有更新... 为了更加巩固js的基础,自己封装类似于JQuery的操作库来对dom进行操作. 一:前度页面的绘制. < ...

  7. H5实现俄罗斯方块(三)

    最高分的面板: (function (window) { 'use strict'; function HighScore() { this.canvas = new Canvas('highscor ...

  8. H5实现俄罗斯方块(二)

    对应的js 1.封装操作dom的js: (function (document) { //游戏的实例 var gameInst; /**封装一个返回原型的DOM对象 */ function DomOb ...

  9. 04 (H5*) Vue第四天

    目录: 1:父组件向子组件传值,通过属性绑定的方式. 2:父组件向子组件传方法,通过事件绑定的方式 . 3:通过ref来获取Dom元素 1:父组件向子组件传值,通过属性绑定的方式 1.1:父组件声明数 ...

随机推荐

  1. Unity3D UGUI学习系列索引(暂未完成)

    U3D UGUI学习1 - 层级环境 U3D UGUI学习2 - Canvas U3D UGUI学习3 - RectTransform U3D UGUI学习4 - Text U3D UGUI学习5 - ...

  2. Viking Village维京村落demo中的地面积水效果

    效果如下: 似乎是通过高光贴图实现的,查找后发现具体在这: 它使用了基于Standard的TerrainSurface自定义Shader,关闭该帖图后效果消失: 这个TerrainSurfaceSha ...

  3. EntityFramework Core 学习笔记 —— 创建模型

    原文地址:https://docs.efproject.net/en/latest/modeling/index.html 前言: EntityFramework 使用一系列的约定来从我们的实体类细节 ...

  4. Android ActionBar的基本用法

    一  说明android 3.0后出现, 在3.0之前称为Title Bar  显示位置在标题栏上可以显示应用程序的图标和activity的标题创建方式的和系统菜单相似, 区别在于: android: ...

  5. ace_admin_1.3.1 wysiwyg 工具条下拉出不来

    试了很久才知道是因为<script src="__PUBLIC__/assets/js/bootstrap.min.js"></script>  这个js加 ...

  6. Java中Object类

    Object类是所有类的父类,如果一个类没有使用extends关键字明确标识继承另一个类,那么这个类默认继承Object类. Object类中的方法,适合所有子类. Object中的几个重要方法: 1 ...

  7. jsp标签精华(持续更新中)

    <%@ taglib uri="/struts-tags" prefix="s" %> <%@ taglib uri="http:/ ...

  8. [poj1200]Crazy Search(hash)

    Crazy Search Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 26713 Accepted: 7449 Descrip ...

  9. Mac 在命令行中获得Root权限

    Mac 在命令行中获得Root权限 作者 firedragonpzy 13 九月, 2012 2条评论 本文为firedragonpzy原创,转载务必在明显处注明:转载自[Softeware MyZo ...

  10. b.Connector配置解析

    前面讲解到Tomcat中使用Digester框架进行server.xml到javaBean对象的映射,这篇文章以Connector的SSL通道为例,来讲解Connector的属性是如何注入的. 先看一 ...