吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1325|回复: 25
收起左侧

[其他原创] 经典记忆卡片游戏html代码

[复制链接]
小鸡眯眼 发表于 2024-8-22 15:11
记忆卡片游戏是一款简单而富有挑战性的经典游戏,旨在锻炼玩家的记忆力和观察力。游戏通常由一组图案相同的卡片组成,玩家需要通过翻转卡片找到匹配的对。每当找到一对匹配的卡片时,玩家将获得一定的分数或奖励,游戏结束时,分数最高者获胜。 无论是与朋友竞技,还是单独训练,这款游戏都适合各个年龄段的玩家。它不仅带来乐趣,还能有效提升记忆力、专注力以及反应能力。在这个快节奏的现代生活中,经典记忆卡片游戏无疑是一个值得一试的好选择。
[HTML] 纯文本查看 复制代码
<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>打砖块小游戏-丢塔游戏网</title>
    <style>
        body {
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
            height: 100vh;
            background-color: #f0e5d3;
            margin: 0;
            font-family: Arial, sans-serif;
        }
        canvas {
            border: 2px solid #4CAF50;
            background-color: #ffffff;
        }
        select {
            margin-bottom: 20px;
            padding: 5px;
        }
    </style>
</head>
<body>

<select id="difficulty">
    <option value="easy">简单</option>
    <option value="medium">中等</option>
    <option value="hard">困难</option>
</select>
<button>开始游戏</button>
<canvas id="brickGame" width="480" height="320"></canvas>

<script>
    const canvas = document.getElementById('brickGame');
    const ctx = canvas.getContext('2d');

    let ballRadius = 10;
    let x, y, dx, dy;
    const paddleHeight = 10;
    const paddleWidth = 75;
    let paddleX;
    let rightPressed = false;
    let leftPressed = false;

    let brickRowCount, brickColumnCount, bricks = [];
    let score = 0;
    let level = 0;
    let timeLimit, elapsedTime = 0;

    function initializeGame(difficulty) {
        if (difficulty === 'easy') {
            brickRowCount = 3 + level;
            brickColumnCount = 5 + level;
            dx = 2 + level;
            dy = -2 - level;
        } else if (difficulty === 'medium') {
            brickRowCount = 5 + level;
            brickColumnCount = 7 + level;
            dx = 3 + level;
            dy = -3 - level;
        } else {
            brickRowCount = 7 + level;
            brickColumnCount = 9 + level;
            dx = 4 + level;
            dy = -4 - level;
        }

        x = canvas.width / 2;
        y = canvas.height - 30;
        paddleX = (canvas.width - paddleWidth) / 2;

        bricks = [];
        for (let c = 0; c < brickColumnCount; c++) {
            bricks[c] = [];
            for (let r = 0; r < brickRowCount; r++) {
                bricks[c][r] = { x: 0, y: 0, status: 1, type: Math.random() < 0.2 }; // 20% 概率生成特殊砖块
            }
        }

        score = 0;
        timeLimit = 30; // 每关 30 秒
        elapsedTime = 0;
    }

    function startGame() {
        const difficulty = document.getElementById('difficulty').value;
        initializeGame(difficulty);

        document.addEventListener('keydown', keyDownHandler, false);
        document.addEventListener('keyup', keyUpHandler, false);
        draw();
    }

    function keyDownHandler(e) {
        if (e.key === 'Right' || e.key === 'ArrowRight') {
            rightPressed = true;
        } else if (e.key === 'Left' || e.key === 'ArrowLeft') {
            leftPressed = true;
        }
    }

    function keyUpHandler(e) {
        if (e.key === 'Right' || e.key === 'ArrowRight') {
            rightPressed = false;
        } else if (e.key === 'Left' || e.key === 'ArrowLeft') {
            leftPressed = false;
        }
    }

    function collisionDetection() {
        for (let c = 0; c < brickColumnCount; c++) {
            for (let r = 0; r < brickRowCount; r++) {
                const b = bricks[c][r];
                if (b.status === 1) {
                    if (x > b.x && x < b.x + 75 && y > b.y && y < b.y + 20) {
                        dy = -dy;
                        b.status = 0;
                        score++;
                        if (b.type) {
                            score += 2; // 特殊砖块额外得分
                        }
                        if (score === brickRowCount * brickColumnCount) {
                            level++;
                            alert('恭喜!进入第 ' + (level + 1) + ' 关!');
                            startGame();
                        }
                    }
                }
            }
        }
    }

    function drawBall() {
        ctx.beginPath();
        ctx.arc(x, y, ballRadius, 0, Math.PI * 2);
        ctx.fillStyle = '#4CAF50';
        ctx.fill();
        ctx.closePath();
    }

    function drawPaddle() {
        ctx.beginPath();
        ctx.rect(paddleX, canvas.height - paddleHeight, paddleWidth, paddleHeight);
        ctx.fillStyle = '#4CAF50';
        ctx.fill();
        ctx.closePath();
    }

    function drawBricks() {
        for (let c = 0; c < brickColumnCount; c++) {
            for (let r = 0; r < brickRowCount; r++) {
                if (bricks[c][r].status === 1) {
                    const brickX = c * (75 + 10) + 30;
                    const brickY = r * (20 + 10) + 30;
                    bricks[c][r].x = brickX;
                    bricks[c][r].y = brickY;
                    ctx.beginPath();
                    ctx.rect(brickX, brickY, 75, 20);
                    ctx.fillStyle = bricks[c][r].type ? '#FF5733' : '#4CAF50'; // 特殊砖块颜色不同
                    ctx.fill();
                    ctx.closePath();
                }
            }
        }
    }

    function drawScore() {
        ctx.font = '16px Arial';
        ctx.fillStyle = '#4CAF50';
        ctx.fillText('分数: ' + score, 8, 20);
        ctx.fillText('关卡: ' + (level + 1), canvas.width - 100, 20);
        ctx.fillText('剩余时间: ' + (timeLimit - Math.floor(elapsedTime)), canvas.width / 2 - 50, 20);
    }

    function draw() {
        ctx.clearRect(0, 0, canvas.width, canvas.height);
        drawBricks();
        drawBall();
        drawPaddle();
        drawScore();
        collisionDetection();

        if (x + dx > canvas.width - ballRadius || x + dx < ballRadius) {
            dx = -dx;
        }
        if (y + dy < ballRadius) {
            dy = -dy;
        } else if (y + dy > canvas.height - ballRadius) {
            if (x > paddleX && x < paddleX + paddleWidth) {
                dy = -dy;
            } else {
                alert('游戏结束!');
                document.location.reload();
            }
        }

        if (rightPressed && paddleX < canvas.width - paddleWidth) {
            paddleX += 7;
        } else if (leftPressed && paddleX > 0) {
            paddleX -= 7;
        }

        x += dx;
        y += dy;

        // 更新时间
        elapsedTime += 1 / 60; // 假设每帧约 1/60 秒
        if (elapsedTime >= timeLimit) {
            alert('时间到!游戏结束!');
            document.location.reload();
        }

        requestAnimationFrame(draw);
    }
