Skip to content

Instantly share code, notes, and snippets.

@max-kov
Last active May 21, 2020 18:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save max-kov/50e2533808a46ddb2e20 to your computer and use it in GitHub Desktop.
Save max-kov/50e2533808a46ddb2e20 to your computer and use it in GitHub Desktop.
sierpetsky triangle drawing using pygame and python
import pygame, math, sys
def draw(dot1,dot2,dot3,order):
pygame.draw.lines(screen, (255,0,0), True, [dot1,dot2,dot3], 1)
if order>0:
newDot1 = (int((dot1[0]+dot2[0])*0.5),int((dot1[1]+dot2[1])*0.5))
newDot2 = (int((dot2[0]+dot3[0])*0.5),int((dot2[1]+dot3[1])*0.5))
newDot3 = (int((dot3[0]+dot1[0])*0.5),int((dot3[1]+dot1[1])*0.5))
pygame.draw.lines(screen, (255,0,0), True, [newDot1,newDot2,newDot3], 1)
pygame.display.update()
draw(dot2,newDot1,newDot2,order-1) # RECURICION
draw(dot1,newDot1,newDot3,order-1)
draw(dot3,newDot3,newDot2,order-1)
# SCREEEEEEEN INIT
pygame.init()
windowSize = (1000, 1000)
screen = pygame.display.set_mode(windowSize)
# FONT
# pygame.font.init()
# myfont = pygame.font.SysFont("Comic Sans MS", 30)
# label = myfont.render("PRESS SPACE!!", 1, (255,0,0))
# textSize = pygame.font.Font.size(label)
squareRoot3 = math.sqrt(3)
order=0
# MAIN LOOP
while 1:
# screen.blit(label, (0,windowSize[1]-textSize[1] ))
x = windowSize[0]
y = windowSize[1]
# dx = y*squareRoot3
# FIRST TRIANGLE
order+=1
draw((0,0),(x,0),(x/2,y),order)
go=1
while go:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
go=0
pygame.display.update()
input()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment