overactive

Untitled

Sep 15th, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.82 KB | None | 0 0
  1. import pygame
  2. import time
  3. import sys
  4. import random
  5.  
  6. WIDTH, HEIGHT = 900, 600
  7. WHITE = (255, 255, 255)
  8. BLUE = (0,100,200)
  9. PINK = (255, 0, 120)
  10.  
  11. #### SETTINGS ###
  12.  
  13. LINE_WIDTH = 1
  14. TREE_ROOT = [WIDTH/2, HEIGHT]
  15. ROOT_LEN = 150
  16. TREE_LEN = 10
  17. ANGLE = 30
  18. SHORTEN = 0.75
  19.  
  20. #### SETTINGS ###
  21.  
  22. pygame.init()
  23. canvas = pygame.display.set_mode((WIDTH,HEIGHT))
  24. background  = pygame.Surface((WIDTH,HEIGHT))
  25. font = pygame.font.SysFont("Calibri", 30)
  26. pygame.display.set_caption('Pretty tree')
  27. clock = pygame.time.Clock()
  28.  
  29. def quitGame():
  30.     pygame.quit()
  31.     sys.exit(0)
  32.  
  33. class Branch():
  34.     def __init__(self, start, end):
  35.         self.start = start
  36.         self.end = end
  37.         self.shown = False
  38.  
  39.     def update(self):
  40.         pass
  41.  
  42.     def show(self):
  43.         pygame.draw.line(canvas, PINK, self.start, self.end, LINE_WIDTH)
  44.  
  45.     def newBranch(self, angle, offset):
  46.         new = self.end - self.start
  47.         new = new.rotate(angle)
  48.         newEnd = new * SHORTEN + offset
  49.         return (newEnd)
  50.  
  51.     def wind(self):
  52.         windX = random.randint(-1,1)
  53.         self.end.x += windX
  54.         windY = random.randint(-1,1)
  55.         self.end.y += windY
  56.  
  57. start = pygame.math.Vector2(WIDTH/2, HEIGHT)
  58. end = pygame.math.Vector2(WIDTH/2, (HEIGHT) - ROOT_LEN)
  59. tree = []
  60. tree.append(Branch(start,end))
  61.  
  62. while True:
  63.     for event in pygame.event.get():
  64.         if event.type == pygame.QUIT:
  65.             quitGame()
  66.         if event.type == pygame.MOUSEBUTTONUP:
  67.             for i in range(len(tree)):
  68.                 offset = tree[i].end
  69.            
  70.                 if tree[i].shown != True:
  71.                     angle = random.randrange(10, 60, 10)
  72.                     tree.append(Branch(tree[i].end, tree[i].newBranch(angle, offset) ))
  73.                    
  74.                     angle = random.randrange(-60, -10, 10)
  75.                     tree.append(Branch(tree[i].end, tree[i].newBranch(angle, offset) ))
  76.                
  77.                 tree[i].shown = True
  78.  
  79.     for i in range(len(tree)):
  80.         tree[i].show()
  81.         # tree[i].wind()
  82.        
  83.     pygame.display.flip()  
  84.     clock.tick(60)
Advertisement
Add Comment
Please, Sign In to add comment