立体骰子实现3D炫酷旋转效果(附完整源码)(附动画代码)

整体思路:首先搭建骰子整体的立方体

1.先搭建前后两个面(front和back),通过定位(子绝父相)将两个盒子定位在立方体正面,此时一面遮挡了另一面,在父盒子中添加transform-style: preserve-3d;达到3d立体效果,将前盒子沿z轴正方向移动高一半距离,后盒子沿z轴负方向移动高一半距离,前后盒子搭建完成,可搭配hover和过渡查看效果;

2.左右盒子与步骤1类似,不过左右盒子是需要进行左右移动高一半距离,且需要绕Z轴转动;

3.与左右盒子搭建类似,需要绕X轴转动,需要沿Y轴上下移动

4.在每个面添加骰子的点数,在front面,给front新加display: flex;(flex布局) justify-content: center;(主轴水平居中)align-items: center;(侧轴居中),子盒子为point,此时在front面正中间位置;

5.在back面,设置点数为2:给back添加flex布局,设置align-items: center;(侧轴居中),justify-content: space-evenly;(弹性容器、弹性盒子之间的间隙相等),在back中放置两个point盒子即可;

6.在left面设置3点:3个点可分为上中下三个盒子,给left添加flex布局,上面的盒子可以利用justify-content: flex-end;实现右上方对齐,中间盒子居中justify-content: center;align-items: center;,下面盒子justify-content: flex-start;左侧对齐,与盒子间距可通过给子盒子添加margin或给父盒子添加padding,其他的点也是类似步骤,完整代码如下:

