yoyo
yoyo

Reputation: 1

is it possible to make this python turtle image code shorter?

image to make

This is my code so far, is there a way to make the code smaller so you don't have to type in every move, and how to move the whole image, because I don't have enough space for the remaining part of the image. I already tried changing the values of screen.setworldcoordinates but then I didn't see the image anymore.

from turtle import *
screen = Screen()
screen.clear()
screen.screensize(200, 200)
screen.setworldcoordinates(-250, -250, 350, 250)

alex = Turtle()
alex.shape("turtle")
alex.speed(10)

alex.forward(30)
alex.left(90)
alex.forward(30)
alex.right(90)
alex.forward(30)
alex.right(90)
alex.forward(60)
alex.left(90)
alex.forward(30)
alex.left(90)
alex.forward(30)
alex.right(90)
alex.forward(30)
alex.left(90)
alex.forward(30)
alex.left(90)
alex.forward(30)
alex.right(90)
alex.forward(30)
alex.right(90)
alex.forward(60)
alex.left(90)
alex.forward(30)
alex.left(90)
alex.forward(30)
alex.right(90)
alex.forward(30)
alex.right(90)
alex.forward(30)
alex.left(90)
alex.forward(30)
alex.right(90)
alex.forward(30)
alex.right(90)
alex.forward(60)
alex.left(90)
alex.forward(30)
alex.left(90)
alex.forward(30)
alex.right(90)
alex.forward(30)
alex.right(90)
alex.forward(30)
alex.left(90)
alex.forward(30)
alex.right(90)
alex.forward(30)
alex.right(90)
alex.forward(60)
alex.left(90)
alex.forward(30)
alex.left(90)
alex.forward(30)
alex.right(90)
alex.forward(60)

Upvotes: 0

Views: 143

Answers (3)

cdlane
cdlane

Reputation: 41872

I'm going to take a simpler, less compact approach. If we examine the curve, we find that it's made up of repetitions of a simpler curve:

enter image description here

The only thing that varies is the turtle's orientation when it starts drawing this shape. So if we can encapsulate the code that draws this shape, we can simplify, and shorten, our program significantly:

from turtle import Screen, Turtle

def wave():
    alex.forward(30)
    alex.left(90)
    alex.forward(30)
    alex.right(90)
    alex.forward(30)
    alex.right(90)

    alex.forward(60)

    alex.left(90)
    alex.forward(30)
    alex.left(90)
    alex.forward(30)
    alex.right(90)
    alex.forward(30)

screen = Screen()
screen.setup(500, 500)

alex = Turtle()
alex.shape('turtle')
alex.speed('fastest')

alex.penup()
alex.setx(-250)
alex.pendown()

for angle in (0, 90, -90, -90, 0, 90, 90, -90):
    alex.left(angle)
    wave()

screen.exitonclick()

I'm not saying this is the best approach, but rather that a first step to reducing the size of any program is to look for patterns in your code and data to see if you can work in larger conceptual chunks.

how to move the whole image, because I don't have enough space for the remaining part of the image

You don't need setworldcoordinates(), the problem is you called screensize() (does something else) when you really want setup(). Then you can use setx() to move the turtle to the left side of the window:

enter image description here

Upvotes: 0

Alain T.
Alain T.

Reputation: 42143

Since you're always moving in steps of 30, the only variation is on the turns (Left, right, or none). So, you can drive the turtle using a string of turning instructions to be executed before each step forward:

from turtle import *

alex = Turtle()
alex.shape("turtle")
alex.speed(10)
alex.up()
alex.setx(-window_width()//2) # start on left side of window
alex.down()


turns = "-LRR-LLRLLRR-LLRRLRR-LLRRLRR-LLR-LRR-LLRLLRR-LLRLLRR-LLRRLRR-LLR"
for turn in turns:
    if turn=="L":alex.left(90)
    if turn=="R":alex.right(90)
    alex.forward(30)

This specific pattern could also be obtained recursively (like a fractal):

def pattern(turns,n,step=30,angle=90):
    if not n: return alex.forward(step)
    for turn in turns:
        if turn=="L":alex.left(angle)
        if turn=="R":alex.right(angle)
        pattern(turns,n-1,step,angle)

pattern("-LRR-LLR",2)

You can play with the pattern function using deeper recursion or different turn sequences or angles to get interesting drawings:

pattern("-LRR-LLR",3,10)
pattern("-LRRL-",4,3)
pattern("-LRRL",4,8)
pattern("-LRR-RRR-",3,15)
pattern("-LRR-LLR-",3,20,angle=120)
pattern("-RRRLLLLLLRRR-",2,10,angle=45)
pattern("-LR-R-R-RR",3,20)
pattern("-LRRRLLLLRRRL",3,3,75)
pattern("-LRRRLLLLRRRL",4,2,75)

Upvotes: 3

orby
orby

Reputation: 352

You can put all the turtle moves in a file and let your program read and parse the file and execute the moves.

The file format, could be:

left 90
forward 30

or:

l90
f30

Or you can invent a json format and practice json as well.

Upvotes: 0

Related Questions