Advertisement
zicaentu

Fractal ractangle

Dec 1st, 2018
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.68 KB | None | 0 0
  1. """
  2. Draw fractal ractangle programm.
  3.  
  4. """
  5.  
  6. import pygame
  7.  
  8.  
  9. BLACK = (0x00, 0x00, 0x00)
  10. WHITE = (0xff, 0xff, 0xff)
  11. GREEN = (0x00, 0xff, 0x00)
  12. RED =   (0xff, 0x00, 0x00)
  13. ALPHA = 0.2
  14.  
  15.  
  16. def main():
  17.     """ Main function for the game. """
  18.     pygame.init()
  19.  
  20.     # Set the width and height of the screen [width,height]
  21.     size = [800, 600]
  22.     screen = pygame.display.set_mode(size)
  23.  
  24.     pygame.display.set_caption("My Game")
  25.  
  26.     done = False
  27.  
  28.     clock = pygame.time.Clock()
  29.  
  30.     def fractal_rectangle(A:tuple, B:tuple, C:tuple, D:tuple,
  31.         depth=10) -> None:
  32.         """ Draw fractal rectangle.
  33.            In: 4 tuple - point coord
  34.                  int - recursion depth """
  35.  
  36.         if depth < 1:
  37.             return
  38.  
  39.         for M, N in (A, B), (B, C), (C, D), (D, A):
  40.             pygame.draw.line(screen, BLACK, M, N)
  41.  
  42.            
  43.         A1 = (A[0]*(1-ALPHA) + B[0]*ALPHA,
  44.             A[1]*(1-ALPHA) + B[1]*ALPHA)
  45.         B1 = (B[0]*(1-ALPHA) + C[0]*ALPHA,
  46.             B[1]*(1-ALPHA) + C[1]*ALPHA)
  47.         C1 = (C[0]*(1-ALPHA) + D[0]*ALPHA,
  48.             C[1]*(1-ALPHA) + D[1]*ALPHA)
  49.         D1 = (D[0]*(1-ALPHA) + A[0]*ALPHA,
  50.             D[1]*(1-ALPHA) + A[1]*ALPHA)
  51.  
  52.         fractal_rectangle(A1, B1, C1, D1, depth-1)
  53.  
  54.  
  55.     # -------- Main Program Loop -----------
  56.     while not done:
  57.         for event in pygame.event.get():
  58.             if event.type == pygame.QUIT:
  59.                 done = True
  60.  
  61.  
  62.  
  63.         screen.fill(WHITE)
  64.         fractal_rectangle((100, 100), (500, 100),
  65.             (500, 500), (100, 500))
  66.  
  67.         pygame.display.flip()
  68.  
  69.         clock.tick(60)
  70.     pygame.quit()
  71.  
  72. if __name__ == "__main__":
  73.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement