three实现一个三维堆积木小游戏效果代码

代码语言:html

所属分类:游戏

代码描述:three实现一个三维堆积木小游戏效果代码

代码标签: 三维 积木 小游戏 效果

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

<html>
<head>
    <meta name="viewport" content="width=device-width,user-scalable=no">
    <style>
@import url("https://fonts.googleapis.com/css?family=Comfortaa");
        html, body {
            margin: 0;
            overflow: hidden;
            height: 100%;
            width: 100%;
            position: relative;
            font-family: "Comfortaa", cursive;
        }

        #container {
            width: 100%;
            height: 100%;
        }
        #container #score {
            position: absolute;
            top: 20px;
            width: 100%;
            text-align: center;
            font-size: 10vh;
            transition: transform 0.5s ease;
            color: #333344;
            transform: translatey(-200px) scale(1);
        }
        #container #game {
            position: absolute;
            top: 0;
            right: 0;
            bottom: 0;
            left: 0;
        }
        #container .game-over {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 85%;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
        }
        #container .game-over * {
            transition: opacity 0.5s ease, transform 0.5s ease;
            opacity: 0;
            transform: translatey(-50px);
            color: #333344;
        }
        #container .game-over h2 {
            margin: 0;
            padding: 0;
            font-size: 40px;
        }
        #container .game-ready {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: space-around;
        }
        #container .game-ready #start-button {
            transition: opacity 0.5s ease, transform 0.5s ease;
            opacity: 0;
            transform: translatey(-50px);
            border: 3px solid #333344;
            padding: 10px 20px;
            background-color: transparent;
            color: #333344;
            font-size: 30px;
        }
        #container #instructions {
            position: absolute;
            width: 100%;
            top: 16vh;
            left: 0;
            text-align: center;
            transition: opacity 0.5s ease, transform 0.5s ease;
            opacity: 0;
        }
        #container #instructions.hide {
            opacity: 0 !important;
        }
        #container.playing #score, #container.resetting #score {
            transform: translatey(0px) scale(1);
        }
        #container.playing #instructions {
            opacity: 1;
        }
        #container.ready .game-ready #start-button {
            opacity: 1;
            transform: translatey(0);
        }
        #container.ended #score {
            transform: translatey(6vh) scale(1.5);
        }
        #container.ended .game-over * {
            opacity: 1;
            transform: translatey(0);
        }
        #container.ended .game-over p {
            transition-delay: 0.3s;
        }
    </style>
    <script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/three.84.js"></script>
    <script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/TweenMax.min.js"></script>


</head>
<body>



    <div id="container">
        <div id="game"></div>
        <div id="score">
            0
        </div>
        <div id="instructions">
            单击鼠标(或按下空格键)放置方块
        </div>
        <div class="game-over">
            <h2>Game Over</h2>
            <p>
                You did great, you're the best.
            </p>
            <p>
                Click or spacebar to start again
            </p>
        </div>
        <div class="game-ready">
            <div id="start-button">
                Start
            </div>
            <div></div>
        </div>
    </div>
    <script>
        "use strict";
        console.clear();
        class Stage {
            constructor() {
                // container
                this.render = function () {
                    this.renderer.render(this.scene, this.camera);
                };
                this.add = function (elem) {
                    this.scene.add(elem);
                };
                this.remove = function (elem) {
                    this.scene.remove(elem);
                };
                this.container = document.getElementById('game');
                // renderer
                this.renderer = new THREE.WebGLRenderer({
                    antialias: true,
                    alpha: false
                });
                this.renderer.setSize(window.innerWidth, window.innerHeight);
                this.renderer.setClearColor('#2ab4ff', 1);
                this.container.appendChild(this.renderer.domElement);
                // scene
                this.scene = new THREE.Scene();
                // camera
                let aspect = window.innerWidth / window.innerHeight;
                let d = 20;
                this.camera = new THREE.OrthographicCamera(-d * aspect, d * aspect, d, -d, -100, 1000);
                this.camera.position.x = 2;
                this.camera.position.y = 2;
                this.camera.position.z = 2;
                this.camera.lookAt(new THREE.Vector3(0, 0, 0));
                //light
                this.light = new THREE.DirectionalLight(0xffffff, 0.5);
                this.light.position.set(0, 499, 0);
                this.scene.add(this.light);
                this.softLight = new THREE.AmbientLight(0xffffff, 0.4);
                this.scene.add(this.softLight);
                window.addEventListener('resize', () => this.onResize());
                this.onResize();
            }
            setCamera(y, speed = 0.3) {
                TweenLite.to(this.camera.position, speed, {
                    y: y + 4, ease: Power1.easeInOut
                });
                TweenLite.to(this.camera.lookAt, speed, {
                    y: y, ease: Power1.easeInOut
                });
            }
            onResize() {
                let viewSize = 30;
                this.renderer.setSize(window.innerWidth, window.innerHeight);
                this.camera.left = window.innerWidth / -viewSize;
                this.camera.right = window.innerWidth / viewSize;
                this.camera.top = window.innerHeight / viewSize;
                this.camera.bottom = window.innerHeight / -viewSize;
                this.camera.updateProjectionMatrix();
            }
        }
        class Block {
            constructor(block) {
                // set size and position
                this.STATES = {
                    ACTIVE: 'active',
                    STOPPED: 'stopped',
                    MISSED: 'missed'
                };
                this.MOVE_AMOUNT = 12;
                this.dimension = {
                    width: 0,
                    height: 0,
                    depth: 0
                };
                this.position = {
                    x: 0,
                    y: 0,
                    z: 0
                };
                this.targetBlock = block;
                this.index = (this.targetBlock ? this.targetBlock.index: 0) + 1;
                this.workingPlane = this.index % 2 ? 'x': 'z';
                this.workingDimension = this.index % 2 ? 'width': 'depth';
                // set the dimensions from the target block, or defaults.
                this.dimension.width = this.targetBlock ? this.targetBlock.dimension.width: 10;
                this.dimension.height = this.targetBlock ? this.targetBlock.dimension.height: 2;
                this.dimension.depth = this.targetBlock ? this.targetBlock.dimension.depth: 10;
                this.position.x = this.targetBlock ? this.targetBlock.position.x: 0;
                this.position.y = this.dimension.height * this.index;
                this.position.z = this.targetBlock ? this.targetBlock.position.z: 0;
                this.colorOffset = this.targetBlock ? this.targetBlock.colorOffset: Math.round(Math.random() * 100);
                // set color
                if (!this.targetBlock) {
                    this.color = 0x333344;
                } else {
                    let offset = this.index + this.colorOffset;
                    var r = Math.sin(0.3 * offset) * 55 + 200;
                    var g = Math.sin(0.3 * offset + 2) * 55 + 200;
                    var b = Math.sin(0.3 * offset + 4) * 55 + 200;
                    this.color = new THREE.Color(r / 255, g / 255, b / 255);
                }
                // state
                this.state = this.index > 1 ? this.STATES.ACTIVE: this.STATES.STOPPED;
                // set direction
                this.speed = -0.1 - (this.index * 0.005);
                if (this.speed < -4)
                    this.speed = -4;
                this.direction = this.speed;
                // create block
                let geometry = new THREE.BoxGeometry(this.dimension.width, this.dimension.height, this.dimension.depth);
                geometry.applyMatrix(new THREE.Matrix4().makeTranslation(this.dimension.width / 2, this.dimension.height / 2, this.dimension.depth / 2));
                this.material = new THREE.MeshToonMaterial({
                    color: this.color, shading: THREE.FlatShading
                });
                this.mesh = new THREE.Mesh(geometry, this.material);
                this.mesh.position.set(this.position.x, this.position.y + (this.state == this.STATES.ACTIVE ? 0: 0), this.position.z);
                if (this.state == this.STATES.ACTIVE) {
                    this.position[this.workingPlane] = Math.random() > 0.5 ? -this.MOVE_AMOUNT: this.MOVE_AMOUNT;
                }
            }
            reverseDirection() {
                this.direction = this.direction > 0 ? this.speed: Math.abs(this.speed);
            }
            place() {
                this.state = this.STATES.STOPPED;
                let overlap = this.targetBlock.dimension[this.workingDimension] - Math..........完整代码请登录后点击上方下载按钮下载查看

网友评论0