首页 前端知识 HTML5 css js制作2048

HTML5 css js制作2048

2024-06-16 01:06:57 前端知识 前端哥 135 876 我要收藏
var sameNeighbors = game.find(function (tile, i) {
  var isRightSame = game[i + 1] && (i + 1) % 4 !== 0 ? tile.value === game[i + 1].value : false;
  var isDownSame = game[i + 4] ? tile.value === game[i + 4].value : false;
  if (isRightSame || isDownSame) {
    return true;
  }
  return false;
});
return !sameNeighbors;

}
}

function generateNewNumber() {
// 0.9 probability of 2, 0.1 probability of 4
var p = Math.random() * 100;
return p <= 90 ? 2 : 4;
}

function addRandomNumber() {
// Adds either a 2 or a 4 to an empty position in the game array
var emptyCells = game.map(function (_, index) {
return index;
}).filter(function (index) {
return game[index] === null;
});
if (emptyCells.length === 0) {
return;
}
var newPos = emptyCells[Math.floor(Math.random() * emptyCells.length)];
var newObj = {
id: nextId++,
index: newPos,
value: generateNewNumber()
};
game.splice(newPos, 1, newObj);
}

function getIndexForPoint(x, y) {
return y * size + x;
}

function reflectGrid(grid) {
var reflectedGame = Array(size * size).fill(0);
for (var row = 0; row < size; row++) {
for (var col = 0; col < size; col++) {
var index1 = getIndexForPoint(col, row);
var index2 = getIndexForPoint(size - col - 1, row);
reflectedGame[index1] = grid[index2];
}
}
return reflectedGame;
}

function rotateLeft90Deg(grid) {
var rotatedGame = Array(size * size).fill(0);
for (var row = 0; row < size; row++) {
for (var col = 0; col < size; col++) {
var index1 = getIndexForPoint(col, row);
var index2 = getIndexForPoint(size - 1 - row, col);
rotatedGame[index1] = grid[index2];
}
}
return rotatedGame;
}

function rotateRight90Deg(grid) {
var rotatedGame = Array(size * size).fill(0);
for (var row = 0; row < size; row++) {
for (var col = 0; col < size; col++) {
var index1 = getIndexForPoint(col, row);
var index2 = getIndexForPoint(row, size - 1 - col);
rotatedGame[index1] = grid[index2];
}
}
return rotatedGame;
}

/*
For any cell whose neighbor to the right is empty, move that cell
to the right. For any cell whose neighbor to the right is equal
to the same value, combine the values together (e.g. 2+2 = 4)
*/
function shiftGameRight(gameGrid) {
// reflect game grid
var reflectedGame = reflectGrid(gameGrid);
// shift left
reflectedGame = shiftGameLeft(reflectedGame);
// reflect back
return reflectGrid(reflectedGame);
}

function shiftGameLeft(gameGrid) {
var newGameState = [];
var totalAdd = 0;
// for rows
for (var i = 0; i < size; i++) {
// for columns
var firstPos = 4 * i;
var lastPos = size + 4 * i;
var currentRow = gameGrid.slice(firstPos, lastPos);
var filteredRow = currentRow.filter(function (row) {
return row;
});
for (var _iterator3 = filteredRow, _isArray3 = Array.isArray(_iterator3), _i3 = 0, _iterator3 = _isArray3 ? _iterator3 : _iterator3Symbol.iterator;😉 {
var _ref3;

  if (_isArray3) {
    if (_i3 >= _iterator3.length) break;
    _ref3 = _iterator3[_i3++];
  } else {
    _i3 = _iterator3.next();
    if (_i3.done) break;
    _ref3 = _i3.value;
  }

  var row = _ref3;

  delete row.mergedIds;
}

for (var j = 0; j < filteredRow.length - 1; j++) {
  if (filteredRow[j].value === filteredRow[j + 1].value) {
    var sum = filteredRow[j].value * 2;
    filteredRow[j] = {
      id: nextId++,
      mergedIds: [filteredRow[j].id, filteredRow[j + 1].id],
      value: sum
    };
    filteredRow.splice(j + 1, 1);
    score += sum;
    totalAdd += sum;
  }
}
while (filteredRow.length < size) {
  filteredRow.push(null);
};
newGameState = [].concat(newGameState, filteredRow);

}

if (totalAdd > 0) {
scoreDiv.innerHTML = score;
addDiv.innerHTML = ‘+’ + totalAdd;
addDiv.classList.add(‘active’);
setTimeout(function () {
addDiv.classList.remove(“active”);
}, 800);
if (score > bestScore) {
localStorage.setItem(‘bestScore’, score);
initBestScore();
}
}
return newGameState;
}

function shiftGameUp(gameGrid) {
var rotatedGame = rotateLeft90Deg(gameGrid);
rotatedGame = shiftGameLeft(rotatedGame);
return rotateRight90Deg(rotatedGame);
}

function shiftGameDown(gameGrid) {
var rotatedGame = rotateRight90Deg(gameGrid);
rotatedGame = shiftGameLeft(rotatedGame);
return rotateLeft90Deg(rotatedGame);
}

var buttons = document.querySelectorAll(“.js-restart-btn”);
var length = buttons.length;
for (var i = 0; i < length; i++) {
if (document.addEventListener) {
buttons[i].addEventListener(“click”, function () {
newGameStart();
});
} else {
buttons[i].attachEvent(“onclick”, function () {
newGameStart();
});
};
};

document.addEventListener(“keydown”, handleKeypress);
document.addEventListener(‘touchstart’, handleTouchStart, false);
document.addEventListener(‘touchmove’, handleTouchMove, false);

var xDown = null;
var yDown = null;

function handleTouchStart(evt) {
xDown = evt.touches[0].clientX;
yDown = evt.touches[0].clientY;
};

function handleTouchMove(evt) {
var prevGame = [].concat(game);
if (!xDown || !yDown) {
return;
}
var xUp = evt.touches[0].clientX;
var yUp = evt.touches[0].clientY;

var xDiff = xDown - xUp;
var yDiff = yDown - yUp;

if (Math.abs(xDiff) > Math.abs(yDiff)) {
if (xDiff > 0) {
game = shiftGameLeft(game);
} else {
game = shiftGameRight(game);
}
} else {
if (yDiff > 0) {
game = shiftGameUp(game);
} else {
game = shiftGameDown(game);
}
}
game = game.map(function (tile, index) {
if (tile) {
return _extends({}, tile, {
index: index
});
} else {
return null;
}
});
addRandomNumber();
updateDOM(prevGame, game);
if (gameOver()) {
setTimeout(function () {
endDiv.classList.add(‘active’);
}, 800);
return;
}
xDown = null;
yDown = null;
};

function handleKeypress(evt) {
var modifiers = event.altKey || event.ctrlKey || event.metaKey || event.shiftKey;
var whichKey = event.which;

var prevGame = [].concat(game);

if (!modifiers) {
event.preventDefault();
switch (whichKey) {
case 37:
game = shiftGameLeft(game);
break;
case 38:
game = shiftGameUp(game);
break;
case 39:
game = shiftGameRight(game);
break;
case 40:
game = shiftGameDown(game);
break;
}
game = game.map(function (tile, index) {
if (tile) {
return _extends({}, tile, {
index: index
});
} else {
return null;
}
});
addRandomNumber();
updateDOM(prevGame, game);
if (gameOver()) {
setTimeout(function () {
endDiv.classList.add(‘active’);
}, 800);
return;
}
}
}

function newGameStart() {
document.getElementById(‘tile-container’).innerHTML = ‘’;
endDiv.classList.remove(‘active’);
score = 0;
scoreDiv.innerHTML = score;
initGame();
drawBackground();
var previousGame = [].concat(game);
addRandomNumber();
addRandomNumber();
updateDOM(previousGame, game);
}

newGameStart();


写好后再把已知隐藏文件扩展名的√去掉,已经去了的就不用了,你会发现有.txt,重命名把“**.**”后面的txt改成js回车再点击确定就可以了。


我们先退出来后建一个重命名为 **css**的文件夹,点击后创建2个txt文件,第一个命名 **reset.min**,第二个命名 **style**先看第一个 **reset.min**代码:



html,body,div,span,applet,object,iframe,h1,h2,h3,h4,h5,h6,p,blockquote,pre,a,abbr,acronym,address,big,cite,code,del,dfn,em,img,ins,kbd,q,s,samp,small,strike,strong,sub,sup,tt,var,b,u,i,center,dl,dt,dd,ol,ul,li,fieldset,form,label,legend,table,caption,tbody,tfoot,thead,tr,th,td,article,aside,canvas,details,embed,figure,figcaption,footer,header,hgroup,menu,nav,output,ruby,section,summary,time,mark,audio,video{margin:0;padding:0;border:0;font-size:100%;font:inherit;vertical-align:baseline}article,aside,details,figcaption,figure,footer,header,hgroup,menu,nav,section{display:block}body{line-height:1}ol,ul{list-style:none}blockquote,q{quotes:none}blockquote:before,blockquote:after,q:before,q:after{content:‘’;content:none}table{border-collapse:collapse;border-spacing:0}


然后看 **style**的代码:



@charset “UTF-8”;
@import url(“https://fonts.googleapis.com/css?family=Arvo”);

  • {
    box-sizing: border-box;
    }

a {
color: #1B9AAA;
text-decoration: none;
border-bottom: 1px solid currentColor;
}
a:hover {
color: #14727e;
}
a:focus, a:active {
color: #0d4a52;
}

body,
html {
position: relative;
width: 100%;
height: 100%;
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-orient: vertical;
-webkit-box-direction: normal;
-ms-flex-direction: column;
flex-direction: column;
font-family: “Arvo”, Helvetica, sans-serif;
font-family: 12px;
color: #555;
background: #F8FFE5;
}

strong {
font-weight: bold;
}

p {
line-height: 1.6;
}

.inspired {
margin-top: 1em;
font-size: 0.9rem;
color: #9a9a95;
}

header {
color: #F8FFE5;
text-align: center;
}
header span {
display: inline-block;
box-sizing: border-box;
width: 4rem;
height: 4rem;
line-height: 4rem;
margin: 0 0.4rem;
background: #FFC43D;
}
header span:nth-of-type(2) {
background: #EF476F;
}
header span:nth-of-type(3) {
background: #1B9AAA;
}
header span:nth-of-type(4) {
background: #06D6A0;
}

h1 {
font-size: 2.2rem;
}

.directions {
padding: 2rem;
border-top: 1px solid #9a9a95;
border-bottom: 1px solid #9a9a95;
}

.container {
margin: 0 auto;
padding-bottom: 3.5rem;
-webkit-box-flex: 1;
-ms-flex: 1;
flex: 1;
width: 100%;
max-width: 550px;
text-align: center;
}
header .container {
padding: 0;
padding: 2rem 4rem;
max-width: 900px;
}

.scores {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-pack: center;
-ms-flex-pack: center;
justify-content: center;
}

.score-container {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-pack: center;
-ms-flex-pack: center;
justify-content: center;
-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
margin: 1.8rem;
font-size: 1.2rem;
line-height: 1;
color: #555;
}
.score-container.best-score {
color: #9a9a95;
}

.score {
margin-left: 1rem;
position: relative;
font-weight: bold;
font-size: 1.5rem;
vertical-align: middle;
text-align: right;
}

.game {
position: relative;
margin: 0 auto;
background: #9a9a95;
padding: 7px;
display: inline-block;
border-radius: 3px;
box-sizing: border-box;
}

.tile-container {
border-radius: 6px;
position: relative;
width: 400px;
height: 400px;
}

.tile, .background {
display: block;
color: #F8FFE5;
position: absolute;
width: 100px;
height: 100px;
box-sizing: border-box;
text-align: center;
}

.background {
z-index: 1;
text-align: center;
border: 7px solid #9a9a95;
background-color: #F8FFE5;
}

.tile {
opacity: 0;
z-index: 2;
background: #FFC43D;
color: #F8FFE5;
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
-webkit-box-pack: center;
-ms-flex-pack: center;
justify-content: center;
font-size: 1.8rem;
align-items: center;
-webkit-transition: 110ms ease-in-out;
transition: 110ms ease-in-out;
border-radius: 3px;
border: 7px solid #9a9a95;
box-sizing: border-box;
}
.tile–4 {
background: #EF476F;
color: #F8FFE5;
}
.tile–8 {
background: #1B9AAA;
color: #F8FFE5;
}
.tile–16 {
background: #06D6A0;
color: #F8FFE5;
}
.tile–32 {
background: #f37694;
color: #F8FFE5;
}
.tile–64 {
background: #22c2d6;
color: #F8FFE5;
}
.tile–128 {
background: #17f8be;
color: #F8FFE5;
}
.tile–256 {
background: #ffd470;
color: #F8FFE5;
}
.tile–512 {
background: #eb184a;
color: #F8FFE5;
}
.tile–1024 {
background: #14727e;
color: #F8FFE5;
}
.tile–2048 {
background: #05a47b;
color: #F8FFE5;
}
.tile–pop {
-webkit-animation: pop 0.3s ease-in-out;
animation: pop 0.3s ease-in-out;
-webkit-animation-fill-mode: forwards;
animation-fill-mode: forwards;
}
.tile–shrink {
-webkit-animation: shrink 0.5s ease-in-out;
animation: shrink 0.5s ease-in-out;
-webkit-animation-fill-mode: forwards;
animation-fill-mode: forwards;
}

.add {
position: absolute;
opacity: 0;
left: 120%;
top: 0;
font-size: 1rem;
color: #1B9AAA;
}
.add.active {
-webkit-animation: add 0.8s ease-in-out;
animation: add 0.8s ease-in-out;
}

@-webkit-keyframes add {
0% {
opacity: 1;
top: 0;
}
100% {
opacity: 0;
top: -100%;
}
}

@keyframes add {
0% {
opacity: 1;
top: 0;
}
100% {
opacity: 0;
top: -100%;
}
}
@-webkit-keyframes pop {
0% {
-webkit-transform: scale(0.5);
transform: scale(0.5);
opacity: 0;
}
90% {
-webkit-transform: scale(1.1);
transform: scale(1.1);
opacity: 1;
}
100% {
-webkit-transform: scale(1);
transform: scale(1);
opacity: 1;
}
}
@keyframes pop {
0% {
-webkit-transform: scale(0.5);
transform: scale(0.5);
opacity: 0;
}
90% {
-webkit-transform: scale(1.1);
transform: scale(1.1);
opacity: 1;
}
100% {
-webkit-transform: scale(1);
transform: scale(1);
opacity: 1;
}
}
@-webkit-keyframes shrink {
0% {
-webkit-transform: scale(1);
transform: scale(1);
opacity: 1;
}
100% {
-webkit-transform: scale(0.9);
transform: scale(0.9);
opacity: 0.9;
}
}
@keyframes shrink {
0% {
-webkit-transform: scale(1);
transform: scale(1);
opacity: 1;
}
100% {
-webkit-transform: scale(0.9);
transform: scale(0.9);
opacity: 0.9;
}
}
.end {
opacity: 0;
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: -1;
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-orient: vertical;
-webkit-box-direction: normal;
-ms-flex-direction: column;
flex-direction: column;
-webkit-box-pack: center;
-ms-flex-pack: center;
justify-content: center;
-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
background: rgba(85, 85, 85, 0.9);
color: white;
font-size: 2rem;
-webkit-transition: opacity 0.3s ease-in-out;
transition: opacity 0.3s ease-in-out;
}
.end btn {
margin-top: 1rem;
}
.end.active {
opacity: 1;
z-index: 1000;
}

.monkey {
font-size: 3rem;
margin: 1rem 0;
}

.btn {
font-family: inherit;
font-size: 1rem;
border: none;
background: #1B9AAA;
letter-spacing: 1px;
color: white;

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数前端工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Web前端开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
img
img
img
img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上前端开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新

如果你觉得这些内容对你有帮助,可以添加V获取:vip1024c (备注前端)
img

最后

资料过多,篇幅有限

自古成功在尝试。不尝试永远都不会成功。勇敢的尝试是成功的一半。

一个人可以走的很快,但一群人才能走的更远。不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎扫码加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
img

-1712829205220)]
[外链图片转存中…(img-Bi4t2VUO-1712829205220)]
[外链图片转存中…(img-YNfpxy7u-1712829205220)]
[外链图片转存中…(img-cUjRIDpY-1712829205221)]
[外链图片转存中…(img-4Ujz1YjR-1712829205221)]

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上前端开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新

如果你觉得这些内容对你有帮助,可以添加V获取:vip1024c (备注前端)
[外链图片转存中…(img-L2pg8jKy-1712829205222)]

最后

[外链图片转存中…(img-UIiElBqR-1712829205222)]

[外链图片转存中…(img-CtbQKjWp-1712829205222)]

资料过多,篇幅有限

自古成功在尝试。不尝试永远都不会成功。勇敢的尝试是成功的一半。

一个人可以走的很快,但一群人才能走的更远。不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎扫码加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
[外链图片转存中…(img-PDHHdZZd-1712829205223)]

转载请注明出处或者链接地址:https://www.qianduange.cn//article/12274.html
标签
评论
发布的文章

前端——12.表单标签

2024-06-21 00:06:57

src、href和url含义及区别

2024-06-21 00:06:56

CSS知识点大全

2024-06-21 00:06:16

JS垃圾回收机制(GC)

2024-06-21 00:06:03

大家推荐的文章
会员中心 联系我 留言建议 回顶部
复制成功!