Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- import random
- from random import randint
- pygame.init()
- class Cell(pygame.sprite.Sprite):
- def __init__(self, size, color, speed, posx, posy):
- super().__init__()
- self.size = size
- self.color = color
- self.speed = speed
- self.posx = posx
- self.posy = posy
- self.image = pygame.Surface([size, size])
- self.image.fill(WHITE)
- self.image.set_colorkey(WHITE)
- pygame.draw.circle(self.image, color, [posx, posy], size)
- self.rect = self.image.get_rect()
- print('Cell initialized')
- def reproduce(self):
- return (SizeMutation(self.size), ColorMutation(self.color), SpeedMutation(self.speed))
- def ColorMutation(parent_color):
- new_red = parent_color[0]
- new_green = parent_color[1]
- new_blue = parent_color[2]
- new_red += randint(-10, 10)
- new_green += randint(-10, 10)
- new_blue += randint(-10, 10)
- if new_red > 255:
- new_red -= (new_red - 255)
- elif new_red < 0:
- new_red += abs(new_red)
- if new_green > 255:
- new_green -= (new_green - 255)
- elif new_green < 0:
- new_green += abs(new_green)
- if new_blue > 255:
- new_blue -= (new_blue - 255)
- elif new_blue < 0:
- new_blue += abs(new_blue)
- return (new_red, new_green, new_blue)
- def SpeedMutation(parent_speed):
- speed_multiplier = random.uniform(0.8, 1.2)
- return round(parent_speed * speed_multiplier, 2)
- def SizeMutation(parent_size):
- size_multiplier = random.uniform(0.9, 1.1)
- return round(parent_size * size_multiplier, 2)
- WHITE = (255, 255, 255)
- BLACK = (0, 0, 0,)
- RED = (255, 0, 0)
- GREEN = (0, 255, 0)
- BLUE = (0, 0, 255)
- window_size = 700, 500
- screen = pygame.display.set_mode(window_size)
- pygame.display.set_caption("Evolution")
- done = False
- clock = pygame.time.Clock()
- cell_list = pygame.sprite.Group()
- a = Cell(5, GREEN, 2, 200, 200)
- cell_list.add(a)
- while not done:
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- done = True
- screen.fill(WHITE)
- cell_list.draw(screen)
- pygame.display.flip()
- clock.tick(60)
Advertisement
Add Comment
Please, Sign In to add comment