使用html5制作一个井字格小游戏

由于家里的小孩老是吵着要玩小游戏 ,于是突发奇想做了一个全屏用符号雨做装饰,中间井字格的小游戏出来玩一玩

话不多说,直接上演示图片在这里插入图片描述
这是代码

<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>井字棋</title>
<style>
    body {
        margin: 0;
        padding: 0;
        display: flex;
        justify-content: center;
        align-items: center;
        height: 100vh;
        background: #000;
        overflow: hidden;
        position: relative;
    }

    .tic-tac-toe {
        display: grid;
        grid-template-columns: repeat(3, 100px);
        grid-template-rows: repeat(3, 100px);
    }

    .tic-tac-toe div {
        display: flex;
        justify-content: center;
        align-items: center;
        font-size: 36px;
        cursor: pointer;
        border: 2px solid #fff;
        color: #fff;
        transition: background-color 0.3s ease;
    }

    .tic-tac-toe div:hover {
        background-color: rgba(255, 255, 255, 0.2);
    }

    .code-rain {
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        pointer-events: none;
        overflow: hidden;
    }

    .code-rain span {
        position: absolute;
        color: #0f0;
        user-select: none;
        pointer-events: none;
        animation: codeRain 0.5s linear infinite;
    }

    @keyframes codeRain {
        0% {
            transform: translateY(-100%);
        }
        100% {
            transform: translateY(100vh);
        }
    }
</style>
</head>
<body>
<div class="tic-tac-toe">
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
</div>

<div class="code-rain">
    <!-- 生成代码雨效果 -->
    <script>
        const codeRainContainer = document.querySelector('.code-rain');

        function generateCodeRain() {
            const codeSymbols = '!@#$%^&*()_+{}|:"<>?`-=\\[];\',./';
            const codeLength = 100;
            const codeElements = [];

            for (let i = 0; i < codeLength; i++) {
                const span = document.createElement('span');
                span.textContent = codeSymbols[Math.floor(Math.random() * codeSymbols.length)];
                span.style.left = `${Math.random() * 100}%`;
                span.style.animationDelay = `${Math.random()}s`;
                span.style.fontSize = `${Math.floor(Math.random() * 16) + 12}px`;
                codeElements.push(span);
                codeRainContainer.appendChild(span);
            }

            return codeElements;
        }

        generateCodeRain();
    </script>
</div>

<script>
    const cells = document.querySelectorAll('.tic-tac-toe div');
    let currentPlayer = 'X';
    let gameActive = true;

    function handleCellClick(e) {
        const cell = e.target;

        if (!gameActive || cell.textContent !== '') return;

        cell.textContent = currentPlayer;
        checkWinner();
        currentPlayer = currentPlayer === 'X' ? 'O' : 'X';
    }

    function checkWinner() {
        const winningConditions = [
            [0, 1, 2],
            [3, 4, 5],
            [6, 7, 8],
            [0, 3, 6],
            [1, 4, 7],
            [2, 5, 8],
            [0, 4, 8],
            [2, 4, 6]
        ];

        for (let condition of winningConditions) {
            const [a, b, c] = condition;

            if (
                cells[a].textContent &&
                cells[a].textContent === cells[b].textContent &&
                cells[a].textContent === cells[c].textContent
            ) {
                cells[a].style.backgroundColor = 'green';
                cells[b].style.backgroundColor = 'green';
                cells[c].style.backgroundColor = 'green';
                gameActive = false;
                setTimeout(() => {
                    restartGame();
                }, 5000);
                return;
            }
        }
    }

    function restartGame() {
        cells.forEach(cell => {
            cell.textContent = '';
            cell.style.backgroundColor = '';
        });
        currentPlayer = 'X';
        gameActive = true;
    }

    cells.forEach(cell => {
        cell.addEventListener('click', handleCellClick);
    });
