以下是我的任务:http://cs.nyu.edu/courses/summer11/V22.0002-002/assign6_summer2011.html
到目前为止,我写了以下内容:
from turtle import *
s = input(Type in a string: )
Characters = {
('A') : MethodA
('B') : MethodB
('C') : MethodC
('D') : MethodD
('E') : MethodE
('F') : MethodF
('G') : MethodG
('H') : MethodH
('I') : MethodI
('J') : MethodJ
('K') : MethodK
for c in s:
c = c.upper() # to fold lowercase into upper case
if c in Characters:
Characters[c](width)
def top_stroke():
forward(10)
penup()
def MethodA (width) :
top_stroke(width)
middle_stroke(width)
left_stroke
right_stroke(width)
def MethodB (width) :
top_stroke(width)
middle_stroke(width)
left_stroke(width)
right_stroke(width)
def MethodC (width) :
top_stroke(width)
middle_stroke(width)
left_stroke(widt)
right_stroke(width)
我的问题是我不确定如何绘制每个笔画。我知道我必须插入如何绘制每个字母和数字,但我是否在正确的轨道上?任何人都可以帮助我绘制top_stroke
让我开始吗?感谢您提供任何帮助。
答案 0 :(得分:0)
六年多以后,这个问题应该得到答案。有点。我将不使用14段的字母数字显示,而是使用7七段(十六进制)数字显示来演示基本概念。我将使用此seven segment display article in Wikipedia中的约定。另外,我将定义一个自定义的乌龟光标而不是绘制段,并根据需要对其进行 stamp
:from turtle import Screen, Turtle
SCALE = 1.75 # arbitrarily scale digits larger or smaller
CURSOR_SIZE = 25 # maximum dimension of our custom turtle cursor
SPACING = CURSOR_SIZE * 1.25 * SCALE # space from start of one digit to the next
DIGITS = { # which segments to turn on encoded as bits
'0': 0x7E,
'1': 0x30,
'2': 0x6D,
'3': 0x79,
'4': 0x33,
'5': 0x5B,
'6': 0x5F,
'7': 0x70,
'8': 0x7F,
'9': 0x7B,
'A': 0x77,
'B': 0x1F,
'C': 0x4E,
'D': 0x3D,
'E': 0x4F,
'F': 0x47,
}
def display_number(turtle, number):
for digit in str(number):
bits = DIGITS[digit]
for bit in range(7):
if 2 ** bit & bits:
position = turtle.position()
segments[bit](turtle)
turtle.stamp()
turtle.setheading(0)
turtle.setposition(position)
turtle.forward(SPACING)
def segment_A(turtle): # top
turtle.setheading(90)
turtle.sety(turtle.ycor() + 20 * SCALE)
def segment_B(turtle): # right upper
turtle.setposition(turtle.xcor() + 10 * SCALE, turtle.ycor() + 10 * SCALE)
def segment_C(turtle): # right lower
turtle.setposition(turtle.xcor() + 10 * SCALE, turtle.ycor() - 10 * SCALE)
def segment_D(turtle): # bottom
turtle.setheading(90)
turtle.sety(turtle.ycor() - 20 * SCALE)
def segment_E(turtle): # left lower
turtle.setposition(turtle.xcor() - 10 * SCALE, turtle.ycor() - 10 * SCALE)
def segment_F(turtle): # left upper
turtle.setposition(turtle.xcor() - 10 * SCALE, turtle.ycor() + 10 * SCALE)
def segment_G(turtle): # center
turtle.setheading(90)
segments = [segment_G, segment_F, segment_E, segment_D, segment_C, segment_B, segment_A]
screen = Screen()
screen.setup(950, 200)
screen.register_shape('segment', ((-12.5, 0), (-10, 2.5), (10, 2.5), (12.5, 0), (10, -2.5), (-10, -2.5))) # <=>
turtle = Turtle('segment', visible=False)
turtle.shapesize(SCALE)
turtle.speed('fastest')
turtle.penup()
turtle.setx(SPACING - screen.window_width() / 2)
display_number(turtle, "0123456789ABCDEF")
screen.exitonclick()
将段数增加到14,使文本在多行上打印并添加小数点作为练习留给读者。