简体   繁体   中英

I want to print each line on a different row

This is my code: (I am trying to print a text file that contains song lyrics as an image. Using python turtle, drawing squares to represent pixels; printing this as the format of the lyrics in the textfile to make a picture.)

import turtle
t=turtle.Turtle()
canvas=turtle.Screen()
canvas.setup(width=1280, height=720)
t.speed(0)

#The drawing starts in the upper left corner of the canvas.
t.penup()
t.goto(-600,325)
t.pendown()

def square(color):
    
    t.fillcolor(color)
    t.begin_fill()
    for i in range(4):
        t.forward(10)
        t.right(90)
    t.end_fill()


def paint_line (file_text):
    """
The paint line() function shall take one parameter as input, a string representing
a line from a text file, and draw a row of colorful squares corresponding to the
characters in the line.
After drawing the row, the turtle shall go to the beginning of the next row to be
ready to paint the next line, if any.
    """

    count=0
    for ch in file_text:
        count+=1
        if (ord(ch)<70):
            square('black')
        elif (ord(ch)>=70 and ord(ch)<100):
            square('pink')
        elif (ord(ch)>=100 and ord(ch)<110):
            square('light blue')
        elif (ord(ch)>=110 and ord(ch)<122):
            square('yellow')
        elif (ord(ch)>122):
            square('green')

        #to print each line of text on a different row
        t.penup()
        t.left(180)
        t.pendown()
    

#do not use readlines()
def picture(file_name):
    '''
The picture() function takes one parameter as input, a file name, opens the file,
and draws the picture by calling the paint line() function for each line of text in
the file. 
    '''
    file_data=open(file_name+".txt","r")
    file_text=file_data.readline()
    count=1
    while file_text:
        print("Line {}: {}".format(count, file_text.strip()))

        file_text=file_data.readline()
        paint_line(file_text)
        count += 1
        
    file_data.close() 

    

def main():
    '''
The main() function prompts the user for a file name and calls picture() to do
the work.
    '''      
    file_name=input("Enter the file Name")
    picture(file_name)

main()

This is what the text file contains:

Strawberries, cherries and an angel kissing spring
My summer wine is really made from all these things
I walked in town on silver spurs that jingled to
A song that I had only sang to just a few
She saw my silver spurs and said let's pass some time
And I will give to you, summer wine
Oh. oh, oh, summer wine
Strawberries, cherries and an angel kissing spring
My summer wine is really made from all these things
Take off your silver spurs and help me pass the time
And I will give to you, summer wine
Oh, summer wine
My eyes grew heavy and my lips they could not speak
I tried to get up but I couldn't find my feet
She reassured me with the unfamiliar line
And then she gave to me, more summer wine
Woh, woh, oh, summer wine
Strawberries, cherries and an angel kissing spring
My summer wine is really made from all these things
Take off your silver spurs and help me pass the time
And I will give to you, summer wine
Mm, summer wine
When I woke up, the sun was shining in my eyes
My silver spurs were gone, my head felt twice its size
She took my silver spurs, a dollar and a dime
And left me craving for, more summer wine
Oh, oh, summer wine
Strawberries, cherries and an angel kissing spring
My summer wine is really made from all these things
Take off those silver spurs, help me pass the time
And I will give to you my summer wine
Oh, oh, summer wine

This is the output I get:

This is the output I expected:

You have forgotten to reset your pen's position correctly.

import turtle
t = turtle.Turtle()
canvas = turtle.Screen()
canvas.setup(width=1280, height=720)
t.speed(0)

#The drawing starts in the upper left corner of the canvas.
t.penup()
t.goto(-600, 325)
t.pendown()


def square(color):

t.fillcolor(color)
t.begin_fill()
for i in range(4):
t.forward(10)
t.right(90)
t.end_fill()
t.forward(10)


def paint_line(file_text):
"""
The paint line() function shall take one parameter as input, a string representing
a line from a text file, and draw a row of colorful squares corresponding to the
characters in the line.
After drawing the row, the turtle shall go to the beginning of the next row to be
ready to paint the next line, if any.
"""

count = 0
for ch in file_text:
count += 1
if (ord(ch) < 70):
square('black')
elif (ord(ch) >= 70 and ord(ch) < 100):
square('pink')
elif (ord(ch) >= 100 and ord(ch) < 110):
square('light blue')
elif (ord(ch) >= 110 and ord(ch) < 122):
square('yellow')
elif (ord(ch) > 122):
square('green')

#to print each line of text on a different row
t.penup()
# t.right(20)
t.pendown()


#do not use readlines()
def picture(file_name):
'''
The picture() function takes one parameter as input, a file name, opens the file,
and draws the picture by calling the paint line() function for each line of text in
the file.
'''
file_data = open(file_name + ".txt", "r")
file_text = file_data.readline()
count = 1
while file_text:
print("Line {}: {}".format(count, file_text.strip()))
file_text = file_data.readline()
paint_line(file_text)
t.penup()
t.goto(-600, 325 - count * 10)
t.pendown()
count += 1
file_data.close()


def main():
'''
The main() function prompts the user for a file name and calls picture() to do
the work.
'''
file_name = 'test'  #input("Enter the file Name")
picture(file_name)


main()

Along with turning your turtle completely around instead of moving it forward, you've also skip over character 122 ('z') in your logic.

To both simplify the code, and speed up the program, I would approach this as a stamping problem rather than a drawing one:

from turtle import Screen, Turtle

WIDTH, HEIGHT = 1280, 720

TILE_SIZE = 10
CURSOR_SIZE = 20

def square(color):

    turtle.fillcolor(color)
    turtle.stamp()

def paint_line(text):
    '''
    The paint line() function shall take one parameter as input, a string representing
    a line from a text file, and draw a row of colorful squares corresponding to the
    characters in the line.
    After drawing the row, the turtle shall go to the beginning of the next row to be
    ready to paint the next line, if any.
    '''

    for character in text:
        code = ord(character)

        if code < 70:
            square('black')
        elif 70 <= code < 100:
            square('pink')
        elif 100 <= code < 110:
            square('light blue')
        elif 110 <= code < 122:
            square('yellow')
        elif code >= 122:
            square('green')

        # to print each line of text on a different row
        turtle.forward(10)

#do not use readlines()
def picture(file_name):
    '''
    The picture() function takes one parameter as input, a file name, opens the file,
    and draws the picture by calling the paint line() function for each line of text in
    the file.
    '''

    with open(file_name + ".txt") as file_data:
        for line, text in enumerate(file_data, start=1):
            print("Line {}: {}".format(line, text.strip()))

            paint_line(text)

            turtle.goto(-WIDTH/2 + TILE_SIZE, turtle.ycor() - TILE_SIZE)

def main():
    '''
    The main() function prompts the user for a file name and calls picture() to do
    the work.
    '''

    file_name = input("Enter the file name: ")
    picture(file_name)

screen = Screen()
screen.setup(WIDTH, HEIGHT)

turtle = Turtle()
turtle.shape('square')
turtle.shapesize(TILE_SIZE / CURSOR_SIZE)
turtle.speed('fastest')
turtle.penup()

# The drawing starts near the upper left corner of the canvas.
turtle.goto(-WIDTH/2 + TILE_SIZE, HEIGHT/2 - TILE_SIZE)

main()

screen.exitonclick()

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM