def main(): path_data = open('path.txt').read() print turtle.position() turtle.penup() turtle.setposition(-400,200) turtle.pendown() turtle.speed(0) turtle.delay(0) for c in path_data: if c in 'NSEW*': if c == 'N': turtle.setheading(90) turtle.forward(1) if c == 'S': turtle.setheading(270) turtle.forward(1) if c == 'E': turtle.setheading(0) turtle.forward(1) if c == 'W': turtle.setheading(180) turtle.forward(1) if c == '*': if turtle.isdown(): turtle.penup() else: turtle.pendown()
def bezier(smooth, x1, y1, x2, y2, x3, y3, *others): if len(others) % 2 != 0: print("Missing point data.") return if smooth < 1: print("Invalid smooth value") return wasDown = turtle.isdown() points = list(others) xval = [x1, x2, x3] + points[0:len(points):2] yval = [y1, y2, y3] + points[1:len(points):2] t, n, factn, step = 0, len(xval) - 1, factoral(len(xval) - 1), 1.0/smooth turtle.up() turtle.goto(x1, y1) if wasDown: turtle.down() while(t <= 1): x, y = 0, 0 for i in range(0, n+1): b = factn / ((factoral(i)) * (factoral(n - i))) #binomial coefficient k = ((1 - t) ** (n - i)) * (t ** i) #powers x += b * k * xval[i] #parametric application y += b * k * yval[i] #to x and y turtle.goto(x, y) t += step
def dessiner(chaine, distance, angle, position, direction, xpos, ypos): """Fonction qui dessine la chaine qu'on lui donne en entrée""" import turtle as tt import random as rd L = [] wasdown = tt.isdown() # on vérifie si il faut dessiner ou pas tt.penup() #on "lève le crayon" pour ne pas dessiner lorsque l'on initialise la tortue tt.setheading(direction) tt.goto(position ) #on commence à la position voulue et avec la direction voulue if wasdown: tt.pendown() tt.speed(0) #on prend la vitesse maximale de la tortue tt.hideturtle() #on cache le curseur for k in chaine: # On peut définir l'angle avec une borne inférieure et une borne supérieure if not tt.fill(): if type(angle) == tuple: theta = rd.uniform(angle[0], angle[1]) else: theta = angle if k == 'F': tt.fd(distance) #on fait un trait simple elif k == 'f': #on "lève le crayon" pour avancer sans tracer tt.penup() tt.fd(distance) if wasdown: tt.pendown() elif k == '+': #on tourne la tortue à gauche tt.left(theta) elif k == '-': tt.right(theta) #on tourne à droite elif k == '|': #on tourne de 180° tt.right(180) elif k == '[': #on enregistre la position et la direction dans une liste x, y = tt.pos() d = tt.heading() L.append((x, y, d)) #tuple composé de la position (x,y) et de la direction (d) elif k == ']': #on déplace à la dernière position enregistrée dans la liste tt.penup() x, y, d = L.pop() #on sort la dernière position enregistrée et on l'affecte à x,y,d tt.goto((x, y)) #on déplace la tortue à la position x,y tt.setheading(d) #et on lui donne la direction d if wasdown: tt.pendown() elif k == '{': tt.fillcolor('green') tt.begin_fill() elif k == '}': tt.end_fill() (x, y) = tt.pos() xpos.append(x) #liste des positions en x ypos.append(y) #et des positions en y return xpos, ypos #on renvoie les positions
def go_along(self, along): x = turtle.xcor() if (turtle.isdown()): turtle.penup() turtle.setx(x + int(along)) turtle.pendown() else: turtle.setx(x + int(along))
def go_down(self, down): y = turtle.ycor() if (turtle.isdown()): turtle.penup() turtle.sety(y + int(down)) turtle.pendown() else: turtle.sety(y + int(down))
def go_down(self, down): # Same idea to go_along currentY = turtle.ycor() if (turtle.isdown()): turtle.penup() turtle.sety(currentY + int(down)) turtle.pendown() else: turtle.sety(currentY + int(down))
def go_along(self, along): # setX is changing X to certain value, so instead of doing that, we want to move from current to new by adding value, not to that coordinate # e.g., turtle is at (100, 200), if we do go_along 200, we want to be at (300, 200) not (200,200) currentX = turtle.xcor() if (turtle.isdown()): turtle.penup() turtle.setx(currentX + int(along)) turtle.pendown() else: turtle.setx(currentX + int(along))
def move(x,y): global oldx global oldy penstatus = t.isdown() t.penup() t.goto(x,y) if penstatus == True: t.pendown() oldx = x oldy = y
def move(x, y): global oldx global oldy penstatus = t.isdown() # 현재 펜 업 / 다운 상태 저장 t.penup() t.goto(x, y) if penstatis == True: t.pendown() old = x old = y
def draw_line(self, direction, distance): # IF YOU WANT TO HAVE DYNAMIC DIRECTIONS YOU NEED TO UPDATE THIS CODE # direction = self._convert_direction(direction) if direction == 90 or direction == 270: direction -= 90 else: direction += 90 distance = int(distance) turtle.seth(direction) if (turtle.isdown()): turtle.forward(distance)
def draw(): global edge t.fd(edge) if t.isdown() == False: t.pendown() t.left(90) t.fd(edge) t.left(90) edge += step t.fd(edge) t.left(90) t.fd(edge) t.left(90)
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(): global edge t.fd(edge) if t.isdown() == False: t.pendown() t.left(90) t.fd(edge) t.left(90) edge += step t.fd(edge) t.left(90) t.fd(edge) t.left(90) edge += step t.setup(600, 400, 20, 20) t.penup()
def on_key(key: KeyMsg): if key.key == 'up': turtle.setheading(90) turtle.forward(10) if key.key == 'right': turtle.setheading(0) turtle.forward(10) if key.key == 'left': turtle.setheading(180) turtle.forward(10) if key.key == 'down': turtle.setheading(-90) turtle.forward(10) if key.key == 'home': if turtle.isdown(): turtle.penup() else: turtle.pendown()
def rectangle(length, width, x = None, y = None, color = "black", fill = False): wasDown = turtle.isdown() turtle.up() if x is not None and y is not None: turtle.setpos(x, y) if wasDown: turtle.down() if fill: turtle.color(color, color) turtle.begin_fill() else: turtle.pencolor(color) turtle.goto(turtle.xcor() + length, turtle.ycor()) turtle.goto(turtle.xcor(), turtle.ycor() + width) turtle.goto(turtle.xcor() - length, turtle.ycor()) turtle.goto(turtle.xcor(), turtle.ycor() - width) if fill: turtle.end_fill()
def draw_node(node): if t.pos() == (0.0, 0.0): t.penup() else: t.pendown() t.goto(node.x_pos, node.y_pos + 2 * node.radius) t.goto(node.x_pos, node.y_pos) text_size = 18 // len(node.label) + 12 t.color('Dark Blue') t.begin_fill() if not t.isdown(): t.pendown() t.circle(node.radius, extent=None, steps=100) t.end_fill() t.penup() t.color('yellow') t.goto(node.x_pos, node.y_pos + node.radius - (text_size // 2)) t.write(node.label, align='center', font=('Times New Roman', text_size)) t.goto(node.x_pos, node.y_pos + node.radius - 5 * (text_size // 2)) t.write(node.tree_id, align='center', font=('Times New Roman', text_size)) t.goto(node.x_pos, node.y_pos) t.color('black')
def chessboard(xstart, ystart, side, color = "black", background = "white"): wasDown = turtle.isdown() turtle.up() turtle.setpos(xstart, ystart) if wasDown: turtle.down() turtle.pensize(3) polygon(side, 4, 0) turtle.pensize(1) colorToggle = True step = side / 8.0 for y in range(0, 8): if y % 2 == 0: colorToggle = True else: colorToggle = False for x in range(0, 8): if colorToggle: polygon(step, 4, 0, xstart + (x * step), ystart + (y * step), color, True) else: polygon(step, 4, 0, xstart + (x * step), ystart + (y * step), background, True) colorToggle = not colorToggle
def plot_point(x, data_point, dot_color): """ Plots an individual point. If the turtle is down, a line will be drawn to the point. """ pensize = turtle.pensize() pencolor = turtle.pencolor() down = turtle.isdown() y = data_point / __MAX_Y * __Y_LENGTH turtle.setpos(x, y) turtle.write(str(data_point)) turtle.down() turtle.pensize(3) turtle.pencolor(dot_color) turtle.dot() # restore the turtle turtle.pensize(pensize) turtle.pencolor(pencolor) if not down: turtle.up()
def polygon(side, numberSides, angle = None, xstart = None, ystart = None, color = "black", fill = False): if numberSides < 3: print("Invalid number of sides.") return wasDown = turtle.isdown() turtle.up() if xstart is not None and ystart is not None: turtle.setpos(xstart, ystart) if angle is not None: turtle.setheading(angle) if wasDown: turtle.down() if fill: turtle.color(color, color) turtle.begin_fill() else: turtle.pencolor(color) degrees = 180 - (((numberSides - 2) * 180.0) / numberSides) for x in range(0, numberSides): turtle.forward(side) turtle.left(degrees) if fill: turtle.end_fill()
def space(): if turtle.isdown(): turtle.penup()
def space(): if turtle.isdown(): turtle.up() else: turtle.down()
def forwardPenup(dist): if not turtle.isdown(): turtle.forward(dist) else: print("pen is down")
def forwardPenup(dist): if not turtle.isdown(): turtle.forward(dist)
def pen_up_down(): if t.isdown(): t.penup() else: t.pendown()
turtle.setheading(180 - angle) turtle.setposition(-2*width - tx,ty) elif tx > width: angle = turtle.heading() if angle < 90 or angle > 270: turtle.setheading(180 - angle) turtle.setposition(2*width - tx,ty) if ty < -height: angle = turtle.heading() if angle > 180: turtle.setheading(- angle) turtle.setposition(tx,-2*height - ty) elif ty > height: angle = turtle.heading() if angle < 180: turtle.setheading( - angle) turtle.setposition(tx,2*height - ty) if b: colour += 1 colour %= len(colours) turtle.pencolor(colours[colour]) if a: if turtle.isdown(): turtle.penup() else: turtle.pendown()
def switch(): if t.isdown(): t.pu() else : t.pd()
# turtle.right(100) # turtle.forward(100) # turtle.right(135) # turtle.forward(100) # turtle.left(45) # turtle.forward(100) # turtle.hideturtle() # turtle.home() # turtle.clear() # turtle.circle(100) # # turtle.setposition(0,100) # turtle.setposition(100,100) # turtle.setposition(100,0) # turtle.setposition(0,0) turtle.setposition(200, 0) turtle.setposition(100, 100) turtle.setposition(0, 0) turtle.penup() turtle.pendown() turtle.isdown()
#pull the pen up while drawing turtle.penup() turtle.pu() turtle.up() turtle.down() #turtle.speed(1) print turtle.pensize() turtle.pensize(10) turtle.fd(100) turtle.width(5) turtle.pen(pencolor='red') turtle.bk(200) print sorted(turtle.pen().items()) print turtle.isdown() turtle.up() print turtle.isdown() turtle.pencolor('yellow') print turtle.colormode() turtle.width(1) turtle.fillcolor('blue') turtle.color('red', 'green') turtle.reset() turtle.goto(100, 100) turtle.clear() turtle.write('Home = ', True, align='center') x = turtle.xcor()
# turtle. isdown() # return True if pen is down, False if it’s up. import turtle turtle = turtle.Turtle() #to chech whether the pen is down or not turtle.penup() print(turtle.isdown()) #to chech whether the pen is down or not turtle.pendown() print(turtle.isdown())
def turtleToggle(): if turtle.isdown(): turtle.pu() else: turtle.pd()
def maybe_forward(distance): if turtle.isdown(): turtle.forward(distance)
angle = turtle.heading() if angle > 90 and angle < 270: turtle.setheading(180 - angle) turtle.setposition(-2 * width - tx, ty) elif tx > width: angle = turtle.heading() if angle < 90 or angle > 270: turtle.setheading(180 - angle) turtle.setposition(2 * width - tx, ty) if ty < -height: angle = turtle.heading() if angle > 180: turtle.setheading(-angle) turtle.setposition(tx, -2 * height - ty) elif ty > height: angle = turtle.heading() if angle < 180: turtle.setheading(-angle) turtle.setposition(tx, 2 * height - ty) if b: colour += 1 colour %= len(colours) turtle.pencolor(colours[colour]) if a: if turtle.isdown(): turtle.penup() else: turtle.pendown()
def togglePen(): # if the pen is "down", penup() if turtle.isdown(): turtle.penup() else: turtle.pendown()