Пример #1
0
class App:
    def __init__(self):
        self.win = tk.Tk()
        self.win.geometry("640x400")
        self.canvas = Canvas()
        self.canvas.pack()
        self.scr = TurtleScreen(self.canvas)

        self.t = RawTurtle(self.scr)
        self.btn = Button(self.win, text="Press me!", command=self.do_action())
        self.btn.pack()

    def do_action(self):
        self.t.pensize(3)
        self.t.pencolor(random.choice(colors))
        self.t.speed(0)

        self.length = 5
        while self.length < 500:
            self.t.left(89)
            self.t.forward(self.length)
            self.t.right(-105)
            self.t.forward(self.length)
            self.length += 3

    def run(self):
        self.win.mainloop()
Пример #2
0
def draw_maze(maze,
              scale,
              *,
              screen=None,
              tracer=False,
              delay=0,
              speed=0,
              updates=False,
              start_x=0,
              start_y=0):
    if screen is None:
        screen = Screen()

    width = scale * maze.width
    height = scale * maze.height

    original_tracer = screen.tracer()
    original_delay = screen.delay()

    screen.tracer(tracer)
    screen.delay(delay)

    turtle = RawTurtle(screen, visible=False)
    turtle.speed(speed)
    turtle.setpos(start_x, start_y)
    turtle.setheading(0)
    turtle.showturtle()

    x, y, dx, dy = 0, 0, 1, 0

    while True:
        sx, sy = rotate_right(dx, dy)

        print(x, y, maze[x, y])

        if maze[x, y][sx, sy]:
            if maze[x, y][rotate_left(sx, sy)]:
                print('a')
                turtle.forward(scale - 1)
                turtle.left(90)
                dx, dy = rotate_left(dx, dy)
            else:
                print('b')
                turtle.forward(scale)
                x, y = x + dx, y + dy
        else:
            print('c')
            turtle.right(90)
            turtle.forward(1)
            dx, dy = rotate_right(dx, dy)
            x, y = x + dx, y + dy

        if (x, y, dx, dy) == (0, 0, 1, 0):
            break

    screen.tracer(original_tracer)
    screen.delay(original_delay)
    screen.update()

    return screen
Пример #3
0
class App :
    def __init__(self):
        self.win = Tk()
        self.btn = Button(self.win, text="확인", command=self.press)
        self.btn.pack()

        self.canvas = Canvas(self.win)
        self.canvas.config(width=600, height=300)
        self.canvas.pack()
        self.src = TurtleScreen(self.canvas)
        self.t = RawTurtle(self.src)
        self.t.pensize(5)

        # 서버와 소켓 연결
        self.client = socket(AF_INET, SOCK_STREAM)
        self.client.connect(('127.0.0.1',9999))


    def move(self):
        print("터틀 제어하기")
        # 서버에서 받은 데이터로 터틀을 제어한다.
        data = self.client.recv(1024)
        obj = json.loads(data.decode('utf-8'))
        direction = obj['direction']
        angle = obj['angle']
        self.t.left(angle) if direction=='L' else self.t.right(angle)
        self.t.forward(obj['length'])


    def press(self):
        self.client.send('I am Client^^'.encode('utf-8'))
        print("서버에 메세지 보내기")
        # 터틀 제어하기
        self.move()
Пример #4
0
def lihtne_demo():
    #tekitame objekti, mis kuulub klassi Screen
    ekraan = Screen()
    #tekitame konna objekti, mis kuulub klassi RawTrutle  ja on ekraani peal
    konn = RawTurtle(ekraan)
    konn.forward(100)
    #tekitame teise konna
    kermit = RawTurtle(ekraan)
    kermit.color("green")
    kermit.left(30)
    kermit.forward(50)
    #liigutame esimest konna
    konn.back(100)
