o1-mini-2024-09-12· OpenAI
WildChat-4.8M (AI2)
寫一個網頁版的游戲,一個3*3的魔方
- User
寫一個網頁版的游戲,一個3*3的魔方
- Assistant
寫一個網頁版的游戲,一個3*3的魔方
要創建一個網頁版的 3x3 魔方(類似於魯比克方塊),我們可以使用 HTML、CSS 和 JavaScript 來實現一個簡單的 2D 模擬版本。這個版本將允許用戶旋轉魔方的各個面來混合和還原顏色。
以下是一個基本的示例,展示如何實現一個簡單的 3x3 魔方遊戲:
首先,建立基本的 HTML 結構,包含六個方塊面(前、後、上、下、左、右)。
<!DOCTYPE html>
<html lang="zh-TW">
<head>
<meta charset="UTF-8">
<title>3x3 魔方遊戲</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h1>3x3 魔方遊戲</h1>
<div class="cube">
<div class="face front" data-face="front"></div>
<div class="face back" data-face="back"></div>
<div class="face up" data-face="up"></div>
<div class="face down" data-face="down"></div>
<div class="face left" data-face="left"></div>
<div class="face right" data-face="right"></div>
</div>
<div class="controls">
<button onclick="rotate('front', 'clockwise')">前順時針旋轉</button>
<button onclick="rotate('front', 'counter')">前逆時針旋轉</button>
<!-- 添加更多旋轉控制按鈕 -->
</div>
<script src="script.js"></script>
</body>
</html>
接下來,使用 CSS 來設計魔方的外觀。這裡我們將每個面劃分為 3x3 的小方格,並賦予不同的顏色。
/* styles.css */
body {
font-family: Arial, sans-serif;
text-align: center;
background-color: #f0f0f0;
}
.cube {
position: relative;
width: 300px;
height: 300px;
margin: 20px auto;
perspective: 1000px;
display: flex;
flex-wrap: wrap;
}
.face {
width: 100px;
height: 100px;
box-sizing: border-box;
border: 2px solid #000;
display: grid;
grid-template: repeat(3, 1fr) / repeat(3, 1fr);
}
.face div {
width: 30px;
height: 30px;
margin: 5px;
background-color: #fff;
}
.front { background-color: #f00; }
.back { background-color: #00f; }
.up { background-color: #fff; }
.down { background-color: #ffff00; }
.left { background-color: #ffa500; }
.right { background-color: #008000; }
.controls {
margin-top: 20px;
}
button {
padding: 10px 20px;
margin: 5px;
font-size: 16px;
cursor: pointer;
}
最後,使用 JavaScript 來實現旋轉功能。這裡我們將實現前面順時針和逆時針旋轉的基本功能。由於完整的魔方旋轉邏輯較為複雜,這裡僅提供一個簡化的示例。
// script.js
// 初始化魔方狀態
const cubeState = {
front: Array(9).fill('red'),
back: Array(9).fill('blue'),
up: Array(9).fill('white'),
down: Array(9).fill('yellow'),
left: Array(9).fill('orange'),
right: Array(9).fill('green')
};
// 渲染魔方
function renderCube() {
document.querySelectorAll('.face').forEach(face => {
const faceName = face.getAttribute('data-face');
face.innerHTML = '';
cubeState[faceName].forEach(color => {
const square = document.createElement('div');
square.style.backgroundColor = color;
face.appendChild(square);
});
});
}
// 旋轉指定面的顏色順序
function rotateFace(face, direction) {
let faceColors = cubeState[face];
if (direction === 'clockwise') {
faceColors = [
faceColors[6], faceColors[3], faceColors[0],
faceColors[7], faceColors[4], faceColors[1],
faceColors[8], faceColors[5], faceColors[2],
];
} else if (direction === 'counter') {
faceColors = [
faceColors[2], faceColors[5], faceColors[8],
faceColors[1], faceColors[4], faceColors[7],
faceColors[0], faceColors[3], faceColors[6],
];
}
cubeState[face] = faceColors;
}
// 主旋轉函數
function rotate(face, direction) {
rotateFace(face, direction);
// 這裡需要添加相鄰面的顏色交換邏輯
// 由於邏輯較為複雜,此處僅實現單個面的旋轉
renderCube();
}
// 初始化渲染
renderCube();
上述示例僅實現了單個面的旋轉。要實現完整的 3x3 魔方旋轉,您需要處理相鄰面的顏色交換邏輯。這涉及到:
由於這些邏輯較為複雜,您可以參考以下資源進一步學習和實現:
為了更好地展示效果,以下是一個更完整的示例,實現了前面和上面的旋轉:
<!DOCTYPE html>
<html lang="zh-TW">
<head>
<meta charset="UTF-8">
<title>3x3 魔方遊戲</title>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
background-color: #f0f0f0;
}
.cube {
display: flex;
justify-content: center;
align-items: center;
flex-wrap: wrap;
width: 310px;
margin: 20px auto;
}
.face {
width: 100px;
height: 100px;
box-sizing: border-box;
border: 2px solid #000;
display: grid;
grid-template: repeat(3, 1fr) / repeat(3, 1fr);
margin: 2px;
}
.face div {
width: 30px;
height: 30px;
margin: 5px;
background-color: #fff;
}
.front { background-color: #f00; }
.back { background-color: #00f; }
.up { background-color: #fff; }
.down { background-color: #ff0; }
.left { background-color: #ffa500; }
.right { background-color: #008000; }
.controls {
margin-top: 20px;
}
button {
padding: 10px 20px;
margin: 5px;
font-size: 16px;
cursor: pointer;
}
</style>
</head>
<body>
<h1>3x3 魔方遊戲</h1>
<div class="cube">
<div class="face up" data-face="up"></div>
<div class="face front" data-face="front"></div>
<div class="face right" data-face="right"></div>
<div class="face back" data-face="back"></div>
<div class="face left" data-face="left"></div>
<div class="face down" data-face="down"></div>
</div>
<div class="controls">
<button onclick="rotate('front', 'clockwise')">前順時針旋轉</button>
<button onclick="rotate('front', 'counter')">前逆時針旋轉</button>
<button onclick="rotate('up', 'clockwise')">上順時針旋轉</button>
<button onclick="rotate('up', 'counter')">上逆時針旋轉</button>
<!-- 添加更多旋轉控制按鈕 -->
</div>
<script>
// 初始化魔方狀態
const cubeState = {
front: Array(9).fill('red'),
back: Array(9).fill('blue'),
up: Array(9).fill('white'),
down: Array(9).fill('yellow'),
left: Array(9).fill('orange'),
right: Array(9).fill('green')
};
// 渲染魔方
function renderCube() {
document.querySelectorAll('.face').forEach(face => {
const faceName = face.getAttribute('data-face');
face.innerHTML = '';
cubeState[faceName].forEach(color => {
const square = document.createElement('div');
square.style.backgroundColor = color;
face.appendChild(square);
});
});
}
// 旋轉指定面的顏色順序
function rotateFace(face, direction) {
let faceColors = cubeState[face];
if (direction === 'clockwise') {
faceColors = [
faceColors[6], faceColors[3], faceColors[0],
faceColors[7], faceColors[4], faceColors[1],
faceColors[8], faceColors[5], faceColors[2],
];
} else if (direction === 'counter') {
faceColors = [
faceColors[2], faceColors[5], faceColors[8],
faceColors[1], faceColors[4], faceColors[7],
faceColors[0], faceColors[3], faceColors[6],
];
}
cubeState[face] = faceColors;
}
// 交換相鄰面的顏色
function swapColors(face, direction) {
// 這裡需要根據旋轉的 face 和 direction 來交換相鄰面的顏色
// 由於實現較為複雜,此處僅示範前面旋轉相鄰面顏色的交換
if (face === 'front') {
if (direction === 'clockwise') {
const upRow = cubeState.up.slice(6, 9);
const rightCol = cubeState.right.filter((_, i) => i % 3 === 0);
const downRow = cubeState.down.slice(0, 3).reverse();
const leftCol = cubeState.left.filter((_, i) => i % 3 === 2).reverse();
// 交換顏色
cubeState.up.splice(6, 3, ...leftCol);
cubeState.left.splice(2, 1, rightCol[0]);
cubeState.left.splice(5, 1, rightCol[1]);
cubeState.left.splice(8, 1, rightCol[2]);
cubeState.down.splice(0, 3, ...upRow);
cubeState.right.splice(0, 1, downRow[0]);
cubeState.right.splice(3, 1, downRow[1]);
cubeState.right.splice(6, 1, downRow[2]);
} else {
const upRow = cubeState.up.slice(6, 9);
const rightCol = cubeState.right.filter((_, i) => i % 3 === 0);
const downRow = cubeState.down.slice(0, 3).reverse();
const leftCol = cubeState.left.filter((_, i) => i % 3 === 2).reverse();
// 交換顏色
cubeState.up.splice(6, 3, ...rightCol);
cubeState.right.splice(0, 1, downRow[0]);
cubeState.right.splice(3, 1, downRow[1]);
cubeState.right.splice(6, 1, downRow[2]);
cubeState.down.splice(0, 3, ...leftCol);
cubeState.left.splice(2, 1, upRow[0]);
cubeState.left.splice(5, 1, upRow[1]);
cubeState.left.splice(8, 1, upRow[2]);
}
}
// 類似地,實現其他面的相鄰面交換
}
// 主旋轉函數
function rotate(face, direction) {
rotateFace(face, direction);
swapColors(face, direction);
renderCube();
}
// 初始化渲染
renderCube();
</script>
</body>
</html>
完整實現:完整實現一個 3x3 魔方涉及到詳細的旋轉邏輯和狀態管理。建議深入學習魔方的旋轉規則,並考慮使用更高效的數據結構來管理魔方狀態。
3D 效果: 若希望實現更加真實的 3D 魔方效果,可以考慮使用 Three.js 等 3D 圖形庫來渲染和控制魔方。
用戶交互: 提供用戶友好的交互,如點擊或拖動來旋轉面,並添加提示和復原功能以增強遊戲體驗。
性能優化: 隨著功能的增加,需注意性能優化,確保遊戲在各種設備上流暢運行。
希望這個基本的示例能幫助您開始開發自己的網頁版 3x3 魔方遊戲!如果您有更具體的需求或問題,歡迎隨時詢問。
Not yet included in a published release.