在线试玩:http://keleyi.com/game/5/

操作指南:
键盘方向键←→控制左右移动,↑键变形,↓键快速下落。

别看这段js代码只有短短的100多行,效果却非常不错,用键盘的方向键操作,向上键是变形,赶紧试试。

把下面代码保存到html文件,打开就可以。

 <html><head><title>俄罗斯方块-柯乐义</title>
<link href="http://keleyi.com/game/5/index/keleyielsfk.css" type="text/css" rel="Stylesheet" /></head>
<body><a href="http://keleyi.com/a/bjac/600xsi0s.htm">原文</a></body></html>
<script>
var over = false, shapes = ("0,1,1,1,2,1,3,1;1,0,1,1,1,2,2,2;2,0,2,1,2,2,1,2;0,1,1,1,1,2,2,2;1,2,2,2,2,1,3,1;1,1,2,1,1,2,2,2;0,2,1,2,1,1,2,2").split(";");
function create(tag, css) {
var elm = document.createElement(tag);
elm.className = css;
document.body.appendChild(elm);
return elm;
}
function Tetris(c, t, x, y) {
var c = c ? c : "c";
this.divs = [create("div", c), create("div", c), create("div", c), create("div", c)];
this.reset = function () {
this.x = typeof x != 'undefined' ? x : 3;
this.y = typeof y != 'undefined' ? y : 0;
this.shape = t ? t : shapes[Math.floor(Math.random() * (shapes.length - 0.00001))].split(",");
this.show();
if (this.field && this.field.check(this.shape, this.x, this.y, 'v') == 'D') {
over = true;
this.field.fixShape(this.shape, this.x, this.y);
alert('游戏结束。http://keleyi.com/game/5/');
}
}
this.show = function () {
for (var i in this.divs) {
this.divs[i].style.left = (this.shape[i * 2] * 1 + this.x) * 20 + 'px';
this.divs[i].style.top = (this.shape[i * 2 + 1] * 1 + this.y) * 20 + 'px';
}
}
this.field = null;
this.hMove = function (step) {
var r = this.field.check(this.shape, this.x - -step, this.y, 'h');
if (r != 'N' && r == 0) {
this.x -= -step;
this.show();
}
}
this.vMove = function () {
if (this.field.check(this.shape, this.x, this.y - -1, 'v') == 'N') {
this.y++;
this.show();
}
else {
this.field.fixShape(this.shape, this.x, this.y);
this.field.findFull();
this.reset();
}
}
this.rotate = function () {
var s = this.shape;
var newShape = [3 - s[1], s[0], 3 - s[3], s[2], 3 - s[5], s[4], 3 - s[7], s[6]];
var r = this.field.check(newShape, this.x, this.y, 'h');
if (r == 'D') return;
if (r == 0) {
this.shape = newShape;
this.show();
}
else if (this.field.check(newShape, this.x - r, this.y, 'h') == 0) {
this.x -= r;
this.shape = newShape;
this.show();
}
}
this.reset();
}
function Field(w, h) {
this.width = w ? w : 10;
this.height = h ? h : 20;
this.show = function () {
var f = create("div", "f")
f.style.width = this.width * 20 + 'px';
f.style.height = this.height * 20 + 'px';
}
this.findFull = function () {
for (var l = 0; l < this.height; l++) {
var s = 0;
for (var i = 0; i < this.width; i++) {
s += this[l * this.width + i] ? 1 : 0;
}
if (s == this.width) {
this.removeLine(l);
}
}
}
this.removeLine = function (line) {
for (var i = 0; i < this.width; i++) {
document.body.removeChild(this[line * this.width + i]);
}
for (var l = line; l > 0; l--) {
for (var i = 0; i < this.width; i++) {
this[l * this.width - -i] = this[(l - 1) * this.width - -i];
if (this[l * this.width - -i]) this[l * this.width - -i].style.top = l * 20 + 'px';
}
}
}
this.check = function (shape, x, y, d) {
var r1 = 0, r2 = 'N';
for (var i = 0; i < 8; i += 2) {
if (shape[i] - -x < 0 && shape[i] - -x < r1)
{ r1 = shape[i] - -x; }
else if (shape[i] - -x >= this.width && shape[i] - -x > r1)
{ r1 = shape[i] - -x; }
if (shape[i + 1] - -y >= this.height || this[shape[i] - -x - -(shape[i + 1] - -y) * this.width])
{ r2 = 'D' }
}
if (d == 'h' && r2 == 'N') return r1 > 0 ? r1 - this.width - -1 : r1;
else return r2;
}
this.fixShape = function (shape, x, y) {
var d = new Tetris("d", shape, x, y);
d.show();
for (var i = 0; i < 8; i += 2) {
this[shape[i] - -x - -(shape[i + 1] - -y) * this.width] = d.divs[i / 2];
}
}
}
var f = new Field();
f.show();
var s = new Tetris();
s.field = f;
s.show();
window.setInterval("if(!over)s.vMove();", 500);
document.onkeydown = function (e) {
if (over) return;
var e = window.event ? window.event : e;
switch (e.keyCode) {
case 38: //up keleyi.com
s.rotate();
break;
case 40: //down
s.vMove();
break;
case 37: //left
s.hMove(-1);
break;
case 39: //right
s.hMove(1);
break;
}
}
</script>

