Guest User

Chaos Game

a guest
Dec 14th, 2021
408
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.01 KB | None | 0 0
  1. import pygame
  2.  
  3. from sys import exit
  4. from random import randint
  5. from math import sqrt
  6.  
  7. pygame.init()
  8.  
  9. size = 1200
  10. WINDOW_SIZE = (size, size)
  11. screen = pygame.display.set_mode(WINDOW_SIZE)
  12.  
  13. triangle = [
  14.     (0, size),
  15.     (size, size),
  16.     (size/2, 0)
  17. ]
  18.  
  19. tracer_point = triangle[0]
  20. visited_points = [tracer_point]
  21.  
  22. def get_midpoint(point1, point2):
  23.     x = (point1[0] + point2[0]) / 2
  24.     y = (point1[1] + point2[1]) / 2
  25.     return (x, y)
  26.  
  27. def draw(display):
  28.     display.fill('white')
  29.  
  30.     pygame.draw.line(display, 'black', triangle[0], triangle[1], 5)
  31.     pygame.draw.line(display, 'black', triangle[1], triangle[2], 5)
  32.     pygame.draw.line(display, 'black', triangle[2], triangle[0], 5)
  33.  
  34.     for point in visited_points:
  35.         pygame.draw.circle(display, 'black', point, 4)
  36.  
  37.     pygame.display.update()
  38.  
  39. while True:
  40.     for event in pygame.event.get():
  41.         if event.type == pygame.QUIT:
  42.             exit()
  43.  
  44.     num = randint(0, 2)
  45.     tracer_point = get_midpoint(tracer_point, triangle[num])
  46.     visited_points.append(tracer_point)
  47.  
  48.     draw(screen)
Advertisement
Add Comment
Please, Sign In to add comment