</script>

</body>
</html>


接把上面的代码放入你网站的.html网页里面访问就要可以了,自己可以随便玩下。

免费评分

参与人数 1吾爱币 +7 热心值 +1 收起 理由
苏紫方璇 + 7 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

sztoplon 发表于 2024-8-22 17:09
[HTML] 纯文本查看 复制代码
<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>打砖块小游戏-丢塔游戏网</title>
<style>
        body {
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            height: 100vh;
            margin: 0;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background-color: #f0e5d3;
        }
        h1 {
            margin-bottom: 20px;
        }
        .grid {
            display: grid;
            grid-template-columns: repeat(4, 100px);
            grid-gap: 10px;
            margin-top: 20px;
        }
        .card {
            width: 100px;
            height: 100px;
            background-color: #4CAF50;
            color: white;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 24px;
            cursor: pointer;
            user-select: none;
        }
        .flipped {
            background-color: #fff;
            color: black;
        }
        .message, .timer {
            margin-top: 20px;
            font-size: 18px;
            font-weight: bold;
        }
        #difficulty {
            margin-bottom: 20px;
        }
    </style>
</head>
<body>
 <p>选择难度:</p>
<select id="difficulty">
<option value="easy">简单</option>
<option value="medium">中等</option>
<option value="hard">困难</option>
</select><button onclick="startGame();">开始游戏</button>
<div id="grid" class="grid"> </div>
<p id="message" class="message"> </p>
<p id="timer" class="timer">时间: 60</p>
 
<canvas id="brickGame" width="480" height="320"></canvas>
  
