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.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Tetris</title>
<style>
body {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
margin: 0;
background-color: #111;
color: #fff;
font-family: Arial, sans-serif;
}
#game {
display: grid;
grid-template-columns: repeat(10, 20px);
grid-template-rows: repeat(20, 20px);
gap: 1px;
background-color: #222;
margin-bottom: 20px;
}
.cell {
width: 20px;
height: 20px;
background-color: #333;
}
.filled {
background-color: #f39c12;
}
#controls {
text-align: center;
}
#controls label {
margin-right: 10px;
}
</style>
</head>
<body>
<div id="game"></div>
<div id="controls">
<label for="rareChance">ロングテトリス棒出現確率:</label>
<input type="range" id="rareChance" min="0.01" max="0.99" step="0.01" value="0.1">
<span id="chanceValue">0.10</span>
</div>
<script src="tetris.js"></script>
</body>
</html>
const game = document.getElementById("game");
const rareChanceInput = document.getElementById("rareChance");
const chanceValueDisplay = document.getElementById("chanceValue");
const ROWS = 20;
const COLS = 10;
// グリッドを初期化
const grid = Array.from({ length: ROWS }, () => Array(COLS).fill(0));
// 通常のテトリミノ
const tetrominoes = [
[[1, 1, 1, 1]], // I
[
[1, 1],
[1, 1],
], // O
[
[0, 1, 0],
[1, 1, 1],
], // T
[
[1, 1, 0],
[0, 1, 1],
], // S
[
[0, 1, 1],
[1, 1, 0],
], // Z
[
[1, 1, 1],
[1, 0, 0],
], // L
[
[1, 1, 1],
[0, 0, 1],
], // J
];
// レアな「ロング棒」長さを8に変更
const rareTetromino = [
[[1, 1, 1, 1, 1, 1, 1, 1]], // 長さ8の棒
];
// テトリミノの状態
let currentTetromino = getRandomTetromino();
let currentRow = 0;
let currentCol = Math.floor((COLS - currentTetromino[0].length) / 2);
// グリッドを描画
function drawGrid() {
game.innerHTML = "";
for (let row = 0; row < ROWS; row++) {
for (let col = 0; col < COLS; col++) {
const cell = document.createElement("div");
cell.classList.add("cell");
if (grid[row][col] === 1) {
cell.classList.add("filled");
}
game.appendChild(cell);
}
}
}
// テトリミノを描画
function drawTetromino() {
currentTetromino.forEach((row, r) => {
row.forEach((value, c) => {
if (value && currentRow + r >= 0) {
grid[currentRow + r][currentCol + c] = 1;
}
});
});
}
// テトリミノを削除
function clearTetromino() {
currentTetromino.forEach((row, r) => {
row.forEach((value, c) => {
if (value && currentRow + r >= 0) {
grid[currentRow + r][currentCol + c] = 0;
}
});
});
}
// 衝突判定
function isValidMove(newRow, newCol, newTetromino) {
return newTetromino.every((row, r) =>
row.every((value, c) => {
const x = newCol + c;
const y = newRow + r;
return (
!value ||
(y >= 0 && y < ROWS && x >= 0 && x < COLS && grid[y][x] === 0)
);
})
);
}
// ラインを削除
function clearLines() {
for (let row = ROWS - 1; row >= 0; row--) {
if (grid[row].every((cell) => cell === 1)) {
grid.splice(row, 1);
grid.unshift(Array(COLS).fill(0));
row++;
}
}
}
// ランダムなテトリミノを取得(レア形状の低確率出現を含む)
function getRandomTetromino() {
const rareChance = parseFloat(rareChanceInput.value); // スライダーの値を取得
if (Math.random() < rareChance) { // ロング棒の出現確率
return rareTetromino[0];
} else {
return tetrominoes[Math.floor(Math.random() * tetrominoes.length)];
}
}
// テトリミノを回転
function rotateTetromino() {
const newTetromino = currentTetromino[0].map((_, colIndex) =>
currentTetromino.map((row) => row[colIndex]).reverse()
);
if (isValidMove(currentRow, currentCol, newTetromino)) {
currentTetromino = newTetromino;
}
}
// ゲームのループ
function gameLoop() {
clearTetromino();
if (isValidMove(currentRow + 1, currentCol, currentTetromino)) {
currentRow++;
} else {
drawTetromino();
clearLines();
// 次のテトリミノを生成
currentTetromino = getRandomTetromino();
currentRow = 0;
currentCol = Math.floor((COLS - currentTetromino[0].length) / 2);
// ゲームオーバー判定
if (!isValidMove(currentRow, currentCol, currentTetromino)) {
alert("Game Over");
grid.forEach((row) => row.fill(0));
currentTetromino = getRandomTetromino();
currentRow = 0;
currentCol = Math.floor((COLS - currentTetromino[0].length) / 2);
}
}
drawTetromino();
drawGrid();
}
// キー操作
document.addEventListener("keydown", (e) => {
clearTetromino();
if (e.key === "ArrowLeft" && isValidMove(currentRow, currentCol - 1, currentTetromino)) {
currentCol--;
} else if (e.key === "ArrowRight" && isValidMove(currentRow, currentCol + 1, currentTetromino)) {
currentCol++;
} else if (e.key === "ArrowDown") {
if (isValidMove(currentRow + 1, currentCol, currentTetromino)) {
currentRow++;
}
} else if (e.key === "ArrowUp") {
rotateTetromino();
}
drawTetromino();
drawGrid();
});
// スライダーの値を表示
rareChanceInput.addEventListener("input", () => {
chanceValueDisplay.textContent = parseFloat(rareChanceInput.value).toFixed(2);
});
// ゲーム開始
setInterval(gameLoop, 500);
drawGrid();
Also see: Tab Triggers