Advertisement
Guest User

sim

a guest
Apr 22nd, 2025
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.35 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Survival Simulation</title>
  6. <style>
  7. body {
  8. margin: 0;
  9. overflow: hidden;
  10. background: #000000; /* Black background for the canvas */
  11. }
  12. canvas {
  13. display: block; /* Removes scrollbars */
  14. }
  15. </style>
  16. </head>
  17. <body>
  18. <canvas id="canvas" width="1000" height="700"></canvas>
  19. <script>
  20. // Get the canvas context
  21. const canvas = document.getElementById('canvas');
  22. const ctx = canvas.getContext('2d');
  23.  
  24. // Utility functions to generate random values and calculate distance
  25. function random(min, max) {
  26. return Math.random() * (max - min) + min;
  27. }
  28.  
  29. function distance(ax, ay, bx, by) {
  30. return Math.hypot(ax - bx, ay - by);
  31. }
  32.  
  33. // The Food class - Represents food objects that creatures can eat
  34. class Food {
  35. constructor(x, y) {
  36. this.x = x;
  37. this.y = y;
  38. this.radius = 4;
  39. this.color = 'green';
  40. }
  41.  
  42. // Draw the food on the canvas
  43. draw() {
  44. ctx.beginPath();
  45. ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2);
  46. ctx.fillStyle = this.color;
  47. ctx.fill();
  48. }
  49. }
  50.  
  51. // The Creature class - Represents creatures that move, eat, reproduce, and die
  52. class Creature {
  53. constructor(x, y, species) {
  54. this.x = x;
  55. this.y = y;
  56. this.radius = 10;
  57. this.species = species;
  58. this.color = species.color;
  59. this.speed = species.speed;
  60. this.sight = species.sight;
  61. this.energy = 100;
  62. this.age = 0;
  63. this.maxAge = 1200; // Maximum age (2 minutes)
  64. this.target = null;
  65. this.matingCooldown = 0;
  66. this.isFemale = Math.random() > 0.5; // Randomly assign gender
  67. this.isNewborn = true;
  68. }
  69.  
  70. // Update the creature's status each frame (movement, energy, etc.)
  71. update(foodArray, creatures) {
  72. this.age++;
  73. this.energy -= 0.05; // Gradually lose energy
  74. if (this.matingCooldown > 0) this.matingCooldown--; // Handle mating cooldown
  75.  
  76. // Handle food search behavior
  77. if (!this.target) {
  78. this.target = foodArray.find(f => distance(this.x, this.y, f.x, f.y) < this.sight);
  79. }
  80.  
  81. if (this.target) {
  82. this.moveTowards(this.target);
  83. if (distance(this.x, this.y, this.target.x, this.target.y) < this.radius + this.target.radius) {
  84. this.energy += 40; // Gain energy from food
  85. foodArray.splice(foodArray.indexOf(this.target), 1); // Remove food
  86. this.target = null;
  87. }
  88. } else {
  89. this.exploreRandomly();
  90. }
  91.  
  92. // Keep the creature within bounds
  93. this.x = Math.max(this.radius, Math.min(canvas.width - this.radius, this.x));
  94. this.y = Math.max(this.radius, Math.min(canvas.height - this.radius, this.y));
  95. }
  96.  
  97. // Move the creature towards its target (food)
  98. moveTowards(target) {
  99. const angle = Math.atan2(target.y - this.y, target.x - this.x);
  100. this.x += Math.cos(angle) * this.speed;
  101. this.y += Math.sin(angle) * this.speed;
  102. }
  103.  
  104. // If no food is found, explore randomly in the area
  105. exploreRandomly() {
  106. this.x += random(-1, 1) * this.speed;
  107. this.y += random(-1, 1) * this.speed;
  108. }
  109.  
  110. // Check if the creature should die based on energy or age
  111. shouldDie() {
  112. return this.energy <= 0 || this.age >= this.maxAge;
  113. }
  114.  
  115. // Check if the creature is ready to reproduce
  116. shouldReproduce() {
  117. return this.energy >= 160 && this.matingCooldown === 0 && !this.isNewborn;
  118. }
  119.  
  120. // Reproduce a new creature with mutated traits
  121. reproduce() {
  122. this.energy -= 80;
  123. this.matingCooldown = 100; // Cooldown period after mating
  124.  
  125. const mutationFactor = 0.1;
  126. const newSpeed = Math.max(0.5, this.speed + random(-mutationFactor, mutationFactor));
  127. const newSight = Math.max(40, this.sight + random(-mutationFactor * 100, mutationFactor * 100));
  128.  
  129. const childSpecies = {
  130. name: this.species.name,
  131. color: this.color,
  132. speed: newSpeed,
  133. sight: newSight
  134. };
  135.  
  136. // Create a new creature with slightly modified traits
  137. return new Creature(this.x + random(-10, 10), this.y + random(-10, 10), childSpecies);
  138. }
  139.  
  140. // Draw the creature on the canvas
  141. draw() {
  142. ctx.beginPath();
  143. ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2);
  144. ctx.fillStyle = this.color;
  145. ctx.fill();
  146.  
  147. // Draw sight radius for visualization
  148. ctx.beginPath();
  149. ctx.arc(this.x, this.y, this.sight, 0, Math.PI * 2);
  150. ctx.strokeStyle = this.color;
  151. ctx.lineWidth = 0.2;
  152. ctx.stroke();
  153.  
  154. // Draw indicator if the creature is a newborn
  155. if (this.isNewborn) {
  156. ctx.strokeStyle = 'yellow';
  157. ctx.lineWidth = 2;
  158. ctx.stroke();
  159. }
  160. }
  161. }
  162.  
  163. // Species definition (e.g., red and blue creatures with different stats)
  164. const speciesList = [
  165. { name: 'Red', color: 'red', speed: 1.2, sight: 100 },
  166. { name: 'Blue', color: 'blue', speed: 1.0, sight: 120 }
  167. ];
  168.  
  169. const creatures = [];
  170. const food = [];
  171.  
  172. // Spawn initial creatures based on species
  173. function spawnInitialCreatures() {
  174. for (let species of speciesList) {
  175. creatures.push(new Creature(random(100, 900), random(100, 600), species));
  176. creatures.push(new Creature(random(100, 900), random(100, 600), species));
  177. }
  178. }
  179.  
  180. // Spawn a specified amount of food at random locations
  181. function spawnFood(amount = 20) {
  182. for (let i = 0; i < amount; i++) {
  183. food.push(new Food(random(0, canvas.width), random(0, canvas.height)));
  184. }
  185. }
  186.  
  187. // Handle the main simulation loop
  188. function loop() {
  189. ctx.clearRect(0, 0, canvas.width, canvas.height);
  190.  
  191. // Update and draw each creature
  192. for (let i = creatures.length - 1; i >= 0; i--) {
  193. const creature = creatures[i];
  194. creature.update(food, creatures);
  195.  
  196. // Remove creatures that should die
  197. if (creature.shouldDie()) {
  198. creatures.splice(i, 1);
  199. } else if (creature.shouldReproduce()) {
  200. creatures.push(creature.reproduce());
  201. }
  202.  
  203. // Draw the creature
  204. creature.draw();
  205. }
  206.  
  207. // Draw food objects
  208. for (let f of food) f.draw();
  209.  
  210. // Spawn new food occasionally
  211. if (Math.random() < 0.02) spawnFood(1);
  212.  
  213. // Update the population counter (number of creatures)
  214. document.getElementById('population').innerText = `Population: ${creatures.length}`;
  215.  
  216. requestAnimationFrame(loop);
  217. }
  218.  
  219. // Initial setup and start the simulation
  220. spawnInitialCreatures();
  221. spawnFood(50);
  222. loop();
  223.  
  224. // Display population
  225. const populationDiv = document.createElement('div');
  226. populationDiv.id = 'population';
  227. populationDiv.style.position = 'absolute';
  228. populationDiv.style.color = 'white';
  229. populationDiv.style.fontSize = '20px';
  230. populationDiv.style.top = '10px';
  231. populationDiv.style.left = '10px';
  232. document.body.appendChild(populationDiv);
  233.  
  234. </script>
  235. </body>
  236. </html>
  237.  
Tags: sim
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement