HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
Any URLs added here will be added as <link>
s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
Any URL's added here will be added as <script>
s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
Search for and use JavaScript packages from npm here. By selecting a package, an import
statement will be added to the top of the JavaScript editor for this package.
Using packages here is powered by esm.sh, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ESM usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<div id='score'>分数: <span id='realScore'>0</span></div>
<section id='container'>
<div id='0-0'></div><div id='0-1'></div><div id='0-2'></div><div id='0-3'></div><div id='1-0'></div><div id='1-1'></div><div id='1-2'></div><div id='1-3'></div><div id='2-0'></div><div id='2-1'></div><div id='2-2'></div><div id='2-3'></div><div id='3-0'></div><div id='3-1'></div><div id='3-2'></div><div id='3-3'></div>
</section>
*{
margin:0;
padding:0;
}
html{
font-size:12px;
}
body{
background:#faf8ef;
color:#776e65;
font-family: "Clear Sans", "Helvetica Neue", Arial, sans-serif;
}
#container{
position:relative;
left:35%;
top:15rem;
background:#bbada0;
width:32rem;
height:32rem;
padding:0.5rem;
border-radius:0.7rem;
}
#score{
display:inline-block;
position:absolute;
font-size:3rem;
padding-top:1rem;
text-align:center;
top:5rem;
left:44%;
width:16rem;
height:4rem;
background:#bbada0;
border-radius:0.5rem;
}
#score span{
color:white;
}
#container div{
font-weight:bold;
padding-top:1.5rem;
font-size:4rem;
color:white;
text-align:center;
box-sizing:border-box;
display:inline-block;
width:8rem;
height:8rem;
background:rgba(238, 228, 218, 0.35);
vertical-align:middle;
border:0.5rem solid #bbada0;
}
.bgcolor-2{
background:'#green';
}
.bgcolor{
background:rgba(238, 228, 218, 0.35);
}
.bgcolor-4{
background:'red';
}
/*
Author: 林水溶
英文名:shuiRongLin
博客: https://shuirong.github.io
!-------转载请保留上面作者信息-------!人最起码的东西得有是吧。
思路:不管是这个1024游戏还是其他任何程序,思路最重要,用代码写出来反而是次要的东西!
程序流程:打开页面,程序运行-->先随机出个4-->人操作(上下左右)-->(根据人的按键)对棋盘进行解析,
该合并合并,该不动不动,该移动的移动,同时随机再出个4-->重复上一步-->在人按了512步之后,每次人按之后判断2048出现了没
PS:之所以512步之前不判断赢了没,是因为最快赢需要512步,但实际情况下不太可能。之前判断没必要
*/
var checkerBoard = [
[0,0,0,0],
[0,0,0,0],
[0,0,0,0],
[0,0,0,0]
] // 初始化游戏棋盘,0就是什么都没有,
var checkerBoardId = [
['0-0','0-1','0-2','0-3'],
['1-0','1-1','1-2','1-3'],
['2-0','2-1','2-2','2-3'],
['3-0','3-1','3-2','3-3']
]//棋盘对应位置的元素id,方便后面用
var color = {
'2': '#eee4da',
'4': '#ede0c8',
'8': '#f2b179',
'16': '#f59563',
'32': '#f67c5f',
'64': '#f65e3b',
'128': '#edcf72',
'256': '#edcc61',
'512': '#edc850',
'1024': '#edc53f',
'2048': '#edc22e',
}
var score = 0; //记录分数
window.onload = function(){
start();
}
//主函数
function start(){
randomTwo();
randomTwo();
//给键盘的上下左右绑定监听事件 上 下 左 右--38 40 37 39
$(document).keyup(function(event){
if(event.which>=37&&event.which<=40){
judge(event.which);//把用户的按键行为传进去
}
});
}
//在棋盘上随机出现2
function randomTwo(){
let status = false;//随机4是不是搞定了
while(!status){//随机xy直到棋盘上对应位置的值是0
let x = Math.floor(Math.random()*4);
let y = Math.floor(Math.random()*4);
if(checkerBoard[x][y]===0){
checkerBoard[x][y]=2;
status = true;
}
}
render();
}
//根据用户的按键对棋盘上现有的棋子依照游戏规则进行计算,将结果体现在全局变量checkerBoard上
// 上下左右不管哪个,棋盘上的棋子每个都有三种可能,拿上举例。1:它上面是0或者是边缘。2:它上面有数字且不一样
//3:上面有数字且一样。这就12中情况,得想办法抽象化,代码重用。
function judge(button){
if(button==38){ //上
for(var j=0; j<4; j++){
for(var i=1; i<4;i++){
if(checkerBoard[i][j]!==0){ //第一排的话,不用动,所以从第二排开始计算
for(var m=0; m<i; m++){
if(checkerBoard[m][j]==0&&!shuCanMove(j,i,m)){
checkerBoard[m][j] = checkerBoard[i][j];
checkerBoard[i][j] = 0;
}else if(checkerBoard[m][j]==checkerBoard[i][j]&&!shuCanMove(j,i,m)){
checkerBoard[m][j] = (checkerBoard[m][j] * 2);
checkerBoard[i][j] = 0;
score += checkerBoard[m][j];
}
}
}
}
}
}else if(button==40){ //下
for(var j=0; j<4; j++){
for(var i=2; i>=0;i--){
if(checkerBoard[i][j]!==0){
for(var m=3; m>i;m--){
if(checkerBoard[m][j]==0&&!shuCanMove(j,i,m)){
checkerBoard[m][j] = checkerBoard[i][j];
checkerBoard[i][j] = 0;
}else if(checkerBoard[m][j] == checkerBoard[i][j] && !shuCanMove(j,i,m)){
checkerBoard[m][j] = (checkerBoard[m][j] * 2);
checkerBoard[i][j] = 0;
score += checkerBoard[m][j];
}
}
}
}
}
}else if(button == 37){
for(var i=0; i<4; i++){
for(var j=1; j<4; j++){
if(checkerBoard[i][j]!=0){
for(var m=0; m<j; m++){
if(checkerBoard[i][m]==0 && !hengCanMove(i,j,m)){
checkerBoard[i][m] = checkerBoard[i][j];
checkerBoard[i][j] = 0;
}else if(checkerBoard[i][m] == checkerBoard[i][j]&&!hengCanMove(i,j,m)){
checkerBoard[i][m] = (checkerBoard[i][m]*2);
checkerBoard[i][j] = 0;
score += checkerBoard[i][m];
}
}
}
}
}
}else if(button == 39){
for(var i=0; i<4; i++){
for(var j=2; j>=0; j--){
if(checkerBoard[i][j]!=0){
for(var m=3; m>j; m--){
if(checkerBoard[i][m]==0&&!hengCanMove(i,j,m)){
checkerBoard[i][m] = checkerBoard[i][j];
checkerBoard[i][j] = 0;
}else if(checkerBoard[i][m] == checkerBoard[i][j]&&!hengCanMove(i,j,m)){
checkerBoard[i][m] = (checkerBoard[i][m]*2);
checkerBoard[i][j] = 0;
score += checkerBoard[i][m];
}
}
}
}
}
}
render();
randomTwo();
}
//根据checkerBoard将棋盘的值重新渲染一遍
function render(){
checkerBoard.forEach(function(x,xIndex){
x.forEach(function(y,yIndex){
if(y!==0){
text(checkerBoardId[xIndex][yIndex],y);
}else{
text(checkerBoardId[xIndex][yIndex],'');
}
})
});
document.getElementById('realScore').innerText = score;
}
//不知道为何 $(id).text(123) 和 $(id).html(123) 不能用了,不管它,自己造了个
function text(id,text){
let ele = document.getElementById(id);
ele.innerText = text;
ele.setAttribute('style','background:'+color[text]);
}
function conso(){
console.log(checkerBoard[0][0],checkerBoard[0][1],checkerBoard[0][2],checkerBoard[0][3]);
console.log(checkerBoard[1][0],checkerBoard[1][1],checkerBoard[1][2],checkerBoard[1][3]);
console.log(checkerBoard[2][0],checkerBoard[2][1],checkerBoard[2][2],checkerBoard[2][3]);
console.log(checkerBoard[3][0],checkerBoard[3][1],checkerBoard[3][2],checkerBoard[3][3]);
console.log('-----------------------');
}
function shuCanMove(j,i,m){
var from,to;
if(i>m){
from = m;
to = i;
}else{
from = i;
to = m;
}
for(var n=from+1; n<to;n++){
if(checkerBoard[n][j]!=0){
return true;
}
}
return false;
}
function hengCanMove(j,i,m){
var from,to;
if(i>m){
from = m;
to = i;
}else{
from = i;
to = m;
}
for(var n=from+1;n<to;n++){
if(checkerBoard[j][n]!=0){
return true;
}
}
return false;
}
Also see: Tab Triggers