Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Can I Fill These Squares in Turtle - Python

I am trying to fill the color in these squares:

https://i.sstatic.net/ALaZV.png

Right now the turtle only fills the corners of theses squares, not the entire square.

Here is my code:

import turtle
import time
import random

print ("This program draws shapes based on the number you enter in a uniform pattern.")
num_str = input("Enter the side number of the shape you want to draw: ")
if num_str.isdigit():
    squares = int(num_str)

angle = 180 - 180*(squares-2)/squares

turtle.up

x = 0 
y = 0
turtle.setpos(x,y)


numshapes = 8
for x in range(numshapes):
    turtle.color(random.random(),random.random(), random.random())
    x += 5
    y += 5
    turtle.forward(x)
    turtle.left(y)
    for i in range(squares):
        turtle.begin_fill()
        turtle.down()
        turtle.forward(40)
        turtle.left(angle)
        turtle.forward(40)
        print (turtle.pos())
        turtle.up()
        turtle.end_fill()

time.sleep(11)
turtle.bye()

I've tried moving around turtle.begin_fill() and end_fill() in numerous locations with no luck… Using Python 3.2.3, thanks.

like image 732
Goose Avatar asked Feb 13 '26 22:02

Goose


2 Answers

I haven't really used turtle, but it looks like this may be what you want to do. Correct me if I've assumed the wrong functionality for these calls:

turtle.begin_fill() # Begin the fill process.
turtle.down() # "Pen" down?
for i in range(squares):  # For each edge of the shape
    turtle.forward(40) # Move forward 40 units
    turtle.left(angle) # Turn ready for the next edge
turtle.up() # Pen up
turtle.end_fill() # End fill.
like image 161
Aesthete Avatar answered Feb 15 '26 11:02

Aesthete


You're drawing a series of triangles, using begin_fill() and end_fill() for each one. What you can probably do is move your calls to begin_fill() and end_fill() outside the inner loop, so you draw a full square and then ask for it to be filled.

like image 25
Greg Hewgill Avatar answered Feb 15 '26 12:02

Greg Hewgill



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!