原文: http://keleyi.com/a/bjac/600xsi0s.htm

web前端:http://www.cnblogs.com/jihua/p/webfront.html

javascript俄罗斯方块游戏的更多相关文章

  1. 经典 HTML5 & Javascript 俄罗斯方块游戏

    Blockrain.js 是一个使用 HTML5 & JavaScript 开发的经典俄罗斯方块游戏.只需要复制和粘贴一段代码就可以玩起来了.最重要的是,它是响应式的,无论你的显示屏多么宽都能 ...

  2. Javascript 俄罗斯方块 游戏代码解释!

    俄罗斯方块代码说明 /** 名称:Javascript 俄罗斯方块! 作者:Gloot 邮箱:glootz@gmail.com QQ:345268267 网站:http://www.cnblogs.c ...

  3. 教你看懂网上流传的60行JavaScript代码俄罗斯方块游戏

    早就听说网上有人仅仅用60行JavaScript代码写出了一个俄罗斯方块游戏,最近看了看,今天在这篇文章里面我把我做的分析整理一下(主要是以注释的形式). 我用C写一个功能基本齐全的俄罗斯方块的话,大 ...

  4. 俄罗斯方块游戏JavaScript代码

    JavaScript代码俄罗斯方块游戏 早就听说网上有人仅仅用60行JavaScript代码写出了一个俄罗斯方块游戏,最近看了看,今天在这篇文章里面我把我做的分析整理一下(主要是以注释的形式). 我用 ...

  5. 使用C#重写网上的60行 Javascript 俄罗斯方块源码 (带注释)

    在很久很久以前,就已经看过 60行Js的俄罗斯方块源码.无奈当时能力不够看明白,当时觉得就是个神作. 现在总算有空再看了,顺便用c#实现一遍(超过60行),顺道熟悉下Js API. 网上其他博客也有分 ...

  6. 使用JS实现俄罗斯方块游戏

    简单的JS俄罗斯方块游戏源码 效果图: 代码如下,复制即可使用: <!DOCTYPE html> <html> <head> <meta charset=&q ...

  7. 从零开始---控制台用c写俄罗斯方块游戏(1)

    从零开始---控制台用c写俄罗斯方块游戏(1) 很少写博文,一来自身知识有限,二来自己知道,已经有很多这样的博文了,三就是因为懒,文笔也一般,四来刚出来工作,时间也不多 之所以写这篇博文,是因为应群里 ...

  8. 用C写的俄罗斯方块游戏 By: hoodlum1980 编程论坛

    /************************************ * Desc: 俄罗斯方块游戏 * By: hoodlum1980 * Email: jinfd@126.com * Dat ...

  9. HTML5游戏开发进阶指南(亚马逊5星畅销书,教你用HTML5和JavaScript构建游戏!)

    HTML5游戏开发进阶指南(亚马逊星畅销书,教你用HTML5和JavaScript构建游戏!) [印]香卡(Shankar,A.R.)著 谢光磊译 ISBN 978-7-121-21226-0 201 ...

随机推荐

  1. Chart.js中文文档-雷达图

    雷达图或蛛网图(Radar chart) 简介 A radar chart is a way of showing multiple data points and the variation bet ...

  2. Javascript刷题 》 查找数组元素位置

    找出元素 item 在给定数组 arr 中的位置 输出描述: function indexOf(arr, item) { ..... } 如果数组中存在 item,则返回元素在数组中的位置,否则返回 ...

  3. C#设计模式系列:迭代器模式(Iterator)

    迭代器模式把对象的职责分离,职责分离可以最大限度减少彼此之间的耦合程度,从而建立一个松耦合的对象.职责分离的要点是对被分离的职责进行封装,并以抽象的方式建立彼此之间的关系. 1.迭代器模式简介 1.1 ...

  4. c++与java中子类中调用父类成员的方法

    java中: import java.util.Scanner; public class ClassTest{ public static void main(String args[]){ chi ...

  5. Neutron 物理部署方案 - 每天5分钟玩转 OpenStack(68)

    前面我们讨论了 Neutron 的架构,本节讨论 Neutron 的物理部署方案:不同节点部署不同的 Neutron 服务组件. 方案1:控制节点 + 计算节点 在这个部署方案中,OpenStack ...

  6. 辨析relative与absolute

    谈起它们,想必大家都不陌生.relative,相对定位嘛:absolute,绝对定位嘛.但是它们到底是个啥东东呢? 看看w3c的定义,见下表 定位 含义 relative 元素框偏移某个距离.元素仍保 ...

  7. ASP.NET:注销功能实现

    原理:清空Session 1.Web窗体:index.aspx <a href="logoutHandler.ashx">注销</a> 2.一般处理程序:L ...

  8. __weak 和 __block 区别

    Blocks理解: Blocks可以访问局部变量,但是不能修改 如果修改局部变量,需要加__block __block int multiplier = 7; int (^myBlock)(int) ...

  9. PHP类的原理

    一.类的实现 类的内部存储结构: struct _zend_class_entry { char type; // 类型:ZEND_INTERNAL_CLASS / ZEND_USER_CLASS c ...

  10. objective-c 语法快速过(2)

    oc类的声明和定义的常见错误 1.只有类的声明,没有类的实现 2.漏了@end 3.@interface和@implementation嵌套,也就是@interface或者@implementatio ...