Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- import time
- import sys
- import random
- WIDTH, HEIGHT = 600, 600
- WHITE = (255, 255, 255)
- BLUE = (0,100,200)
- PINK = (255, 0, 120)
- #### SETTINGS ###
- LINE_WIDTH = 3
- TREE_ROOT = [WIDTH/2, HEIGHT]
- ROOT_LEN = 100
- TREE_LEN = 10
- #### SETTINGS ###
- pygame.init()
- canvas = pygame.display.set_mode((WIDTH,HEIGHT))
- background = pygame.Surface((WIDTH,HEIGHT))
- font = pygame.font.SysFont("Calibri", 30)
- pygame.display.set_caption('Pretty tree')
- clock = pygame.time.Clock()
- def quitGame():
- pygame.quit()
- sys.exit(0)
- class Branch():
- def __init__(self, start, end):
- self.start = start
- self.end = end
- def show(self):
- pygame.draw.line(canvas, PINK, self.start, self.end, LINE_WIDTH)
- def leftBranch(self):
- newLeftEnd=[]
- newLeftEnd.append(self.end[0] + 50)
- newLeftEnd.append(self.end[1] - 50)
- return newLeftEnd
- def rightBranch(self):
- newRightEnd=[]
- newRightEnd.append(self.end[0] - 50)
- newRightEnd.append(self.end[1] - 50)
- return newRightEnd
- start = TREE_ROOT
- end = [WIDTH/2, HEIGHT - ROOT_LEN]
- tree = []
- root = Branch(start,end)
- tree.append(root)
- while True:
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- quitGame()
- if event.type == pygame.MOUSEBUTTONUP:
- for i in range(len(tree)):
- tree.append(Branch(end, tree[i].leftBranch()))
- tree.append(Branch(end, tree[i].rightBranch()))
- tree[i].show()
- pygame.display.flip()
- clock.tick(60)
Advertisement
Add Comment
Please, Sign In to add comment