<script>
    let cards = [];
    let firstCard = null;
    let secondCard = null;
    let hasFlippedCard = false;
    let lockBoard = false;
    let matchedPairs = 0;
    let timer;
    let timeLeft = 60;
  
    function setCardData(difficulty) {
        if (difficulty === 'easy') {
            cards = ['A', 'A', 'B', 'B'];
            timeLeft = 30;
        } else if (difficulty === 'medium') {
            cards = [
                'A', 'A', 'B', 'B', 
                'C', 'C', 'D', 'D'
            ];
            timeLeft = 60;
        } else {
            cards = [
                'A', 'A', 'B', 'B', 
                'C', 'C', 'D', 'D',
                'E', 'E', 'F', 'F', 
                'G', 'G', 'H', 'H'
            ];
            timeLeft = 90;
        }
    }
  
    function startGame() {
        const difficulty = document.getElementById('difficulty').value;
        setCardData(difficulty);
  
        document.getElementById('grid').innerHTML = '';
        matchedPairs = 0;
        document.getElementById('message').innerText = '';
        document.getElementById('timer').innerText = `时间: ${timeLeft}`;
        clearInterval(timer);
        createBoard();
    }
  
    function shuffle(array) {
        return array.sort(() => Math.random() - 0.5);
    }
  
    function createBoard() {
        const grid = document.getElementById('grid');
        shuffle(cards).forEach(value => {
            const card = document.createElement('div');
            card.classList.add('card');
            card.setAttribute('data-value', value);
            card.addEventListener('click', flipCard);
            grid.appendChild(card);
        });
        startTimer();
    }
  
    function startTimer() {
        timer = setInterval(() => {
            timeLeft--;
            document.getElementById('timer').innerText = `时间: ${timeLeft}`;
            if (timeLeft <= 0) {
                clearInterval(timer);
                document.getElementById('message').innerText = '时间到!游戏结束!';
                lockBoard = true;
            }
        }, 1000);
    }
  
    function flipCard() {
        if (lockBoard || this === firstCard) return;
  
        this.classList.toggle('flipped');
        this.innerText = this.getAttribute('data-value');
  
        if (!hasFlippedCard) {
            hasFlippedCard = true;
            firstCard = this;
        } else {
            secondCard = this;
            lockBoard = true;
            checkForMatch();
        }
    }
  
    function checkForMatch() {
        const isMatch = firstCard.getAttribute('data-value') === secondCard.getAttribute('data-value');
         
        if (isMatch) {
            matchedPairs++;
            resetBoard();
            document.getElementById('message').innerText = `找到 ${matchedPairs} 对!`;
            if (matchedPairs === cards.length / 2) {
                clearInterval(timer);
                document.getElementById('message').innerText = '恭喜你,完成游戏!';
            }
        } else {
            setTimeout(() => {
                firstCard.classList.remove('flipped');
                secondCard.classList.remove('flipped');
                firstCard.innerText = '';
                secondCard.innerText = '';
                resetBoard();
            }, 1000);
        }
    }
  
    function resetBoard() {
        [hasFlippedCard, lockBoard] = [false, false];
        [firstCard, secondCard] = [null, null];
    }
  
</script>
  
</body>
</html>
zngray 发表于 2024-8-22 16:26
526841308 发表于 2024-8-22 16:36
zay1983 发表于 2024-8-22 16:42
学习一下,谢谢分享。
a97313 发表于 2024-8-22 16:44
貌似运行不了
mingmingzhaolin 发表于 2024-8-22 16:47
多谢分享
LOVEmiI 发表于 2024-8-22 16:48
这啥代码 推箱子啊
506874511 发表于 2024-8-22 16:48
运行不了,也没有报错 WX20240822-164744@2x.png
yushuai033X 发表于 2024-8-22 16:49
本帖最后由 yushuai033X 于 2024-8-22 16:53 编辑

完善一下:
加上这一句可以开始游戏document.querySelector('button').addEventListener('click', startGame);

这个方法改一下,游戏结束可以重新开始
function draw() {
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    drawBricks();
    drawBall();
    drawPaddle();
    drawScore();
    collisionDetection();

    if (x + dx > canvas.width - ballRadius || x + dx < ballRadius) {
        dx = -dx;
    }
    if (y + dy < ballRadius) {
        dy = -dy;
    } else if (y + dy > canvas.height - ballRadius) {
        if (x > paddleX && x < paddleX + paddleWidth) {
            dy = -dy;
        } else {
            alert('游戏结束!');
            setTimeout(function() {
                document.location.reload();
            }, 100);
            return;
        }
    }

    if (rightPressed && paddleX < canvas.width - paddleWidth) {
        paddleX += 7;
    } else if (leftPressed && paddleX > 0) {
        paddleX -= 7;
    }

    x += dx;
    y += dy;

    elapsedTime += 1 / 60;
    if (elapsedTime >= timeLimit) {
        alert('时间到!游戏结束!');
        setTimeout(function() {
            document.location.reload();
        }, 100);
        return;
    }

    requestAnimationFrame(draw);
}
506874511 发表于 2024-8-22 16:52
改动下可以玩了。
[HTML] 纯文本查看 复制代码
<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>打砖块小游戏-丢塔游戏网</title>
<style>
        body {
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            height: 100vh;
            margin: 0;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background-color: #f0e5d3;
        }
        h1 {
            margin-bottom: 20px;
        }
        .grid {
            display: grid;
            grid-template-columns: repeat(4, 100px);
            grid-gap: 10px;
            margin-top: 20px;
        }
        .card {
            width: 100px;
            height: 100px;
            background-color: #4CAF50;
            color: white;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 24px;
            cursor: pointer;
            user-select: none;
        }
        .flipped {
            background-color: #fff;
            color: black;
        }
        .message, .timer {
            margin-top: 20px;
            font-size: 18px;
            font-weight: bold;
        }
        #difficulty {
            margin-bottom: 20px;
        }
    </style>
