图片加载的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. noi 1768 最大子矩阵

    题目链接:http://noi.openjudge.cn/ch0206/1768/ 可能是数据修改了吧,O(n6)过不了了. 主要是在求一个矩阵的和时,重复计算了很多次. 矩阵首先压缩一下.在输入的时 ...

  2. python 学习笔记十一 SQLALchemy ORM(进阶篇)

    SqlAlchemy ORM SQLAlchemy是Python编程语言下的一款ORM框架,该框架建立在数据库API之上,使用关系对象映射进行数据库操作,简言之便是:将对象转换成SQL,然后使用数据A ...

  3. (转)MySQL命令行--导入导出数据库

    MySQL命令行导出数据库:   1,进入MySQL目录下的bin文件夹:cd MySQL中到bin文件夹的目录 如我输入的命令行:cd C:\Program Files\MySQL\MySQL Se ...

  4. ArrayList代码示例

    package com.shushine.framework.第七章Java标准类库;import java.util.ArrayList;/** * * <p> * 描述该类情况 {@l ...

  5. 9----Lua中的面向对象

    什么是面向对象? 使用对象.类.继承.封装.消息等基本概念来进行程序设计 面向对象最重要的两个概念就是:对象和类 对象是系统中用来描述客观事物的一个实体,它是构成系统的一个基本单位 一个对象由一组属性 ...

  6. 《点石成金:访客至上的Web和可用性设计秘笈(原书第3版)》--- 读书笔记

    这是一本绝妙的书, 它的英语书名是“Don't make me think”.更确切的说是个小册子, 但是作者的语言实在是让人忍俊不禁. 真TM的有趣, 为毛外国人就能写出如此美妙的书? 而国人却不能 ...

  7. Unquotted string '"2016-07-19"'

    自己挖的坑,含泪跳进去也要填平.   ---题记 1.问题: a. 在前端使用JSON.stringify(json)转化数组对象为字符串,然后传给后台: var dateArray = new Ar ...

  8. 转!!mybatis在xml文件中处理大于号小于号的方法

    第一种方法: 用了转义字符把>和<替换掉,然后就没有问题了. SELECT * FROM test WHERE 1 = 1 AND start_date  <= CURRENT_DA ...

  9. Security » Authorization » 基于自定义策略的授权

    Custom Policy-Based Authorization¶ 基于自定义策略的授权 98 of 108 people found this helpful Underneath the cov ...

  10. 51nod 1051 最大子矩阵和(dp)

    题目链接:51nod 1051 最大子矩阵和 实质是把最大子段和扩展到二维.读题注意m,n... #include<cstdio> #include<cstring> #inc ...