jquery编写俄罗斯方块

时间: 2024-01-19 07:04:19 浏览: 31
以下是使用jQuery编写俄罗斯方块的示例代码: ```html <!DOCTYPE html> <html> <head> <title>Tetris Game</title> <style type="text/css"> #game-board { width: 300px; height: 600px; border: 1px solid black; margin: 0 auto; position: relative; } .block { width: 30px; height: 30px; border: 1px solid black; position: absolute; } </style> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <script type="text/javascript"> $(document).ready(function() { var gameBoard = $('#game-board'); var blockWidth = parseInt(gameBoard.css('width')) / 10; var blockHeight = parseInt(gameBoard.css('height')) / 20; var currentBlock = null; var currentBlockType = null; var currentBlockOrientation = null; var currentBlockPosition = null; var currentBlockTimer = null; var score = 0; var scoreBoard = $('#score-board'); var highScore = localStorage.getItem('tetris-high-score') || 0; var highScoreBoard = $('#high-score-board'); highScoreBoard.text(highScore); function createBlock(type, orientation, position) { var block = $('<div class="block"></div>'); block.css({ 'width': blockWidth + 'px', 'height': blockHeight + 'px', 'top': position[0] * blockHeight + 'px', 'left': position[1] * blockWidth + 'px' }); block.addClass(type + '-' + orientation); gameBoard.append(block); return block; } function moveBlockDown() { var newPosition = [currentBlockPosition[0] + 1, currentBlockPosition[1]]; if (isValidPosition(currentBlockType, currentBlockOrientation, newPosition)) { currentBlock.css('top', newPosition[0] * blockHeight + 'px'); currentBlockPosition = newPosition; } else { clearInterval(currentBlockTimer); currentBlockTimer = null; checkForCompletedRows(); createNewBlock(); } } function isValidPosition(type, orientation, position) { var blocks = getBlocksForTypeAndOrientation(type, orientation); for (var i = 0; i < blocks.length; i++) { var row = position[0] + blocks[i][0]; var col = position[1] + blocks[i][1]; if (row < 0 || row >= 20 || col < 0 || col >= 10) { return false; } if ($('#game-board .block[row="' + row + '"][col="' + col + '"]').length > 0) { return false; } } return true; } function getBlocksForTypeAndOrientation(type, orientation) { var blocks = []; switch (type) { case 'i': switch (orientation) { case 0: case 2: blocks = [[0, 0], [1, 0], [2, 0], [3, 0]]; break; case 1: case 3: blocks = [[0, 0], [0, 1], [0, 2], [0, 3]]; break; } break; case 'j': switch (orientation) { case 0: blocks = [[0, 0], [1, 0], [1, 1], [1, 2]]; break; case 1: blocks = [[0, 2], [1, 2], [2, 2], [2, 1]]; break; case 2: blocks = [[2, 0], [2, 1], [1, 1], [0, 1]]; break; case 3: blocks = [[0, 0], [0, 1], [1, 1], [2, 1]]; break; } break; case 'l': switch (orientation) { case 0: blocks = [[0, 2], [1, 0], [1, 1], [1, 2]]; break; case 1: blocks = [[0, 1], [1, 1], [2, 1], [2, 2]]; break; case 2: blocks = [[2, 0], [1, 0], [1, 1], [1, 2]]; break; case 3: blocks = [[0, 1], [1, 1], [2, 1], [0, 0]]; break; } break; case 'o': blocks = [[0, 0], [0, 1], [1, 0], [1, 1]]; break; case 's': switch (orientation) { case 0: case 2: blocks = [[0, 1], [0, 2], [1, 0], [1, 1]]; break; case 1: case 3: blocks = [[0, 1], [1, 1], [1, 2], [2, 2]]; break; } break; case 't': switch (orientation) { case 0: blocks = [[0, 1], [1, 0], [1, 1], [1, 2]]; break; case 1: blocks = [[0, 1], [1, 1], [2, 1], [1, 2]]; break; case 2: blocks = [[1, 0], [0, 1], [1, 1], [2, 1]]; break; case 3: blocks = [[0, 1], [1, 0], [1, 1], [2, 1]]; break; } break; case 'z': switch (orientation) { case 0: case 2: blocks = [[0, 0], [0, 1], [1, 1], [1, 2]]; break; case 1: case 3: blocks = [[0, 2], [1, 1], [1, 2], [2, 1]]; break; } break; } return blocks; } function createNewBlock() { var types = ['i', 'j', 'l', 'o', 's', 't', 'z']; var type = types[Math.floor(Math.random() * types.length)]; var orientation = Math.floor(Math.random() * 4); var position = [0, Math.floor(Math.random() * 7)]; currentBlock = createBlock(type, orientation, position); currentBlockType = type; currentBlockOrientation = orientation; currentBlockPosition = position; currentBlockTimer = setInterval(moveBlockDown, 500); } function checkForCompletedRows() { for (var row = 0; row < 20; row++) { var blocksInRow = $('#game-board .block[row="' + row + '"]'); if (blocksInRow.length === 10) { blocksInRow.remove(); score += 10; scoreBoard.text(score); if (score > highScore) { highScore = score; highScoreBoard.text(highScore); localStorage.setItem('tetris-high-score', highScore); } $('#game-board .block').each(function() { var blockRow = parseInt($(this).attr('row')); var blockCol = parseInt($(this).attr('col')); if (blockRow < row) { $(this).css('top', (blockRow + 1) * blockHeight + 'px'); $(this).attr('row', blockRow + 1); } }); row--; } } } $(document).keydown(function(event) { if (currentBlockTimer !== null) { switch (event.which) { case 37: // left arrow var newPosition = [currentBlockPosition[0], currentBlockPosition[1] - 1]; if (isValidPosition(currentBlockType, currentBlockOrientation, newPosition)) { currentBlock.css('left', newPosition[1] * blockWidth + 'px'); currentBlockPosition = newPosition; } break; case 38: // up arrow var newOrientation = (currentBlockOrientation + 1) % 4; if (isValidPosition(currentBlockType, newOrientation, currentBlockPosition)) { currentBlock.removeClass(currentBlockType + '-' + currentBlockOrientation); currentBlock.addClass(currentBlockType + '-' + newOrientation); currentBlockOrientation = newOrientation; } break; case 39: // right arrow var newPosition = [currentBlockPosition[0], currentBlockPosition[1] + 1]; if (isValidPosition(currentBlockType, currentBlockOrientation, newPosition)) { currentBlock.css('left', newPosition[1] * blockWidth + 'px'); currentBlockPosition = newPosition; } break; case 40: // down arrow moveBlockDown(); break; case 32: // space bar var newOrientation = (currentBlockOrientation + 1) % 4; if (isValidPosition(currentBlockType, newOrientation, currentBlockPosition)) { currentBlock.removeClass(currentBlockType + '-' + currentBlockOrientation); currentBlock.addClass(currentBlockType + '-' + newOrientation); currentBlockOrientation = newOrientation; } break; case 13: // enter key if (currentBlockTimer !== null) { clearInterval(currentBlockTimer); currentBlockTimer = null; } else { currentBlockTimer = setInterval(moveBlockDown, 500); } break; } } }); createNewBlock(); }); </script> </head> <body> <div id="game-board"></div> <div id="score-board">0</div> <div id="high-score-board"></div> </body> </html> ```