Пример #5
0
def main():
    root = TK.Tk()
    cv1 = TK.Canvas(root, width=300, height=200, bg="#ddffff")
    cv2 = TK.Canvas(root, width=300, height=200, bg="#ffeeee")
    cv1.pack()
    cv2.pack()

    s1 = TurtleScreen(cv1)
    s1.bgcolor(0.85, 0.85, 1)
    s2 = TurtleScreen(cv2)
    s2.bgcolor(1, 0.85, 0.85)

    p = RawTurtle(s1)  #上面的乌龟
    q = RawTurtle(s2)  #下面的乌龟

    p.color("red", (1, 0.85, 0.85))
    p.width(3)
    q.color("blue", (0.85, 0.85, 1))
    q.width(3)

    for t in p, q:
        t.shape("turtle")
        t.left(36)

    q.left(180)

    for t in p, q:
        t.begin_fill()

    for i in range(5):  #画五边形
        for t in p, q:
            t.forward(50)
            t.left(72)

    for t in p, q:
        t.end_fill()
        t.left(54)
        t.penup()
        t.back(50)

    return "EVENTLOOP"
Пример #6
0
class Sierpinski:
    def __init__(self, x):
        self.it = x
        S = "E+D+D"
        for i in range(self.it):
            pesan = ""
            for j in range(len(S)):
                if S[j] == "E":
                    pesan += "E+D-E-D+E"
                elif S[j] == "D":
                    pesan += "DD"
                else:
                    pesan += S[j]
            S = pesan
        root3 = tk.Tk()
        if self.it != 1:
            root3.title('Sierpinski Fractal with ' + str(self.it) +
                        ' iterations')
        else:
            root3.title('Sierpinski Fractal with an iteration')
        self.canvas = ScrolledCanvas(master=root3, width=1000, height=1000)
        self.canvas.pack(fill=tk.BOTH, expand=tk.YES)
        screen = TurtleScreen(self.canvas)
        screen.screensize(10000, 10000)
        self.turtle = RawTurtle(screen)
        self.turtle.ht()
        self.turtle.speed(0)
        for i in range(len(S)):
            if S[i] == "E" or S[i] == "D":
                self.turtle.forward(10)
            elif S[i] == "+":
                self.turtle.left(120)
            else:
                self.turtle.right(120)
        self.canvas.bind('<MouseWheel>', self.zoom)
        screen.mainloop()

    def zoom(self, event):
        amount = 0.9 if event.delta < 0 else 1.1
        self.canvas.scale(tk.ALL, 0, 0, amount, amount)
    def draw(list_rectangles, list_squares):
        """
        Opens a window and draws all the Rectangles and Squares using turtle module

        Args:
            list_rectangles (list): list of rectangles to draw
            list_squares (list): list of squares to draw
        """

        screen = Screen()
        screen.setup()
        screen.bgcolor("black")
        colors = ["cyan", "red", "blue", "white",
                  "purple", "green", "brown", "#285078"]
        square = RawTurtle(screen)
        rectangle = RawTurtle(screen)
        # square.speed(10)
        # rectangle.speed(10)
        for sq in list_squares:
            square.penup()
            square.home()
            square.color(random.choice(colors))
            square.goto(sq.x, sq.y)
            square.pendown()
            square.begin_fill()
            i = 0
            while i < 4:
                square.forward(sq.size)
                square.left(90)
                i += 1
            square.end_fill()
        square.hideturtle()

        for rect in list_rectangles:
            rectangle.penup()
            rectangle.home()
            rectangle.color(random.choice(colors))
            rectangle.goto(rect.x, rect.y)
            rectangle.pendown()
            i = 0
            while i < 2:
                rectangle.forward(rect.width)
                rectangle.left(90)
                rectangle.forward(rect.height)
                rectangle.left(90)
                i += 1
        rectangle.hideturtle()
        done()
