我用 JavaScript 制作了 Squid 游戏
你的游戏很糟糕.........兄弟
我刚刚用 JavaScript 和 THREE.JS 在 Squid Game 上制作了一个非常简单的红灯绿灯游戏版本。
你可以在这里试玩。
如果你想看一步步教你如何制作这个游戏,我创建了一个YouTube 教程,你可以看看。
但如果您只想简单了解一下,请继续阅读本文。
以下是我创建游戏所采取的步骤:
i. 使用三个 js 进行基本项目设置。
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000 );
const renderer = new THREE.WebGLRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
renderer.setClearColor( 0xb7c3f3, 1 );
const light = new THREE.AmbientLight( 0xffffff );
scene.add( light )
camera.position.z = 5;
function animate() {
if(gameStat == "over") return
renderer.render( scene, camera );
requestAnimationFrame( animate );
player.update()
}
animate();
window.addEventListener( 'resize', onWindowResize, false );
function onWindowResize(){
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}
ii. 使用鱿鱼游戏中的玩偶 3D 模型。
const loader = new THREE.GLTFLoader()
loader.load("../models/scene.gltf", (gltf) => {
scene.add( gltf.scene );
gltf.scene.scale.set(.4, .4, .4);
gltf.scene.position.set(0, -1, 0);
this.doll = gltf.scene;
})
iii.为了方便起见,我还做了一个娃娃。 class
function delay(ms){
return new Promise(resolve => setTimeout(resolve, ms));
}
class Doll{
constructor(){
loader.load("../models/scene.gltf", (gltf) => {
scene.add( gltf.scene );
gltf.scene.scale.set(.4, .4, .4);
gltf.scene.position.set(0, -1, 0);
this.doll = gltf.scene;
})
}
lookBackward(){
gsap.to(this.doll.rotation, {y: -3.15, duration: .45})
setTimeout(() => isLookingBackward = true, 150)
}
lookForward(){
gsap.to(this.doll.rotation, {y: 0, duration: .45})
setTimeout(() => isLookingBackward = false, 450)
}
//Makes the doll look back for 1.5 - 3 seconds then look forward for .75 - 1.5 seconds and keep repeating these.
async start(){
this.lookBackward()
await delay((Math.random() * 1000) + 1000)
this.lookForward()
await delay((Math.random() * 750) + 750)
this.start()
}
}
iv. 然后我用一些立方体制作了玩家必须穿过的赛道。
function createCube(size, positionX, rotY = 0, color = 0xfbc851){
const geometry = new THREE.BoxGeometry(size.w, size.h, size.d);
const material = new THREE.MeshBasicMaterial( { color: color } );
const cube = new THREE.Mesh( geometry, material );
cube.position.x = positionX;
cube.rotation.y = rotY;
scene.add( cube );
return cube
}
function createTrack(){
createCube({w: start_position * 2 + .2, h: 1.5, d: 1}, 0, 0, 0xe5a716).position.z = -1;
createCube({w: .2, h: 1.5, d: 1}, start_position, -.35);
createCube({w: .2, h: 1.5, d: 1}, end_position, .35);
}
createTrack()
v.然后我也创建了一个Player class
(Player只是一个球体)
class Player{
constructor(){
const geometry = new THREE.SphereGeometry( .3, 32, 16 );
const material = new THREE.MeshBasicMaterial( { color: 0xffffff } );
const sphere = new THREE.Mesh( geometry, material );
sphere.position.z = 1
sphere.position.x = start_position
scene.add( sphere )
this.player = sphere
this.playerInfo = {
positionX: start_position,
velocity: 0
}
}
run(){
this.playerInfo.velocity = .03
}
stop(){
gsap.to(this.playerInfo, {velocity: 0, duration: .1})
}
update(){ //Update function is called in animation loop
this.check()
this.playerInfo.positionX -= this.playerInfo.velocity
this.player.position.x = this.playerInfo.positionX
}
}
vi. 然后我向播放器添加了按键事件。
window.addEventListener('keydown', (e) => {
if(e.key == "ArrowUp"){
player.run()
}
})
window.addEventListener('keyup', (e) => {
if(e.key == "ArrowUp"){
player.stop()
}
})
vii. 最后,我将所有内容整合在一起,并实现游戏逻辑以使其正常运行。
您可以在此处获取完整代码
您可能会发现我的文章和 YouTube 视频很有趣,值得一看。