def draw_circle(x,y): turtle.penup() turtle.goto(x,y) turtle.pendown() turtle.begin_fill() turtle.circle(10) turtle.end_fill()
def hexagone(c, longueur,m, col1, col2, col3,deform): """ Draws a hexagon with or without deformation """ lo = longueur x,y,z = c #Hexagon centre pa1,pa2,pa3 = (x+lo,y,z), (x+(lo/2),y-m,z), (x-(lo/2),y-m,z)#First losange coordinates (lower right) pb1,pb2,pb3 = (x+lo,y,z), (x+(lo/2),y+m,z), (x-(lo/2),y+m,z)#Losange 2 (upper right) pc1,pc2,pc3 = (x-(lo/2),y+m,z), (x-lo,y,z), (x-(lo/2),y-m,z)#Losange 3 (left) pts = [pa1,pa2,pa3,c,pb1,pb2,pb3,c,pc1,pc2,pc3,c] d = [] for point in pts: xd,yd,zd = deform(point) d.extend((xd,yd)) up() setpos(d[6],d[7])#Turtle resets to c down() col = [col1,col2,col3] i = 0 for e in col: color(e) begin_fill() goto(d[i],d[i+1]) goto(d[i+2],d[i+3]) goto(d[i+4],d[i+5]) goto(d[i+6],d[i+7]) end_fill() i += 8
def cleardisk(self): turtle.pu() turtle.goto(self.x,self.y) turtle.pd() turtle.color("white","white") turtle.begin_fill() turtle.goto(self.x+(self.w/2),self.y) turtle.goto(self.x+(self.w/2),self.y-self.h) turtle.goto(self.x-(self.w/2),self.y-self.h) turtle.goto(self.x-(self.w/2),self.y) turtle.goto(self.x,self.y) turtle.end_fill() turtle.pu() turtle.pu() turtle.goto(self.x,self.y) turtle.pd() turtle.color("red","red") turtle.begin_fill() turtle.goto(self.x+(10/2),self.y) turtle.goto(self.x+(10/2),self.y-self.h) turtle.goto(self.x-(10/2),self.y-self.h) turtle.goto(self.x-(10/2),self.y) turtle.goto(self.x,self.y) turtle.end_fill() turtle.pu() return 0
def polygon(side = 50, angle = None, xstart = None, ystart = None, numberSides = 3, color = 'black', fill = False): turtle.pensize(3) turtle.speed('fastest') turtle.hideturtle() if angle != None: turtle.left(angle) turtle.penup() if fill == True: if xstart != None or ystart != None: turtle.goto(xstart, ystart) else: turtle.goto(0, 0) turtle.color(color) turtle.pendown() turtle.begin_fill() turtle.circle(side, 360, numberSides) turtle.end_fill() turtle.penup() else: turtle.goto(xstart, ystart) turtle.color(color) turtle.pendown() turtle.circle(side, 360, numberSides) turtle.penup() return
def draw_tree(x,y): startPosX = x startPosY = y turtle.setpos(x,y) turtle.fillcolor("green") turtle.begin_fill() for i in range(0,4): x -=40 y -=80 turtle.goto(x,y) coords.append(turtle.pos()) x += 20 turtle.goto(x,y) bottomCorner = turtle.pos() x = startPosX y = startPosY turtle.setpos(x,y) for i in range(0,4): x +=40 y -=80 turtle.goto(x,y) coords.append(turtle.pos()) x -= 20 turtle.goto(x,y) turtle.goto(bottomCorner) turtle.end_fill()
def sky( x, y, scale ): '''draws a huge blue square given location and scale''' t.tracer(False) t.begin_fill() t.color('light blue') buildingblock(x-1000*scale, y+0*scale, 2000*scale, 1075*scale) t.end_fill()
def cross( x, y, scale, fill, color ): '''draws a cross given location, scale, and color''' goto( x, y ) if fill == "True": '''if the scale is 1, and fill == True then this function will draw a cross with its left point at (x,y) and will have lengths of 50 and widths of 15 and filled with the color given''' t.begin_fill() t.color(color) for i in range(4): t.forward(50*scale) t.right(90) t.forward(50*scale) t.left(90) t.forward(15*scale) t.left(90) t.end_fill() else: '''if the scale is 1, and fill == False then this function will draw a cross with its left point at (x,y) and will have lengths of 50 and widths of 15 and with no color fill''' for i in range(4): t.forward(50*scale) t.right(90) t.forward(50*scale) t.left(90) t.forward(15*scale) t.left(90)
def grass( x, y, scale ): '''draws a huge blue square given location and scale''' t.tracer(False) t.begin_fill() t.color('green') buildingblock(x-1000*scale, y-1000*scale, 2000*scale, 1075*scale) t.end_fill()
def cloud( x, y, scale, color): '''draws a cloud given location and scale''' goto( x, y ) t.begin_fill() t.color(color) t.circle(50*scale) t.end_fill() t.begin_fill() t.color(color) t.up() t.forward(25*scale) t.down() t.circle(50*scale) t.end_fill() t.begin_fill() t.color(color) t.up() t.forward(35*scale) t.down() t.circle(50*scale) t.end_fill() t.begin_fill() t.color(color) t.up() t.forward(25*scale) t.down() t.circle(50*scale) t.end_fill() t.begin_fill() t.color(color) t.up() t.forward(25*scale) t.down() t.end_fill()
def block( x, y, width, height, fill, color ): goto( x, y ) print 'block(): drawing block of size', width, height # tell the turtle to go foward by width # tell the turtle to turn left by 90 degrees # tell the turtle to go forward by height # tell the turtle to turn left by 90 degrees # repeat the above 4 commands # if the parameter fill is true then do this if fill == "True": ''' draw a block at position (x,y) with the given width and height, and fill with the color ''' t.begin_fill() t.color(color) for i in range(2): t.forward(width) t.left(90) t.forward(height) t.left(90) t.end_fill() # if the parameter fill is false then do this else: ''' draw a block at position (x,y) with the given width and height, and fill with the color ''' for i in range(2): t.forward(width) t.left(90) t.forward(height) t.left(90)
def drawmountain(x,y,color): t.up t.goto(x,y) t.down t.color(color) t.begin_fill() t.backward(200) t.right(120) t.backward(200) t.right(120) t.backward(200) t.right(120) t.end_fill() t.up t.goto(x-75,y+125) t.down t.color("White") t.begin_fill() t.backward(50) t.right(120) t.backward(50) t.right(120) t.backward(50) t.right(120) t.end_fill() t.up
def star( x, y, scale, fill, color ): '''draws a star given location, scale, and color''' goto( x, y ) if fill == "True": '''if the scale is 1, and fill == True then this function will draw a star with its left point at (x,y) and will have star ray lengths of 50 and filled with the color given''' t.begin_fill() t.color(color) for i in range(10): t.forward(50*scale) t.right(108) t.forward(50*scale) t.left(144) t.end_fill() else: '''if the scale is 1, and fill == False then this function will draw a star with its left point at (x,y) and will have star ray lengths of 50 and with no color fill''' t.begin_fill() for i in range(10): t.forward(50*scale) t.right(108) t.forward(50*scale) t.left(144)
def pythagoras_tree(size, n): turtle.begin_fill() for _ in range(4): turtle.forward(size) turtle.left(90) turtle.end_fill() if n > 0: roof = .5 * math.sqrt(2) * size turtle.left(90) turtle.forward(size) turtle.right(45) pythagoras_tree(roof, n - 1) turtle.forward(roof) turtle.right(90) pythagoras_tree(roof, n - 1) turtle.left(90) turtle.backward(roof) turtle.left(45) turtle.backward(size) turtle.right(90)
def circunferencia(simbolos,identificador,linea): p1= obtener_punto(2,identificador,simbolos) radio = obtener_radio(identificador,simbolos) x1 = obtener_x(p1,simbolos) y1 = obtener_y(p1,simbolos) escalar = obtener_escalar(identificador, simbolos,linea) relleno = obtener_color(obtener_relleno(identificador,simbolos,linea)) borde = obtener_color(obtener_borde(identificador,simbolos,linea)) turtle.color(borde) if escalar == 0: escalar=1 tx = obtener_tx(identificador, simbolos,linea) ty = obtener_ty(identificador, simbolos,linea) turtle.pensize(8) turtle.penup() #Trasladar circunferencia x1 = x1 + tx y1 = y1 + ty #turtle.setposition(x1, y1-(radio*44)) #turtle.pendown() #turtle.circle(radio*44) #Escalar circunferencia turtle.penup() #turtle.setposition(x1, y1-(radio*44*escalar)) turtle.setposition(x1*44, (y1*44)-(radio*44*escalar)) turtle.pendown() turtle.fillcolor(relleno) turtle.begin_fill() turtle.circle(radio*44*escalar) turtle.end_fill()
def circle(x,y,size): turtle.pu() turtle.goto(x,y) turtle.pd() turtle.begin_fill() turtle.circle(size) turtle.end_fill()
def dope_flowers(x, y): turtle.pendown() turtle.begin_fill() move(turtle, 100) flower(turtle, 10, 20.0, 60.0) turtle.end_fill() turtle.penup()
def tree( x, y, scale ): '''draws a leaf given location and scale''' goto( x, y ) turtle.setheading(0) turtle.begin_fill() turtle.color('dark green') turtle.forward(25*scale) turtle.left(150) turtle.forward(25*scale) turtle.right(150) turtle.forward(20*scale) turtle.left(150) turtle.forward(30*scale) turtle.left(60) turtle.forward(30*scale) turtle.left(150) turtle.forward(20*scale) turtle.right(150) turtle.forward(25*scale) turtle.left(150) turtle.end_fill() turtle.forward(30*scale) goto( x, y ) turtle.begin_fill() turtle.color('brown') turtle.right(90) turtle.forward(15*scale) turtle.right(90) turtle.forward(5*scale) turtle.right(90) turtle.forward(15*scale) turtle.right(90) turtle.end_fill() turtle.forward(5*scale)
def draw_rectangle(length_float, width_float, color_str): """ Asks for the length, width, and color of the rectangle and draws it using turtle Recieve: The length, width and color of the triangle Return: Nothing Algorithm: Use a for loop and draw a rectangle by going forward the specified length and making a 90 degree turn to the right and then going forward the width and turning 90 degrees to the right Then do the loop again """ turtle.fillcolor(color_str) turtle.pendown() turtle.begin_fill() for i in range(2): turtle.forward(length_float) turtle.right(90) turtle.forward(width_float) turtle.right(90) turtle.end_fill() turtle.penup()
def cross( x, y, scale, color ): '''draws a cross given location, scale, and color''' goto( x, y ) turtle.begin_fill() turtle.color(color) turtle.forward(50*scale) turtle.right(90) turtle.forward(50*scale) turtle.left(90) turtle.forward(15*scale) turtle.left(90) turtle.forward(50*scale) turtle.right(90) turtle.forward(50*scale) turtle.left(90) turtle.forward(15*scale) turtle.left(90) turtle.forward(50*scale) turtle.right(90) turtle.forward(50*scale) turtle.left(90) turtle.forward(15*scale) turtle.left(90) turtle.forward(50*scale) turtle.right(90) turtle.forward(50*scale) turtle.left(90) turtle.forward(15*scale) turtle.end_fill()
def leaf( x, y, scale, color ): '''draws a leaf given location and scale''' goto( x, y ) turtle.begin_fill() turtle.color(color) turtle.right(30) turtle.forward(8.33*scale) turtle.left(120) turtle.forward(3.33*scale) turtle.right(105) turtle.forward(15*scale) turtle.left(110) turtle.forward(5*scale) turtle.right(95) turtle.forward(15*scale) turtle.left(150) turtle.forward(15*scale) turtle.right(95) turtle.forward(5*scale) turtle.left(110) turtle.forward(15*scale) turtle.right(105) turtle.forward(3.33*scale) turtle.left(120) turtle.forward(8.33*scale) turtle.left(60) turtle.forward(10*scale) turtle.left(30) turtle.forward(10*scale) turtle.left(180) turtle.forward(10*scale) turtle.left(75) turtle.end_fill() turtle.forward(8.33*scale)
def drawPoint(x, y): turtle.penup() # Pull the pen up turtle.goto(x, y) turtle.pendown() # Pull the pen down turtle.begin_fill() # Begin to fill color in a shape turtle.circle(3) turtle.end_fill() # Fill the shape
def rand_circle(): color = choice(colors) turtle.color(color, color) radius = randint(10, 100) turtle.begin_fill() turtle.circle(radius) turtle.end_fill()
def drawCircleAt(turtleX, turtleY, circleSize): turtle.penup() turtle.goto(turtleX,turtleY) turtle.pendown() turtle.begin_fill() turtle.circle(circleSize) turtle.end_fill()
def draw_vertrect(length,width,color): turtle.pendown() turtle.color(color) turtle.begin_fill() #uses color to determine length of cross if(color=="blue" or color == "red" or color == "light coral" or color=="yellow"): length*=.4375 elif(color == "snow"or color=="navy" ): length*=.42857 else: length*=.375 print("the length of the first " , length, " and the width is ", width) #loops to draw vertical rectangle for x in range(5): if(x%5==0): #draws first half of left vertical line turtle.forward((length)) print("drawing length") #draws from top of vertical to bottom of flag elif(x%2==0): turtle.forward(length*2+width) print("drawing long side") #draws small side of vertical rectangle elif(x!=5): turtle.forward(width) turtle.right(90) turtle.end_fill()
def draw_star(size, color): turtle.pendown() turtle.begin_fill() turtle.color(1,1,1) turtle.forward(2.5) turtle.left(size) turtle.forward(2.5) turtle.right(144) turtle.forward(2.5) turtle.left(size) turtle.forward(2.5) turtle.right(144) turtle.forward(2.5) turtle.left(size) turtle.forward(2.5) turtle.right(144) turtle.forward(2.5) turtle.left(size) turtle.forward(2.5) turtle.right(144) turtle.forward(2.5) turtle.left(size) turtle.forward(2.5) turtle.right(144) turtle.end_fill() turtle.penup()
def draw_rectangle(x,y,width,height): """ Draws a rectangle with the upper left hand corner starting at point (x,y). The said rectangle has the dimensions width x height. :param x: :param y: :param width: :param height: :return: None """ turtle.penup() turtle.setx(x) turtle.sety(y) turtle.pendown() turtle.setheading(0) # Set heading in x+ direction turtle.begin_fill() turtle.begin_poly() turtle.fd(width) turtle.right(90) turtle.fd(height) turtle.right(90) turtle.fd(width) turtle.right(90) turtle.fd(height) turtle.end_poly() turtle.end_fill() return None
def drawHead(circum, colour="yellow"): #turtle.pencolor("yellow") turtle.color("black", colour) turtle.begin_fill() drawCircle(circum) turtle.end_fill()
def drawFins(size): turtle.fillcolor("red") turtle.setheading(90) turtle.begin_fill() turtle.forward(0.2*size) turtle.left(120) turtle.forward(0.6*size) turtle.right(120) turtle.forward(0.3*size) turtle.right(40) turtle.forward(0.8*size) turtle.end_fill() turtle.setheading(0) turtle.begin_fill() turtle.penup() turtle.forward(size) turtle.pendown() turtle.begin_fill() turtle.right(50) turtle.forward(0.8*size) turtle.right(40) turtle.forward(0.3*size) turtle.right(120) turtle.forward(0.6*size) turtle.end_fill()
def draw_leaf(no_of_leafs): """ Draws leafs at the end of branch. Min 0 and max = no_of_leafs :pre: pos(0,0), heading east, up :post: pos(0,0), heading east, up :param no_of_leafs: maximum number of leads drawn :return: None """ for i in range(no_of_leafs): # draws random poylgon from triangle to hexagon sides = random.randint(3, 6) color = random.choice(COLORS) size = 10 angle = 360/sides t.left(90 - i * angle) t.right(90) t.begin_fill() t.down() t.color(color) for _ in range(sides): t.forward(size) t.left(angle) t.left(90) t.up() t.end_fill() t.right(90 - i * angle) global LEAF_COUNTER LEAF_COUNTER += 1
def draw_rectangle(): Fline = line.split() if Fline[1] == 'not_int': print(Fline) print("I'm sorry, I cannot understand that integer") return if len(Fline) < 4: print(Fline) print("I'm sorry, I do not understand that value") return x = int(Fline[1]) y = int(Fline[2]) width = int(Fline[3]) height = int(Fline[4]) turtle.penup() turtle.setpos(x, y) turtle.setheading(0) turtle.pendown() turtle.begin_fill() turtle.forward(width) turtle.setheading(-90) turtle.forward(height) turtle.setheading(180) turtle.forward(width) turtle.setheading(90) turtle.forward(height) turtle.end_fill()
def drawString(self, dstring, distance, angle): stack = [] cstack = [] modstring = '' modval = None modgrab = False for c in dstring: if c == '(': modstring = '' modgrab = True continue elif c == ')': modval = float(modstring) modgrab = False continue elif modgrab: modstring += c continue elif c == 'F' or c == 'f': if modval == None: self.forward(distance) else: self.forward(distance*modval) elif c == '-': if modval == None: turtle.right(angle) else: turtle.right(modval) elif c == '+': if modval == None: turtle.left(angle) else: turtle.left(modval) elif c == '!': if modval == None: w = turtle.width() if w > 1: turtle.width(w-1) else: turtle.width(modval) elif c == '[': stack.append(turtle.position()) stack.append(turtle.heading()) elif c == ']': turtle.up() turtle.setheading(stack.pop()) turtle.goto(stack.pop()) turtle.down() elif c == 'L': #draws a leaf #begin and end fil calls work better in this section of code turtle.begin_fill() turtle.circle(5) turtle.end_fill() elif c == 'Q': #created for task3 #draws a flower pos = turtle.position() heading = turtle.heading() turtle.begin_fill() turtle.right(90) turtle.forward(5) turtle.left(120) turtle.forward(10) turtle.left(120) turtle.forward(10) turtle.left(120) turtle.forward(10) turtle.end_fill() turtle.up() turtle.goto(pos) turtle.setheading(heading) turtle.down() elif c == 'P': #draws a petal #CREATED FOR EXTENSION1 turtle.begin_fill() for i in range(12): turtle.forward(5) turtle.right(108) turtle.forward(5) turtle.left(144) turtle.end_fill() elif c == '<': color = turtle.color()[0] cstack.append(color) elif c == '>': turtle.color(cstack.pop()) elif c == 'g': #makes the leaf color medium orchid turtle.color("Medium Orchid") elif c == 'y': #makes the leaf color turquoise turtle.color("Turquoise") elif c == 'r': #makes the leaf color salmon turtle.color("Salmon") elif c == 'b': #CREATED FOR EXTENSION1 #makes the leaf color salmon turtle.color("Plum") elif c == 'o': #CREATED FOR EXTENSION1 #makes the leaf color salmon turtle.color("Khaki") elif c == '{': turtle.fill(True) elif c == '}': turtle.fill(False) modval = None turtle.update()
turtle.pendown() turtle.circle(70) turtle.penup() turtle.goto(28, -25) turtle.pendown() turtle.setheading(60) turtle.circle(30, steps = 3) turtle.penup() turtle.goto(-30, 20) turtle.setheading(0) turtle.pendown() turtle.begin_fill() # Begin to fill color in a shape turtle.circle(10) # Draw the left eye turtle.end_fill() # Fill the shape turtle.penup() turtle.goto(30, 20) turtle.pendown() turtle.begin_fill() # Begin to fill color in a shape turtle.circle(10) # Draw the right eye turtle.end_fill() # Fill the shape turtle.penup() turtle.goto(0, -50) turtle.setheading(30) turtle.pendown() turtle.forward(50) turtle.penup()
@author: yang """ import turtle turtle.speed(10) turtle.color("black") for i in range(8): for j in range(8): if (i + j) % 2 == 0: turtle.begin_fill() turtle.penup() x = 50 * i - 200 y = 50 * j - 200 turtle.goto(x, y) turtle.pendown() turtle.setheading(0) turtle.forward(50) turtle.left(90) turtle.forward(50) turtle.left(90) turtle.forward(50) turtle.left(90) turtle.forward(50) if (i % 50 + j % 50) % 2 == 0: turtle.end_fill() turtle.hideturtle() turtle.done()
def tscheme_end_fill(): """Fill in shape drawn since last begin_fill.""" _tscheme_prep() turtle.end_fill()
#body t.goto(-90,100) rect(100,150,'tan') #arm t.goto(-150,70) rect(60,15,'purple') t.goto(-150,110) rect(15,40,'purple') #hand t.color('green') t.goto(-143,105) t.begin_fill() t.circle(10) t.end_fill() #arm t.goto(10,70) rect(60,15,'purple') t.goto(55,110) rect(15,40,'purple') #hand t.color('green') t.goto(63,105) t.begin_fill() t.circle(10) t.end_fill() #neck
def drawString(self, dstring, distance, angle): stack = [] cstack = [] for c in dstring: if c == 'F': turtle.forward(distance) elif c == '-': turtle.right(angle) elif c == '+': turtle.left(angle) elif c == '[': stack.append(turtle.position()) stack.append(turtle.heading()) elif c == ']': turtle.up() turtle.setheading(stack.pop()) turtle.goto(stack.pop()) turtle.down() elif c == 'L': #draws a leaf #begin and end fil calls work better in this section of code turtle.begin_fill() turtle.circle(5) turtle.end_fill() elif c == 'Q': #created for task3 #draws a flower turtle.begin_fill() turtle.right(90) turtle.forward(5) turtle.left(120) turtle.forward(10) turtle.left(120) turtle.forward(10) turtle.left(120) turtle.forward(10) turtle.end_fill() elif c == 'P': #draws a petal #CREATED FOR EXTENSION1 turtle.begin_fill() for i in range(12): turtle.forward(5) turtle.right(108) turtle.forward(5) turtle.left(144) turtle.end_fill() elif c == '<': color = turtle.color()[0] cstack.append(color) elif c == '>': turtle.color(cstack.pop()) elif c == 'g': #makes the leaf color medium orchid turtle.color("Medium Orchid") elif c == 'y': #makes the leaf color turquoise turtle.color("Turquoise") elif c == 'r': #makes the leaf color salmon turtle.color("Salmon") elif c == 'b': #CREATED FOR EXTENSION1 #makes the leaf color salmon turtle.color("Plum") elif c == 'o': #CREATED FOR EXTENSION1 #makes the leaf color salmon turtle.color("Khaki") turtle.update()
def skirt(x=0, y=0): """ 裙子 小猪佩奇的 裙子 (-20,-18) left --leftmiddle----leftmiddle_second---- middle --- --- right_middle_second ---right_middle -- right 画笔颜色 要深红色 , 粗细 就是主题色 就可以了. :param left: (x,y) 元祖, 一个点的x,y 坐标 :return: """ # 保存画笔的属性 pen = turtle.pen() # 设置颜色 turtle.color("#CD0000") turtle.pensize(5) turtle.showturtle() turtle.penup() turtle.goto(x, y) print(f"postion:{turtle.position()}") turtle.down() turtle.setheading(0) turtle.left(-100) # 开始填充 裙子的颜色 turtle.begin_fill() # 左边斜线1 turtle.forward(120) turtle.setheading(0) # 下面直线2 turtle.forward(170) turtle.left(100) # 第三条线3 turtle.forward(115 + 2 + 2) # 上面一条线4 turtle.setheading(180) begin = (x, y) # 脸颊 的相对中间点 middle = (50, -41) # 最后一条线.. 第四条线,要找到对应的弧度, 还是有一定的难度 turtle.pensize(1) # turtle.color("black") # 用粉色 可以 盖住 多边形的缺陷 turtle.color((255, 155, 192), "pink") left_middle = (12, -32) left_middle_second = (18 + 5, -32 - 2 - 2) right_middle = (80, -34) right_middle_second = (67, -39) turtle.goto(*right_middle) turtle.goto(*right_middle_second) turtle.goto(*middle) turtle.goto(*left_middle_second) turtle.goto(*left_middle) turtle.goto(*begin) turtle.setheading(180) # 裙子的颜色,设置画笔的颜色 turtle.color("#CD5C5C") # 填充裙子完成 turtle.end_fill() # 恢复画笔 turtle.pen(pen)
# 정오각형을 그리기 프로그램 import turtle as t n = 5 # 오각형을 그립니다(다른 값을 입력하면 다른 도형을 그립니다). t.color("purple") t.begin_fill() # 색칠할 영역을 시작합니다. for x in range(n): # n번 반복합니다. t.forward(50) # 거북이 50만큼 앞으로 이동합니다. t.left(360 / n) # 거북이 360/n 만큼 왼쪽으로 회전합니다. t.end_fill() # 색칠할 영역을 마무리합니다.
# 爱心 import turtle as a a.setup(800, 500, 100, 100) a.pensize(6) a.speed(89) a.color("red", "pink") a.begin_fill() a.left(135) a.fd(140) for i in range(200): a.right(1) a.fd(1) a.left(135) for i in range(200): a.right(1) a.fd(1) a.fd(140) a.end_fill() a.hideturtle() a.done()
import turtle turtle.color("green","yellow") turtle.begin_fill() # Begin the fill process. turtle.down() # "Pen" down? turtle.circle(100) turtle.up() # Pen up turtle.end_fill() # End fill.
import turtle turtle.shape('turtle') turtle.fillcolor('red') turtle.begin_fill() # fill in (with the fillcolor) the following shape turtle.forward(100) turtle.left(90) turtle.forward(100) turtle.left(90) turtle.forward(100) turtle.left(90) turtle.forward(100) turtle.end_fill() # I'm done drawing, please fill in my shape Mr. Turtle
def hexagone(point, longueur, col, centre, rayon): """Dessine un hexagone divisé en trois losanges de différents couleurs et déformé par une sphère émergente; utilise la fonction deformation Entrées : point : triple contenant les coordonnées (x,y,z) du centre de l'hexagone avant déformation longueur : float qui précise la distance entre le centre et n'importe quel sommet de l'hexagone (étant un hexagone régulier, il represent aussi la longueur des arrête) col : triple contenant les couleurs des trois losanges par lesquels l'hexagone est divisé centre : triple contenant le coordonnées (x,y,z) du centre de la sphère émergente rayon : float qui précise le rayon de la sphère émergente Sorties: dessin de l'hexagone """ def deformation(p, centre, rayon): """ Calcul des coordonnées d'un point suite à la déformation engendrée par la sphère émergeante Entrées : p : coordonnées (x, y, z) du point du dalage à tracer (z = 0) AVANT déformation centre : coordonnées (X0, Y0, Z0) du centre de la sphère rayon : rayon de la sphère Sorties : coordonnées (xprim, yprim, zprim) du point du dallage à tracer APRÈS déformation """ x, y, z = p xprim, yprim, zprim = x, y, z xc, yc, zc = centre if rayon ** 2 > zc ** 2: zc = zc if zc <= 0 else -zc r = sqrt( (x - xc) ** 2 + (y - yc) ** 2) # distance horizontale # depuis le point à dessiner jusqu'à l'axe de la sphère rayon_emerge = sqrt(rayon ** 2 - zc ** 2) # rayon de la partie # émergée de la sphère rprim = rayon * sin(acos(-zc / rayon) * r / rayon_emerge) if 0 < r <= rayon_emerge: # calcul de la déformation # dans les autres cas xprim = xc + (x - xc) * rprim / r # les nouvelles coordonnées # sont proportionnelles aux anciennes yprim = yc + (y - yc) * rprim / r if r <= rayon_emerge: beta = asin(rprim / rayon) zprim = zc + rayon * cos(beta) if centre[2] > 0: zprim = -zprim return (xprim, yprim, zprim) cen=deformation(point,centre,rayon) #calcule les coordonnées du #centre de l'hexagone après déformation v1=deformation((point[0]+longueur,point[1],point[2]),centre,rayon) #calcule les coordonnées du sommet droit de l'hexagone après #déformation v2=deformation((point[0]+(1/2)*longueur,point[1]+ sin(pi/3)*longueur,point[2]),centre,rayon) #calcule les coordonnées du sommet supérieur-droit de l'hexagone #après déformation v3=deformation((point[0]-(1/2)*longueur,point[1]+ sin(pi/3)*longueur,point[2]),centre,rayon) #calcule les coordonnées du sommet supérieur-gauche de l'hexagone #après déformation v4=deformation((point[0]-longueur,point[1],point[2]),centre,rayon) #calcule les coordonnées du sommet gauche de l'hexagone après #déformation v5=deformation((point[0]-(1/2)*longueur,point[1]- sin(pi/3)*longueur,point[2]),centre,rayon) #calcule les coordonnées du sommet inférieur-gauche de l'hexagone #après déformation v6=deformation((point[0]+(1/2)*longueur,point[1]- sin(pi/3)*longueur,point[2]),centre,rayon) #calcule les coordonnées du sommet inférieur-droit de l'hexagone #après déformation turtle.up() turtle.goto(cen[0], cen[1]) #positionne la tortue au centre de #l'hexagone turtle.down() for i in range(3): #dessine une losange de l'hexagone par loop #dans l'ordre: haut-droit, gauche, bas-droit a=v1 #sommets de la losange en haut-droit b=v2 c=v3 if i==1: #variation des sommets de la losange à gauche par #rapport à la losange en haut-droit a=v5 b=v4 elif i==2: #variation des sommets de la losange en bas-droit #par rapport à la losange en haut-droit b=v6 c=v5 turtle.color(col[i]) #sélectionne la couleur parmi #les trois donnés en triple turtle.begin_fill() turtle.goto(a[0],a[1]) turtle.goto(b[0],b[1]) turtle.goto(c[0],c[1]) turtle.goto(cen[0],cen[1]) turtle.end_fill() return
def hexagone (coordonnee,longueur,col,centre,rayon): turtle.speed(0) # vitesse d'excution du dessin: vitesse lente turtle.up() x=coordonnee[0] #coordonnes d'origine du premier cube: cela correspond au coinf inferieur gauche et coin sup droite y=coordonnee[1] # de la fenetre du dessin turtle.goto(x,y) # deplacement de la tortue au point d'origine du dessin turtle.down() # dessin de la premiere face du cube turtle.color(col[0]) # couleur de la face sup turtle.begin_fill() # debuter le remplissage x = x + longueur * (math.cos(0)) # abscisse avant deformation y = y + longueur * (math.sin(0)) # ordonnée avant deformation p = (x, y, 0) pprim = deformation(p, centre, rayon) # coordonnées apres deformation turtle.goto(pprim[0], pprim[1]) # la tortue va au point déformé x = x + longueur * (math.cos(-1 * math.pi / 3)) y = y + longueur * (math.sin(-1 * math.pi / 3)) p = (x, y, 0) pprim = deformation(p, centre, rayon) turtle.goto(pprim[0], pprim[1]) x = x + longueur * (math.cos(-1 * math.pi)) y = y + longueur * (math.sin(-1 * math.pi)) p = (x, y, 0) pprim = deformation(p, centre, rayon) turtle.goto(pprim[0], pprim[1]) x = x + longueur * (math.cos(-120 * math.pi / 180)) y = y + longueur * (math.sin(120 * math.pi / 180)) p = (x, y, 0) pprim = deformation(p, centre, rayon) turtle.goto(pprim[0], pprim[1]) turtle.end_fill() # dessin de la deuxieme face du cube turtle.color(col[1]) turtle.begin_fill() x=x+longueur*(math.cos(-60*math.pi/180)) y=y+longueur*(math.sin(-60*math.pi/180)) p = (x, y, 0) pprim = deformation(p, centre, rayon) turtle.goto(pprim[0],pprim[1]) x=x+longueur*(math.cos(-120*math.pi/180)) y=y+longueur*(math.sin(-120*math.pi/180)) p = (x, y, 0) pprim = deformation(p, centre, rayon) turtle.goto(pprim[0],pprim[1]) x=x+longueur*(math.cos(-240*math.pi/180)) y=y+longueur*(math.sin(-240*math.pi/180)) p = (x, y, 0) pprim = deformation(p, centre, rayon) turtle.goto(pprim[0],pprim[1]) x = x + longueur * (math.cos(60 * math.pi / 180)) y = y + longueur * (math.sin(60 * math.pi / 180)) p = (x, y, 0) pprim = deformation(p, centre, rayon) turtle.goto(pprim[0], pprim[1]) turtle.end_fill() # dessin de la troisieme face du cube turtle.color(col[2]) turtle.down() turtle.begin_fill() x=x+longueur*(math.cos(0*math.pi/180)) y=y+longueur*(math.sin(0*math.pi/180)) p = (x, y, 0) pprim = deformation(p, centre, rayon) turtle.goto(pprim[0],pprim[1]) x=x+longueur*(math.cos(-120*math.pi/180)) y=y+longueur*(math.sin(-120*math.pi/180)) p = (x, y, 0) pprim = deformation(p, centre, rayon) turtle.goto(pprim[0],pprim[1]) x=x+longueur*(math.cos(-180*math.pi/180)) y=y+longueur*(math.sin(-180*math.pi/180)) p = (x, y, 0) pprim = deformation(p, centre, rayon) turtle.goto(pprim[0],pprim[1]) x = x + longueur * (math.cos(60 * math.pi / 180)) y = y + longueur * (math.sin(60 * math.pi / 180)) p = (x, y, 0) pprim = deformation(p, centre, rayon) turtle.goto(pprim[0], pprim[1]) turtle.end_fill()
t.pu() # 提笔 t.goto(-100,100) # 画笔前往坐标(-100,100) t.pd() # 下笔 t.seth(-30) # 笔的角度为-30° t.begin_fill() # 外形填充的开始标志 a=0.4 for i in range(120): if 0<=i<30 or 60<=i<90: a=a+0.08 t.lt(3) #向左转3度 t.fd(a) #向前走a的步长 else: a=a-0.08 t.lt(3) t.fd(a) t.end_fill() # 依据轮廓填充 t.pu() # 提笔 t.seth(90) # 笔的角度为90度 t.fd(25) # 向前移动25 t.seth(0) # 转换画笔的角度为0 t.fd(10) t.pd() t.pencolor(255,155,192) # 设置画笔颜色 t.seth(10) t.begin_fill() t.circle(5) # 画一个半径为5的圆 t.color(160,82,45) # 设置画笔和填充颜色 t.end_fill() t.pu() t.seth(0) t.fd(20)
def __del__(self): time.sleep(1) turtle.end_fill()
def __init__(self): t.setup(500,500); t.screensize(100,100); t.setworldcoordinates(0,-50,100,50); t.bgcolor("skyblue"); t.penup(); t.setpos(60,0); t.left(90); t.pensize(5); t.pendown(); t.pencolor("black"); t.fillcolor("white"); t.begin_fill(); t.circle(10,360); t.end_fill(); t.penup(); t.setpos(56,3) t.pendown(); t.pensize(1); t.fillcolor("black"); t.begin_fill(); t.circle(1,360); t.end_fill(); t.penup(); t.setpos(44,3); t.left(180); t.pendown(); t.begin_fill(); t.circle(1,360); t.end_fill(); t.penup(); t.pensize(5); t.setpos(50,3); t.pendown(); t.forward(3); t.penup(); t.setpos(53,-3); t.right(90); t.pendown(); t.forward(6); t.penup(); t.setpos(50,-10); t.left(90); ##t.pensize(5); t.pendown(); t.forward(10); t.penup(); t.setpos(67.07,-12.93); t.right(45); t.pendown(); t.forward(10); t.right(45); t.forward(20); t.right(45); t.forward(10); t.penup(); t.setpos(50,-20); t.left(135); t.pendown(); t.forward(10); t.penup(); t.setpos(47.07,-37.07); t.right(135); t.pendown(); t.forward(10); t.right(135); t.forward(20); t.right(135); t.forward(10); t.penup(); t.hideturtle(); t.onscreenclick(None); t.mainloop();
def circle(radius, color): turtle.color(color) turtle.begin_fill() turtle.circle(radius) turtle.end_fill()
import turtle # 导入turtle模块 turtle.setup(1200, 800, 0, 0) turtle.bgcolor("red") # 背景颜色 turtle.color('yellow') # 五角星颜色 turtle.speed(5) # 设置画笔绘制速度 # 绘制最大的主五角星 turtle.begin_fill() # 填充绘制的五角星 turtle.up() # 抬笔不绘制 turtle.goto(-520, 240) # 画笔设置到起始位置 turtle.down() # 落笔进行绘制 for i in range(5): # 循环5次 turtle.forward(240) # 向前移动150 turtle.right(144) # 以角度单位向右转动 turtle.end_fill() # 结束填充 # 绘制第1颗副星 turtle.begin_fill() turtle.up() turtle.goto(-230, 345) turtle.setheading(305) turtle.down() for i in range(5): turtle.forward(70) turtle.left(144) turtle.end_fill() # 绘制第2颗副星 turtle.begin_fill() turtle.up() turtle.goto(-150, 230) turtle.setheading(30) turtle.down()
def mainmassage(name,money): #打印姓名 t.goto(-280,68) t.write('收款人: 出票人姓名:XXX',move=False,align="left",font=("Arial",10,"normal")) t.goto(-215,68) t.write(name,move=False,align="left",font=("Arial",10,"normal")) #绘制金额外方框 t.goto(-280,68) t.pendown() t.fd(540) t.right(90) t.fd(50) t.right(90) t.fd(540) t.right(90) t.fd(50) #绘制内方框 t.penup() t.goto(-220,65) t.right(90) t.color("pink") t.begin_fill() t.pendown() t.fd(200) t.right(90) t.fd(30) t.right(90) t.fd(200) t.right(90) t.fd(30) t.end_fill() t.penup() t.color("black") #绘制金额表格 t.right(180) num_list=["亿","仟","百","十","万","千","百","十","元","角"] for i in range(10): t.penup() t.goto(10+i*25,68) t.pendown() t.fd(25) t.write(num_list[i],move=False,align="left",font=("Arial",15,"normal")) t.fd(25) t.penup() t.goto(10,43) t.pendown() t.left(90) t.fd(252) #打印金额 t.penup() t.goto(-280,40) t.write('人民币',move=False,align="left",font=("Arial",15,"normal")) t.goto(-220,40) t.write(money,move=False,align="left",font=("Arial",15,"normal")) m_str=str(money) for i in range(11-len(m_str)): t.goto(10+i*25,18) t.write("0",move=False,align="left",font=("Arial",15,"normal")) for i in range(11-len(m_str),9): t.goto(10+i*25,18) t.write(m_str[i+len(m_str)-11],move=False,align="left",font=("Arial",15,"normal")) t.goto(240,18) t.write(m_str[len(m_str)-1],move=False,align="left",font=("Arial",15,"normal"))
def arc(x, y, r, angle_1, angle_2): tr.penup() tr.goto(x + r * np.cos(angle_1 * np.pi / 180), y + r * np.sin(angle_1 * np.pi / 180)) tr.pendown() for i in range(angle_1, angle_2 + 1, 1): tr.goto(x + r * np.cos(i * np.pi / 180), y + r * np.sin(i * np.pi / 180)) tr.begin_fill() tr.color('black') circle(0, 0, 100) tr.color('yellow') tr.end_fill() for k in range(-1, 3, 2): tr.begin_fill() tr.color('black') circle(40 * k, 30, 20) tr.color('blue') tr.end_fill() tr.color('black') tr.penup() tr.goto(0, 30) tr.pendown() tr.width(8) tr.goto(0, -10) tr.color('red') arc(0, -10, 70, 180, 360) tr.hideturtle()
def draw(self): turtle.begin_fill() super().draw() turtle.end_fill()
def main(): # put label on top of page turtle.title('Colorful Shapes') # setup screen size turtle.setup(800, 800, 0, 0) # draw a triangle turtle.pensize(3) turtle.penup() turtle.goto(-200, -50) turtle.pendown() turtle.begin_fill() turtle.color('red') turtle.circle(40, steps=3) turtle.end_fill() # draw a square turtle.penup() turtle.goto(-100, -50) turtle.pendown() turtle.begin_fill() turtle.color('navy') turtle.circle(40, steps=4) turtle.end_fill() # draw a pentagon turtle.penup() turtle.goto(0, -50) turtle.pendown() turtle.begin_fill() turtle.color('green') turtle.circle(40, steps=5) turtle.end_fill() # draw a hexagon turtle.penup() turtle.goto(100, -50) turtle.pendown() turtle.begin_fill() turtle.color('yellow') turtle.circle(40, steps=6) turtle.end_fill() # draw a circle turtle.penup() turtle.goto(200, -50) turtle.pendown() turtle.begin_fill() turtle.color('purple') turtle.circle(40) turtle.end_fill() # write header turtle.penup() turtle.goto(-100, 50) turtle.write('Cool Colorful Shapes', font=('Times', 18, 'bold')) # hide turtle turtle.hideturtle() # persist drawing turtle.done()
def draw(self, turtle): turtle.end_fill()
def drawCircle(x,y,color,radius) : turtle.goto(x,y) turtle.color(color) turtle.begin_fill() turtle.circle(radius) turtle.end_fill()
def draw_circle(r, color): turtle.fillcolor(color) turtle.begin_fill() turtle.circle(r) turtle.end_fill()
def color_triangle(side_length, color): t.fillcolor(color) t.begin_fill() draw_triangle(side_length) t.end_fill() return None
tt.left(180 / value) # рисуем большой круг BIG = 200 # уходим из центра tt.goto(0, -BIG / 1.55) # рисуем жёлтое лицо tt.width(5) tt.begin_fill() circle(BIG) tt.color("yellow") tt.end_fill() tt.color("black") # рисуем глаза EYES = 40 # идём к первому глазу tt.penup() tt.goto(-BIG / 3, BIG / 3) tt.pendown() # рисуем tt.width(2) tt.begin_fill() circle(EYES)
def focalLength(obj, img): print("Welcome to the Focal Length Calculator!!") object_distance = (input("Enter an object distance, in mm: ") ) #input object distance image_distance = (input("Enter an image distance, in mm: ") ) #input image distance focal_length = 1 / (float(image_distance)) + 1 / (float(object_distance)) print("Focal Length of your lens is: ", 1 / float((focal_length)), "mm") line = (1 / float((focal_length)) / 2) print("Lens:", line, "mm") #distance between f points, placed at origin turtle.speed(1) #speed to see the turtle in motion turtle.showturtle() turtle.screensize(2000, 2000) #object turtle.forward( -float(object_distance)) #to left if positive, to right if negative turtle.color(str("#bc1818")) turtle.begin_fill() turtle.forward(10) turtle.left(90) turtle.forward(10) turtle.left(90) turtle.forward(10) turtle.left(90) turtle.forward(10) turtle.left(90) turtle.end_fill() turtle.color("black") turtle.penup() turtle.goto(-float(object_distance), 20) turtle.write("Object", True, align="center", font=("Ariel", 10, "normal")) #label above lens axis turtle.goto(-float(object_distance), 0) turtle.pendown() #focalpoint1 -- -120 #have to go to origin for it to be the right distance turtle.goto(0, 0) #go to origin; "reset" turtle.forward(-float(1 / focal_length)) turtle.begin_fill() turtle.dot(9, str("#176825")) turtle.end_fill() turtle.penup() turtle.goto(-float(1 / focal_length), -30) turtle.write("Focal Point", True, align="center", font=("Ariel", 10, "normal")) #label below lens axis turtle.color("black") turtle.goto(-float(1 / focal_length), 0) turtle.pendown() #focalpoint2 -- 120 #have to go to the origin for it to be the right distance turtle.goto(0, 0) #go to origin; "reset" turtle.goto(float(1 / focal_length), 0) turtle.begin_fill() turtle.dot(9, str("#176825")) turtle.end_fill() turtle.penup() turtle.goto(float(1 / focal_length), -30) turtle.write("Focal Point", True, align="center", font=("Ariel", 10, "normal")) turtle.color("black") turtle.goto(float(1 / focal_length), 0) turtle.pendown() turtle.goto(0, 0) #go to origin; "reset" #image turtle.forward( float(image_distance)) #to right if positive, to left if negative turtle.color(str("#183870")) turtle.begin_fill() turtle.forward(10) turtle.left(90) turtle.forward(10) turtle.left(90) turtle.forward(10) turtle.left(90) turtle.forward(10) turtle.left(90) turtle.end_fill() turtle.color("black") turtle.penup() turtle.goto(float(image_distance), 20) turtle.write("Image", True, align="center", font=("Ariel", 10, "normal")) #label above lens axis turtle.goto(float(image_distance), 0) turtle.pendown() turtle.goto(0, 0) #lens turtle.color(str("#5e5e5e")) turtle.goto(0, 100) #to make the vertical line turtle.goto(0, -100) #to make the vertical line
#정오각형 그리기 프로그램 import turtle as t n = 5 #오각형을 그림 t.color("purple") t.begin_fill() #색칠할 영역을 시작함 for x in range(n): #n 번 반복 t.forward(50) #거북이 50만큼 앞으로 이동 t.left(360 / n) #거북이 360/n만큼 왼쪽으로 회전 t.end_fill() #색칠할 영역을 마무리
def yanjing(): t.begin_fill() t.penup() t.goto(-125, 140) t.pendown() t.fillcolor("#000000") t.pencolor("black") t.seth(100) t.circle(-25, 80) t.seth(40) t.circle(-200, 23) t.seth(-90) t.fd(45) t.seth(195) t.circle(200, 27) t.seth(150) t.circle(-12, 90) t.goto(-125, 140) t.end_fill() # 黑色 t.begin_fill() t.penup() t.goto(-39, 205) t.pendown() t.fillcolor("#E6E8FA") t.setheading(90) t.circle(-8, 180) t.seth(-90) t.fd(45) t.circle(-8, 180) t.goto(-39, 205) t.end_fill() # 银色 t.begin_fill() t.penup() t.goto(-23, 160) t.pendown() t.fillcolor("#E6E8FA") t.seth(-78) t.circle(85, 130) t.goto(-23, 160) t.end_fill() # 银色 t.begin_fill() t.penup() t.goto(-23, 190) t.pendown() t.fillcolor("#E6E8FA") t.seth(-90) t.circle(90) t.end_fill() # 银色 t.begin_fill() t.penup() t.goto(155, 205) t.pendown() t.fillcolor("#000000") t.seth(-15) t.circle(-100, 20) t.seth(-60) t.circle(-105, 25) t.seth(160) t.circle(200, 13.5) t.seth(75) t.circle(90, 20) t.goto(155, 205) t.end_fill() # 黑色 t.begin_fill() t.penup() t.goto(128, 195) t.pendown() t.fillcolor("#ffffff") t.circle(60) t.end_fill() # 白色 t.begin_fill() t.penup() t.goto(110, 150) t.pendown() t.fillcolor("#000000") t.seth(70) t.circle(70, 110) t.seth(20) t.circle(-60, 150) t.end_fill() # 黑色 t.begin_fill() t.penup() t.goto(25, 210) t.pendown() t.fillcolor("#B85300") t.circle(20) t.end_fill() # 白色 t.begin_fill() t.penup() t.goto(32, 204) t.pendown() t.fillcolor("#000000") t.circle(8) t.end_fill()