我将为您创建一个完整的UNO游戏HTML单页面,玩家可以与3个AI对手一起游戏,或者观看4个AI自动对战。
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>UNO 游戏</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: 'Arial', sans-serif;
}
body {
background-color: #f0f0f0;
padding: 20px;
}
.container {
max-width: 1000px;
margin: 0 auto;
background-color: white;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
padding: 20px;
position: relative;
}
h1 {
text-align: center;
color: #d32f2f;
margin-bottom: 20px;
}
.game-controls {
display: flex;
justify-content: center;
margin-bottom: 20px;
gap: 10px;
}
button {
padding: 10px 15px;
background-color: #d32f2f;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
font-weight: bold;
}
button:hover {
background-color: #b71c1c;
}
button:disabled {
background-color: #cccccc;
cursor: not-allowed;
}
.game-area {
display: flex;
flex-direction: column;
align-items: center;
}
.player-area {
width: 100%;
margin: 10px 0;
padding: 10px;
border-radius: 5px;
position: relative;
}
.player-area.active {
background-color: rgba(211, 47, 47, 0.1);
box-shadow: 0 0 5px rgba(211, 47, 47, 0.5);
}
.player-name {
font-weight: bold;
margin-bottom: 5px;
}
.player-cards {
display: flex;
flex-wrap: wrap;
gap: 5px;
}
.card {
width: 60px;
height: 90px;
border-radius: 5px;
display: flex;
justify-content: center;
align-items: center;
color: white;
font-weight: bold;
font-size: 16px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
position: relative;
cursor: pointer;
transition: transform 0.2s;
}
.card:hover {
transform: translateY(-5px);
}
.card.red {
background-color: #d32f2f;
}
.card.blue {
background-color: #1976d2;
}
.card.green {
background-color: #388e3c;
}
.card.yellow {
background-color: #fbc02d;
}
.card.black {
background-color: #212121;
}
.card-back {
background-color: #212121;
background-image: linear-gradient(45deg, #d32f2f 25%, transparent 25%, transparent 50%, #d32f2f 50%, #d32f2f 75%, transparent 75%, transparent);
background-size: 10px 10px;
color: white;
display: flex;
justify-content: center;
align-items: center;
font-weight: bold;
}
.discard-pile {
width: 70px;
height: 105px;
margin: 20px 0;
position: relative;
}
.discard-pile .card {
position: absolute;
top: 0;
left: 0;
}
.draw-pile {
width: 70px;
height: 105px;
position: absolute;
top: 50%;
right: 30px;
transform: translateY(-50%);
cursor: pointer;
}
.center-area {
display: flex;
justify-content: center;
align-items: center;
gap: 20px;
margin: 20px 0;
position: relative;
}
.game-info {
position: absolute;
top: 0;
left: 30px;
background-color: rgba(255, 255, 255, 0.8);
padding: 10px;
border-radius: 5px;
box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
}
.color-selector {
display: none;
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background-color: white;
padding: 20px;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.3);
z-index: 100;
}
.color-options {
display: flex;
gap: 10px;
margin-top: 10px;
}
.color-option {
width: 50px;
height: 50px;
border-radius: 50%;
cursor: pointer;
}
.color-option.red {
background-color: #d32f2f;
}
.color-option.blue {
background-color: #1976d2;
}
.color-option.green {
background-color: #388e3c;
}
.color-option.yellow {
background-color: #fbc02d;
}
.overlay {
display: none;
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.5);
z-index: 99;
}
.game-log {
max-height: 150px;
overflow-y: auto;
margin-top: 20px;
padding: 10px;
background-color: #f5f5f5;
border-radius: 5px;
font-size: 14px;
}
.log-entry {
margin-bottom: 5px;
padding-bottom: 5px;
border-bottom: 1px solid #e0e0e0;
}
.uno-button {
position: absolute;
bottom: 10px;
right: 10px;
background-color: #d32f2f;
color: white;
padding: 10px 15px;
border-radius: 5px;
font-weight: bold;
cursor: pointer;
display: none;
}
.uno-button:hover {
background-color: #b71c1c;
}
.ai-thinking {
position: absolute;
top: 5px;
right: 5px;
background-color: rgba(0, 0, 0, 0.7);
color: white;
padding: 5px 10px;
border-radius: 10px;
font-size: 12px;
display: none;
}
@media (max-width: 768px) {
.card {
width: 40px;
height: 60px;
font-size: 12px;
}
.discard-pile, .draw-pile {
width: 50px;
height: 75px;
}
.game-info {
position: static;
margin-bottom: 10px;
width: 100%;
}
.center-area {
flex-direction: column;
}
.draw-pile {
position: static;
transform: none;
margin-top: 10px;
}
}
</style>
</head>
<body>
<div class="container">
<h1>UNO 游戏</h1>
<div class="game-controls">
<button id="start-game">开始游戏</button>
<button id="ai-only-game">AI 对战</button>
<button id="restart-game" disabled>重新开始</button>
</div>
<div class="game-area">
<div class="player-area" id="player-0">
<div class="player-name">AI 玩家 1</div>
<div class="player-cards"></div>
<div class="ai-thinking">思考中...</div>
</div>
<div class="player-area" id="player-1">
<div class="player-name">AI 玩家 2</div>
<div class="player-cards"></div>
<div class="ai-thinking">思考中...</div>
</div>
<div class="center-area">
<div class="game-info">
<div id="current-color">当前颜色: -</div>
<div id="current-value">当前数值: -</div>
<div id="direction">方向: 顺时针</div>
</div>
<div class="discard-pile" id="discard-pile"></div>
<div class="draw-pile" id="draw-pile">
<div class="card card-back">UNO</div>
</div>
</div>
<div class="player-area" id="player-2">
<div class="player-name">AI 玩家 3</div>
<div class="player-cards"></div>
<div class="ai-thinking">思考中...</div>
</div>
<div class="player-area" id="player-3">
<div class="player-name">玩家</div>
<div class="player-cards"></div>
<div class="uno-button" id="uno-button">UNO!</div>
</div>
<div class="game-log" id="game-log"></div>
</div>
<div class="overlay" id="overlay"></div>
<div class="color-selector" id="color-selector">
<h3>选择颜色</h3>
<div class="color-options">
<div class="color-option red" data-color="red"></div>
<div class="color-option blue" data-color="blue"></div>
<div class="color-option green" data-color="green"></div>
<div class="color-option yellow" data-color="yellow"></div>
</div>
</div>
</div>
<script>
document.addEventListener('DOMContentLoaded', function() {
// 游戏状态
const gameState = {
players: [
{ name: "AI 玩家 1", cards: [], isAI: true },
{ name: "AI 玩家 2", cards: [], isAI: true },
{ name: "AI 玩家 3", cards: [], isAI: true },
{ name: "玩家", cards: [], isAI: false }
],
deck: [],
discardPile: [],
currentPlayer: 0,
direction: 1, // 1 顺时针, -1 逆时针
currentColor: null,
currentValue: null,
gameStarted: false,
aiOnlyMode: false,
waitingForColorChoice: false,
unoButtonVisible: false,
unoCalled: false,
penaltyPending: false
};
// DOM 元素
const startGameBtn = document.getElementById('start-game');
const aiOnlyGameBtn = document.getElementById('ai-only-game');
const restartGameBtn = document.getElementById('restart-game');
const drawPile = document.getElementById('draw-pile');
const discardPileEl = document.getElementById('discard-pile');
const colorSelector = document.getElementById('color-selector');
const overlay = document.getElementById('overlay');
const gameLog = document.getElementById('game-log');
const unoButton = document.getElementById('uno-button');
const currentColorEl = document.getElementById('current-color');
const currentValueEl = document.getElementById('current-value');
const directionEl = document.getElementById('direction');
// 卡牌定义
const colors = ['red', 'blue', 'green', 'yellow'];
const values = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'Skip', 'Reverse', 'Draw2'];
const specialCards = ['Wild', 'Wild Draw4'];
// 初始化牌组
function initializeDeck() {
const deck = [];
// 添加数字和功能牌
colors.forEach(color => {
// 每种颜色一张0
deck.push({ color, value: '0' });
// 每种颜色两张1-9和功能牌
for (let i = 0; i < 2; i++) {
values.slice(1).forEach(value => {
deck.push({ color, value });
});
}
});
// 添加万能牌
for (let i = 0; i < 4; i++) {
specialCards.forEach(value => {
deck.push({ color: 'black', value });
});
}
return deck;
}
// 洗牌
function shuffleDeck(deck) {
for (let i = deck.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[deck[i], deck[j]] = [deck[j], deck[i]];
}
return deck;
}
// 发牌
function dealCards() {
for (let i = 0; i < 7; i++) {
gameState.players.forEach(player => {
player.cards.push(gameState.deck.pop());
});
}
// 翻开第一张牌
let firstCard = gameState.deck.pop();
// 确保第一张牌不是特殊牌
while (firstCard.color === 'black' || firstCard.value === 'Skip' ||
firstCard.value === 'Reverse' || firstCard.value === 'Draw2') {
gameState.deck.unshift(firstCard);