Python Forum
Drawing piled triangles in Python
Thread Rating:
  • 2 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Drawing piled triangles in Python
#1
Hey everyone, I need to draw a pile of triangles that looks like this: [Image: 3JT8sfr]

I have written code that writes 3 triangles from left to right and one that makes 3 on top of each other, but I can't move on from there.

Here are my codes:

from turtle import Screen, Turtle
from random import randint

def repeat_triangle(t, l, n):     
    setcolor(t, 1)                #function I wrote for turtle and filled triangle being the same coloring
    for i in range(n):            
        t.color(randint(0,255),randint(0,255),randint(0,255)) 
        t.begin_fill()
        t.fd(100) 
        t.lt(120)
        t.fd(100)
        t.lt(120)
        t.fd(100)
        t.lt(120)
        t.fd(100)
        otto.end_fill()

def tiled_triangle(t, length):
    height = length * 3 ** 0.5 / 2 

    for _ in range(4):
        t.color(randint(0, 255), randint(0, 255), randint(0, 255))
        t.begin_fill()
        for _ in range(3):
            t.fd(length)
            t.lt(120)
        t.end_fill()
        t.sety(t.ycor() + height)

screen = Screen()
screen.colormode(255)

otto = Turtle('turtle')
otto.penup()

screen.mainloop()
turtle.bye()
Thanks in advance! I need this for a part of my homework for uni. Really thanks a lot! (I'm using Jupyter Notebooks so maybe there are some differences to regular Python, but I don't think any that matter.)
Reply
#2
Use goto to position the Turtle at a new location.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  HomeWork Python - Drawing window with text center. Voraman 8 3,209 Jan-09-2021, 06:53 PM
Last Post: Voraman
  Need help to finish the hat on the snowman in this Python drawing soowoosamuel 1 3,306 Apr-10-2019, 06:31 AM
Last Post: j.crater

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020