* {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        /* 红点 */
        .point {
            width: 15px;
            height: 15px;
            background-color: red;
            border-radius: 50%;
        }

        /* 骰子立方体样式 */
        .touzi {
            position: relative;
            width: 100px;
            height: 100px;
            margin: 100px auto;
            /*3d立体效果*/
            transform-style: preserve-3d;
            /* hover的过渡 */
            transition: all 3s;
            /* 动画-转动骰子 */
            /* animation: move 3s infinite; */
        }

        /* 立方体六个面 */
        .front,
        .back,
        .left,
        .right,
        .under,
        .above {
            position: absolute;
            left: 0;
            top: 0;
            width: 100px;
            height: 100px;
            background-color: rgba(0, 0, 0, .1);
            border-radius: 10px;
        }

        /* 正面1点 */
        .front {
            display: flex;
            justify-content: center;
            align-items: center;
            /* background-color: skyblue; */
            transform: translateZ(50px);
        }

        /* 后面 2点*/
        .back {
            display: flex;
            justify-content: space-evenly;
            align-items: center;
            /* background-color: rgb(10, 7, 14); */
            transform: translateZ(-50px);
        }

        /* 左侧3点 */
        .left {
            display: flex;
            justify-content: center;
            align-items: center;
            /* background-color: blue; */
            padding: 10px 10px;
            transform: translateX(-50px) rotateY(-90deg);
        }

        .left .box {
            width: 100%;
        }

        .left .box .top {
            display: flex;
            justify-content: flex-end;
            margin-bottom: 5px;
        }

        .left .box .content {
            display: flex;
            justify-content: center;
            align-items: center;
        }

        .left .box .bottom {
            display: flex;
            justify-content: flex-start;
        }

        /* 右侧4点样式 */
        .right {
            display: flex;
            justify-content: center;
            align-items: center;
            /* background-color: pink; */
            transform: translateX(50px) rotateY(90deg);
        }

        .right .box {
            width: 100%;
        }

        .right .box .top,
        .right .box .bottom {
            display: flex;
            justify-content: space-evenly;
            width: 100%;
        }

        .right .box .top {
            margin-bottom: 15px;
        }

        /* 顶部6点 */
        .above {
            display: flex;
            justify-content: space-evenly;
            align-items: center;
            /* background-color: green; */
            transform: translateY(-50px) rotateX(90deg);
        }

        .above .box {
            width: 100%;
        }

        .above .top,
        .above .content,
        .above .bottom {
            display: flex;
            justify-content: space-evenly;
            align-items: center;
        }

        .above .content {
            margin: 10px 0;
        }

        /* 底面5点 */
        .under {
            display: flex;
            justify-content: center;
            align-items: center;
            /* background-color: #d4f73a; */
            transform: translateY(50px) rotateX(-90deg);
        }

        .under .box {
            width: 100%;
        }

        .under .box .top,
        .under .box .bottom {
            display: flex;
            justify-content: space-evenly;
            width: 100%;
        }

        .under .box .content {
            display: flex;
            justify-content: center;
            margin: 5px 0;
        }


        /* 当鼠标放到骰子上时,触发转动  */
        .touzi:hover {
            /* 绕Y轴旋转  */
            transform: rotateY(360deg);
            /* 绕X轴旋转  */
            /* transform: rotateX(360deg); */
        }

        /* @keyframes move {
            20% {
                transform: rotateX(360deg);
            }

            40% {
                transform: rotateY(360deg);
            }

            60% {
                transform: rotateX(360deg);
            }

            80% {
                transform: rotateY(360deg);
            }
        } */
<div class="touzi">
        <!-- 正面1点 -->
        <div class="front">
            <div class="point"></div>
        </div>
        <!-- 后面2点 -->
        <div class="back">
            <div class="point"></div>
            <div class="point"></div>
        </div>
        <!-- 左侧3点 -->
        <div class="left">
            <div class="box">
                <div class="top">
                    <div class="point"></div>
                </div>
                <div class="content">
                    <div class="point"></div>
                </div>
                <div class="bottom">
                    <div class="point"></div>
                </div>
            </div>
        </div>
        <!-- 右侧4点 -->
        <div class="right">
            <div class="box">
                <div class="top">
                    <div class="point"></div>
                    <div class="point"></div>
                </div>
                <div class="bottom">
                    <div class="point"></div>
                    <div class="point"></div>
                </div>
            </div>
        </div>
        <!-- 底面5点 -->
        <div class="under">
            <div class="box">
                <div class="top">
                    <div class="point"></div>
                    <div class="point"></div>
                </div>
                <div class="content">
                    <div class="point"></div>
                </div>
                <div class="bottom">
                    <div class="point"></div>
                    <div class="point"></div>
                </div>
            </div>
        </div>
        <!-- 顶部6点 -->
        <div class="above">
            <div class="box">
                <div class="top">
                    <div class="point"></div>
                    <div class="point"></div>
                </div>
                <div class="content">
                    <div class="point"></div>
                    <div class="point"></div>
                </div>
                <div class="bottom">
                    <div class="point"></div>
                    <div class="point"></div>
                </div>
            </div>
        </div>
    </div>

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是 Java 实现大富翁游戏的完整源码: ```java import java.util.Random; import java.util.Scanner; public class Monopoly { private static final int BOARD_SIZE = 30; // 定义游戏板大小 private static final int MAX_ROUNDS = 50; // 定义最大回合数 private final int[] board; // 游戏板 private int currentPos; // 当前玩家位置 private int currentPlayer; // 当前玩家编号 private int currentRound; // 当前回合数 private final Random random = new Random(); // 随机数生成器 private final Scanner scanner = new Scanner(System.in); // 输入读取器 public Monopoly(int numPlayers) { board = new int[BOARD_SIZE]; for (int i = 0; i < BOARD_SIZE; i++) { board[i] = random.nextInt(100); // 初始化游戏板 } currentPos = 0; currentPlayer = 1; currentRound = 1; System.out.printf("欢迎来到大富翁游戏!\n本次游戏有 %d 个玩家参与\n", numPlayers); } // 打印游戏板 private void printBoard() { System.out.print("游戏板:"); for (int i = 0; i < BOARD_SIZE; i++) { System.out.printf("%d ", board[i]); } System.out.println(); } // 掷骰子并移动玩家 private void rollDice() { int steps = random.nextInt(6) + 1; // 随机生成 1~6 的整数 System.out.printf("玩家 %d 掷出了 %d 点\n", currentPlayer, steps); currentPos = (currentPos + steps) % BOARD_SIZE; // 计算新位置 System.out.printf("玩家 %d 移动到了第 %d 格\n", currentPlayer, currentPos + 1); } // 处理该格子的事件 private void handleEvent() { int prize = board[currentPos]; // 获取奖金 System.out.printf("玩家 %d 获得了 %d 元奖金\n", currentPlayer, prize); } // 判断游戏是否结束 private boolean isGameOver() { if (currentRound > MAX_ROUNDS) { System.out.printf("游戏结束,最大回合数 %d 已达到\n", MAX_ROUNDS); return true; } if (currentPos == BOARD_SIZE - 1) { System.out.printf("玩家 %d 获胜!\n", currentPlayer); return true; } return false; } // 开始游戏 public void start() { while (true) { System.out.printf("第 %d 回合,轮到玩家 %d\n", currentRound, currentPlayer); printBoard(); rollDice(); handleEvent(); if (isGameOver()) { break; } currentPlayer = (currentPlayer % 2) + 1; // 切换到下一个玩家 currentRound++; System.out.println("按 Enter 键继续..."); scanner.nextLine(); // 等待用户输入 } } public static void main(String[] args) { Monopoly game = new Monopoly(2); // 创建一个有两个玩家的游戏 game.start(); } } ``` 运行该程序将启动一个大富翁游戏,有两个玩家参与。程序随机生成奖金和骰子点数,并在游戏板上移动玩家。当玩家到达终点或者回合数超过最大值时,游戏结束。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值