字母表点阵显示弯曲Hello World.

字母表点阵显示弯曲Hello World.


"""
   字母表点阵显示弯曲Hello World.
"""

import turtle
import random
from math import cos, sin, atan2, radians, degrees 

alphabet = {
    'A': ((0,0),(0.5,1),(0.75,0.5),(0.25,0.5),(0.75,0.5),(1,0)),
    'B': ((0,0),(0,1),(0.625 ,1),(0.75,0.875),(0.75,0.625),(0.625,0.5),(0,0.5),(0.625,0.5),(0.75,0.375),(0.75,0.125),(0.625,0),(0,0)),
    'C': ((0.75,0.125),(0.625,0),(0.125,0),(0,0.125),(0,0.875),(0.125,1),(0.625,1),(0.75,0.875)),
    'D': ((0,0),(0,1),(0.625 ,1),(0.75,0.875),(0.75,0.125),(0.625,0),(0,0)),
    'E': ((0.75,0),(0,0),(0,0.5),(0.75,0.5),(0,0.5),(0,1),(0.75,1)),
    'F': ((0,0),(0,0.5),(0.75,0.5),(0,0.5),(0,1),(0.75,1)),
    'G': ((0.75,0.5),(0.625,0.5),(0.75,0.5),(0.75,0.125),(0.625,0),(0.125,0),(0,0.125),(0,0.875),(0.125,1),(0.625,1),(0.75,0.875)),
    'H': ((0,0),(0,1),(0,0.5),(0.75,0.5),(0.75,1),(0.75,0)),
    'I': ((0,0),(0.25,0),(0.125,0),(0.125,1),(0,1),(0.25,1)),
    'J': ((0,0.125),(0.125,0),(0.375,0),(0.5,0.125),(0.5,1)),
    'K': ((0,0),(0,1),(0,0.5),(0.75,1),(0,0.5),(0.75,0)),
    'L': ((0,0),(0,1),(0,0),(0.75,0)),
    'M': ((0,0),(0,1),(0.5,0),(1,1),(1,0)),
    'N': ((0,0),(0,1),(0.75,0),(0.75,1)),
    'O': ((0.75,0.125),(0.625,0),(0.125,0),(0,0.125),(0,0.875),(0.125,1),(0.625,1),(0.75,0.875),(0.75,0.125)),
    'P': ((0,0),(0,1),(0.625,1),(0.75,0.875),(0.75,0.625),(0.625,0.5),(0,0.5)),
    'Q': ((0.75,0.125),(0.625,0),(0.125,0),(0,0.125),(0,0.875),(0.125,1),(0.625,1),(0.75,0.875),(0.75,0.125),(0.875,0)),
    'R': ((0,0),(0,1),(0.625,1),(0.75,0.875),(0.75,0.625),(0.625,0.5),(0,0.5),(0.625,0.5),(0.875,0)),
    'S': ((0,0.125),(0.125,0),(0.625,0),(0.75,0.125),(0.75,0.375),(0.675,0.5),(0.125,0.5),(0,0.625),(0,0.875),(0.125,1),(0.625,1),(0.75,0.875)),
    'T': ((0,1),(0.5,1),(0.5,0),(0.5,1),(1,1)),
    'U': ((0,1),(0,0.125),(0.125,0),(0.625,0),(0.75,0.125),(0.75,1)),
    'V': ((0,1),(0.375,0),(0.75,1)),
    'W': ((0,1),(0.25,0),(0.5,1),(0.75,0),(1,1)),
    'X': ((0,0),(0.375,0.5),(0,1),(0.375,0.5),(0.75,1),(0.375,0.5),(0.75,0)),
    'Y': ((0,1),(0.375,0.5),(0.375,0),(0.375,0.5),(0.75,1)),
    'Z': ((0,1),(0.75,1),(0,0),(0.75,0)),
}


writer = turtle.Turtle()
writer.hideturtle()
writer.speed(0)
window = turtle.Screen()
window.bgcolor("#000000")
writer.pensize(2)

def display_string(message,font_size,color,x,y):
  writer.color(color)
  message=message.upper()
  
  for character in message:
    if character in alphabet:
      letter=alphabet[character]
      writer.penup()
      for dot in letter:
        writer.goto(x + dot[0]*font_size, y + dot[1]*font_size)
        writer.pendown()
        
      x += font_size
      
    if character == " ":
      x += font_size
    x += char_spacing
    
def display_string2(message,font_size,color,x,y,rotationAngle):
  writer.color(color)
  message=message.upper()
  writer.penup()
  writer.goto(x,y)  
  for char in message:
    if char in alphabet:
      letter=alphabet[char]
      writer.setheading(rotationAngle)
      writer.penup()
    
      x=0
      y=0
      for dot in letter:
        angle = atan2((dot[1]-y),(dot[0]-x))
        angle= degrees(angle)    
  
        distance = ((dot[0]-x)**2 + (dot[1]-y)**2)**0.5
        writer.setheading(rotationAngle)
  
        writer.left(angle)
        writer.forward(distance*font_size)
        x = dot[0]
        y = dot[1]
        writer.pendown()
  
      writer.penup()
      angle = atan2((0-y),(0-x))
      angle = degrees(angle)    
  
      distance = ((0-x)**2 + (0-y)**2)**0.5
      writer.setheading(rotationAngle)
  
      writer.left(angle)
      writer.forward(distance*font_size)
  
    writer.setheading(rotationAngle)
    writer.penup()
    writer.forward(font_size)    
    
    writer.forward(char_spacing)


# 主要执行程序
font_size = 30
font_color="#FF00FF"
char_spacing = 5
cursorX = -150
cursorY = -100

message = "Hello World"
rotationAngle=90
writer.goto(cursorX,cursorY)

display_string(message,font_size,font_color,-190,0)
writer.up()
writer.goto(cursorX+50,cursorY+150)
for char in message:
  pos=writer.position()
  display_string2(char,font_size,'cyan',pos[0],pos[1],rotationAngle)
  rotationAngle-=180/(len(message)-1)


点阵字A示例图:

李兴球

李兴球的博客是Python创意编程原创博客

评论已关闭。