</script>
</body>
</html>
html
  • 15
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,下面是基于EASYX库的井字棋小游戏的C语言代码: ```c #include <graphics.h> #include <conio.h> #include <stdio.h> #include <stdlib.h> #include <time.h> #define ROW 3 #define COL 3 #define SIZE 100 int board[ROW][COL]; // 棋盘数组,0表示空位,1表示玩家1,2表示玩家2 // 初始化棋盘 void init_board() { int i, j; for (i = 0; i < ROW; i++) { for (j = 0; j < COL; j++) { board[i][j] = 0; } } } // 绘制棋盘 void draw_board() { int i, j; setcolor(WHITE); for (i = 0; i <= ROW; i++) { line(SIZE, i * SIZE + SIZE, COL * SIZE + SIZE, i * SIZE + SIZE); } for (j = 0; j <= COL; j++) { line(j * SIZE + SIZE, SIZE, j * SIZE + SIZE, ROW * SIZE + SIZE); } } // 绘制棋子 void draw_piece(int row, int col, int player) { int x = col * SIZE + SIZE; int y = row * SIZE + SIZE; if (player == 1) { setcolor(YELLOW); circle(x, y, SIZE / 2 - 5); } else if (player == 2) { setcolor(RED); line(x - SIZE / 2 + 10, y - SIZE / 2 + 10, x + SIZE / 2 - 10, y + SIZE / 2 - 10); line(x - SIZE / 2 + 10, y + SIZE / 2 - 10, x + SIZE / 2 - 10, y - SIZE / 2 + 10); } } // 判断胜负 int check_win(int player) { int i, j; // 判断每一行 for (i = 0; i < ROW; i++) { if (board[i][0] == player && board[i][1] == player && board[i][2] == player) { return 1; } } // 判断每一列 for (j = 0; j < COL; j++) { if (board[0][j] == player && board[1][j] == player && board[2][j] == player) { return 1; } } // 判断对角线 if (board[0][0] == player && board[1][1] == player && board[2][2] == player) { return 1; } if (board[0][2] == player && board[1][1] == player && board[2][0] == player) { return 1; } return 0; } // 判断平局 int check_tie() { int i, j; for (i = 0; i < ROW; i++) { for (j = 0; j < COL; j++) { if (board[i][j] == 0) { return 0; } } } return 1; } int main() { int player = 1; // 当前玩家,初始为玩家1 int row, col; // 选择的行列 int win = 0; // 是否胜利 int tie = 0; // 是否平局 init_board(); initgraph(COL * SIZE + 2 * SIZE, ROW * SIZE + 2 * SIZE); draw_board(); while (!win && !tie) { if (player == 1) { outtextxy(10, ROW * SIZE + SIZE + 10, "玩家1,请选择位置:"); } else { outtextxy(10, ROW * SIZE + SIZE + 10, "玩家2,请选择位置:"); } while (1) { if (kbhit()) { char c = getch(); if (c == 'q') { closegraph(); exit(0); } row = (int) (c - '1') / COL; col = (int) (c - '1') % COL; if (row >= 0 && row < ROW && col >= 0 && col < COL && board[row][col] == 0) { break; } } delay(10); } board[row][col] = player; draw_piece(row, col, player); win = check_win(player); tie = check_tie(); player = (player == 1) ? 2 : 1; // 切换玩家 } if (win) { if (player == 1) { outtextxy(10, ROW * SIZE + SIZE + 10, "玩家2胜利!"); } else { outtextxy(10, ROW * SIZE + SIZE + 10, "玩家1胜利!"); } } else { outtextxy(10, ROW * SIZE + SIZE + 10, "平局!"); } while (1) { if (kbhit()) { char c = getch(); if (c == 'q') { break; } } delay(10); } closegraph(); return 0; } ``` 这个程序使用了EASYX库来实现图形界面,通过键盘输入来选择棋子的位置。你可以在开发环境中编译运行此程序,体验一下这个简单的井字棋小游戏

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

2201_75396384

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值