Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- import random
- from random import randint
- pygame.init()
- WHITE = 255, 255, 255
- BLACK = 0, 0, 0,
- def GenerateColor():
- return randint(0, 255), randint(0, 255), randint(0, 255)
- def GenerateSpeed():
- return random.uniform(0.8, 1.2)
- def GenerateSize():
- return randint(10, 20)
- def MutateColor(color):
- old_red, old_green, old_blue = color[0], color[1], color[2]
- change_red, change_blue, change_green = randint(-5, 5), randint(-5, 5), randint(-5, 5)
- new_red = old_red + change_red
- if new_red > 255:
- new_red -= abs(change_red)
- elif new_red < 0:
- new_red += abs(change_red)
- new_green = old_green + change_green
- if new_green > 255:
- new_green -= abs(change_green)
- elif new_green < 0:
- new_green += abs(change_green)
- new_blue = old_blue + change_blue
- if new_blue > 255:
- new_blue -= abs(change_blue)
- elif new_blue < 0:
- new_blue += abs(change_blue)
- return new_red, new_green, new_blue
- def MutateSpeed(speed):
- return random.uniform(0.8, 1.6) * speed
- class Cell(pygame.sprite.Sprite):
- def __init__(self, color, speed, size):
- super().__init__()
- self.color = color
- self.speed = speed
- self.size = size
- self.image = pygame.Surface([size * 4, size * 4])
- self.image.fill(WHITE)
- self.image.set_colorkey(WHITE)
- self.rect = self.image.get_rect()
- pygame.draw.circle(self.image, color, [size, size], size)
- w_size = 700, 500
- screen = pygame.display.set_mode(w_size)
- pygame.display.set_caption('Evolution')
- done = False
- clock = pygame.time.Clock()
- cell_list = pygame.sprite.Group()
- for i in range(25):
- cell = Cell(GenerateColor(), GenerateSpeed(), GenerateSize())
- cell.rect.x = randint(0, w_size[0] - 100)
- cell.rect.y = randint(0, w_size[1] - 100)
- cell_list.add(cell)
- while not done:
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- done = True
- screen.fill(WHITE)
- for cell in cell_list:
- direction = randint(0, 3)
- if direction == 0:
- cell.rect.x += cell.speed
- elif direction == 1:
- cell.rect.x -= cell.speed
- elif direction == 2:
- cell.rect.y += cell.speed
- elif direction == 3:
- cell.rect.y -= cell.speed
- cell_list.draw(screen)
- pygame.display.flip()
- clock.tick(60)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement