o1-preview-2024-09-12· OpenAI WildChat-4.8M (AI2)
Make 3d space game using babylon.js with controllable rocket and planets, implement all…
- User
Make 3d space game using babylon.js with controllable rocket and planets, implement all in one file
- Assistant
Here's a complete HTML file that implements a simple 3D space game using Babylon.js. In this game, you can control a rocket and navigate around planets. Everything is contained within a single file as per your request.
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>3D Space Game with Babylon.js</title> <style> html, body { overflow: hidden; width: 100%; height: 100%; margin: 0; padding: 0; } #renderCanvas { width: 100%; height: 100%; touch-action: none; } /* Styles for the info text */ #info { position: absolute; top: 10px; left: 10px; color: white; font-family: Arial, sans-serif; z-index: 1; background-color: rgba(0, 0, 0, 0.5); padding: 10px; border-radius: 5px; } </style> </head> <body> <!-- Information about controls --> <div id="info"> <h2>3D Space Game Controls</h2> <p><strong>Arrow Up:</strong> Move Forward</p> <p><strong>Arrow Down:</strong> Move Backward</p> <p><strong>Arrow Left:</strong> Turn Left</p> <p><strong>Arrow Right:</strong> Turn Right</p> </div> <canvas id="renderCanvas"></canvas> <!-- Canvas for Babylon.js --> <!-- Include Babylon.js from CDN --> <script src="https://cdn.babylonjs.com/babylon.js"></script> <!-- The main script for the game --> <script> // Get the canvas element var canvas = document.getElementById("renderCanvas"); // Generate the Babylon 3D engine var engine = new BABYLON.Engine(canvas, true); // Create the scene var createScene = function () { var scene = new BABYLON.Scene(engine); // Create a universal camera and position it at the origin var camera = new BABYLON.UniversalCamera("UniversalCamera", new BABYLON.Vector3(0, 2, -10), scene); camera.attachControl(canvas, true); // Add ambient light to the scene var light = new BABYLON.HemisphericLight("light", new BABYLON.Vector3(0, 1, 0), scene); // Create the rocket using basic shapes var rocketBody = BABYLON.MeshBuilder.CreateCylinder("rocketBody", { diameterTop: 0.5, diameterBottom: 0.5, height: 3 }, scene); rocketBody.position.y = 1.5; var rocketNose = BABYLON.MeshBuilder.CreateCone("rocketNose", { diameter: 0.5, height: 1 }, scene); rocketNose.position.y = 3.5; // Merge rocket parts into a single mesh var rocket = BABYLON.Mesh.MergeMeshes([rocketBody, rocketNose], true, false, null, false, true); rocket.name = "rocket"; // Apply a material to the rocket var rocketMaterial = new BABYLON.StandardMaterial("rocketMaterial", scene); rocketMaterial.diffuseColor = new BABYLON.Color3(0.8, 0.2, 0.2); rocket.material = rocketMaterial; // Set the rocket as the parent of the camera to follow it camera.parent = rocket; // Create planets var planet1 = BABYLON.MeshBuilder.CreateSphere("planet1", { diameter: 5 }, scene); planet1.position = new BABYLON.Vector3(20, 0, 0); var planet1Material = new BABYLON.StandardMaterial("planet1Material", scene); planet1Material.diffuseColor = new BABYLON.Color3(0, 0.5, 1); planet1.material = planet1Material; var planet2 = BABYLON.MeshBuilder.CreateSphere("planet2", { diameter: 7 }, scene); planet2.position = new BABYLON.Vector3(-30, 0, 15); var planet2Material = new BABYLON.StandardMaterial("planet2Material", scene); planet2Material.diffuseColor = new BABYLON.Color3(1, 0.5, 0); planet2.material = planet2Material; var planet3 = BABYLON.MeshBuilder.CreateSphere("planet3", { diameter: 4 }, scene); planet3.position = new BABYLON.Vector3(10, 0, -25); var planet3Material = new BABYLON.StandardMaterial("planet3Material", scene); planet3Material.diffuseColor = new BABYLON.Color3(0.5, 1, 0); planet3.material = planet3Material; // Input map for tracking user input var inputMap = {}; // Keydown event handler scene.actionManager = new BABYLON.ActionManager(scene); scene.actionManager.registerAction(new BABYLON.ExecuteCodeAction(BABYLON.ActionManager.OnKeyDownTrigger, function (evt) { inputMap[evt.sourceEvent.key] = evt.sourceEvent.type == "keydown"; })); // Keyup event handler scene.actionManager.registerAction(new BABYLON.ExecuteCodeAction(BABYLON.ActionManager.OnKeyUpTrigger, function (evt) { inputMap[evt.sourceEvent.key] = evt.sourceEvent.type == "keydown"; })); // Variables for movement var speed = 0.2; var rotationSpeed = 0.05; // Game loop scene.onBeforeRenderObservable.add(function () { // Forward/backward movement if (inputMap["ArrowUp"]) { rocket.moveWithCollisions(rocket.forward.scaleInPlace(speed)); } if (inputMap["ArrowDown"]) { rocket.moveWithCollisions(rocket.forward.scaleInPlace(-speed)); } // Rotation if (inputMap["ArrowLeft"]) { rocket.rotation.y -= rotationSpeed; } if (inputMap["ArrowRight"]) { rocket.rotation.y += rotationSpeed; } }); return scene; }; // Call the createScene function var scene = createScene(); // Run the render loop engine.runRenderLoop(function () { scene.render(); }); // Resize the engine when the window is resized window.addEventListener("resize", function () { engine.resize(); }); </script> </body> </html>Instructions to Run the Game:
- Copy the entire code above.
- Paste it into a text editor and save the file with an
.htmlextension (e.g.,space_game.html). - Open the saved HTML file in a modern web browser (like Chrome, Firefox, or Edge).
- Use the arrow keys to control the rocket:
- Arrow Up: Move Forward
- Arrow Down: Move Backward
- Arrow Left: Turn Left
- Arrow Right: Turn Right
Game Features:
- Rocket Controls: Control the rocket using arrow keys to navigate through space.
- Planets: There are three planets positioned in different locations. You can navigate around them.
- Camera: The camera follows the rocket, giving a third-person perspective.
- Single File: All code is contained within one HTML file for simplicity.
Screenshot:
[image: 3D Space Game Screenshot] open (Note: Since we cannot include actual images here, please run the game to see the visuals.)
Explanation of Key Components:
- Babylon.js CDN: The script tag includes Babylon.js from the content delivery network.
- Canvas Element: The
<canvas>element withid="renderCanvas"is where Babylon.js renders the 3D scene. - Scene Creation:
- A scene is created using
new BABYLON.Scene(engine);. - A universal camera is set up and attached to the rocket to follow it.
- Hemispheric light provides basic illumination.
- A scene is created using
- Rocket Construction:
- The rocket is constructed using a cylinder for the body and a cone for the nose.
- The parts are merged into a single mesh for easier control.
- Planet Creation:
- Planets are created using spheres positioned at various points in space.
- Each planet has a distinct color for identification.
- Controls:
- An input map tracks the state of the arrow keys.
- The rocket's position and rotation are updated based on the input.
- Game Loop:
scene.onBeforeRenderObservableis used to update the game state before each frame is rendered.
Customization:
- Adding More Planets: You can add more planets by creating additional spheres and setting their position and materials.
- Changing Rocket Appearance: Modify the dimensions and colors in the rocket construction section to change its appearance.
- Enhancing Controls: Implement additional controls, such as strafing or vertical movement, by expanding the input handling logic.
Enjoy exploring space with your controllable rocket!