使用Python图形的十四段显示
这是我的任务: 到目前为止,我写了以下内容:使用Python图形的十四段显示,python,turtle-graphics,Python,Turtle Graphics,这是我的任务: 到目前为止,我写了以下内容: 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
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)
我的问题是,我不知道如何画每一笔。我知道,然后我必须插入它将如何绘制每个字母和数字,但我在正确的轨道上吗?有谁能帮我画一个顶划来让我开始吗?感谢您提供的帮助。六年多来,这个问题值得回答。某种程度上。我将使用7个七段(十六进制)数字显示来演示基本思想,而不是实现14段字母数字显示。我将使用此文件中的约定。此外,我将不绘制线段,而是定义一个自定义的海龟光标作为线段,并根据需要标记它:
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()
从海龟导入屏幕,海龟
刻度=1.75#任意大小的刻度数字
CURSOR_SIZE=25#定制海龟光标的最大尺寸
间距=光标大小*1.25*刻度#从一个数字开始到下一个数字的间距
DIGITS={#要打开的段编码为位
“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显示_编号(海龟,编号):
对于str中的数字(数字):
位=位数[位数]
对于范围(7)中的位:
如果2**位和位:
位置=海龟。位置()
段[位](乌龟)
乌龟邮票()
乌龟。设置标题(0)
乌龟。设置位置(位置)
乌龟。前进(间隔)
def段_A(海龟):#顶部
乌龟.设置航向(90)
海龟.sety(海龟.ycor()+20*SCALE)
def段_B(海龟):#右上
turtle.setposition(turtle.xcor()+10*刻度,turtle.ycor()+10*刻度)
def段_C(海龟):#右下
turtle.setposition(turtle.xcor()+10*刻度,turtle.ycor()-10*刻度)
def段D(海龟):#底部
乌龟.设置航向(90)
海龟.sety(海龟.ycor()-20*SCALE)
def段_E(海龟):#左下
turtle.setposition(turtle.xcor()-10*刻度,turtle.ycor()-10*刻度)
def段F(海龟):#左上
turtle.setposition(turtle.xcor()-10*刻度,turtle.ycor()+10*刻度)
def段(海龟):#中心
乌龟.设置航向(90)
段=[段G、段F、段E、段D、段C、段B、段A]
screen=screen()
屏幕设置(950200)
屏幕。寄存器_形状((-12.5,0),(-10,2.5),(10,2.5),(12.5,0),(10,-2.5),(-10,-2.5))#
海龟=海龟('段',可见=假)
龟形(鳞片)
乌龟。速度(“最快”)
乌龟
turtle.setx(间距-屏幕.窗口宽度()/2)
显示编号(海龟,“0123456789ABCDEF”)
screen.exitonclick()
将段数增加到14,将文本打印在多行上,并添加小数点,这些都是留给读者的练习。您是否熟悉?添加的家庭作业标记并格式化为代码。注意,我必须做一些缩进——你应该检查以确保我做对了。另外,在
MethodA
中,你有左划
——应该是左划(宽度)
?@Chris B.,我不想告诉你,但它是去年8月1日到期的(是的,应该是。谢谢你重新格式化。我是第一次发帖,也是一个全新的程序员(才两周)。我现在正在查看文档,谢谢你的链接。另外请注意,你的字符词典缺少一个结尾}
。