Advertisement
MrLunk

Python/PyGame Life-Simulation of 3 competing Life forms and Food

Feb 23rd, 2023
868
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.70 KB | None | 0 0
  1. # this is just a script containing the basics for a life simulation with 3 competing lifeforms and food items...
  2. # for Edu purposes ;)
  3. # File : Three_lifeForms_and_food.py
  4. # Script by: MrLunk
  5. # https://github.com/mrlunk/
  6.  
  7. """
  8. This script is a basic simulation of three competing life forms and food items using the Pygame library in Python. It simulates the interactions and behaviors of the life forms in a two-dimensional environment.
  9. """
  10.  
  11.  
  12. import pygame
  13. import random
  14.  
  15. # Define some colors
  16. WHITE = (255, 255, 255)
  17. RED = (255, 0, 0)
  18. BLUE = (0, 0, 255)
  19. YELLOW = (255, 255, 0)
  20. GREEN = (0, 255, 0)
  21.  
  22. # Set the width and height of the screen
  23. WIDTH = 1000
  24. HEIGHT = 1000
  25.  
  26. # Define the life form class
  27. class LifeForm(pygame.sprite.Sprite):
  28.     def __init__(self, color, x, y):
  29.         super().__init__()
  30.         self.color = color
  31.         self.energy = 50
  32.         self.speed = self.energy / 10
  33.         self.image = pygame.Surface([20, 20])
  34.         self.image.fill(self.color)
  35.         self.rect = self.image.get_rect()
  36.         self.rect.x = x
  37.         self.rect.y = y
  38.  
  39.     def update(self, food_group, life_group):
  40.         self.energy -= 1
  41.        
  42.         # Move towards food or other life forms of a different color
  43.         targets = []
  44.         for food in food_group:
  45.             distance = ((food.rect.x - self.rect.x) ** 2 + (food.rect.y - self.rect.y) ** 2) ** 0.5
  46.             if distance < 100:
  47.                 targets.append(food)
  48.         for life in life_group:
  49.             if life.color != self.color:
  50.                 distance = ((life.rect.x - self.rect.x) ** 2 + (life.rect.y - self.rect.y) ** 2) ** 0.5
  51.                 if distance < 100:
  52.                     targets.append(life)
  53.        
  54.         if len(targets) > 0:
  55.             target = min(targets, key=lambda t: ((t.rect.x - self.rect.x) ** 2 + (t.rect.y - self.rect.y) ** 2) ** 0.5)
  56.             dx = target.rect.x - self.rect.x
  57.             dy = target.rect.y - self.rect.y
  58.             distance = ((dx ** 2) + (dy ** 2)) ** 0.5
  59.             if distance > 0:
  60.                 self.rect.x += int((dx / distance) * self.speed)
  61.                 self.rect.y += int((dy / distance) * self.speed)
  62.                
  63.                 # If the life form is close enough to food, eat it
  64.                 if isinstance(target, Food) and distance < 20:
  65.                     self.energy += 10
  66.                     target.kill()
  67.                
  68.                 # If the life form is close enough to a life form of a different color, fight it
  69.  
  70. # READ !
  71. # ... ... ...
  72. # the FULL SCRIPT can be found on my GitHUB here: https://github.com/mrlunk/Fun-with-Python/tree/main/Life_Simulations
  73. # give us a STAR there if you like this basic educational script.
  74.  
  75.  
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement