overactive

Untitled

Sep 10th, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.44 KB | None | 0 0
  1. import pygame
  2. import time
  3. import sys
  4. import random
  5.  
  6. WIDTH, HEIGHT = 600, 600
  7. WHITE = (255, 255, 255)
  8. BLUE = (0,100,200)
  9. PINK = (255, 0, 120)
  10.  
  11. #### SETTINGS ###
  12.  
  13. LINE_WIDTH = 3
  14. TREE_ROOT = [WIDTH/2, HEIGHT]
  15. ROOT_LEN = 100
  16. TREE_LEN = 10
  17.  
  18. #### SETTINGS ###
  19.  
  20. pygame.init()
  21. canvas = pygame.display.set_mode((WIDTH,HEIGHT))
  22. background  = pygame.Surface((WIDTH,HEIGHT))
  23. font = pygame.font.SysFont("Calibri", 30)
  24. pygame.display.set_caption('Pretty tree')
  25. clock = pygame.time.Clock()
  26.  
  27. def quitGame():
  28.     pygame.quit()
  29.     sys.exit(0)
  30.  
  31. class Branch():
  32.     def __init__(self, start, end):
  33.         self.start = start
  34.         self.end = end
  35.  
  36.     def show(self):
  37.         pygame.draw.line(canvas, PINK, self.start, self.end, LINE_WIDTH)
  38.  
  39.     def leftBranch(self):
  40.         newLeftEnd=[]
  41.         newLeftEnd.append(self.end[0] + 50)
  42.         newLeftEnd.append(self.end[1] - 50)
  43.         return newLeftEnd
  44.  
  45.     def rightBranch(self):
  46.         newRightEnd=[]
  47.         newRightEnd.append(self.end[0] - 50)
  48.         newRightEnd.append(self.end[1] - 50)
  49.         return newRightEnd
  50.  
  51. start = TREE_ROOT
  52. end = [WIDTH/2, HEIGHT - ROOT_LEN]
  53.  
  54. tree = []
  55. root = Branch(start,end)
  56. tree.append(root)
  57.  
  58. while True:
  59.     for event in pygame.event.get():
  60.        
  61.         if event.type == pygame.QUIT:
  62.             quitGame()
  63.         if event.type == pygame.MOUSEBUTTONUP:
  64.             for i in range(len(tree)):
  65.                 tree.append(Branch(end, tree[i].leftBranch()))
  66.                 tree.append(Branch(end, tree[i].rightBranch()))
  67.                 tree[i].show()
  68.  
  69.     pygame.display.flip()  
  70.     clock.tick(60)
Advertisement
Add Comment
Please, Sign In to add comment