H5实现俄罗斯方块(四)
图片加载的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实现俄罗斯方块(四)的更多相关文章
- H5版俄罗斯方块(2)---游戏的基本框架和实现
前言: 上文中谈到了H5版俄罗斯方块的需求和目标, 这次要实现一个可玩的版本. 但饭要一口一口吃, 很多东西并非一蹴而就. 本文将简单实现一个可玩的俄罗斯方块版本. 下一步会引入AI, 最终采用coc ...
- H5版俄罗斯方块(3)---游戏的AI算法
前言: 算是"long long ago"的事了, 某著名互联网公司在我校举行了一次"lengend code"的比赛, 其中有一题就是"智能俄罗斯方 ...
- H5版俄罗斯方块(1)---需求分析和目标创新
前言: 俄罗斯方块和五子棋一样, 规则简单, 上手容易. 几乎每个开发者, 都会在其青春年华时, 签下"xx到此一游". 犹记得大一老师在布置大程作业的时候提过: "什么 ...
- H5版俄罗斯方块(4)---火拼对战的雏形
前言: 勿忘初心, 本系列的目标是实现一款类似QQ"火拼系列"的人机对战版俄罗斯方块. 在完成了基本游戏框架和AI的算法探索后, 让我们来尝试一下人机大战雏形编写. 本系列的文章链 ...
- H5版俄罗斯方块(5)---需求演进和产品迭代
前言: 产品的形态是不断迭代的, 从粗糙到精致, 从简易到立体. 有了最初的技术积累和时间思考后, 终于明确了该游戏的方向. 我想说的是: 技术不是重点, 产品/用户体验才是核心议题. 结合朋友的游戏 ...
- H5实现俄罗斯方块(一)
这几天一直忙于公司的项目,涉及到流程问题,(有时间会写成博客的)...一直没有更新... 为了更加巩固js的基础,自己封装类似于JQuery的操作库来对dom进行操作. 一:前度页面的绘制. < ...
- H5实现俄罗斯方块(三)
最高分的面板: (function (window) { 'use strict'; function HighScore() { this.canvas = new Canvas('highscor ...
- H5实现俄罗斯方块(二)
对应的js 1.封装操作dom的js: (function (document) { //游戏的实例 var gameInst; /**封装一个返回原型的DOM对象 */ function DomOb ...
- 04 (H5*) Vue第四天
目录: 1:父组件向子组件传值,通过属性绑定的方式. 2:父组件向子组件传方法,通过事件绑定的方式 . 3:通过ref来获取Dom元素 1:父组件向子组件传值,通过属性绑定的方式 1.1:父组件声明数 ...
随机推荐
- nwjs如何打包文件为exe文件并修改exe图标
1.下载nw.js,如果是SDK版的可以调试页面,打包后可不可以调试还没有试,不是SDK的话没有调试选项,试了一下,打包后的文件也一样调试不了. 2.把要打包的文件和package.json都放在nw ...
- 自己写的java用jxl导出到excel工具
package com; import java.io.BufferedOutputStream; import java.io.File; import java.io.IOException; i ...
- easyui 动态列
$.post('${createLink(action:"build Columns url ")}', params, function(data){ var columns = ...
- chrome的timeline中stalled问题解析
原文地址 :http://foio.github.io/chrome-stalled/ 在公司国做一个运营活动,上线后PM总是抱怨访问速度过慢,影响运营效果.然而从前端的角度来说我已经做了如下优化: ...
- jquery总结03-遍历节点
这是用的最多的 向下遍历节点 children() 第一级子元素 相当于li>span find() 多级子孙元素 相当于li span 注意:.filter(':contains(&qu ...
- PHP基础知识之————php5-cli 的安装以及phpredis的安装
在系统安装完后最好执行下列命令更新下软件 sudo apt-get update 安装php5-cli apt-get install php5-cli 下载phpredis wget https:/ ...
- spring主要的作用?
在SSH框假中spring充当了管理容器的角色.我们都知道Hibernate用来做持久层,因为它将JDBC做了一个良好的封装,程序员在与数据库进行交互时可以不用书写大量的SQL语句.Struts是用来 ...
- excel模版从xp复制到win7系统后出现错误 运行时错误 '429' ActiveX 部件不能创建对象
运行时错误 '429' ActiveX 部件不能创建对象Set objDialog = CreateObject("UserAccounts.CommonDialog")解决办法把 ...
- block(闭包)
使用方式 1定义为类的属性 最后用来发布通知,执行block即可 甚至同时发送数据参数,给方法调用者,这样返回数据比返回值形式,更好, 因为这样传参,不是同步的,而是异步响应式的,更加灵活安全. 2定 ...
- windows递归拷贝(或删除等操作)文件
SHFileOperation 以拷贝为例. CString strFrom = ....._T("src");CString strTo = ....._T("dest ...