Advertisement
neruomorphic-

MasterJet

Jun 1st, 2024
396
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 25.77 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Pixel Jets</title>
  7. <style>
  8. body { margin: 0; }
  9. canvas { display: block; }
  10. #scoreboard {
  11. position: absolute;
  12. top: 10px;
  13. left: 10px;
  14. color: white;
  15. font-family: Arial, sans-serif;
  16. font-size: 18px;
  17. z-index: 100;
  18. }
  19. </style>
  20. </head>
  21. <body>
  22. <div>
  23. <input type="text" id="username" placeholder="Enter your username">
  24. <button onclick="startGame()">Start Game</button>
  25. </div>
  26. <div id="scoreboard"></div>
  27.  
  28. <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
  29. <script src="https://cdnjs.cloudflare.com/ajax/libs/tensorflow/2.8.0/tf.min.js"></script>
  30. <script>
  31. // Set up the scene, camera, renderer...
  32. // Your existing code here...
  33. // Initialize username and score list
  34. let username = '';
  35. const scoreList = [];
  36.  
  37. // Function to start the game
  38. function startGame() {
  39. // Get username from input field
  40. username = document.getElementById('username').value;
  41. // Hide the input field and button
  42. document.getElementById('username').style.display = 'none';
  43. document.querySelector('button').style.display = 'none';
  44. }
  45.  
  46. // Function to update scoreboard with username
  47. function updateScoreboard() {
  48. scoreDisplay.textContent = `Player: ${username} - Score: ${score}`;
  49. }
  50.  
  51. // Function to check if all spheres are destroyed
  52. function allSpheresDestroyed() {
  53. return activeEnemySpheres === 0 && destroyedEnemySpheres === maxEnemySpheres;
  54. }
  55.  
  56. // Render loop...
  57. // Your existing code here...
  58.  
  59. // Inside the render loop, check if all spheres are destroyed
  60. if (allSpheresDestroyed()) {
  61. // Add username and score to the list
  62. scoreList.push({ username: username, score: score });
  63. // Reset the game or show game over message, etc.
  64. console.log(scoreList); // Display the score list in the console
  65. }
  66. </script>
  67. <script>
  68. // Set up the scene, camera, renderer
  69. const scene = new THREE.Scene();
  70. const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
  71. camera.position.set(0, 0, 5); // Move the camera back
  72. const renderer = new THREE.WebGLRenderer();
  73. renderer.setSize(window.innerWidth, window.innerHeight);
  74. document.body.appendChild(renderer.domElement);
  75.  
  76. // Add a directional light to the scene
  77. const light = new THREE.DirectionalLight(0xffffff, 1);
  78. light.position.set(0, 1, 1);
  79. scene.add(light);
  80.  
  81. // Create a cube to represent the player
  82. const playerGeometry = new THREE.BoxGeometry(1, 1, 1);
  83. const playerMaterial = new THREE.MeshBasicMaterial({ color: 0x00ff00 });
  84. const player = new THREE.Mesh(playerGeometry, playerMaterial);
  85. scene.add(player);
  86.  
  87. // Create the ground
  88. //const groundGeometry = new THREE.PlaneGeometry(100, 100);
  89. // const groundMaterial = new THREE.MeshBasicMaterial({ color: 0xaaaaaa, side: THREE.DoubleSide });
  90. // const ground = new THREE.Mesh(groundGeometry, groundMaterial);
  91. // ground.rotation.x = -Math.PI / 2; // Rotate the ground to be horizontal
  92. //scene.add(ground);
  93.  
  94. // Create the skybox (a large sphere with a texture)
  95. const skyGeometry = new THREE.SphereGeometry(1000, 32, 32);
  96. const skyMaterial = new THREE.MeshBasicMaterial({ color: 0x87CEEB, side: THREE.BackSide }); // Light blue color
  97. const skybox = new THREE.Mesh(skyGeometry, skyMaterial);
  98. scene.add(skybox);
  99.  
  100. // Create AI cube
  101. const aiCubeGeometry = new THREE.BoxGeometry(0.5, 0.5, 0.5);
  102. const aiCubeMaterial = new THREE.MeshBasicMaterial({ color: 0xff0000 });
  103. const aiCube = new THREE.Mesh(aiCubeGeometry, aiCubeMaterial);
  104. aiCube.position.set(1, 0.75, 0); // Initial position relative to the player cube
  105. scene.add(aiCube);
  106.  
  107. // Update function to make the AI cube follow the player cube
  108. function updateAICubePosition() {
  109. aiCube.position.copy(player.position); // Set AI cube's position to player cube's position
  110. aiCube.translateX(1); // Move the AI cube to the right (shoulder position)
  111. aiCube.translateY(0.75); // Move the AI cube upward (shoulder position)
  112. }
  113.  
  114. // Create AI projectiles
  115. const aiProjectiles = [];
  116. const maxAIProjectiles = 3; // Maximum number of AI projectiles
  117. for (let i = 0; i < maxAIProjectiles; i++) {
  118. const aiProjectileGeometry = new THREE.SphereGeometry(0.1, 16, 16); // Use sphere geometry
  119. const aiProjectileMaterial = new THREE.MeshBasicMaterial({ color: 0xff0000 });
  120. const aiProjectiles = [];
  121. const maxAIProjectiles = 3; // Maximum number of AI projectiles
  122. for (let i = 0; i < maxAIProjectiles; i++) {
  123. const aiProjectile = new THREE.Mesh(aiProjectileGeometry, aiProjectileMaterial);
  124. aiProjectile.visible = false;
  125. aiProjectiles.push(aiProjectile);
  126. scene.add(aiProjectile);
  127. }
  128. }
  129. // Function to make the AI cube shoot projectiles continuously
  130. function shootAIProjectilesContinuously() {
  131. shootAIProjectiles(); // Shoot projectiles towards the player
  132. setTimeout(shootAIProjectilesContinuously, 5000); // Shoot every 1 second (adjust as needed)
  133. }
  134.  
  135. // Function to make the AI cube shoot projectiles
  136. function shootAIProjectiles() {
  137. const direction = player.position.clone().sub(aiCube.position).normalize();
  138. const availableProjectile = aiProjectiles.find(proj => !proj.visible);
  139. if (availableProjectile) {
  140. availableProjectile.position.copy(aiCube.position);
  141. availableProjectile.direction = direction;
  142. availableProjectile.visible = true;
  143. }
  144. }
  145. shootAIProjectilesContinuously();
  146. // Declare enemySpheres array
  147. // Function to make the AI cube shoot homing missiles towards the closest enemy sphere
  148. // Function to make the AI cube shoot homing missiles towards nearby enemy spheres
  149.  
  150. // Function to make the AI cube shoot homing missiles towards nearby enemy spheres
  151. function shootHomingMissiles() {
  152. const closestEnemies = findClosestEnemies();
  153. closestEnemies.forEach((enemy) => {
  154. const missile = new THREE.Mesh(
  155. new THREE.SphereGeometry(0.1, 16, 16),
  156. new THREE.MeshBasicMaterial({ color: 0xff0000 })
  157. );
  158. missile.position.copy(aiCube.position);
  159. missile.target = enemy; // Store the enemy object as the target
  160. homingMissiles.push(missile);
  161. scene.add(missile);
  162. });
  163. }
  164.  
  165. // Function to find the closest enemy spheres
  166. function findClosestEnemies() {
  167. const closestEnemies = [];
  168. enemySpheres.forEach((enemy) => {
  169. if (enemy.visible) {
  170. const distance = aiCube.position.distanceTo(enemy.position);
  171. if (distance < 1.2) { // Change the range as needed
  172. closestEnemies.push(enemy);
  173. }
  174. }
  175. });
  176. return closestEnemies;
  177. }
  178. const enemySpheres = [];
  179.  
  180. // Set up keyboard input
  181. const keyboard = {};
  182. document.addEventListener('keydown', (event) => {
  183. keyboard[event.key] = true;
  184. });
  185. document.addEventListener('keyup', (event) => {
  186. keyboard[event.key] = false;
  187. });
  188.  
  189. // Update player position based on keyboard input
  190. const playerSpeed = 0.1;
  191. function updatePlayerPosition() {
  192. if (keyboard['a']) player.position.x -= playerSpeed;
  193. if (keyboard['d']) player.position.x += playerSpeed;
  194. if (keyboard['w']) player.position.y += playerSpeed;
  195. if (keyboard['s']) player.position.y -= playerSpeed;
  196. }
  197.  
  198. // Create projectile (ball) objects
  199. const projectiles = [];
  200. const maxProjectiles = 10000; // Maximum number of projectiles
  201. for (let i = 0; i < maxProjectiles; i++) {
  202. const projectileGeometry = new THREE.SphereGeometry(0.1, 32, 32);
  203. const projectileMaterial = new THREE.MeshBasicMaterial({ color: 0xFFFF00 });
  204. const projectile = new THREE.Mesh(projectileGeometry, projectileMaterial);
  205. projectile.visible = false;
  206. projectiles.push(projectile);
  207. scene.add(projectile);
  208. }
  209.  
  210. // Create enemy spheres
  211. const maxEnemySpheres = 3; // Maximum number of enemy spheres
  212. let activeEnemySpheres = 0; // Track the number of active enemy spheres
  213. let destroyedEnemySpheres = 0; // Track the number of destroyed enemy spheres
  214.  
  215. // Function to spawn enemy spheres
  216. function spawnEnemySphere() {
  217. if (activeEnemySpheres < maxEnemySpheres) {
  218. const enemyGeometry = new THREE.SphereGeometry(0.2, 32, 32);
  219. const enemyMaterial = new THREE.MeshBasicMaterial({ color: 0xff0000 });
  220. const enemy = new THREE.Mesh(enemyGeometry, enemyMaterial);
  221. const spawnPosition = new THREE.Vector3(Math.random() * 10 - 5, Math.random() * 5, -10 - Math.random() * 10);
  222. enemy.position.copy(spawnPosition);
  223. enemy.userData.spawnPosition = spawnPosition; // Set the spawn position in userData
  224.  
  225. // Set the targetPlayer property with a 10% chance
  226. enemy.targetPlayer = Math.random() <= 0.1;
  227.  
  228. enemy.visible = true; // Initially visible
  229. activeEnemySpheres++;
  230. scene.add(enemy);
  231. enemySpheres.push(enemy);
  232.  
  233. // Create projectiles for the enemy sphere
  234. enemy.projectiles = [];
  235. const maxEnemyProjectiles = 5; // Maximum number of enemy projectiles
  236. for (let i = 0; i < maxEnemyProjectiles; i++) {
  237. const enemyProjectileGeometry = new THREE.SphereGeometry(0.05, 16, 16);
  238. const enemyProjectileMaterial = new THREE.MeshBasicMaterial({ color: 0xFFFF00 });
  239. const enemyProjectile = new THREE.Mesh(enemyProjectileGeometry, enemyProjectileMaterial);
  240. enemyProjectile.visible = false;
  241. enemy.projectiles.push(enemyProjectile);
  242. scene.add(enemyProjectile);
  243. }
  244. }
  245. }
  246.  
  247. // Initial spawn of enemy spheres
  248. for (let i = 0; i < maxEnemySpheres; i++) {
  249. spawnEnemySphere();
  250. }
  251.  
  252. // Score variables
  253. let score = 0;
  254. const scoreDisplay = document.getElementById('scoreboard');
  255.  
  256. // Function for enemies to shoot at the player
  257. function enemyShootProjectiles() {
  258. enemySpheres.forEach((enemy) => {
  259. if (enemy.visible) {
  260. const availableProjectile = enemy.projectiles.find(proj => !proj.visible);
  261. if (availableProjectile) {
  262. availableProjectile.position.copy(enemy.position);
  263. const direction = player.position.clone().sub(enemy.position).normalize();
  264. availableProjectile.direction = direction;
  265. availableProjectile.visible = true;
  266. }
  267. }
  268. });
  269. }
  270. const homingMissiles = [];
  271.  
  272. // Set initial camera position and orientation
  273. let currentView = 2;
  274. function setCameraView(view) {
  275. currentView = view;
  276. switch (view) {
  277. case 1: // First person view
  278. camera.position.copy(player.position);
  279. camera.position.z += 0.5; // Slightly in front of the player
  280. camera.lookAt(player.position.clone().add(new THREE.Vector3(0, 0, -1)));
  281. break;
  282. case 2: // Default view
  283. camera.position.set(0, 0, 5);
  284. camera.lookAt(0, 0, 0);
  285. break;
  286. case 3: // Third person view
  287. camera.position.copy(player.position.clone().add(new THREE.Vector3(0, 5, 10)));
  288. camera.lookAt(player.position);
  289. break;
  290. }
  291. }
  292.  
  293. // Event listener for key presses to change camera view
  294. document.addEventListener('keydown', (event) => {
  295. if (event.key === '1') setCameraView(1);
  296. if (event.key === '2') setCameraView(2);
  297. if (event.key === '3') setCameraView(3);
  298. });
  299.  
  300.  
  301. // Render loop
  302. function animate() {
  303. requestAnimationFrame(animate);
  304. updatePlayerPosition();
  305. updateAICubePosition();
  306. shootAIProjectiles(); // Shoot AI projectiles continuously
  307. enemyShootProjectiles(); // Enemies shoot projectiles at the player
  308. shootHomingMissiles(); // AI cube shoots homing missiles automatically
  309. moveHomingMissiles(); // Move homing missiles towards their targets
  310. dodgeProjectiles();
  311. // Move enemy spheres forward or towards the player
  312. enemySpheres.forEach((enemy) => {
  313. if (enemy.visible) {
  314. if (enemy.targetPlayer) {
  315. // Calculate direction towards the player
  316. const direction = player.position.clone().sub(enemy.position).normalize();
  317. enemy.position.add(direction.multiplyScalar(0.05)); // Move towards the player
  318. } else {
  319. enemy.position.z += 0.05; // Move forward
  320. }
  321. // Respawn enemy sphere if it goes past the cube or is not visible
  322. if (enemy.position.z > 10 || !isSphereVisible(enemy)) {
  323. enemy.visible = false;
  324. activeEnemySpheres--;
  325. // Respawn enemy sphere
  326. respawnEnemySphere(enemy);
  327. }
  328. }
  329. });
  330. // Update camera for first-person view
  331. if (currentView === 1) {
  332. camera.position.copy(player.position);
  333. camera.position.z += 0.5; // Slightly in front of the player
  334. camera.lookAt(player.position.clone().add(new THREE.Vector3(0, 0, -1)));
  335. }
  336. // Move projectiles
  337. for (let i = 0; i < maxProjectiles; i++) {
  338. const projectile = projectiles[i];
  339. if (projectile.visible) {
  340. projectile.position.add(projectile.direction.clone().multiplyScalar(0.2)); // Adjust speed of projectiles
  341. // Check if projectile is out of bounds
  342. if (projectile.position.z > 10) {
  343. projectile.visible = false; // Hide the projectile
  344. }
  345. }
  346. }
  347.  
  348. // Move AI projectiles
  349. aiProjectiles.forEach((projectile) => {
  350. if (projectile.visible) {
  351. projectile.position.add(projectile.direction.clone().multiplyScalar(0.2));
  352. if (projectile.position.z > 10) {
  353. projectile.visible = false;
  354. }
  355. }
  356. });
  357.  
  358. // Move enemy projectiles
  359. enemySpheres.forEach((enemy) => {
  360. enemy.projectiles.forEach((projectile) => {
  361. if (projectile.visible) {
  362. projectile.position.add(projectile.direction.clone().multiplyScalar(0.2)); // Adjust speed of projectiles
  363. // Check if projectile is out of bounds
  364. if (projectile.position.z > 10) {
  365. projectile.visible = false; // Hide the projectile
  366. }
  367. }
  368. });
  369. });
  370.  
  371. // Check for collisions between enemy spheres and projectiles or cube
  372. enemySpheres.forEach((enemy) => {
  373. if (enemy.visible) {
  374. // Check for collisions with projectiles
  375. projectiles.forEach((projectile) => {
  376. if (projectile.visible && enemy.position.distanceTo(projectile.position) < 0.3) {
  377. // Enemy hit by projectile, respawn with a random new origin
  378. respawnEnemySphere(enemy);
  379. projectile.visible = false; // Hide the projectile
  380. score++; // Increment score
  381. updateScoreboard();
  382. }
  383. });
  384. // Check for collisions with the cube (player)
  385. if (enemy.position.distanceTo(player.position) < 0.6) {
  386. // Cube hit by enemy, respawn with a random new origin
  387. respawnEnemySphere(enemy);
  388. score--; // Decrement score
  389. updateScoreboard();
  390. }
  391. }
  392. });
  393.  
  394. // Check for collisions between enemy projectiles and player
  395. enemySpheres.forEach((enemy) => {
  396. enemy.projectiles.forEach((projectile) => {
  397. if (projectile.visible && player.position.distanceTo(projectile.position) < 0.5) {
  398. // Player hit by enemy projectile
  399. projectile.visible = false; // Hide the projectile
  400. score--; // Decrement score
  401. updateScoreboard();
  402. }
  403. });
  404. });
  405.  
  406. renderer.render(scene, camera);
  407. }
  408. animate();
  409. // Function to move homing missiles towards their targets
  410. function moveHomingMissiles() {
  411. homingMissiles.forEach((missile) => {
  412. if (missile.visible && missile.target) {
  413. const direction = missile.target.position.clone().sub(missile.position).normalize();
  414. missile.position.add(direction.multiplyScalar(0.1)); // Adjust missile speed
  415. if (missile.position.distanceTo(missile.target.position) < 0.2) {
  416. missile.visible = false; // Hide the missile upon reaching the target
  417. missile.target.visible = false; // Hide the target enemy sphere
  418. }
  419. }
  420. });
  421. }
  422. // Function to move enemy spheres and dodge projectiles
  423. function moveEnemySpheres() {
  424. enemySpheres.forEach((enemy) => {
  425. if (enemy.visible) {
  426. homingMissiles.forEach((missile) => {
  427. if (missile.visible && missile.target === enemy) {
  428. const distance = enemy.position.distanceTo(missile.position);
  429. if (distance < 2) { // If missile is close to the enemy
  430. const dodgeDirection = enemy.position.clone().sub(missile.position).normalize();
  431. enemy.position.add(dodgeDirection.multiplyScalar(0.05)); // Adjust dodge speed
  432. }
  433. }
  434. });
  435. }
  436. });
  437. }
  438.  
  439. // Automatically shoot homing missiles at a set interval
  440. setInterval(shootHomingMissiles, 3000); // Shoot every 3 seconds (slower)
  441. // Function to shoot AI projectiles continuously
  442. function shootAIProjectilesContinuous() {
  443. setInterval(shootAIProjectiles, 5000); // Adjust interval as needed (milliseconds)
  444. }
  445. // Function to make enemy spheres dodge projectiles
  446. function dodgeProjectiles() {
  447. enemySpheres.forEach((enemy) => {
  448. if (enemy.visible) {
  449. projectiles.forEach((projectile) => {
  450. if (projectile.visible) {
  451. const distance = enemy.position.distanceTo(projectile.position);
  452. if (distance < 1.49) { // Adjust this distance as needed
  453. const dodgeDirection = enemy.position.clone().sub(projectile.position).normalize();
  454. enemy.position.add(dodgeDirection.multiplyScalar(0.019)); // Adjust dodge speed as needed
  455. }
  456. }
  457. });
  458. }
  459. });
  460. }
  461. // Function to make the AI cube shoot projectiles towards the player
  462. function shootAIProjectiles() {
  463. const direction = player.position.clone().sub(aiCube.position).normalize();
  464. const availableProjectile = aiProjectiles.find(proj => !proj.visible);
  465. if (availableProjectile) {
  466. availableProjectile.position.copy(aiCube.position);
  467. availableProjectile.direction = direction;
  468. availableProjectile.visible = true;
  469. }
  470. }
  471.  
  472. // Call the function to shoot AI projectiles continuously
  473. shootAIProjectilesContinuous();
  474. // Listen for spacebar press to shoot projectiles
  475. document.addEventListener('keydown', (event) => {
  476. if (event.key === ' ') { // Spacebar
  477. shootProjectiles();
  478. playSound(); // Play sound on spacebar press
  479. }
  480. });
  481.  
  482. let canShoot = true;
  483. function shootProjectiles() {
  484. if (canShoot) {
  485. canShoot = false;
  486. for (let i = 0; i < maxProjectiles; i++) {
  487. const projectile = projectiles[i];
  488. if (!projectile.visible) {
  489. projectile.position.copy(player.position); // Set projectile position to player position
  490. projectile.visible = true; // Show the projectile
  491. projectile.direction = new THREE.Vector3(0, 0, -1); // Set projectile direction
  492. break; // Break out of loop after shooting one projectile
  493. }
  494. }
  495. setTimeout(() => {
  496. canShoot = true;
  497. }, 10); // Reload cooldown (milliseconds)
  498. }
  499. }
  500.  
  501. // Check if a sphere is visible on the screen
  502. function isSphereVisible(sphere) {
  503. const frustum = new THREE.Frustum();
  504. const cameraViewProjectionMatrix = new THREE.Matrix4();
  505. camera.updateMatrixWorld(); // make sure the camera matrix is updated
  506. camera.matrixWorldInverse.copy(camera.matrixWorld).invert();
  507. cameraViewProjectionMatrix.multiplyMatrices(camera.projectionMatrix, camera.matrixWorldInverse);
  508. frustum.setFromProjectionMatrix(cameraViewProjectionMatrix);
  509. return frustum.intersectsObject(sphere);
  510. }
  511.  
  512. // Respawn enemy sphere with a random new origin
  513. function respawnEnemySphere(enemy) {
  514. const distanceFactor = Math.random() >= 0.5 ? 2 : 1; // Randomly choose between 1x or 2x
  515. const newOrigin = new THREE.Vector3(
  516. Math.random() * 10 - 5,
  517. Math.random() * 5,
  518. -10 - Math.random() * 10
  519. ).addScaledVector(new THREE.Vector3(0, 0, -1), 10 * distanceFactor);
  520.  
  521. if (enemy) {
  522. enemy.position.copy(newOrigin);
  523. enemy.visible = true;
  524. activeEnemySpheres++;
  525. }
  526. }
  527.  
  528. // Update scoreboard
  529. function updateScoreboard() {
  530. scoreDisplay.textContent = `Score: ${score}`;
  531. }
  532.  
  533. // Function to play sound
  534. function playSound() {
  535. const context = new AudioContext();
  536. const oscillator = context.createOscillator();
  537. oscillator.connect(context.destination);
  538. oscillator.type = 'sine';
  539. oscillator.frequency.setValueAtTime(1000, context.currentTime); // Set frequency
  540. oscillator.start();
  541. setTimeout(() => {
  542. oscillator.stop();
  543. }, 100); // Stop after 100 milliseconds
  544. }
  545.  
  546. // TensorFlow.js integration
  547. const model = tf.sequential();
  548. model.add(tf.layers.dense({units: 64, inputShape: [3], activation: 'relu'}));
  549. model.add(tf.layers.dense({units: 64, activation: 'relu'}));
  550. model.add(tf.layers.dense({units: 2, activation: 'softmax'}));
  551. model.compile({loss: 'meanSquaredError', optimizer: 'sgd'});
  552.  
  553. // Prepare data for training (very basic, just for demonstration)
  554. const trainingData = [];
  555. const outputData = [];
  556. for (let i = 0; i < 1000; i++) {
  557. const x1 = Math.random();
  558. const x2 = Math.random();
  559. const y = x1 + x2 > 1 ? 1 : 0; // Sum greater than 1 => 1, otherwise 0
  560. trainingData.push([x1, x2, 1]); // Third input (1) to simulate the bias
  561. outputData.push([y, 1 - y]); // One-hot encoded labels
  562. }
  563.  
  564. // Convert data to tensors
  565. const xs = tf.tensor2d(trainingData);
  566. const ys = tf.tensor2d(outputData);
  567.  
  568. // Train the model
  569. model.fit(xs, ys, {epochs: 10}).then(() => {
  570. // Model training complete
  571. console.log('Model training complete');
  572. });
  573. </script>
  574. </body>
  575. </html>
  576.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement