def drawfus_alt(): global basesize B = basesize ship = turtle.Shape("compound") mesh = ((1 * B, 0), (2 * B, 2 * B), (-2 * B, 0), (2 * B, -2 * B), (1 * B, 0)) ship.addcomponent(mesh, "black", "black") redship = turtle.Shape("compound") redship.addcomponent(mesh, "red", "red") turtle.register_shape('fusee', ship) turtle.register_shape('fusee reac', redship)
def register_tank_shape(screen, name, color): shape = turtle.Shape("compound") tank = turtle.Turtle(visible=False) tank.speed('fastest') tank.penup() tank.goto(-30, 30) tank.begin_poly() tank.goto(15, 30) tank.goto(15, 15) tank.goto(5, 15) tank.goto(5, 5) tank.goto(30, 5) tank.goto(30, -5) tank.goto(5, -5) tank.goto(5, -15) tank.goto(15, -15) tank.goto(15, -30) tank.goto(-30, -30) tank.goto(-30, -15) tank.goto(-15, -15) tank.goto(-15, 15) tank.goto(-30, 15) tank.goto(-30, 30) tank.end_poly() shape.addcomponent(tank.get_poly(), color) screen.register_shape(name, shape)
def createRobot(position, angle, robotColor, shadow=False): # # CREATES ROBOT SHAPE AND PLACES IN THE INITIAL POSITION # INPUTS: initial position (x, y), orientation, color(red for shadow, grey for real) # OUTPUTS: created turtle object # global tsize, shapelinesize, robotSize, turtleSpeed, turtlePos, turtleHead s = turtle.Shape("compound") cir = ((10, 0), (9.51, 3.09), (8.09, 5.88), (5.88, 8.09), (3.09, 9.51), (0, 10), (-3.09, 9.51), (-5.88, 8.09), (-8.09, 5.88), (-9.51, 3.09), (-10, 0), (-9.51, -3.09), (-8.09, -5.88), (-5.88, -8.09), (-3.09, -9.51), (-0.0, -10.0), (3.09, -9.51), (5.88, -8.09), (8.09, -5.88), (9.51, -3.09)) s.addcomponent(cir, robotColor, "black") if not shadow: line = ((0, 0), (0, 10)) s.addcomponent(line, "black", "black") turtle.Screen().register_shape(robotColor, s) robotTurtle = turtle.Turtle(visible=False) robotTurtle.shapesize(outline=shapelinesize) robotTurtle.shape(robotColor) robotTurtle.speed(turtleSpeed) robotTurtle.turtlesize(robotSize / tsize) robotTurtle.penup() robotTurtle.goto(position) if turtleHead is not None: robotTurtle.setheading(turtleHead) robotTurtle.left(angle) robotTurtle.showturtle() return robotTurtle
def Pen(self): sQuare = PhotoImage(file="images/block.gif").zoom(1, 1) self.myscreen.addshape("sQuare", turtle.Shape("image", sQuare)) self.pen = turtle.Turtle() self.pen.shape("sQuare") self.pen.penup() self.pen.speed(0)
def __init__(self, x, y, color, x2, y2, speed): self.color = color pen = turtle.Turtle(visible=False) pen.speed(0) pen.color(color) pen.penup() pen.setpos(x=x, y=y) pen.pendown() heading = pen.towards(x2, y2) pen.setheading(heading) pic_path = os.path.join(BASE_PATH, "images", "missile.gif") raw_pic = Image.open(pic_path).convert('RGBA') rotated_pic = raw_pic.rotate(angle=heading - 90, expand=1) pic = ImageTk.PhotoImage(rotated_pic) pic_name = f"pic_{heading - 90}" window.register_shape(pic_name, turtle.Shape("image", pic)) pen.shape(pic_name) pen.showturtle() self.pen = pen self.state = 'launched' self.target = x2, y2 self.radius = 0 self.speed = speed
def register_shape_from_path(path): with open(path, 'r') as f: lines = f.readlines() l1 = lines[0].split() name = l1[0] shape_radius_maybe = l1[-1] # it is the radius if the file is a skin shape = turtle.Shape("compound") for line_part in lines[1:]: line = line_part.split() color_in = line[1] color_edge = line[2] if line[0] == "poly": poly = [] assert len(line[3:]) % 2 == 0, "Inconsistent file: " + path for i in range(1, len(line) // 2): x, y = float(line[2 * i + 1]), float(line[2 * i + 2]) poly.append((x, y)) if "no_compound" in l1: # in order to change the color of the shape shape = tuple(poly) else: shape.addcomponent(poly, color_in, color_edge) elif line[0] == "circle": x_center, y_center = float(line[3]), float(line[4]) radius = float(line[5]) circle = make_circle((x_center, y_center), radius) shape.addcomponent(circle, color_in, color_edge) turtle.register_shape(name, shape) return name, shape_radius_maybe
def drawfus_alt(): global basesize B = basesize ship = turtle.Shape("compound") mesh = ((1*B,0), (2*B, 2*B), (-2*B,0), (2*B, -2*B), (1*B,0) ) ship.addcomponent(mesh, "#555555", "#555555") redship = turtle.Shape("compound") reaction = ((1.5*B, 1*B), (2*B, 0), (1.5*B, -1*B), (1*B, 0)) redship.addcomponent(mesh, "#555555", "#555555") redship.addcomponent(reaction, "yellow", "yellow") # réacteur, (c) Antonin turtle.register_shape('fusee', ship) turtle.register_shape('fusee reac', redship)
def loadImage(self, file): image = PhotoImage(file=file) self.screen.register_shape(file, turtle.Shape("image", image)) self.images[file] = image return file
def __init__(self): turtle.Turtle.__init__(self) goal = PhotoImage(file="images/goal.png").zoom(1, 1) myscreen.addshape("goal", turtle.Shape("image", goal)) self.shape("goal") self.penup() self.speed(0)
def register_shapes(): def rotate(point, angle, origin=(0, 0)): """ Rotate a point counterclockwise by a given angle around a given origin. The angle should be given in radians. """ ox, oy = origin px, py = point sa, ca = sin(angle), cos(angle) dx, dy = px - ox, py - oy qx = ox + ca * dx - sa * dy qy = oy + sa * dx + ca * dy return qx, qy def map_transform(points, angle=pi / 2, dx=0, dy=0, scale=1): return list( map( lambda x: rotate((scale * (x[0] + dx), scale * (x[1] + dy)), pi / 2), points)) def build_tank(side, objects): tank = turtle.Shape("compound") for o in objects: tank.addcomponent(*o) turtle.addshape("tank_" + side, tank) # Bullet b = turtle.Shape("polygon", ((-5, -5), (-5, 5), (5, 5), (5, -5))) turtle.addshape("bullet", b) circle = screen._shapes["circle"]._data # 3 wheels components = [] components.append((map_transform(circle, dy=-5), "black")) components.append((map_transform(circle, dx=20, dy=-5), "yellow")) components.append((map_transform(circle, dx=-20, dy=-5), "black")) # Tank body trapezoid = ((-35, 5), (35, 5), (30, 25), (-30, 25)) components.append((map_transform(trapezoid), "red")) # Tank turret / gun rectangle = ((30, 15), (30, 20), (60, 20), (60, 15)) components.append((map_transform(rectangle), "red")) build_tank("left", components) components.pop() components.pop() components.append((map_transform(trapezoid), "blue")) rectangle = ((-30, 15), (-30, 20), (-60, 20), (-60, 15)) components.append((map_transform(rectangle), "blue")) # Uncomment to draw bounding box for debugging #components.append((map_transform(circle, 0, 0, 0, 3), "yellow")) build_tank("right", components)
def reg_ship(): global basesize B = basesize ship = turtle.Shape("compound") mesh = ((1 * B, 0), (2 * B, 2 * B), (-2 * B, 0), (2 * B, -2 * B), (1 * B, 0)) ship.addcomponent(mesh, "#555555", "#555555") redship = turtle.Shape("compound") reaction = ((1.5 * B, 1 * B), (2 * B, 0), (1.5 * B, -1 * B), (1 * B, 0)) redship.addcomponent(mesh, "#555555", "#555555") redship.addcomponent(reaction, "yellow", "yellow") turtle.register_shape('lander', ship) turtle.register_shape('powered_lander', redship)
def __init__(self, compounds=[]): self.poly = compounds ground = turtle.Shape('compound') for poly in compounds: poly = tuple((-y, x) for (x, y) in poly) ground.addcomponent(poly, 'black') turtle.register_shape('ground', ground) super().__init__(0, 0, 0, 0, 'ground', 'black')
def makeShape(self): shape = turtle.Shape("compound") points = ((-self.height / 2, -self.width / 2), (-self.height / 2, self.width / 2), (self.height / 2, self.width / 2), (self.height / 2, -self.width / 2)) shape.addcomponent(points, ELEMENT_COLOR) SCREEN.register_shape("paddle", shape)
def drawground(): s = turtle.Shape("compound") ground = ((-320, 120), (-280, 41), (-240, 27), (-200, 59), (-160, 25), (-120, 43), (-80, 56), (-40, 20), (0, 20), (40, 20), (80, 44), (120, 28), (160, 66), (200, 29), (240, 64), (280, 34), (320, 140), (320, 0), (-320, 0)) s.addcomponent(ground, "black", "black") turtle.register_shape('ground', s)
def classTurtle(): tk = turtle.Pen() s = turtle.Shape("compound") poly = ((0, 0), (10, -5), (0, 10), (-10, -5)) s.addcomponent(poly, "red", "blue") print(tk.shape()) tk.shape(poly) print(tk.shape())
def getShape(): world = turtle.Shape("compound") for shape in AnimatedObject.shapes: for polyColorDic in shape.getPolyColorDics(): poly = polyColorDic['poly'] color = polyColorDic['color'] world.addcomponent(poly, color) turtle.register_shape("world", world) return "world"
def change_size(enlarge_picture, img_path, x, y): if enlarge_picture: larger = PhotoImage(file=img_path).subsample(x, y) else: larger = PhotoImage(file=img_path).subsample(x, y) wn.register_shape("larger", turtle.Shape("image", larger)) food_name = turtle.Turtle("larger") food_name.stamp() return food_name
def register(): a = turtle.Turtle() a.ht() a.speed(500) a.width(2) a.color(colour) a.begin_poly() a.goto(-10, -5) a.goto(0, 15) a.goto(10, -5) a.goto(0, 0) a.end_poly() b = turtle.Shape("polygon", a.get_poly()) turtle.register_shape("ship", b) a.begin_poly() a.penup() a.goto(0, 5) a.pendown() a.goto(0, 20) a.goto(0, 5) a.penup() a.goto(0, 0) a.pendown() a.end_poly() c = turtle.Shape("polygon", a.get_poly()) turtle.register_shape("shot", c) a.begin_poly() a.penup() a.goto(0, -10) a.pendown() a.circle(10) a.penup() a.goto(0, 0) a.pendown() a.end_poly() d = turtle.Shape("polygon", a.get_poly()) turtle.register_shape("target", d) a.clear()
def loadImage(self, file): try: image = PhotoImage(file=file) self.screen.register_shape(file, turtle.Shape("image", image)) self.images[file] = image return file except Exception as e: print(e) return None
def makeshape(): house = turtle.Shape("compound") wall = ((0, -5), (100, -5), (100, -60), (0, -60)) house.addcomponent(wall, "white", "black") door = ((80, -60), (80, -20), (55, -20), (55, -60)) house.addcomponent(door, "brown", "black") window = ((15, -22), (40, -22), (40, -45), (15, -45)) house.addcomponent(window, "blue", "black") roof = ((-10, -5), (110, -5), (50, 30)) house.addcomponent(roof, "red", "red") turtle.register_shape('house', house)
def makeshape_sun(scale): sun_shape = turtle.Shape("compound") shape = [] n = 100 for k in range(n): x = np.sin(2 * k * np.pi / n) y = np.cos(2 * k * np.pi / n) shape.append((x, y)) sun_shape.addcomponent(np.array(shape) * scale, "yellow") turtle.register_shape('sun', sun_shape)
def reg_enemy(): global basesize B = 2 * basesize enemyship = turtle.Shape("compound") enemy_mesh = ((0, -1 * B), (1 * B, -0.33 * B), (1 * B, 0.33 * B), (0, 1 * B), (-1 * B, 0)) left_antenna = ((-0.33 * B, -0.66 * B), (-B, -B), (-0.66 * B, -0.33 * B)) right_antenna = ((-0.33 * B, 0.66 * B), (-B, B), (-0.66 * B, 0.33 * B)) enemyship.addcomponent(enemy_mesh, "red", "green") enemyship.addcomponent(left_antenna, "red", "green") enemyship.addcomponent(right_antenna, "red", "green") turtle.register_shape("enemy", enemyship)
def makeshapes(): # Draw the 'car' shape using the turtle B = 25 # base unit size turtle.begin_poly() turtle.fd(B) # roof turtle.rt(45) turtle.fd(B * 3 / 4) # windshield turtle.lt(45) turtle.fd(B) # hood turtle.rt(90) turtle.fd(B * 3 / 4) # front turtle.rt(90) turtle.fd(B * 1 / 7) turtle.lt(90) turtle.circle(-B / 2, 180) # front tire turtle.lt(90) turtle.fd(B) turtle.lt(90) turtle.circle(-B / 2, 180) # back tire turtle.lt(90) turtle.fd(B * 1 / 7) turtle.rt(90) turtle.fd(B * 5 / 6) # back turtle.end_poly() poly = turtle.get_poly() turtle.register_shape('car', poly) # Draw the 'ball' shape as a simple circle. # (we need to move the position before drawing the circle, because turtle.circle() does not put the circle center at the initial position) turtle.begin_poly() turtle.left(90) turtle.forward(40) # radius turtle.left(90) turtle.circle(15, 360) # radius, angle turtle.end_poly() turtle.register_shape('ball', turtle.get_poly()) # Define the 'house' as a set of polygons, each of them being defined by its points house = turtle.Shape("compound") wall = ((0, -5), (100, -5), (100, -60), (0, -60)) house.addcomponent(wall, "white", "black") door = ((80, -60), (80, -20), (55, -20), (55, -60)) house.addcomponent(door, "brown", "black") window = ((15, -22), (40, -22), (40, -45), (15, -45)) house.addcomponent(window, "blue", "black") roof = ((-10, -5), (110, -5), (50, 30)) house.addcomponent(roof, "red", "red") turtle.register_shape('house', house)
def createSquares(screen, colors): """Creates square shapes in the given colors to be used as turtle graphics stamps in a cellular automaton. Parameters: screen: a Screen object colors: a list of color strings Return value: None """ square = ((0, 0), (0, SCALE), (SCALE, SCALE), (SCALE, 0)) for color in colors: squareShape = turtle.Shape('compound') squareShape.addcomponent(square, color, 'gray') screen.register_shape(color, squareShape)
def prepareMissile(self): temporary = turtle.Turtle() temporary.color(self.color) screen = turtle.getscreen() delay = screen.delay() screen.delay(0) temporary.hideturtle() temporary.penup() missile = turtle.Shape("compound") body = ((0, 2), (-4, 0), (-4, -12), (-8, -16), (-8, -20), (8, -20), (8, -16), (4, -12), (4, 0)) missile.addcomponent(body, self.color, 'black') fire = ((-2, -20), (0, -32), (2, -20)) missile.addcomponent(fire, 'red', 'red') name = 'missile_{0}'.format(self.color) turtle.addshape(name, shape=missile) del temporary screen.delay(delay)
def draw_elevator(self): # TODO: Make into shapes self.elevator = turtle.Shape('compound') # https://stackoverflow.com/questions/47144177/how-do-i-move-a-turtle-graphics-drawn-object-using-only-built-in-functions self.elevator = turtle.Turtle() self.elevator.penup() self.elevator.goto(self.initial_x, self.initial_y) self.elevator.color("red") self.elevator.pendown() self.elevator.forward(self.WIDTH) self.elevator.left(90) self.elevator.forward(self.WIDTH) self.elevator.left(90) self.elevator.forward(self.WIDTH) self.elevator.left(90) self.elevator.forward(self.WIDTH) self.elevator.left(90)
def wombotCursor(): temporary = turtle.Turtle() screen = turtle.getscreen() delay = screen.delay() screen.delay(0) temporary.hideturtle() temporary.penup() wombot = turtle.Shape("compound") #legs leg1 = polyDodec(temporary, ORIGIN, ORIGIN, LENGTH_LONG) wombot.addcomponent(leg1, BROWN, "black") leg2 = polyDodec(temporary, ORIGIN, FOOT, LENGTH_LONG) wombot.addcomponent(leg2, BROWN, "black") leg3 = polyDodec(temporary, FOOT, ORIGIN, LENGTH_LONG) wombot.addcomponent(leg3, BROWN, "black") leg4 = polyDodec(temporary, FOOT, FOOT, LENGTH_LONG) wombot.addcomponent(leg4, BROWN, "black") #body body = polyTwoHalfDodec(temporary, BODY_X, BODY_Y, LENGTH_BODY) wombot.addcomponent(body, BROWN, "black") #eyes eye1 = polyDodec(temporary, EYE_X, EYE_Y1, LENGTH_MED) wombot.addcomponent(eye1, "white", "black") pupil1 = polyDodec(temporary, PUP_X, PUP_Y1, LENGTH_SHORT) wombot.addcomponent(pupil1, "black", "black") eye2 = polyDodec(temporary, EYE_X, EYE_Y2, LENGTH_MED) wombot.addcomponent(eye2, "white", "black") pupil2 = polyDodec(temporary, PUP_X, PUP_Y2, LENGTH_SHORT) wombot.addcomponent(pupil2, "black", "black") eyelid1 = polyHalfDodec(temporary, LID_X, LID_Y1, LENGTH_MED1) wombot.addcomponent(eyelid1, BROWN, "black") eyelid2 = polyHalfDodec(temporary, LID_X, LID_Y2, LENGTH_MED1) wombot.addcomponent(eyelid2, BROWN, "black") #nose nose = polyRoundishTri(temporary, NOSE_X, NOSE_Y, LENGTH_TRI) wombot.addcomponent(nose, "black", "black") turtle.addshape("wombot", shape=wombot) del temporary
def compoundCar(length, width, color, name): shape = tr.Shape("compound") #body body = ((-.5*width,-.5*length),(-.5*width,.5*length),(.5*width,.5*length),(.5*width,-.5*length)) shape.addcomponent(body, color, "black") #tires tire1 = ((.55 * width, -.35 * length), (.55 * width, -.15 * length), (.45 * width, -.15 * length), (.45 * width, -.35 * length)) tire2 = ((-.55 * width, -.35 * length), (-.55 * width, -.15 * length), (-.45 * width, -.15 * length), (-.45 * width, -.35 * length)) tire3 = ((-.55 * width, .35 * length), (-.55 * width, .15 * length), (-.45 * width, .15 * length), (-.45 * width, .35 * length)) tire4 = ((.55 * width, .35 * length), (.55 * width, .15 * length), (.45 * width, .15 * length), (.45 * width, .35 * length)) shape.addcomponent(tire1, "black", "black") shape.addcomponent(tire2, "black", "black") shape.addcomponent(tire3, "black", "black") shape.addcomponent(tire4, "black", "black") #backglass backwindow = ((width*-.35,length*-.3),(width*-.35,length*-.2),(width*.35,length*-.2),(width*.35,length*-.3)) shape.addcomponent(backwindow, "darkgray","black") #frontglass frontwindow = ((width*-.40,length*.05),(width*-.40,length*.25),(width*.40,length*.25),(width*.40,length*.05)) shape.addcomponent(frontwindow, "lightgrey", "black") #light front frontlight1 = ((width * -.45, length * .48), (width * -.45, length * .44), (width * -.30, length * .44), (width * -.30, length * .48)) frontlight2 = ((width * .45, length * .48), (width * .45, length * .44), (width * .30, length * .44), (width * .30, length * .48)) shape.addcomponent(frontlight1, "yellow", "orange") shape.addcomponent(frontlight2, "yellow", "orange") #light back backlight1 = ((width * -.45, length * -.48), (width * -.45, length * -.44), (width * -.22, length * -.44), (width * -.22, length * -.48)) backlight2 = ((width * .45, length * -.48), (width * .45, length * -.44), (width * .22, length * -.44), (width * .22, length * -.48)) shape.addcomponent(backlight1, "crimson", "silver") shape.addcomponent(backlight2, "crimson", "silver") tr.register_shape(name+"shape",shape) return shape
def __init__(self, p1, p2, color_in="white", color_edge="black", static=True): """Create a rectangle object with opposite corners p1 and p2""" if (p1, p2) in Rectangle.rect_done: name = Rectangle.rect_done[(p1, p2)] else: shape = turtle.Shape("compound") poly = (p1, (p2[0], p1[1]), p2, (p1[0], p2[1])) shape.addcomponent(poly, color_in, color_edge) name = "rectangle" + str(Rectangle.rect_id) Rectangle.rect_id += 1 turtle.register_shape(name, shape) Rectangle.rect_done[(p1, p2)] = name super().__init__(0, 0, 0, 0, name, color_in, static)
def _robot_shape(): shape = turtle.Shape("compound") WIDTH = 15 HEIGHT = 20 def add_box(center, extents, fill="black", outline=None): low = (center[0] - extents[0], center[1] - extents[1]) high = (center[0] + extents[0], center[1] + extents[1]) poly = ( (low[0], low[1]), (low[0], high[1]), (high[0], high[1]), (high[0], low[1]), ) shape.addcomponent(poly, fill, outline) # body # add_box((-WIDTH, -HEIGHT), (WIDTH, HEIGHT), "blue", "black") add_box((0, 0), (WIDTH, HEIGHT), "blue", "black") # eyes SIZE_EYES = 5 add_box((WIDTH / 2, HEIGHT), (SIZE_EYES, SIZE_EYES), "white", "black") add_box((WIDTH / -2, HEIGHT), (SIZE_EYES, SIZE_EYES), "white", "black") # pupils SIZE_PUPILS = 2 add_box((WIDTH / 2, HEIGHT + SIZE_EYES - SIZE_PUPILS), (SIZE_PUPILS, SIZE_PUPILS)) add_box((WIDTH / -2, HEIGHT + SIZE_EYES - SIZE_PUPILS), (SIZE_PUPILS, SIZE_PUPILS)) # wheels SIZE_WHEEL = (3, 6) add_box((WIDTH, -1 + HEIGHT / 2), SIZE_WHEEL) add_box((WIDTH, -1 + HEIGHT / -2), SIZE_WHEEL) add_box((-WIDTH, -1 + HEIGHT / 2), SIZE_WHEEL) add_box((-WIDTH, -1 + HEIGHT / -2), SIZE_WHEEL) return shape