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:父组件声明数 ...
随机推荐
- zigbee学习之路(八):定时器1(中断)
一.前言 通过上次的实验,我们已经学会了定时器3的中断方式,这次,我们来看看定时器1通过中断怎么控制. 二.原理分析 定时器1的初始化跟前面提到的一样,也是要配置寄存器T1CTL,还要进行开中断的操作 ...
- WebForm带有图片的验证码
验证码形成的部分在一个aspx文件中: 页面设置 <%@ Page Language="C#" AutoEventWireup="true" CodeFi ...
- linux下解压war格式的包
linux解压 .war 包 war格式的包可以解决web应用程序部署时候不用按照目录层次结构部署,而是将war包当作部署单元来使用. 下面就讲下怎么去解压 .war 格式的压缩包: 1.安装jdk, ...
- [问题2015S12] 复旦高等代数 II(14级)每周一题(第十三教学周)
[问题2015S12] 设 \(A\) 为 \(n\) 阶实矩阵, 若对任意的非零 \(n\) 维实列向量 \(\alpha\), 总有 \(\alpha'A\alpha>0\), 则称 \( ...
- [问题2014A01] 解答三(升阶法,由董麒麟同学提供)
[问题2014A01] 解答三(升阶法,由董麒麟同学提供) 引入变量 \(y\),将 \(|A|\) 升阶,考虑如下行列式: \[|B|=\begin{vmatrix} 1 & x_1-a & ...
- [问题2014S04] 解答
[问题2014S04] 解答 由于 \(A\) 可对角化, 可设 \(\alpha_1,\alpha_2,\cdots,\alpha_n\in\mathbb{C}^n\) 是 \(A\) 的 \(n ...
- 405 Method Not Allowed
今天在发布一个网站的时候遇到 标题上的问题,一直不明白是为何,刚开始以为是我的程序写的有问题,随即将项目发给同事来发布试试,在他的IIS上发布却没出现问题,一切正常,这可就怪了,于是想到了应该是IIS ...
- REST 架构风格
目前基于网络应用的架构风格主要有三种: RPC架构风格 将服务器看作是由一些过程组成,客户端调用这些过程来执行特定的任务.SOAP就是RPC风格的一种架构.过程是动词性的(做某件事),因此RPC建 ...
- 解决sublime text3中的输入法不根随光标问题
日本的一位大神开发了一款插件用在Sublime Text上,以缓解输入法不跟随光标移动的问题.当然这个问题并没有完美的解决,据一些用户的反映,输入过程中还是偶尔会发生输入法不跟随光标移动的问题,不过确 ...
- Monkey环境配置
安卓APP想要测试稳定性,monkey是最佳选则. 首先搭建monkey的运行环境 在Windows下基于SDK 1.下载SDK for Windows 解压:android-sdk-windows ...