</head>
<body>
 <p>选择难度:</p>
<select id="difficulty">
<option value="easy">简单</option>
<option value="medium">中等</option>
<option value="hard">困难</option>
</select><button>开始游戏</button>
<div id="grid" class="grid"> </div>
<p id="message" class="message"> </p>
<p id="timer" class="timer">时间: 60</p>

<canvas id="brickGame" width="480" height="320"></canvas>
 
<script>
    let cards = [];
    let firstCard = null;
    let secondCard = null;
    let hasFlippedCard = false;
    let lockBoard = false;
    let matchedPairs = 0;
    let timer;
    let timeLeft = 60;
 
    function setCardData(difficulty) {
        if (difficulty === 'easy') {
            cards = ['A', 'A', 'B', 'B'];
            timeLeft = 30;
        } else if (difficulty === 'medium') {
            cards = [
                'A', 'A', 'B', 'B', 
                'C', 'C', 'D', 'D'
            ];
            timeLeft = 60;
        } else {
            cards = [
                'A', 'A', 'B', 'B', 
                'C', 'C', 'D', 'D',
                'E', 'E', 'F', 'F', 
                'G', 'G', 'H', 'H'
            ];
            timeLeft = 90;
        }
    }
 
    function startGame() {
        const difficulty = document.getElementById('difficulty').value;
        setCardData(difficulty);
 
        document.getElementById('grid').innerHTML = '';
        matchedPairs = 0;
        document.getElementById('message').innerText = '';
        document.getElementById('timer').innerText = `时间: ${timeLeft}`;
        clearInterval(timer);
        createBoard();
    }
 
    function shuffle(array) {
        return array.sort(() => Math.random() - 0.5);
    }
 
    function createBoard() {
        const grid = document.getElementById('grid');
        shuffle(cards).forEach(value => {
            const card = document.createElement('div');
            card.classList.add('card');
            card.setAttribute('data-value', value);
            card.addEventListener('click', flipCard);
            grid.appendChild(card);
        });
        startTimer();
    }
 
    function startTimer() {
        timer = setInterval(() => {
            timeLeft--;
            document.getElementById('timer').innerText = `时间: ${timeLeft}`;
            if (timeLeft <= 0) {
                clearInterval(timer);
                document.getElementById('message').innerText = '时间到!游戏结束!';
                lockBoard = true;
            }
        }, 1000);
    }
 
    function flipCard() {
        if (lockBoard || this === firstCard) return;
 
        this.classList.toggle('flipped');
        this.innerText = this.getAttribute('data-value');
 
        if (!hasFlippedCard) {
            hasFlippedCard = true;
            firstCard = this;
        } else {
            secondCard = this;
            lockBoard = true;
            checkForMatch();
        }
    }
 
    function checkForMatch() {
        const isMatch = firstCard.getAttribute('data-value') === secondCard.getAttribute('data-value');
        
        if (isMatch) {
            matchedPairs++;
            resetBoard();
            document.getElementById('message').innerText = `找到 ${matchedPairs} 对!`;
            if (matchedPairs === cards.length / 2) {
                clearInterval(timer);
                document.getElementById('message').innerText = '恭喜你,完成游戏!';
            }
        } else {
            setTimeout(() => {
                firstCard.classList.remove('flipped');
                secondCard.classList.remove('flipped');
                firstCard.innerText = '';
                secondCard.innerText = '';
                resetBoard();
            }, 1000);
        }
    }
 
    function resetBoard() {
        [hasFlippedCard, lockBoard] = [false, false];
        [firstCard, secondCard] = [null, null];
    }
 
</script>
 
</body>
</html>
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-12-12 22:34

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表