[英]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.)这是我的代码:(我试图将包含歌词的文本文件打印为图像。使用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()
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哦,哦,夏酒
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.除了将您的海龟完全转动而不是向前移动之外,您还跳过了逻辑中的字符 122 ('z')。
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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.