def juhuslikud_konnad(n=100, t=100): """ Tekitab n juhuslikult liikuvat konna """ ekraan = Screen() konnad = [] for i in range(n): # Paigutame konna i juhuslikele koordinaatidele konn_i = RawTurtle(ekraan) konn_i.up() x = randint(-100, 100) y = randint(-100, 100) konn_i.goto(x, y) konn_i.down() # Värvime konna juhusliku värviga varv = choice(['green', 'brown', 'black', 'red', 'blue']) konn_i.color(varv) # Lisame konna i konnade hulka konnad.append(konn_i) # Teeme t juhuslikku liikumist juhusliku konnaga for ti in range(t): suunamuutus = randint(-60, 60) konn = choice(konnad) konn.left(suunamuutus) konn.fd(20)
def juhuslikud_konnad(n=10, t=10000): """ tekitab n juhuslike konne """ ekraan = Screen() konnad = [] for i in range(n): #paigutame konna i juhuslikudele koordinaatidele konn_i = RawTurtle(ekraan) konn_i.up() x = randint(-100, 100) y = randint(-100, 100) konn_i.goto(x, y) konn_i.down() #lisame konna i konnade hulka konnad.append(konn_i) #varvilised konnad varv = choice(['green', 'brown', 'black', 'blue', 'red', 'yellow']) konn_i.color(varv) #lisame varvilisi konne konnad.append(konn_i) #teeme tee juhuslikku liikumist juhusliku konnaga for ti in range(t): suunamuutus = randint(-61, 61) juhuslikk_indeks = randint(0, n - 1) konn = choice(konnad) konn.left(suunamuutus) konn.fd(20)
def pen(self): turt = RawTurtle(self.screen) turt.ht() turt.speed(0) turt.up() turt.goto(-self.width, self.height) turt.down() return turt
def draw(ans): """Вывод топологической схемы""" global xax, moves, oboz, window turx, tury = xax - int(xax*0.3), int(yax*0.25) bg_def = window.cget('bg') canvas = Canvas(window, relief='sunken', borderwidth=5, width=turx, height=tury, background=bg_def) canvas.place(relx=0.5, rely=0.1, y=int(yax*0.19), anchor=CENTER) screen = TurtleScreen(canvas) global turtle turtle = RawTurtle(canvas) turtle.speed(5) turtle.hideturtle() allen = (turx - int(xax*0.2)) global let if len(ans) > 7: if ans[5] == 1 and ans[8] == 1: let = allen // 3 else: if ans[5] == 1: let = allen // 4 elif ans[8] == 1: let = allen // 4 else: let = allen // 5 if ans[5] == 2: allen -= let//2 if ans[8] == 2: allen -= let//2 else: if ans[5] == 1: let = allen // 2 else: let = allen // 3 if ans[5] == 2: allen -= let//2 turtle.color('white') turtle.up() turtle.goto(x=-allen // 2, y=-int(yax*0.09)) turtle.down() turtle.color('black') turtle.dot(5, 'black') turtle.write('A') for i in [moves[k + 1][x] for k, x in enumerate(ans) if k + 1 in moves]: print(i) eval(i)
def draw(lines): from tkinter import Tk, LEFT from turtle import Canvas, RawTurtle, TurtleScreen # set up the environment root = Tk() canvas = Canvas(root, width=800, height=800) canvas.pack() s = TurtleScreen(canvas) t = RawTurtle(canvas) t.speed(0) t.width(1) for line in lines: x, y = line[0] t.up() t.goto(x * 800 / 1024 - 400, -(y * 800 / 1024 - 400)) for point in line: t.down() t.goto(point[0] * 800 / 1024 - 400, -(point[1] * 800 / 1024 - 400)) s.mainloop()
# set up canvas canvas = Canvas(root, width=WIDTH, height=HEIGHT, highlightthickness=0) turtle_screen = TurtleScreen(canvas) turtle_screen.bgcolor("black") canvas.pack() # set up turtle turt = RawTurtle(turtle_screen) turt.hideturtle() turt.speed(0) turt.pencolor("RED") turt.width(3) turt.up() turt.setx(START_POS[0]) turt.sety(START_POS[1]) turt.down() farthest_pos = [0, 0] def update_pos(): tpos = turt.pos() pos = [tpos[0] - START_POS[0], tpos[1] - START_POS[1]] if abs(pos[0]) > abs(farthest_pos[0]): farthest_pos[0] = pos[0] if abs(pos[1]) > abs(farthest_pos[1]): farthest_pos[1] = pos[1] def draw_fractal(n = 0): if n <= 0:
def leftDragon(t, sz, level): if level == 0: t.left(90) t.fd(sz) else: leftDragon(t, sz * .8, level - 1) t.left(90) rightDragon(t, sz * 8, level - 1) def rightDragon(t, sz, level): if level == 0: t.fd(sz) t.right(90) else: leftDragon(t, sz * .8, level - 1) t.right(90) rightDragon(t, sz * .8, level - 1) window = Screen() window.setup(1400, 800, 50, 50) t = RawTurtle(window) t.up() t.home() print(t.pen()) t.down() draw_line(t) window.mainloop()
class Board: """ Area of the screen dedicated to the connect 4 game board. """ def __init__(self,screen,corners,width,height,x,y): self.corners = corners self.screen = screen self.pen = RawTurtle(screen) self.pen.speed(8) self.width = width self.height = height self.x = x self.y = y self.spaces = [] self.draw() self.draw_spaces() def draw(self): self.pen.up() self.pen.goto(self.corners[-1]) self.pen.color("#ddd") self.pen.down() self.pen.begin_fill() for i in self.corners: self.pen.goto(i) self.pen.ht() self.pen.end_fill() return def check_winner(self,space): r, c = space.idx if self.check_row(space,r,c) or \ self.check_column(space,r,c) or \ self.check_direction(space,r,c): return True return False def check_row(self,space,r,c): if space.state*4 in "".join([str(i.state) for i in self.spaces[r]]): return True def check_column(self,space,r,c): if space.state*4 in "".join([str(i[c].state) for i in self.spaces]): return True def check_direction(self,space,r,c): direct,score = [(-1,-1),(-1,1),(1,1),(1,-1)],[0,0] for i,(x,y) in enumerate(direct): idx = 0 if i % 2 == 0 else 1 score[idx] += self.check_angle(space,r,c,x,y,(x,y)) if max(score) >= 3: return True return False def check_angle(self,space,r,c,x,y,i): if r+x >= 0 and r+x < 6 and c+y >= 0 and c+y < 7: if self.spaces[r+x][c+y].state == space.state: return 1 + self.check_diag(space,r,c,x+i[0],y+i[1],i) return 0 return 0 def animate_drop(self,space): r,c = space.idx for i in range(r): self.spaces[i][c].draw() self.spaces[i][c].remove() return def space_empty(self,space): r,c = space.idx if r == len(self.spaces)-1 or self.spaces[r+1][c].state: return space for row in range(r+1,len(self.spaces)): if self.spaces[row][c].state: return self.spaces[row-1][c] return self.spaces[len(self.spaces)-1][c] def find_space(self,x,y): for row in self.spaces: cent,rad = row[0].center, row[0].radius if y > cent[1] - rad and y < cent[1] + rad: return self.search_column(row,x) return False def search_column(self,row,x): for space in row: x2 = space.center[0] if x > x2-space.radius and x < x2 + space.radius: return space return False def draw_spaces(self): row,size = [],self.width/7 radius = (size*.9)/2 x,y = self.corners[0] for j in range(6): for i in range(7): space_x = x + (size*i) space_y = y - (size*j) center = space_x+(size/2),space_y-(size/2) color = "#643" idx = (j,i) space = Space(self,center,radius,color,idx) row.append(space) self.spaces.append(row) row = [] return
# 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() mary.right(22.5) #---------------------------------------------------------------------- # Challenge Problem 2 # Implement a program to draw the pattern shown in the Lab05 description. #---------------------------------------------------------------------- # Keep the window open until the user closes it. TK.mainloop()
class Stage: @classmethod def create(cls, screen): stage = cls(screen) start = screen.start blocks = screen.blocks for i in range(blocks): stop = start + screen.blockwidth stage.positions.append((start,stop)) base = -screen.base height = screen.increment * (i+1) color = next(screen.gradient) block = Block(screen, base=base, index=i, height=height, parent=stage, color=color) stage.blocks.append(block) block.draw() start = stop + 1 stage.get_pen() return stage def append(self, other): if other.index: other.clear() l = len(self) other.index = (l) self.blocks.append() def slice(self, *args): if len(args) > 2: raise Exception if len(args) == 2: start, stop = args if len(args) == 1: start, stop = 0, args[0] if len(args) == 0: start, stop = 0, len(self.blocks) stage = Stage(self.screen) for idx, i in enumerate(range(start,stop)): stage.positions.append(self.positions[i]) block = self.blocks[i].new() block.index = idx stage.blocks.append(block) return stage def __init__(self, screen): self.screen = screen self.positions = [] self.blocks = [] self.operations = 0 self.pen = None def get_pen(self): self.pen = RawTurtle(self.screen) self.pen.color("#f0d1bf") xpos = 0 ypos = self.screen.height - 30 self.pen.up() self.pen.ht() self.pen.goto(xpos, ypos) self.pen.down() def __getitem__(self, idx): return self.blocks[idx] def __setitem__(self, idx, other): if other.index not in [idx, None]: other.clear() self.blocks[other.index] = None if self.blocks[idx] != None: self.blocks[idx].clear() self.blocks[idx].delindex() self.blocks[idx] = other self.blocks[idx].setindex(idx) other.draw() def __str__(self): return str([i.value for i in self.blocks]) def __repr__(self): return str(self) def __len__(self): return len(self.blocks) def __iter__(self): self.iterable = iter(self.blocks) return self.iterable def __next__(self): try: block = next(self.iterable) return block except StopIteration: raise StopIteration
# 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) #setup and draw the 2 sammy.up() sammy.setposition(0, 0) sammy.down()