Python Forum
Trying to make a board with turtle, nothing happens when running script - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Trying to make a board with turtle, nothing happens when running script (/thread-41006.html)



Trying to make a board with turtle, nothing happens when running script - Quascia - Oct-28-2023

Hello,
I'm trying to make a board with turtle ( which I'm new to ) using loops and functions, but nothing seems to work. When I run the script, nothing happens. (I use Pyzo as an IDE)
There are 2 functions : one for making the lines, and one for making the columns. There must be a loop that repeats for n times which determines the number of lines there will be. ; length is obviously the number of pixels the line will be. It's the same thing for columns . Also, between each lines / columns, there must be 20 pixels.

Here is my code :

import turtle
def lines(n,length):
    for i in range(n):
        turtle.pen(fillcolor = "blue", pencolor = "black", pensize = 3)
        turtle.forward(length)
        turtle.up()
        turtle.right(90)
        turtle.forward(20)
        turtle.left(90)
        turtle.down()
        turtle.up()
    turtle.end_fill()
    turtle.done()
    window.exitonclick()

lines(3,32)

def columns(n,length):
    for i in range(n):
        turtle.pen(fillcolor = "green", pencolor = "black", pensize = 3)
        turtle.right(90)
        turtle.forward(length)
        turtle.up()
        turtle.left(90)
        turtle.forward(20)
        turtle.right(90)
        turtle.down()
    turtle.end_fill()
    turtle.done()
    window.exitonclick()
columns(5,7)
Can someone help a beginner ?
Thanks !


RE: Trying to make a board with turtle, nothing happens when running script - DPaul - Oct-28-2023

Some possible improvements:
- turtle up and down must be in the right sequence
- end fill should be preceded by begin fill.
- How many rows & columns are you drawing ?
Paul


RE: Trying to make a board with turtle, nothing happens when running script - Quascia - Nov-01-2023

(Oct-28-2023, 01:52 PM)DPaul Wrote: Some possible improvements:
- turtle up and down must be in the right sequence
- end fill should be preceded by begin fill.
- How many rows & columns are you drawing ?
Paul
The number of rows and columns depends on what we put instead of " n ". So for example, if I put lines(5,87) there will be 5 lines of 87 pixels each. It's the same for columns.


RE: Trying to make a board with turtle, nothing happens when running script - deanhystad - Nov-01-2023

This loop draws 1 line.
    for i in range(n):
        turtle.pen(fillcolor = "blue", pencolor = "black", pensize = 3)  # Place in front of loop.
        turtle.forward(length)
        turtle.up()
        turtle.right(90)
        turtle.forward(20)
        turtle.left(90)
        turtle.down()
        turtle.up()  # <- Pen is up when second iteration of loop runs
I would start by changing the code to this:
import turtle


def lines(n, length):
    turtle.pen(fillcolor="blue", pencolor="black", pensize=3)
    turtle.begin_fill()
    for i in range(n):
        turtle.forward(length)
        turtle.up()
        turtle.right(90)
        turtle.forward(20)
        turtle.left(90)
        turtle.down()
    turtle.end_fill()


def columns(n, length):
    turtle.pen(fillcolor="green", pencolor="black", pensize=3)
    turtle.begin_fill()
    for i in range(n):
        turtle.right(90)
        turtle.forward(length)
        turtle.up()
        turtle.left(90)
        turtle.forward(20)
        turtle.right(90)
        turtle.down()
    turtle.end_fill()


lines(3, 32)
columns(5, 7)
turtle.done()
Now you can at least see the turtle errors.