Пример #8
0
class Robot:

    def __init__(self, scene, robot_id):
        self.robot_id = robot_id
        self.done = False
        self.signal = None

        self.turtle = RawTurtle(scene.canvas)
        self.scene = scene

        self.scr = self.turtle.getscreen()
        self.scr.setworldcoordinates(0, scene.height, scene.width, 0)

        self.turtle.penup()
        self.reset()

    def reset(self):
        positions = [((15,15), 45), 
                    ((self.scene.width-15, 15), 135),
                    ((15, self.scene.height-15), 315),
                    ((self.scene.height-15, self.scene.width-15), 135)]

        self.turtle.speed(0)
        self.turtle.setpos(positions[self.robot_id][0])
        print positions[self.robot_id]
        self.turtle.left(positions[self.robot_id][1])
        self.turtle.forward(20)
        self.turtle.speed("normal")

    def process(self):
        ##sets variables for checking collision
        turtle_x = self.turtle.xcor()
        turtle_y = self.turtle.ycor()
        t_heading = self.turtle.heading()
        ##variables used to check right
        xr = turtle_x + 15*math.cos(math.radians(self.turtle.heading()+30))
        yr = turtle_y + 15*math.sin(math.radians(self.turtle.heading()+30))
        ##variables used to check left
        xl = turtle_x + 15*math.cos(math.radians(self.turtle.heading()-30))
        yl = turtle_y + 15*math.sin(math.radians(self.turtle.heading()-30))
        ##turns the robot at window boundries
        st_orient = [0, 90, 180, 270]
        bounce_d = 20
        if turtle_x - bounce_d < 0:
            self.turtle.setheading(180-t_heading)
        if turtle_x + bounce_d > self.scene.width:
            self.turtle.setheading(180-t_heading)
        if turtle_y - bounce_d < 0:
            self.turtle.setheading(360-t_heading)
        if turtle_y + bounce_d > self.scene.height:
            self.turtle.setheading(360-t_heading)
        ##check collisions
        left = self.scene.canvas.find_overlapping(xl-1,yl-1,xl+1,yl+1)
        right = self.scene.canvas.find_overlapping(xr-1,yr-1,xr+1,yr+1)
        if self.goal_id in left + right:
            self.done = True
        elif left:
            ##turn away
            self.turtle.left(10)
        elif right:
            ##turn away
            self.turtle.right(10)
        else:
            ##else move forward
            self.turtle.forward(5)
        ##if goal not reached:
        if self.signal:
            if self.signal == "STOP":
                self.signal = None
                return
        elif not self.done:
            self.scene.master.after(20, self.process)
Пример #9
0
from guizero import App, Drawing
from turtle import RawTurtle

app = App()
drawing = Drawing(app)
turtle = RawTurtle(drawing.tk)
turtle.forward(100)
turtle.left(720)

app.display()
Пример #10
0
class TurtleCanvas():
    def __init__(self,canvas):
        #self.window = master
        #self.canvas = ScrolledCanvas(master=self.window, width=800, height=600)
        #self.canvas.pack(fill=tk.BOTH, expand=tk.YES)
        self.canvas = canvas
        self.screen = TurtleScreen(canvas)
        self.turtle = RawTurtle(self.screen)
        self.turtle.speed("fastest")
        #self.window.geometry('%dx%d+%d+%d' % (cWidth, cHeight, x, y))
        self.canvas.bind('<MouseWheel>', self.zoom)
        self.canvas.bind("<ButtonPress-1>", self.scroll_start)
        self.canvas.bind("<B1-Motion>", self.scroll_move)
        self.canvas.bind("<ButtonPress-3>", self.changeDirection)
        #self.canvas.bind("<c>", self.changeColor)
        self.rightDirection = True
    def changeDirection(self,event):
        #print(self.rightDirection)
        if(self.rightDirection):
            self.rightDirection = False
        else:
            self.rightDirection = True
    def changeColor(self,event):
        currentColorIndex = colors.index(self.turtle.color()[0])
        if (currentColorIndex == (len(colors) - 1)):
            self.turtle.color(colors[0])
        else:
            self.turtle.color(colors[currentColorIndex + 1])
    def scroll_start(self,event):
        self.canvas.scan_mark(event.x, event.y)

    def scroll_move(self,event):
        self.canvas.scan_dragto(event.x, event.y, gain=1)
    def zoom(self,event):
        amount = 0.9 if event.delta < 0 else 1.1
        self.canvas.scale(tk.ALL, 0, 0, amount, amount)

    def square(self,sidelength = 50):
        for i in range(4):
            self.turtle.forward(sidelength)
            self.turtle.right(90)
    def triangle(self,sidelength = 50):
        for i in range(3):
            self.turtle.forward(sidelength)
            self.turtle.right(120)

    def star(self,sidelength = 50):
        for i in range(5):
            self.turtle.forward(sidelength)
            self.turtle.right(144)

    def shapeDriver(self, shapeFunc, steps):
        self.turtle.st()
        i = 0
        for j in range(steps):
            shapeFunc(1 + i)
            if(self.rightDirection == True):
                self.turtle.right(1)
            else:
                self.turtle.left(1)
            i += 0.1
        self.turtle.ht()
    def helperDriver(self, shape, steps, color):
        print(color)
        self.turtle.color(color)
        if(shape == "Square"):
            self.shapeDriver(self.square,steps)
        if(shape == "Triangle"):
            self.shapeDriver(self.triangle,steps)
        if(shape == "Star"):
            self.shapeDriver(self.star,steps)
Пример #11
0
from turtle import TurtleScreen, RawTurtle, TK

root = TK.Tk()
root.title("Ejemplo 1")
canvas = TK.Canvas(root, width=500, height=500)
canvas.pack()

turtle = RawTurtle(screen)

turtle.shape("turtle")

turtle.left(20)

turtle.forward(50)
turtle.left(90)
turtle.forward(50)
turtle.left(90)
turtle.forward(50)
turtle.left(90)
turtle.forward(50)
turtle.left(90)
delay(1000)
turtle.left(30)

turtle.forward(50)
turtle.left(90)
turtle.forward(50)
turtle.left(90)
turtle.forward(50)
turtle.left(90)
turtle.forward(50)
Пример #12
0
# The second is color we want to use to fill the figure
pen1.color("misty rose", "violet")
pen1.width(3)

# Configuring the second pen
pen2.color("violet", "misty rose")
pen2.width(3)

# Drawing the figure
pen1.begin_fill()
pen2.begin_fill()
pen1.goto(0, 100)
pen2.goto(0, 100)
pen1.goto(0, -50)
pen2.goto(0, -50)
pen2.left(40)
pen1.left(140)
pen2.forward(100)
pen1.forward(100)
for side in range(185):
    pen2.forward(1)
    pen1.forward(1)
    pen2.left(1)
    pen1.right(1)

# Filling the figure
pen2.end_fill()
pen1.end_fill()

# Deleting the arrows of the pens when the figure is finished
pen2.hideturtle()
Пример #13
0
# tess.left(3600)
# tess.right(-90)
# tess.left(3600)
# tess.left(3645)
# tess.forward(-100)

#----------------------------------------------------------------------
# Challenge Problems 1
# Implement a loop within a loop that draws the pattern shown in the Lab05
# description. It is created by drawing a set of square where each square
# is orientated 30 degrees from the previous square.
#----------------------------------------------------------------------

for i in range(4):
    mary.forward(25)
    mary.left(90)
    mary.forward(50)
    mary.left(90)
    mary.forward(25)
    mary.left(90)
    mary.forward(25)
    mary.left(90)
    mary.forward(50)

    for j in range(4):
        mary.forward(50)
        mary.left(90)

    mary.up()
    mary.setpos(0, 0)
    mary.down()
class Robot:
    def __init__(self, scene, robot_id):
    #sets variables                     
        self.robot_id = robot_id
        self.turtle = RawTurtle(scene.canvas)
        self.scene = scene
        self.scr = self.turtle.getscreen()
        self.scr.setworldcoordinates(0, scene.height, scene.width, 0)
        self.turtle.penup()
        if robot_id == 1:
            self.turtle.color("blue")
        else:
            self.turtle.color("red")
    #create turtles sprite
##        self.turtle.register_shape("ship",((-7,-2),(-6,-1),(-3,0),(-3,1),(-2,5),
##                                           (0,7),(2,5),(3,1),(3,0),(6,-1),(7,-2),
##                                           (3,-1),(3,-2),(2,-7),(-2,-7),(-3,-2),
##                                           (-3,-1),(-2,-1),(-2,-2),(-1,-6),(1,-6),
##                                           (2,-2),(2,-1),(-2,-1),(-2,0),(2,0),
##                                           (2,1),(1,4),(0,5),(-1,4),(-2,1),
##                                           (-2,-1),(-3,-1))
##                         )
##        self.turtle.shape("ship")
##        self.turtle.shapesize(2,2)
    #place robot using reset
        self.reset()

    def reset(self):
    #set start positions for robots
        positions = [((15,15), 45), 
                    ((self.scene.width-15, 15), 135),
                    ((15, self.scene.height-15), 315),
                    ((self.scene.height-15, self.scene.width-15), 135)]
    #move robot to starting possition
        self.turtle.speed(0)
        self.turtle.setpos(positions[self.robot_id][0])
        #print positions[self.robot_id]
        self.turtle.left(positions[self.robot_id][1])
        self.turtle.forward(20)
        self.turtle.speed(0)
        self.turtle.screen.bgcolor("sky blue")

    def orientate(self, landmarks, canvas, goal):