相关推荐

最新推荐

recommend-type

jQuery截取指定长度字符串代码

例子,截取字符串代码。 &lt;!DOCTYPE HTML&gt; &lt;... ...jQuery截取字符串操作---www.jb51.net&lt;/title&gt; [removed][removed] &lt;style&gt; * { margin:0; padding:0; font-family:"宋体", Arial, Helveti
recommend-type

jQuery实现图像旋转动画效果

jquery动画旋转效果在项目中经常遇到这样的需求,下面小编给大家分享具体实现代码,感兴趣的朋友一起学习吧
recommend-type

解决jquery版本冲突的有效方法

用过jQuery的朋友都知道jQuery不同版本会引发冲突,本文就此问题提出有效的解决方案如下: 案例:解决jQuery1.3.2和1.4.2的冲突。(本例已测试通过!) 第一步:在1.4.2的源代码的最后加上一句 : var $j4 = ...
recommend-type

jQuery实现的中英文切换功能示例

主要介绍了jQuery实现的中英文切换功能,结合实例形式分析了jQuery结合插件translate.js实现中英文翻译的相关操作技巧,需要的朋友可以参考下
recommend-type

jQuery实现跨域iframe接口方法调用

页面a.html域名为www.a.com嵌入页面http://www.b.com/b.html,b.html要调用a.html中的js函数,由于两个页面不在一个域中,会提示没权限。如何解决该问题呢,请看下面示例代码。
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

用 Python 画一个可以动的爱心

好的,我可以帮助您使用Python来绘制一个动态的爱心。您可以使用turtle库来实现。以下是实现代码: ```python import turtle import math # 设置画布和画笔 canvas = turtle.Screen() canvas.bgcolor("black") pencil = turtle.Turtle() pencil.speed(0) pencil.color("red", "pink") pencil.pensize(3) # 定义爱心函数 def draw_love(heart_size, x_offset=0, y_offset=0):
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。