繁体   English   中英

我想在不同的行上打印每一行

[英]I want to print each line on a different row

这是我的代码:(我试图将包含歌词的文本文件打印为图像。使用python乌龟,绘制正方形来表示像素;将其打印为文本文件中歌词的格式以制作图片。)

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()

这是文本文件包含的内容:

草莓、樱桃和天使亲吻春天
我的夏酒真的是用这些东西制成的
我踩着叮叮当当的银色马刺在镇上行走
一首我只唱给少数人听的歌
她看到了我的银色马刺,说让我们过去一段时间
我会给你,夏酒
哦。 哦,哦,夏酒
草莓、樱桃和天使亲吻春天
我的夏酒真的是用这些东西制成的
脱下你的银色马刺,帮我打发时间
我会给你,夏酒
哦,夏酒
我的眼睛变得沉重,我的嘴唇无法说话
我试图站起来,但我找不到我的脚
她用陌生的台词让我放心
然后她给了我更多的夏酒
呜呜呜夏天酒
草莓、樱桃和天使亲吻春天
我的夏酒真的是用这些东西制成的
脱下你的银色马刺,帮我打发时间
我会给你,夏酒
嗯,夏酒
当我醒来时,阳光照在我的眼睛里
我的银色马刺不见了,我的头感觉是它的两倍大
她拿走了我的银马刺,一美元一角钱
让我渴望更多的夏日美酒
哦,哦,夏酒
草莓、樱桃和天使亲吻春天
我的夏酒真的是用这些东西制成的
脱下那些银色的马刺,帮我打发时间
我会把我的夏酒给你
哦,哦,夏酒

这是我得到的输出:

这是我期望的输出:

您忘记正确重置笔的位置。

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()

除了将您的海龟完全转动而不是向前移动之外,您还跳过了逻辑中的字符 122 ('z')。

为了简化代码并加速程序,我会将其视为冲压问题而不是绘图问题:

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()

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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