Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- Draw fractal ractangle programm.
- """
- import pygame
- BLACK = (0x00, 0x00, 0x00)
- WHITE = (0xff, 0xff, 0xff)
- GREEN = (0x00, 0xff, 0x00)
- RED = (0xff, 0x00, 0x00)
- ALPHA = 0.01
- def main():
- """ Main function for the game. """
- global ALPHA
- pygame.init()
- # Set the width and height of the screen [width,height]
- size = [600, 600]
- screen = pygame.display.set_mode(size)
- pygame.display.set_caption("My Game")
- done = False
- clock = pygame.time.Clock()
- def fractal_rectangle(A:tuple, B:tuple, C:tuple, D:tuple,
- depth=10) -> None:
- """ Draw fractal rectangle.
- In: 4 tuple - point coord
- int - recursion depth """
- if depth < 1:
- return
- for M, N in (A, B), (B, C), (C, D), (D, A):
- pygame.draw.line(screen, BLACK, M, N)
- A1 = (A[0]*(1-ALPHA) + B[0]*ALPHA,
- A[1]*(1-ALPHA) + B[1]*ALPHA)
- B1 = (B[0]*(1-ALPHA) + C[0]*ALPHA,
- B[1]*(1-ALPHA) + C[1]*ALPHA)
- C1 = (C[0]*(1-ALPHA) + D[0]*ALPHA,
- C[1]*(1-ALPHA) + D[1]*ALPHA)
- D1 = (D[0]*(1-ALPHA) + A[0]*ALPHA,
- D[1]*(1-ALPHA) + A[1]*ALPHA)
- fractal_rectangle(A1, B1, C1, D1, depth-1)
- # -------- Main Program Loop -----------
- while not done:
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- done = True
- screen.fill(WHITE)
- fractal_rectangle((100, 100), (500, 100),
- (500, 500), (100, 500))
- ALPHA += 0.01
- pygame.display.flip()
- clock.tick(60)
- pygame.quit()
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement