Advertisement
zicaentu

Fractal ractangle 2

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