##sd = shortest distance
##x1,x2,y1,y2 = circles corners
##lx,ly = length x/y
##h = hypothinus
##ln = landmark number
        sd = 40000000
        ln = 0
        
        for ID in landmarks:
            if canvas.itemcget(ID, "fill") == "dark green":
                ln+=1
                x1,y1,x2,y2 = canvas.coords(ID)
                lx = ((x1+x2)/2) - self.turtle.xcor()
                ly = ((y1+y2)/2) - self.turtle.ycor()
                h = math.sqrt(lx*lx + ly*ly)
                if h < sd:
                    sd = h
                    stored_ID = ID
                    stored_x = lx
                    stored_y = ly
        
        if ln == 0:
            stored_ID = goal
            x1,y1,x2,y2 = canvas.coords(goal)
            lx = ((x1+x2)/2) - self.turtle.xcor()
            ly = ((y1+y2)/2) - self.turtle.ycor()
            sd = math.sqrt(lx*lx + ly*ly)
            stored_x = ((x1+x2)/2) - self.turtle.xcor()
            stored_y = ((y1+y2)/2) - self.turtle.ycor()
        
        if sd < 37:
            return stored_ID
        
        if stored_x < 0:
            if stored_y < 0:
                new_heading = 180 + math.degrees(math.atan((-stored_y)/(-stored_x)))
            else:
                new_heading = 180 - math.degrees(math.atan(stored_y/(-stored_x)))
        elif stored_y < 0:
            new_heading = 360 - math.degrees(math.atan((-stored_y)/stored_x))
        else:
            new_heading = math.degrees(math.atan(stored_y/stored_x))

        self.turtle.seth(new_heading)
        return False

    def collisions_move(self, speed, depth):
    ##breaks the recursion if the robots get to close
        if depth > 10:
            return
    ##sets variables for checking collision
        turtle_x = self.turtle.xcor()
        turtle_y = self.turtle.ycor()
        t_heading = self.turtle.heading()
    ##variables used to check right
        xr = turtle_x + 15*math.cos(math.radians(self.turtle.heading()+30))
        yr = turtle_y + 15*math.sin(math.radians(self.turtle.heading()+30))
    ##variables used to check left
        xl = turtle_x + 15*math.cos(math.radians(self.turtle.heading()-30))
        yl = turtle_y + 15*math.sin(math.radians(self.turtle.heading()-30))
    ##check for the collision
        left = self.scene.canvas.find_overlapping(xl-1,yl-1,xl+1,yl+1)
        right = self.scene.canvas.find_overlapping(xr-1,yr-1,xr+1,yr+1)
        if left:
            ##turn away
            self.turtle.left(20)
            self.collisions_move(speed, depth+1)
            #self.turtle.forward(speed/5)
        elif right:
            ##turn away
            self.turtle.right(20)
            self.collisions_move(speed, depth+1)
            #self.turtle.forward(speed/5)
        else:
            ##else move forward
            self.turtle.forward(speed)
        return
Пример #15
0
turtle_canvas = TurtleScreen(my_canvas)
turtle_canvas.bgcolor("white")

# Create a turtle to draw on the canvas
sammy = RawTurtle(turtle_canvas)

# If you want the drawing to be as fast as possible, uncomment these lines
# turtle_canvas.delay(0)
# sammy.hideturtle()
# sammy.speed("fastest")

# Draw squadron NN patch (your code goes here)

#get turtle in position for first side of triangle
sammy.up()
sammy.left(90)
sammy.forward(50)
sammy.right(90)

#draw first side of triangle
sammy.down()
sammy.forward(75)
sammy.backward(150)

#draw second side of triangle
sammy.left(300)
sammy.forward(150)

#draw final side of triangle
sammy.left(120)
sammy.forward(150)