乌龟为循环创建图像

时间:2017-10-06 10:30:03

标签: python for-loop turtle-graphics

frame = [
    ",,,,,,,,,,,,,,,,,,,,,,,,,",
    ",,,,,,****,,,,,,,,,*,,,,,",
    ",,,,,*!!!!*,,,,,,,*¤*,,,,",
    ",,,,*!!!!!!*,,,,,,*¤¤*,,,",
    ",,,,*!!!!!!*,,,,,,*¤¤*,,,",
    ",,,*!!!!!!!!*,,,,*¤¤¤¤*,,",
    ",,*!!!!,*!!!*,,,,*¤¤%¤*,,",
    ",,*!!!!**!!!!*,,,*¤%%¤*,,",
    ",,*!!!!**!!!!*,,,,*%**,,,",
    ",,,*!!!!!!!!!!*,,,*!*,,,,",
    ",,,,**!!!!!!!!!*,*!!*,,,,",
    ",,,,,,***!!*!!!**!!*,,,,,",
    ",,,,,,,*%%*!!!!!*!!*,,,,,",
    ",,,,,,,*%%%**!!!*!*,,,,,,",
    ",,,,,,*,*%%%!!!!**,,,,,,,",
    ",,,,,,,***%%!!!**,,,,,,,,",
    ",,,,,,,,,,***!**,,,,,,,,,",
    ",,,,,,,,,,,*,!,*,,,,,,,,,",
    ",,,,,,,,,,,,****,,,,,,,,,",
    ",,,,,,,,,,,,,,,,,,,,,,,,,"
]

colors = {
            ","  :  "white",
            "*"  :  "black",
            "!"  :  "orange",
            "¤"  :  "red",
            "%"  :  "yellow"
}

from mobilechelonian import Turtle
dragon = Turtle()

dragon.speed(10)
for row in frame:
    for symbol in row:
        dragon.pencolor(colors[symbol])
        dragon.forward(1)
    dragon.penup
    dragon.backward(25)
    dragon.right(90)
    dragon.forward(1)
    dragon.left(90)

我在jupyterhub中有这个代码,假设是绘制图片。但是我只得到一个小的白色方块。另外,如果有人知道如何重新调整屏幕以全尺寸显示图片,我将非常感激!

1 个答案:

答案 0 :(得分:0)

以下内容替换您的代码(在代码之前保持数据/变量相同),使用Python附带的turtle模块并添加缩放:

from turtle import Turtle, Screen
import tkinter as _

_.ROUND = _.BUTT

SCALE = 4

screen = Screen()
width, height = screen.window_width() / SCALE, screen.window_height() / SCALE
screen.setworldcoordinates(-width//2, -height//2, width//2, height//2)

dragon = Turtle(visible=False)
dragon.speed('fastest')
dragon.width(SCALE)

for row in frame:
    dragon.pendown()
    for symbol in row:
        dragon.pencolor(colors[symbol])
        dragon.forward(1)
    dragon.penup()
    dragon.backward(len(row))
    dragon.right(90)
    dragon.forward(1)
    dragon.left(90)

screen.exitonclick()

enter image description here