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 o(fill=False): '''draws a capital O''' turtle.setheading(0) pu() fd(20) pd() if fill: bf() circle(20, 90) fd(70) circle(20, 180) fd(70) circle(20, 90) if fill: ef() pu() turtle.sety(turtle.ycor() + 10) pd() cfc = fc() fc(turtle.getscreen().bgcolor()) bf() circle(10, 90) fd(70) circle(10, 180) fd(70) circle(10, 90) ef() pu() turtle.goto(turtle.xcor() + 30, turtle.ycor() - 10) pd() fc(cfc)
def main(): file_name = "go" file_name = raw_input( 'Enter a file name or exit to quit program: ') while (file_name != "exit" and file_name != "Exit" and file_name != "quit" and file_name != "Quit"): f = open( file_name, 'r' ) first_line = f.readline() first_line = first_line.split() distance = float( first_line[0] ) angle = float( first_line[1] ) stack = [] wn = tur.Screen() for line in f: wn.clear() tur.penup() tur.seth(90) tur.setx(0) tur.sety(-200) tur.pendown() interprit_line(tur, line, angle, distance, stack) ts = tur.getscreen() ts.getcanvas().postscript(file=file_name +".eps") wn.exitonclick() file_name = raw_input( 'Enter a file name or exit to quit program: ')
def draw_circle(x,y,radius): turtle.penup() turtle.setx(x) turtle.sety(y) turtle.begin_fill() turtle.pendown() turtle.circle(radius) turtle.end_fill()
def init(): """ Initializing the drawing window. :return: """ turtle.up() turtle.sety(-200) turtle.down() turtle.left(90) turtle.speed(0)
def main(): rd=int(input("Enter the recursion depth: ")) length=int(input('Enter the length of the square: ')) turtle.speed("fastest") turtle.penup() turtle.setx(-300) turtle.sety(-300) turtle.pendown() drawNestedSquare(rd,length) print('Total Distance travelled by the pointer: ',sum) input("Hit enter to exit...")
def init(x , y): """ :input: x and y corrdinate for setting intial/starting corrdinates :pre:(relative) pos (0,0), heading (east), up :post:(relative) pos (x,y), heading (east), up :return: None """ t.up() t.title('forest') t.setx(x) t.sety(y)
def placeX(self,x,y): turtle.up() tx=x*harsha ty=y*harsha turtle.setx(tx) turtle.sety(ty) turtle.down() turtle.goto(tx-harsha,ty-harsha) turtle.goto(tx-harsha,ty) turtle.goto(tx,ty-harsha) turtle.up() turtle.goto(-harsha,-harsha) turtle.down() return
def init(): """ :pre: pos (0,0), heading (east), up :post: pos (-180,0), heading (east), up :return: None """ turtle.up() x_cor = -180 y_cor = 0 turtle.setx(x_cor) turtle.sety(y_cor) print("Starting Position (" + str(x_cor) + "," + str(y_cor) + ")") turtle.setheading(0) turtle.title("Typography")
def checkpos(turtlelist): screen.tracer(0) for turtle in turtlelist: x = turtle.xcor() y = turtle.ycor() if x > 200: turtle.setx(-200) elif x < -200: turtle.setx(200) if y > 200: turtle.sety(-200) elif y < -200: turtle.sety(200) screen.tracer(1) return
def circle(radius, cx = None, cy = None, color = "black", fill = False): wasDown = turtle.isdown() turtle.up() if cx is not None and cy is not None: turtle.setpos(cx, cy) turtle.sety(turtle.ycor() - radius) if wasDown: turtle.down() if fill: turtle.color(color, color) turtle.begin_fill() else: turtle.pencolor(color) turtle.circle(radius) if fill: turtle.end_fill()
def draw_circle(x,y,radius): """ A function that draws a circle centered at point (x,y) with a radius="param radius: :param x: :param y: :param radius: :return:None """ turtle.penup() turtle.setx(x+radius) turtle.sety(y) turtle.begin_fill() turtle.pendown() turtle.circle(radius) turtle.end_fill() return None
def init_for_day(): """ Initialize for drawing in the day. (-WINDOW_WIDTH/2, -WINDOW_HEIGHT/2) is in the lower left and(WINDOW_WIDTH/2, WINDOW_HEIGHT/2) is in the upper right. : pre: (relative) pos (0,0), heading (east), up : post:(relative) pos (-500,-333.33), heading (east), up heading (east), up : return: None """ turtle.up() turtle.hideturtle() turtle.setx(-WINDOW_WIDTH/2) turtle.sety(-WINDOW_HEIGHT/3) turtle.setheading(0) turtle.showturtle()
def __init__(self): for k in range(0,harsha*3,harsha): for j in range(0,harsha*3,harsha): #a=turtle.pos() turtle.sety(j) turtle.setx(k) for i in range(4): turtle.right(90) turtle.forward(harsha) #turtle.setx(k) turtle.up() turtle.setx(-harsha) turtle.sety(-harsha) turtle.down() self.ox=-harsha self.oy=-harsha
def draw_line(x,y,heading,length): """ A function that draws a line start at point (x,y) in the direction param:heading for distance param:length. :param x: :param y: :param heading: :param length: :return: None """ turtle.penup() turtle.setx(x) turtle.sety(y) turtle.pendown() turtle.setheading(0) turtle.left(heading) turtle.forward(length) return None
def city_circle(city): turtle.penup() turtle.setx(city.xcoord) turtle.sety(city.ycoord) if city.getpop() > 0 and city.getpop() < 90001: turtle.setx(turtle.xcor() + 2) turtle.pendown() turtle.circle(2) turtle.penup() turtle.setx(turtle.xcor() - 2) if city.getpop() > 90000: turtle.setx(turtle.xcor() + 3) turtle.pendown() turtle.begin_fill() turtle.circle(3) turtle.end_fill() turtle.penup() turtle.setx(turtle.xcor() - 3)
def joonistaLipp(riik, lipud=lipud): värvid = lipud[riik] turtle.pendown() for värv in värvid: turtle.begin_fill() turtle.fillcolor(värv) turtle.forward(100) turtle.right(90) turtle.forward(20) turtle.right(90) turtle.forward(100) turtle.right(90) turtle.forward(20) turtle.end_fill() turtle.sety(turtle.ycor()-20) turtle.right(90) turtle.penup() turtle.sety(turtle.ycor()+60) turtle.setx(turtle.xcor()+110)
def draw_flag(height): tt.penup() # assign parameters defined from # https://en.wikipedia.org/wiki/Flag_of_the_United_States A = height B,C = 1.9*A, (7.0/13.0)*A D = 2.0*B/5.0 E = C/10.0 G = D/12.0 L = A/13.0 K = 4.0*L/5.0 # draw the bottom 6 stripes for color in ['red','white']*3: draw_rectangle(B, L, color) tt.sety(tt.ycor()+L) # draw the top left blue rectangle draw_rectangle(D, C, 'blue') # draw the top right 7 stripes tt.setx(tt.xcor()+D) for color in ['red','white']*3: draw_rectangle(B-D, L, color) tt.sety(tt.ycor()+L) draw_rectangle(B-D, L, 'red') tt.sety(tt.ycor()+L) # draw the 50 stars tt.setx(G) tt.sety(tt.ycor()-E) nums_star = [6,5]*4 nums_star.append(6) for nstar in nums_star: if (nstar == 5): tt.setx(tt.xcor()+G) for i in range(nstar): draw_star(K, 'white') tt.setx(tt.xcor()+2.0*G) tt.setx(G) tt.sety(tt.ycor()-E)
def main(): x=int(input('Enter the number of trees: ')) house=input('Is there a House in the forest (y/n)? ') StartPos() findmax=sketch(x,house) input('Night is done, press enter for the day') turtle.clearscreen() turtle.penup() turtle.setx(-150) turtle.sety(-150) turtle.pendown() total=WoodQuantity(findmax) """ Adding the wood obtained from the trunks and the house built during the night """ dayTime(total+341.4) showSun() print('Day is done, house is built, ') input('Press ENTER to quit')
def view(self): import turtle coords = [ref.image_coord_mm for ref in self.reflections] x, y = zip(*coords) min_x, max_x = min(x), max(x) min_y, max_y = min(y), max(y) low = min(min_x, min_y) high = max(max_x, max_y) turtle.title("Reflections from " + self.reflections_filename) turtle.setworldcoordinates(low, low, high, high) turtle.pen(speed=0,pensize=2) turtle.hideturtle() for ref in self.reflections: (x, y) = ref.image_coord_mm turtle.penup() turtle.setx(x) turtle.sety(y) turtle.pendown() turtle.circle(1.0, steps=8) turtle.done()
def checkpos(turtlelist, screen): """ Takes a list of turtle objects and a Screen object. Sets the turtles to the opposite edge of the screen. """ screen.tracer(0) for turtle in turtlelist: turtle.penup() x = turtle.xcor() y = turtle.ycor() if x > 200: turtle.setx(-200) elif x < -200: turtle.setx(200) if y > 200: turtle.sety(-200) elif y < -200: turtle.sety(200) turtle.pendown() screen.tracer(1) return
def duzyKwadrat(dlBokMalKw): numPow = 1 wnd = turtle.Screen() wnd.colormode(255) turtle.fillcolor(255, 255, 0) turtle.pu() turtle.bk(225) turtle.lt(90) turtle.bk(225 - dlBokMalKw) turtle.pd() turtle.begin_fill() while(numPow <= 4): turtle.fd(450 - dlBokMalKw * 2 - 15) turtle.rt(90) numPow = numPow + 1 turtle.end_fill() turtle.pu() turtle.setx(0) turtle.sety(0) turtle.pd() turtle.rt(90)
def oczySowy(r, bokKw, bokMalKw, rMalegoKola): rKolaWOku = bokMalKw / 8 turtle.pu() turtle.rt(90) turtle.fd(r - rMalegoKola*3) turtle.rt(90) turtle.pd() elementSowy(r, bokMalKw, bokMalKw / 4) powrNaSrodekSowy(r, bokMalKw) turtle.lt(90) turtle.pu() turtle.fd(r*3) turtle.pd() turtle.lt(90) elementSowy(r, bokMalKw, bokMalKw / 4) powrNaSrodekSowy(r, bokMalKw) turtle.lt(180) turtle.fillcolor(209, 183, 255) turtle.begin_fill() turtle.circle(rMalegoKola) turtle.end_fill() turtle.lt(180) turtle.begin_fill() turtle.circle(rMalegoKola) turtle.end_fill() turtle.pu() turtle.rt(90) turtle.setx(0) turtle.sety(0) turtle.fd(r) turtle.lt(90) turtle.pd() turtle.begin_fill() turtle.circle(rMalegoKola) turtle.end_fill() turtle.lt(180) turtle.begin_fill() turtle.circle(rMalegoKola) turtle.end_fill()
def drawFrame(self, i, j, colour=None): if colour is not None: turtle.color("red") else: turtle.color("black") turtle.ht() turtle.up() turtle.tracer(10000,0) x = self.index_to_pixel(i, "x") y = self.index_to_pixel(j, "y") print("going to " + str(x) + " " + str(y)) turtle.setx(x) turtle.sety(y) turtle.down() turtle.forward(self.xside_box) turtle.left(90) turtle.forward(self.yside_box) turtle.left(90) turtle.forward(self.xside_box) turtle.left(90) turtle.forward(self.yside_box) turtle.left(90) turtle.update()
def w(fill=False): '''draws a capital W.''' turtle.setheading(0) pu() turtle.sety(turtle.ycor() + 110) pd() if fill: bf() turtle.goto(turtle.xcor() + 10, turtle.ycor() - 110) turtle.goto(turtle.xcor() + 5, turtle.ycor()) turtle.goto(turtle.xcor() + 5, turtle.ycor() + 50) turtle.goto(turtle.xcor() + 5, turtle.ycor() - 50) turtle.goto(turtle.xcor() + 5, turtle.ycor()) turtle.goto(turtle.xcor() + 10, turtle.ycor() + 110) turtle.goto(turtle.xcor() - 10, turtle.ycor()) turtle.goto(turtle.xcor() - 5, turtle.ycor() - 55) turtle.goto(turtle.xcor() - 2, turtle.ycor() + 20) turtle.goto(turtle.xcor() - 6, turtle.ycor()) turtle.goto(turtle.xcor() - 2, turtle.ycor() - 20) turtle.goto(turtle.xcor() - 5, turtle.ycor() + 55) turtle.goto(turtle.xcor() - 10, turtle.ycor()) if fill: ef() pu() turtle.goto(turtle.xcor() + 50, turtle.ycor() - 110) pd()
def gridlines(physics=False,xmin=-100,xmax=100): # Makes graph for window import turtle ymin = -(abs(xmin-xmax)/2.) ymax = abs(xmin-xmax/2.) turtle.setworldcoordinates(xmin,ymin,xmax,ymax) turtle.home() turtle.hideturtle() xadjust = 0 yadjust = 0 if physics: #physics window is fixed-size xadjust = -50 yadjust = -50 turtle.up() turtle.setx(xmin + xadjust) #starting x-axis turtle.sety(yadjust) turtle.down() turtle.setheading(0) xvals = xmin graph_range = 10 + 1 if physics: graph_range = 20 + 1 for x in range(graph_range): turtle.write(str(turtle.xcor()-xadjust)) #writing values for x axis turtle.forward(abs(xmin-xmax)/10.) #advancing along x axis xvals += abs(xmin-xmax)/10. turtle.update() turtle.setx(0 + xadjust) turtle.sety(0 + yadjust) turtle.sety(ymin + yadjust) #starting y-axis turtle.setheading(90) yvals = ymin for y in range(graph_range): turtle.write(str(turtle.ycor()-yadjust)) #writing values for y axis turtle.forward(abs(ymin-ymax)/10.) #advancing along y axis yvals += abs(ymin-ymax)/10. turtle.update() turtle.setx(0 + xadjust) turtle.setx(0 + yadjust) turtle.penup() return xadjust, yadjust return
"gemini": "x142y38r138c20r89c63l9c27l25c9l22c12x142y38r76c42l30c20r8c38r87c18", "auriga": "x-10y-23r50c54l136c48l102c38r80c64r69c38r93c54r41c38", "piscis": "x-256y127r99c44l7c40r5c42l48c18l15c14r44c20x-256y127r56c12l15c19r14c17l3c39r17c19l9c66r4c26r103c25l118c18l26c25l94c19l76c21" } texto_menu = "Bienvenido a la Constelacion de Santa Maria\n" \ "Opciones:\n" \ "|------------------------------------------\n" \ "| 1 - Buscar constelaciones\n" \ "| 2 - Ingresar Constelacion\n" \ "| 3 - Distancia entre dos constelaciones\n" \ "| 4 - Salir\n" \ "|------------------------------------------\n" pantalla = turtle.Screen() pantalla.screensize(880,880) pantalla.setup(width=900,height=900) pantalla.bgpic("image.gif") turtle.speed(0) turtle.up() turtle.setx(0) turtle.sety(-440) turtle.down() turtle.color("white") turtle.circle(440) turtle.up() turtle.sety(-200) turtle.down() turtle.circle(200) pantalla.exitonclick()
import turtle as t t.shape("turtle") t.pos() # 거북이 좌표(x,y) t.forward(100) t.pos() a = t.xcor() # x좌표 b = t.ycor() # y좌표 t.goto(50, 50) # 특정 좌표로 이동 t.setx(-50) # x좌표 값 조정 t.sety(-50) # y좌표 값 조정 t.pos() t.distance(100, 100) # 현재 위치와 (100,100)의 거리 t.heading() t.towards(10, 10) # 현재 위치에서 (10,10)을 바라보는 각도 t.setheading(45) # 거북이 머리 각도 조정 t.forward(50) t.home() # 이벤트 설정(함수) def f(): t.forward(10)
st_sign_initial_y = 145 st_sign_length = 300 st_sign_height = 80 st_sign_word_x = 0 st_sign_word_y = 165 st_sign_font_size = 30 # set turtle speed t.speed(turtle_speed) # draw sign post t.penup() t.setx(post_initial_x) t.sety(post_initial_y) t.left(90) t.pensize(post_thickness) t.color('grey','grey') t.pendown() t.forward(post_length) t.right(90) t.penup() # position turtle for octagon t.setx(oct_initial_x) t.sety(oct_initial_y) t.pendown() # set octagon color
def cross(x): if x == 1: t.setx(-120) t.sety(10) elif x == 2: t.setx(-20) t.sety(10) elif x == 3: t.setx(60) t.sety(10) elif x == 4: t.setx(-120) t.sety(-70) elif x == 5: t.setx(-20) t.sety(-70) elif x == 6: t.setx(60) t.sety(-70) elif x == 7: t.setx(-120) t.sety(-150) elif x == 8: t.setx(-20) t.sety(-150) elif x == 9: t.setx(60) t.sety(-150) t.pd() t.pencolor('white') t.left(45) t.fd(70.71) t.right(135) t.color('white') t.up() t.fd(50) t.pd() t.color('white') t.left(225) t.fd(70.71) t.right(135) t.up() t.home()
def move_up(): canvas.yview_scroll(-1, "units") turtle.sety(turtle.ycor() + MAGNIFICATION)
tr.goto(aX[1], 0) # Y tr.up() tr.goto(0, aY[1]) tr.down() tr.goto(0, aY[0]) # labels tr.up() for x in range(aX[0], aX[1]): tr.goto(x, 0.1) tr.down() tr.goto(x, 0) tr.up() tr.sety(-0.4) coords = str(x) tr.write(coords) for y in range(aY[0], aY[1]): tr.goto(0, y) tr.down() tr.goto(0.1, y) tr.up() tr.setx(0.2) coords = str(y) tr.write(coords) sf = (aX[1] - aX[0]) * (aY[1] - aY[0]) * min_fun / NMax s = (pi * r1**2 + pi * (r2**2 - r1**2)) / 4 tr.goto(2, 2)
def getPosition(x, y): turtle.setx(x) turtle.sety(y) print(x, y)
def gui(state, size, path): turtle.bgcolor("yellow") #arr=[1,1,1,0,1,0,0,0,0,1,1,0,0,1,1,1,0,0,0,1,1,0,0,0,1,1,1,0,0,0,1,0,1,1,1,1,0,0,0,1] arr = state so = size gu = turtle.Turtle() start_pos = (-50, 50) #print(path) turtle.penup() turtle.setx(start_pos[0]) turtle.sety(start_pos[1]) turtle.pendown() for j in range(so + 1): for i in range(so): if (arr[j * so + i] is 1): turtle.forward(30) turtle.dot() else: turtle.penup() turtle.forward(30) turtle.pendown() #end turtle.penup() turtle.setx(start_pos[0]) turtle.sety(start_pos[1] - 30 * (j + 1)) turtle.pendown() turtle.penup() turtle.setx(start_pos[0]) turtle.sety(start_pos[1]) turtle.pendown() turtle.right(90) for j in range(0, so + 1): for i in range(so): if (arr[(j + so + 1) * so + i] is 1): turtle.forward(30) turtle.dot() else: turtle.penup() turtle.forward(30) turtle.pendown() #end turtle.penup() turtle.setx(start_pos[0] + 30 * (j + 1)) turtle.sety(start_pos[1]) turtle.pendown() turtle.left(90) turtle.color("yellow") for i in range(len(path)): if (path[i] <= so * (so + 1)): xc = (path[i] - 1) % so yc = (int)(path[i] - 1) // so turtle.penup() turtle.setx(start_pos[0] + 30 * xc) turtle.sety(start_pos[1] - 30 * yc) turtle.pendown() turtle.forward(30) #turtle.dot() turtle.right(90) for i in range(len(path)): if (path[i] > so * (so + 1)): yc = ((path[i] - so * (so + 1)) - 1) % so xc = ((path[i] - so * (so + 1)) - 1) // so turtle.penup() turtle.setx(start_pos[0] + 30 * xc) turtle.sety(start_pos[1] - 30 * yc) turtle.pendown() turtle.forward(30) #turtle.dot() turtle.getscreen()._root.mainloop()
def base(size): tu.setx(1) tu.sety(1) tu.color('grey') tu.fd(size) tu.backward(size) tu.fd(size/5) tu.left(90) velo(size/4) tu.up() tu.setx(1) tu.sety(1) tu.down() tu.color('grey') tu.right(130) tu.fd(size*2) tu.backward(size/5) tu.left(90) #tronc d'arbre tu.color('brown') tu.fd(size/1.5) tu.color('green') exagone(size/5) #branche 1 tu.color('brown') tu.fd(size/10) tu.right(100) tu.fd(size / 10) tu.color('green') exagone(size/8) tu.color('brown') tu.fd(size / 10) tu.right(80) #branche 2 tu.fd(size/4) tu.color('brown') tu.right(100) tu.fd(size / 10) tu.color('green') exagone(size/8) tu.color('brown') tu.fd(size / 10) tu.right(80) #Nuages tu.up() tu.setx(20) tu.sety(180) tu.down() tu.color('blue') exagone(size/10) #Nuages tu.up() tu.setx(80) tu.sety(200) tu.down() tu.color('blue') exagone(size/10) #Nuages tu.up() tu.setx(150) tu.sety(220) tu.down() tu.color('blue') exagone(size/10)
t.fillcolor('pink') #fillcolor设置填充色粉色 t.speed(1) #2.开始填充 t.begin_fill() t.circle(100) t.fd(120) t.left(90) t.fd(160) t.home() t.right(90) t.circle(80) #3.结束填充 t.end_fill() #连续画图 t.up() t.sety(-88) t.down() t.fillcolor('cyan') t.begin_fill() t.fd(150) t.right(90) t.fd(150) t.right(135) t.fd(220) t.end_fill() t.mainloop()
import turtle as t t.hideturtle() t.penup() t.setx(-300) t.sety(-300) p = t.Pen() p.reset() p.speed(22) t.showturtle() t.pendown() def koch(t, order, size): """ Make turtle t draw a Koch fractal of 'order' and 'size'. Leave the turtle facing the same direction. """ if order == 0: # The base case is just a straight line t.forward(size) else: koch(t, order - 1, size / 3) # Go 1/3 of the way t.left(60) koch(t, order - 1, size / 3) t.right(120) koch(t, order - 1, size / 3) t.left(60) koch(t, order - 1, size / 3) koch(t, 4, 1000)
# Set penFillcolor to the RGB color represented by the tuple of r, g, and b. Each of r, gand b must be in the range 0..colormode, where colormode is either 1.0 or 255 turtle.setx(-100) turtle.pd() tup = (0.2, 0.8, 0.55) turtle.fillcolor(tup) print(turtle.fillcolor()) turtle.fd(100) turtle.lt(90) turtle.fd(100) turtle.lt(90) turtle.fd(100) turtle.lt(90) turtle.fd(100) turtle.pu() # pencolor(r, g, b) # Set penFillcolor to the RGB color represented by r, g, and b. Each of r, gand b must be in the range 0..colormode. turtle.sety(-150) turtle.pd() turtle.fillcolor('#32c18f') print(turtle.fillcolor()) turtle.fd(100) turtle.lt(90) turtle.fd(100) turtle.lt(90) turtle.fd(100) turtle.lt(90) turtle.fd(100)
def SetupClock(): """ 两者模式对比: 标准模式 logo 模式 0 -东 0 - 北 90 -北 90 - 东 180-西 180 - 南 270-南 270 - 西 """ t.mode("logo") # 设置为logo模式,不设置时默认是标准模式 """ 关于setup有明确的定义,它包括4个参数width,height,startx,starty, 即定义了窗体的大小和相对于桌面的起始点的坐标以及窗口的宽度高度,缺省是居中占整个屏幕的一半 screensize包括3个参数,定义了画布的大小和背景色 需要注意的是,screensize定义画布大小,缺省是400,300 """ t.setup(600, 600, 10, 100) # 大小600*600的画布 和 相对于桌面的起始点的坐标以及窗口的宽度高度, # t.screensize(600,600) # #测试用 # t.tracer(False) # 不显示绘制的过程,直接显示绘制结果 t.dot(10) # 在中心画个点 t.seth(-90) # 非logo模式时 方向朝下 ,用了logo模式方向朝左 # t.seth(180) # 方向朝下(logo模式),非logo模式是朝左 Skip(200) t.pensize(3) t.seth(0) # 方向朝右 t.circle(-200) #钟表外框半径是 200 # 上移50 t.seth(90) # 方向朝上 Skip(50) #内框圆半径是150=200-50 t.dot(5) # 画一个点 t.seth(0) # 方向朝右 # 间隔性的画第二层的小点 for _ in range(59): t.pu() t.circle(-150, 6) # 移动60弧度 t.pd() t.dot(5) # 画一个点 t.pu() # #测试用 # t.tracer(True) # # 画12个线段 并写上对应的时间数字 t.home() # 回到起始圆点和状态 t.pensize(5) t.seth(30) for i in range(12): t.fd(150) # 前进150 t.pd() # 落笔 t.fd(10) # 前进10 if (i < 2 or i > 8): Skip(5) elif (i == 2 or i == 8): # 对应3和9 因为i+1 Skip(10) elif (i == 3 or i == 7): # 对应4和8 因为i+1 Skip(20) else: Skip(25) t.write(i + 1, align="center", font=('Courier', 15, 'bold')) # 写上对应的时间数字 t.up() # 起笔 t.setx(0) # 回来 t.sety(0) # 回来 t.rt(30) # 右转30度
def drawStarPattern(side2): turtle.hideturtle() clr = "#1656bc" turtle.penup() turtle.setx(50) turtle.sety(-100) turtle.pendown() drawTriangle(side2, clr) turtle.sety(50) turtle.setx(125) drawTriangle(side2,clr) turtle.setx(-side2/2) turtle.sety(150) drawTriangle(side2, clr) turtle.right(180) turtle.setx(-(125+side2)) turtle.sety(50) turtle.right(180) drawTriangle(side2, clr) turtle.setx(-(50+side2)) turtle.sety(-100) drawTriangle(side2, clr) turtle.hideturtle() turtle.setx(0) turtle.sety(0)
if symbol == 0: gen.extend([1,'push','left','div',0,'pop',1,"+",'right',0,"-",'restore',0]) elif symbol == 1: gen.extend([1, 1]) elif symbol == 'div': gen.extend(['div', 'add']) else: gen.append(symbol) li = gen print(li) #------dessin--------- #positionement initiale turtle.speed(0) turtle.lt(90) turtle.pu() turtle.sety(-270) turtle.pd() #piles pos_stack=list() head_stack=list() main_pos_stack=list() main_head_stack=list() fd_stack=[5] for symbol in li: if symbol == 0: turtle.pd() turtle.fd(fd_stack[0]) elif symbol == 1: turtle.pd()
def drawBoard(size, boardArg): startingPos = turtle.pos() #Draw thick edges turtle.width(10) drawSquare(size / 2) turtle.penup() turtle.sety(turtle.ycor() + size / 2) turtle.pendown() drawSquare(size / 2) turtle.penup() turtle.setx(turtle.xcor() + size / 2) turtle.pendown() drawSquare(size / 2) turtle.penup() turtle.sety(turtle.ycor() - size / 2) turtle.pendown() drawSquare(size / 2) turtle.penup() #Draw thin edges turtle.width(5) #Vertical thin edges turtle.setx(turtle.xcor() + size / 4) turtle.pendown() turtle.left(90) turtle.forward(size) turtle.left(90) turtle.forward(size / 2) turtle.left(90) turtle.forward(size) #Horizontal thin edges turtle.right(90) turtle.forward(size / 4) turtle.right(90) turtle.forward(size / 4) turtle.right(90) turtle.forward(size) turtle.left(90) turtle.forward(size / 2) turtle.left(90) turtle.forward(size) turtle.penup() #Reset turtle back to bottom left turtle.setpos(startingPos) #Set draw turtles according to the board parameter #Could rewrite this to use loops, but it works. a4.setpos(size / 10, size / 15) b4.setpos(size / 10, size / 15) c4.setpos(size / 10, size / 15) d4.setpos(size / 10, size / 15) b4.setx(a4.xcor() + size / 4) c4.setx(b4.xcor() + size / 4) d4.setx(c4.xcor() + size / 4) a3.setpos(size / 10, size / 15 + size / 4) b3.setpos(size / 10, size / 15 + size / 4) c3.setpos(size / 10, size / 15 + size / 4) d3.setpos(size / 10, size / 15 + size / 4) b3.setx(a3.xcor() + size / 4) c3.setx(b3.xcor() + size / 4) d3.setx(c3.xcor() + size / 4) a2.setpos(size / 10, size / 15 + 2 * size / 4) b2.setpos(size / 10, size / 15 + 2 * size / 4) c2.setpos(size / 10, size / 15 + 2 * size / 4) d2.setpos(size / 10, size / 15 + 2 * size / 4) b2.setx(a2.xcor() + size / 4) c2.setx(b2.xcor() + size / 4) d2.setx(c2.xcor() + size / 4) a1.setpos(size / 10, size / 15 + 3 * size / 4) b1.setpos(size / 10, size / 15 + 3 * size / 4) c1.setpos(size / 10, size / 15 + 3 * size / 4) d1.setpos(size / 10, size / 15 + 3 * size / 4) b1.setx(a1.xcor() + size / 4) c1.setx(b1.xcor() + size / 4) d1.setx(c1.xcor() + size / 4) for x in range(len(boardArg)): for y in range(len(boardArg[x])): if boardArg[x][y] != 0: coordinates[x][y].write(str(boardArg[x][y]), move=False, align="left", font=("Arial", 18, "normal"))
>>> import turtle as t >>> t.bk(21) >>> t.bk(20) >>> t.right(20) >>> t.fd(30) >>> t.goto(20.30) Traceback (most recent call last): File "<pyshell#53>", line 1, in <module> t.goto(20.30) File "<string>", line 8, in goto File "C:\Users\PC-23\AppData\Local\Programs\Python\Python37\lib\turtle.py", line 1774, in goto self._goto(Vec2D(*x)) TypeError: type object argument after * must be an iterable, not float >>> t.goto(300,300) >>> t.setx(20) >>> t.sety(30) >>> t.circle(20) >>> t.heading <function heading at 0x00000169EA31DE18> >>> t.distance(200.430) Traceback (most recent call last): File "<pyshell#59>", line 1, in <module> t.distance(200.430) File "<string>", line 8, in distance File "C:\Users\PC-23\AppData\Local\Programs\Python\Python37\lib\turtle.py", line 1858, in distance return abs(pos - self._position) UnboundLocalError: local variable 'pos' referenced before assignment >>> t.distance(20.24) Traceback (most recent call last): File "<pyshell#60>", line 1, in <module> t.distance(20.24)
import turtle as t t.circle(80) #模拟抬笔的效果 t.pencolor('white') #更改笔的颜色,白色 t.setx(-50) #更改坐标 t.pencolor('green') #笔的颜色绿色 t.fd(150) #向前150 #方法二 #up() 抬起笔 turtle.up() t.up() t.sety(-100) #donw() 放下笔 turtle.dowm() t.down() t.fd(150) t.pencolor('red') t.circle(90) t.mainloop()
import turtle turtle.color("green") turtle.hideturtle() turtle.backward(100) turtle.setx(100) turtle.sety(100) turtle.speed(1) turtle.setheading(90) turtle.showturtle() turtle.home() turtle.fillcolor("red") turtle.circle(100) turtle.write("hello 你好", font=("隶书", 30, "normal")) turtle.done()
"""Turtle graphics project file.""" import turtle # Create our screen SCREEN = turtle.Screen() SCREEN.title("Napster Spacebook") SCREEN.setup(1280, 800) SCREEN.bgpic("assets/Background.png") # Set the shape to be a car. SCREEN.register_shape("car", ((-4, 10), (4, 10), (4, -10), (-4, -10))) turtle.shape("car") # This code moves the turtle to its starting location. turtle.penup() turtle.setx(460) turtle.sety(-275) turtle.pendown() # Write a for loop to go back and forth 5 times. # If you don't know what to do, come back after you learn loops. # Write code to go to the school and then back # (You can use your easy solution for a lot of this!) # End the loop here. # This line stops the window from closing once we make it to the end. turtle.done()
def move_down(): canvas.yview_scroll(1, "units") turtle.sety(turtle.ycor() - MAGNIFICATION)
def Plot_Guide_Tree(Order_List, Tree_Table): ''' turtle.hideturtle() turtle.setup(width=0.6, height=0.6) turtle.penup() turtle.goto(100,100) turtle.pendown() turtle.setheading(180) TT = Tree_Table.copy() Tmp = [] position = [[100,100]] for i in Tree_Table: if isinstance(i,str): Tmp.append(i) else: position.append([position[-1][0]- 10*i[0],position[-1][1]]) position.append([position[-1][0],position[-1][1] - 5]) ''' turtle.setup(width=0.6, height=0.8) turtle.penup() turtle.speed('slowest') TT = [] position = [(100, 100)] for i in Tree_Table: if isinstance(i, list): TT.append(i) for i in range(len(Order_List)): if (Order_List[i][0] == '-') & (Order_List[i][1] == '-'): pos1 = position.pop(1) pos2 = position.pop(1) turtle.goto(pos1[0], pos1[1]) turtle.setheading(180) turtle.pendown() turtle.forward(100 * TT[i][0]) pos = turtle.pos() turtle.lt(90) turtle.goto(pos[0], (pos2[1] + pos1[1]) / 2) pos = turtle.pos() position.append(pos) turtle.goto(pos[0], pos2[1]) turtle.lt(90) turtle.goto(pos2[0], pos2[1]) turtle.penup() elif (Order_List[i][0] == '-') & (Order_List[i][1] != '-'): pos = position.pop(-1) turtle.goto(pos[0], pos[1]) turtle.setheading(180) turtle.pendown() turtle.forward(500 * TT[i][0]) pos_tmp = turtle.pos() turtle.lt(90) turtle.goto(pos_tmp[0], position[0][1]) pos = turtle.pos() position.append(pos) turtle.goto(pos[0], 2 * position[0][1] - pos_tmp[1]) turtle.lt(90) pos = turtle.pos() turtle.goto(100, pos[1]) position[0] = turtle.pos() #turtle.setx(110) turtle.write(Order_List[i][1], False, align="left") turtle.penup() else: turtle.goto(100, position[0][1]) turtle.setheading(180) turtle.sety(position[0][1] - 50) turtle.pendown() turtle.write(Order_List[i][0], False, align="left") turtle.forward(100 * TT[i][0]) turtle.lt(90) turtle.forward(50 * (TT[i][0] + TT[i][1])) pos = turtle.pos() position.append(pos) turtle.forward(50 * (TT[i][0] + TT[i][1])) turtle.lt(90) turtle.forward(100 * TT[i][1]) turtle.write(Order_List[i][1], False, align="left") position[0] = turtle.pos() turtle.penup() ts = turtle.getscreen() ts.getcanvas().postscript(file=("Guide_Tree.eps")) turtle.done()
def position(x): if x == 1: t.setx(-80) t.sety(10) elif x == 2: t.sety(10) elif x == 3: t.setx(80) t.sety(10) elif x == 4: t.setx(-80) t.sety(-70) elif x == 5: t.sety(-70) elif x == 6: t.setx(80) t.sety(-70) elif x == 7: t.setx(-80) t.sety(-150) elif x == 8: t.sety(-150) elif x == 9: t.setx(80) t.sety(-150) else: print 'wrong input' t.pd() t.color('black', 'green') t.begin_fill() t.circle(25) t.end_fill() t.up() t.home() t.ht
def FuntionToPrint(x, y): turtle.setx(x) turtle.sety(y) print(x, y)
if (horizontal > 0 and horizontal % 2 != 0): pushTurtleForward() drawRect("white") if (horizontal > 0 and horizontal % 2 == 0): pushTurtleForward() drawRect("black") if (horizontal == 0): drawRect("black") else: for horizontal in range(0, 8): if (horizontal > 0 and horizontal % 2 != 0): pushTurtleForward() drawRect("black") if (horizontal > 0 and horizontal % 2 == 0): pushTurtleForward() drawRect("white") if (horizontal == 0): drawRect("white") for drawVertical in range(0, 8): turtle.setx(0) turtle.sety(40 * drawVertical) if (drawVertical % 2 == 0): drawHorizontal(inverted=True) else: drawHorizontal(inverted=False) turtle.setx(0) turtle.sety(0) turtle.done()
l.append(tartaruga) t.penup() ran = r.randrange(0,7) if ran == 0: t.color("red") elif ran == 1: t.color("yellow") elif ran == 2: t.color("blue") elif ran == 3: t.color("orange") elif ran == 4: t.color("purple") elif ran == 5: t.color("green") elif ran == 6: t.color("black") t.setx(0-(wndSize/2)) t.sety(cont+wndDivision) cont = cont + wndDivision l.append(t) uscita = True while uscita: for tartaruga in l: tartaruga.forward(r.randrange(0,16)) if tartaruga.xcor() > (wndSize/2): uscita = False print("turtle ") print(i+1) print("ha vinto!")
Poly(9, p) turtle.left(30) def makeTriple(): turtle.speed(44) for i in range(3): i = (i + 1) * 5 turtle.color(random.random(), random.random(), random.random()) makeFlower(i) xa = -255 y = -244 numberColumns = 5 numberRows = 5 turtle.penup() turtle.setpos(xa, y) for i in range(numberRows): for c in range(numberColumns): c = (c + 1) * 100 makeTriple() turtle.penup() turtle.sety(y + c) turtle.pendown() i = (i + 1) * 100 turtle.penup() turtle.setpos(xa + i, y) turtle.pendown()
import turtle turtle.circle(80) # 模拟了一个抬起来的效果 # turtle.pencolor('white') #更改笔的颜色 白色 # turtle.setx(-100) #更改坐标 # # turtle.pencolor('green') #笔的颜色 绿色 # turtle.fd(150) # 向前 150 # # 方法二 # up() 抬起笔 turtle.up() turtle.up() turtle.sety(-100) # down() 放下笔 turtle.down() turtle.down() turtle.fd(150) turtle.mainloop()
def initialise_turtle(): turtle.penup() turtle.setx(-50) turtle.sety(100) turtle.pendown()
def runTurtle(self): #CLEAR ANY PREVIOUS OUTPUT DATA self.box1.delete(0, 'end') #OPTIMIZE THE TURTLE WINDOW let = [] input1 = self.box.get() turtle.setup(width=1200, height=600) turtle.bgcolor("lightgreen") #SET INITIAL COORDINATES FOR THE TURTLE my_start = (1, 1) turtle.setx(my_start[0]) turtle.sety(my_start[1]) turtle.setheading(90) #GETTING AND PARSING THE USER INPUT COMMAND i = 0 for letter in input1: let.append(letter) i += 1 #EXECUTING EACH COMMAND AS PER THE RULES OF THE GAME for x in let: if x == 'f' or x == 'F': turtle.pendown() turtle.forward(1) #CHECKING THE PREVAILING OBSTACLE CONDITIONS AND BACKTRACKING IF THERE IS OBSTACLE IN THE PATH if (turtle.position() == (3.00, 3.00) or turtle.position() == (1.00, 6.00) or turtle.position() == (6.00, 5.00) or turtle.position() == (5.00, 6.00) or turtle.position() == (2.00, 7.00) or turtle.position() == (10.00, 8.00) or turtle.position() == (3.00, 9.00) or turtle.position() == (9.00, 2.00) or turtle.position() == (10.00, 1.00) or turtle.position() == (10.00, 10.00)): turtle.backward(1) print "Obstacle on the way, Cant move forward!!, current position is ", turtle.position( ) tkMessageBox.showinfo("Warning!!", "OBSTACLE DETECTED!!") turtle.penup() elif x == 'l' or x == 'L': turtle.pendown() turtle.left(90) turtle.penup() elif x == 'r' or x == 'R': turtle.pendown() turtle.right(90) turtle.penup() #EXECUTING WARNING FOR INVALID COMMAND INPUT else: print "Command could not be executed because ", x, " is an invalid command!!" tkMessageBox.showinfo("Warning!!", "INVALID COMMAND FOUND!!") #GETTING THE CURRENT DIRECTION OF THE TURTLE if (turtle.heading() == 0.0): print "Turtle is headed in EAST direction" direction = "E" elif (turtle.heading() == 90.0): print "Turtle is headed in NORTH direction" direction = "N" elif (turtle.heading() == 180.0): print "Turtle is headed in WEST direction" direction = "W" else: print "Turtle is headed in SOUTH direction" direction = "S" #PRINTING THE OUTPUT AS BOTH COMMAND LINE AS WELL AS IN THE GUI OUTPUT BOX str1 = turtle.position(), direction print "The final position of the turtle is ", turtle.position( ), " on XY plane in ", direction, " direction" self.box1.insert(0, str1)
import turtle radius = 90#eval(input("Enter a radius: ")) turtle.circle(radius) turtle.penup() turtle.setx(turtle.xcor()+(radius*2)) turtle.pendown() turtle.circle(radius) turtle.penup() turtle.sety(turtle.ycor()+(radius*2)) turtle.pendown() turtle.circle(radius) turtle.penup() turtle.setx(turtle.xcor()-(radius)*2) turtle.pendown() turtle.circle(radius) turtle.done()
#Jednoduchy program na kreslení from turtle import forward, right, left, exitonclick, shape, penup, pendown, setx, sety penup() sety(200) pendown() for i in range(18): for i in range(4): forward(50) left(90) left(20) for i in range(1): right(90) forward(150) # Kytička není dokončena chybí ji listy. penup() exitonclick()
import turtle as t t.speed(1000) t.ht() t.color('white') t.sety(-300) t.color('black') t.circle(300) t.color('white') t.sety(300) t.color('black') t.pensize(3) t.sety(300 - 20) t.color('white') t.goto(300, 0) t.color('black') t.setx(300 - 20) t.color('white') t.goto(0, -300) t.color('black') t.sety(-300 + 20) t.color('white') t.goto(-300, 0) t.color('black') t.setx(-300 + 20) t.color('white') t.goto(0, 0) t.color('black') b = 90 t.seth(b) t.fd(250)
def name(x, y): clearScreen() # position turtle turtle.penup() turtle.setx(-400) turtle.sety(400) turtle.pendown() # Z turtle.forward(150) turtle.right(180 - 45) turtle.forward(150 * math.sqrt(2)) turtle.left(180 - 45) turtle.forward(150) # position turtle turtle.penup() turtle.forward(30) turtle.pendown() # A a = math.sqrt(150**2 + 30**2) turtle.left(90 - 20) turtle.forward(a) turtle.right(180 - 40) turtle.forward(a) turtle.penup() line = 2 * (((a / 2) * math.sin(math.radians(20))) / math.sin(math.radians(90))) turtle.right(180) turtle.forward(a / 2) turtle.left(70) turtle.pendown() turtle.forward(line) # position turtle turtle.penup() turtle.right(180) turtle.forward(line) turtle.right(70) turtle.forward(a / 2) turtle.left(70) turtle.forward(30) turtle.pendown() # N turtle.left(90) turtle.forward(150) turtle.right(180 - 25) turtle.forward(150 / math.sin(math.radians(65))) turtle.left(180 - 25) turtle.forward(150) # position turtle turtle.penup() turtle.right(180) turtle.forward(150) turtle.left(90) turtle.forward(30) turtle.pendown() # D turtle.circle((150 // 2), 180) turtle.left(90) turtle.forward(150) # position turtle turtle.penup() turtle.left(90) turtle.forward((150 // 2) + 30) turtle.pendown() # E longLine = 100 shortLine = 60 turtle.left(90) turtle.forward(150) turtle.right(90) turtle.forward(longLine) turtle.penup() turtle.right(180) turtle.forward(longLine) turtle.left(90) turtle.forward(150 // 2) turtle.left(90) turtle.pendown() turtle.forward(shortLine) turtle.penup() turtle.right(180) turtle.forward(shortLine) turtle.left(90) turtle.forward (150 // 2) turtle.left(90) turtle.pendown() turtle.forward(longLine) # position turtle turtle.penup() turtle.forward(30) turtle.pendown() # R turtle.left(90) turtle.forward(150 // 2) turtle.right(90) turtle.circle((150 / 4), 180) turtle.left(90) turtle.forward(150 // 2) diagonal = math.sqrt((75**2) + (37.5**2)) angle = math.degrees(math.asin((150 / 4) / diagonal)) turtle.left(angle) turtle.forward(diagonal)