o1-mini-2024-09-12· OpenAI
WildChat-4.8M (AI2)
テトリスをHTML+CSS+JSを使って一つのファイルで作成してください
- User
テトリスをHTML+CSS+JSを使って一つのファイルで作成してください
- Assistant
もちろんです!以下に、HTML、CSS、JavaScriptを一つのファイルにまとめたシンプルなテトリスゲームのコードを提供します。このファイルを保存してブラウザで開くと、テトリスを楽しむことができます。
<!DOCTYPE html> <html lang="ja"> <head> <meta charset="UTF-8"> <title>シンプルテトリス</title> <style> body { background: #000; display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0; color: #fff; font-family: Arial, sans-serif; } canvas { border: 2px solid #fff; background: #111; } #score { position: absolute; top: 20px; left: 20px; font-size: 20px; } #game-over { position: absolute; top: 50%; transform: translateY(-50%); text-align: center; font-size: 36px; display: none; } </style> </head> <body> <div id="score">スコア: 0</div> <div id="game-over">ゲームオーバー<br>リロードして再挑戦</div> <canvas width="300" height="600" id="tetris"></canvas> <script> const canvas = document.getElementById('tetris'); const ctx = canvas.getContext('2d'); ctx.scale(30, 30); const matrix = [ [], ]; function arenaSweep() { let rowCount = 1; outer: for (let y = arena.length -1; y > 0; --y){ for(let x=0; x < arena[y].length; ++x){ if(arena[y][x] === 0){ continue outer; } } const row = arena.splice(y,1)[0].fill(0); arena.unshift(row); ++y; player.score += rowCount * 10; rowCount *= 2; } document.getElementById('score').innerText = 'スコア: ' + player.score; } function collide(arena, player){ const m = player.matrix; const o = player.pos; for(let y=0; y < m.length; ++y){ for(let x=0; x < m[y].length; ++x){ if(m[y][x] !==0 && (arena[y + o.y] && arena[y + o.y][x + o.x]) !==0){ return true; } } } return false; } function createMatrix(w, h){ const matrix = []; while(h--){ matrix.push(new Array(w).fill(0)); } return matrix; } function createPiece(type){ if(type === 'T'){ return [ [0,0,0], [1,1,1], [0,1,0], ]; } else if(type === 'O'){ return [ [2,2], [2,2], ]; } else if(type === 'L'){ return [ [0,3,0], [0,3,0], [0,3,3], ]; } else if(type === 'J'){ return [ [0,4,0], [0,4,0], [4,4,0], ]; } else if(type === 'I'){ return [ [0,5,0,0], [0,5,0,0], [0,5,0,0], [0,5,0,0], ]; } else if(type === 'S'){ return [ [0,6,6], [6,6,0], [0,0,0], ]; } else if(type === 'Z'){ return [ [7,7,0], [0,7,7], [0,0,0], ]; } } function draw() { ctx.fillStyle = '#111'; ctx.fillRect(0, 0, canvas.width, canvas.height); drawMatrix(arena, {x:0, y:0}); drawMatrix(player.matrix, player.pos); } function drawMatrix(matrix, offset){ matrix.forEach((row, y) => { row.forEach((value, x) => { if(value !==0){ ctx.fillStyle = colors[value]; ctx.fillRect(x + offset.x, y + offset.y, 1,1); } }); }); } function merge(arena, player){ player.matrix.forEach((row, y) => { row.forEach((value, x) => { if(value !==0){ arena[y + player.pos.y][x + player.pos.x] = value; } }); }); } function playerDrop(){ player.pos.y++; if(collide(arena, player)){ player.pos.y--; merge(arena, player); playerReset(); arenaSweep(); updateScore(); } dropCounter = 0; } function playerMove(dir){ player.pos.x += dir; if(collide(arena, player)){ player.pos.x -= dir; } } function playerReset(){ const pieces = 'ILJOTSZ'; player.matrix = createPiece(pieces[pieces.length * Math.random() |0]); player.pos.y = 0; player.pos.x = (arena[0].length / 2 |0) - (player.matrix[0].length / 2 |0); if(collide(arena, player)){ arena.forEach(row => row.fill(0)); player.score = 0; document.getElementById('score').innerText = 'スコア: ' + player.score; document.getElementById('game-over').style.display = 'block'; cancelAnimationFrame(animationId); } } function playerRotate(dir){ rotate(player.matrix, dir); if(collide(arena, player)){ rotate(player.matrix, -dir); } } function rotate(matrix, dir){ for(let y=0; y < matrix.length; ++y){ for(let x=0; x < y; ++x){ [ matrix[x][y], matrix[y][x], ] = [ matrix[y][x], matrix[x][y], ]; } } if(dir >0){ matrix.forEach(row => row.reverse()); } else { matrix.reverse(); } } let dropCounter = 0; let dropInterval = 1000; let lastTime = 0; function update(time = 0){ const deltaTime = time - lastTime; lastTime = time; dropCounter += deltaTime; if(dropCounter > dropInterval){ playerDrop(); } draw(); animationId = requestAnimationFrame(update); } function updateScore(){ document.getElementById('score').innerText = 'スコア: ' + player.score; } document.addEventListener('keydown', event => { if(event.keyCode === 37){ playerMove(-1); } else if(event.keyCode === 39){ playerMove(1); } else if(event.keyCode === 40){ playerDrop(); } else if(event.keyCode === 81){ playerRotate(-1); } else if(event.keyCode === 87){ playerRotate(1); } }); const colors = [ null, '#FF0D72', '#0DC2FF', '#0DFF72', '#F538FF', '#FF8E0D', '#FFE138', '#3877FF', ]; const arena = createMatrix(10, 20); const player = { pos: {x:0, y:0}, matrix: null, score: 0, }; playerReset(); update(); </script> </body> </html>説明
-
HTML部分:
<canvas>タグがゲームの描画に使用されています。幅は300ピクセル、高さは600ピクセルに設定されています。- スコア表示用の
<div id="score">とゲームオーバー表示用の<div id="game-over">があります。
-
CSS部分:
- 全体の背景を黒に設定し、キャンバスに白い枠線を付けています。
- スコアとゲームオーバーのテキストのスタイルも設定されています。
-
JavaScript部分:
- キャンバス設定: テトリスのブロックは30pxのグリッドにスケーリングされています。
- ゲームロジック:
- テトリスの「アリーナ」(ゲームエリア)を2D配列で管理。
- 各テトリミノ(I, O, T, S, Z, J, L)の形を定義。
- ブロックの衝突判定、回転、ラインの消去、スコア計算などの機能を実装。
- ユーザー入力: キーボードの矢印キーや
Q、Wキーでブロックの移動や回転が可能。 - 描画:
draw関数でアリーナとプレイヤーのブロックを描画。 - ゲームループ:
requestAnimationFrameを使ってゲームを継続的に更新。
操作方法
- 左矢印キー(←): ブロックを左に移動
- 右矢印キー(→): ブロックを右に移動
- 下矢印キー(↓): ブロックを素早く落下
- Qキー: ブロックを反時計回りに回転
- Wキー: ブロックを時計回りに回転
使用方法
- 上記のコードを全てコピーします。
.html拡張子でファイルを保存します(例:tetris.html)。- 保存したファイルをブラウザ(Chrome、Firefox、Edgeなど)で開きます。
- キーボードを使ってテトリスを楽しんでください!
シンプルなテトリスゲームですが、基本的な機能は備えています。さらに機能を追加したり、デザインをカスタマイズしたりして、自分だけのテトリスゲームを作成することも可能です。楽しんでください!
-