def main(): screen.mode("logo") t = Turtle(shape="triangle") t.penup(); t.back(280); t.pendown() t.pensize(3) itree(t, 250, 0.63, ["black", "brown", "red", "orange", "violet", "lightblue"])
def main(): screen.mode("logo") t = Turtle(shape="triangle") t.penup() t.back(280) t.pendown() t.pensize(3) itree(t, 250, 0.63, ["black", "brown", "red", "orange", "violet", "lightblue"])
def ShapeT(): from turtle import Turtle, Screen, done from time import sleep Screen().clear() t = Turtle() t.hideturtle() t.left(90) t.forward(200) t.right(90) t.forward(50) t.back(100) sleep(1) test() done()
def ShapeV(): from turtle import Turtle, Screen, done from time import sleep Screen().clear() t = Turtle() t.hideturtle() t.up() t.back(50) t.down() t.right(55) t.forward(250) t.left(115) t.forward(250) sleep(1) test() done()
def ShapeQ(): from turtle import Turtle, Screen, done from time import sleep Screen().clear() t = Turtle() t.hideturtle() t.circle(100) t.up() t.right(-45) t.forward(50) t.down() t.left(90) t.back(150) sleep(1) test() done()
def ShapeX(): from turtle import Turtle, Screen, done from time import sleep Screen().clear() t = Turtle() t.hideturtle() t.left(90) t.forward(200) t.left(90) t.forward(50) t.back(50) t.right(-90) t.forward(200) t.left(-270) t.circle(90, extent=-90) sleep(1) test() done()
def shoot(): if ready: bullet = Turtle(shape='circle') bullet.penup() bullet.shapesize(ball_size, ball_size, 0) bullet.hideturtle() bullet.setpos(you.xcor(), you.ycor()) bullet.color("yellow") bullet.showturtle() bullet.speed(10) if you.shape() == "dog_right.gif": bullet.forward(100) elif you.shape() == "dog_left.gif": bullet.back(100) global ball_list ball_list.append(bullet) if len(ball_list) > ball_count: for each in range(0, len(ball_list)): ball_list[each].hideturtle() ball_list.clear()
from turtle import Turtle, Screen import colorgram import random colors = colorgram.extract("day-18/image.jpg", 30) new_color = [] for i, color in enumerate(colors): new_color.append(tuple(colors[i].rgb)) print(random.choice(new_color)) turtle = Turtle() screen = Screen() screen.colormode(255) screen.setworldcoordinates(0, 0, 500, 500) turtle.penup() turtle.speed("fastest") for i in range(10): for j in range(10): turtle.dot(20, random.choice(new_color)) turtle.forward(50) turtle.left(90) turtle.forward(50) turtle.right(90) turtle.back(500) screen.exitonclick()
t.penup() t.goto(cached_x, cached_y) t.pendown() ##if __name__ == "__main__": ## for path in paths: ## instructions = parse_path(path) ## for inst in instructions: ## print(inst) ## complete_instruction(inst) # In case the SVG parsing isn't done in time. I am heartbroken t.penup() t.left(90) t.back(300) t.right(90) t.pendown() t.circle(300) t.penup() t.left(90) t.forward(450) t.left(90) t.forward(80) t.pendown() t.circle(80) t.penup() t.right(180) t.forward(160) t.pendown() t.circle(80)
tulip.forward(270) # tulip.dot(20, random.choice(color_list)) # tulip.right(90) # for _ in range(12): # tulip.forward(50) # tulip.dot(20, random.choice(color_list)) tulip.right(90) tulip.forward(50) for _ in range(10): tulip.dot(20, random.choice(color_list)) tulip.forward(50) for _ in range(10): tulip.back(500) tulip.right(90) tulip.forward(50) tulip.left(90) for _ in range(10): tulip.dot(20, random.choice(color_list)) tulip.forward(50)
(3, 213, 207), (159, 33, 24), (8, 140, 85), (145, 227, 217), (122, 193, 147), (220, 177, 216), (100, 218, 229), (117, 171, 192), (79, 135, 178)] def random_color(): return random.choice(color_list) t = Turtle() s = Screen() t.hideturtle() t.penup() t.speed(0) t.setpos(-100, -100) s.title("Spot Painting by Arihant") s.colormode(255) for _ in range(10): for _ in range(10): # Prints 1 row of dots t.pendown() t.dot(20, random_color()) t.penup() t.forward(50) t.penup() t.back(50) t.sety(t.pos()[1] + 50) t.right(180) s.exitonclick()
@author: 小周 """ ''' ======================= 绘制龟兔赛跑现场 ======================= ''' from turtle import Turtle, ontimer, mainloop rabbit = Turtle() rabbit.hideturtle() rabbit.shape('turtle') rabbit.up() rabbit.back(300) rabbit.left(50) rabbit.showturtle() rabbit.down() def rabbitMove(): if usedTime < 50: # 奔跑 rabbit.forward(3) elif usedTime < 550: # 睡觉,位移不变 rabbit.setheading(0) rabbit.forward(0.5) else: # 追赶 rabbit.setheading(50) rabbit.forward(4)
(46, 122, 86), (72, 43, 35), (145, 178, 148), (13, 99, 71), (233, 175, 164), (161, 142, 158), (105, 74, 77), (55, 46, 50), (183, 205, 171), (36, 60, 74), (18, 86, 90), (81, 148, 129), (148, 17, 20), (14, 70, 64), (30, 68, 100), (107, 127, 153), (174, 94, 97), (176, 192, 209) ] timmy = Turtle() screen = Screen() screen.colormode(255) timmy.speed(5) # timmy starting position: timmy.penup() timmy.back(200) timmy.right(90) timmy.forward(300) timmy.left(90) timmy.pendown() def hirst_painting(): for i in range(100): timmy.dot(20, choice(color_list)) timmy.penup() timmy.forward(50) timmy.pendown() if timmy.xcor() > 220: timmy.left(90)
class Maze: def __init__(self, row, col, xCoor=-375, yCoor=350, blockSize=25, hardPoints=True): self.row = row self.col = col self.startXCoor = xCoor self.startYCoor = yCoor self.blockSize = blockSize self.currentlyDrawing = False # to prevent drawing when currently drawing self.currentTurn = 0 # keep track of how many moves # initializing for start and end blocks Coordinates (usefull for randomly placed blocks) self.startBlockCoors = list() self.endBlockCoors = list() # initializing for Current x and y in array, very useful for walking through the maze self.currentArrayLocation = list() #Make a X by Y 2d array filled with 0's self.blocks = [[0 for x in range(self.col)] for y in range(self.row)] # Fill all blocks as closed for i in range(self.row): for j in range(self.col): self.blocks[i][j] = Block('closed', blockSize) # Setting up Turtle for drawing self.t = Turtle() self.t.screen.bgcolor('peach puff') self.t.color("dark slate gray") self.t.shape("turtle") self.t.speed(0) # Turn off animations to make it go fast self.t.screen.tracer(0) # move starting drawing point to coordinates self.t.up() self.t.goto(xCoor, yCoor) self.t.down() # make the maze pathway itself self.makePaths(hardPoints) # draw what is inside the 2d array self.drawMaze() # update the screen after making changes (I disabled automatically updating the screen to make it draw instantly) self.t.screen.update() # Turns back on animation and puts turtle at starting block self.setUpForSolving() # Draws a block and moves the cursor to bottom left hand corner of new block, facing east of new def drawBlock(self, block, MovedOn=None, stopColoring=False): self.currentlyDrawing = True # For drawing blocks normally if MovedOn is None: # Draws a block if it is closed, start, or end if block.state in ["closed", 'start', 'end', 'wall']: self.t.color(block.color) self.t.begin_fill() # Draw the square for _ in range(4): self.t.fd(block.size) self.t.right(90) self.t.end_fill() # Move to bottom left corner of block just made self.t.up() self.t.right(90) self.t.fd(block.size) self.t.down() self.t.left(90) else: # For drawing blocks when walking through the maze if block.state in ['closed', 'wall']: raise ValueError("Can't move on a Wall or closed Block!") else: self.currentTurn += 1 block.turnMovedOn = self.currentTurn # set color of block if (block.visited <= 1): self.t.color('yellow') elif (block.visited == 2): self.t.color('goldenrod') elif (block.visited == 3): self.t.color('chocolate') elif (block.visited == 4): self.t.color('saddle brown') elif (block.visited == 5): self.t.color('maroon') else: self.t.color('black') # Draw the square if (stopColoring): self.t.up() else: self.t.down() self.t.begin_fill() for _ in range(4): self.t.fd(block.size) self.t.right(90) if (not stopColoring): self.t.end_fill() # move to top right left of block just made self.t.up() self.t.fd(block.size) self.currentlyDrawing = False # set it back to False # Calls Draw for each block def drawMaze(self): # Go through list and draw blocks for i in range(self.row): for j in range(self.col): self.drawBlock(self.blocks[i][j]) # Move to the next Column self.t.up() self.t.fd(self.blocks[i][j].size) self.t.left(90) self.t.fd(self.blocks[i][j].size * (j + 1)) self.t.right(90) self.t.down() # Maze Code based on: #https://github.com/Malmactor/maze-solver/blob/master/src/generator.py #Which uses Prim's method: #https://en.wikipedia.org/wiki/Maze_generation_algorithm def makePaths(self, hardPoints=True): maze_r = self.row maze_c = self.col # Set edges as wall for i in range(self.row): for j in range(self.col): if (i == 0 or j == 0 or i == (self.row - 1) or j == (self.col - 1)): self.blocks[i][j].state = 'wall' self.blocks[i][j].color = 'black' # Puts starting point in the top left corner if hardPoints: # For starting in the top left corner start_x = 0 start_y = 1 else: # for starting in random location start_x = np.random.random_integers(maze_r) - 1 start_y = np.random.random_integers(maze_c) - 1 # Store coordinates of starting block self.startBlockCoors.append(start_x) self.startBlockCoors.append(start_y) # declare it a start block and make it green self.blocks[start_x][start_y].state = 'start' self.blocks[start_x][start_y].color = 'green' start = Point(start_x, start_y) # iterate through direct neighbors of node frontier = list() for x in range(-1, 2): for y in range(-1, 2): if x * y == 0 and x + y != 0: if start.r + x >= 0 and start.r + x < maze_r and start.c + y >= 0 and start.c + y < maze_c: frontier.append(Point(start.r + x, start.c + y, start)) last = None # Initializing last for later # Go through current neighbors while frontier: # pick current neighbor at random current = frontier.pop( np.random.random_integers(len(frontier)) - 1) opposite = current.opposite() # if both current neighbor and its opposite are walls if opposite.r >= 0 and opposite.r < maze_r and opposite.c >= 0 and opposite.c < maze_c: if self.blocks[current.r][ current.c].state == 'closed' and self.blocks[ opposite.r][opposite.c].state == 'closed': # open path between the nodes self.blocks[current.r][current.c].state = 'open' self.blocks[opposite.r][opposite.c].state = 'open' # store last node in order to mark it later last = opposite # iterate through direct neighbors of current node, same as earlier for x in range(-1, 2): for y in range(-1, 2): if x * y == 0 and x + y != 0: if opposite.r + x >= 0 and opposite.r + x < maze_r and opposite.c + y >= 0 and opposite.c + y < maze_c: frontier.append( Point(opposite.r + x, opposite.c + y, opposite)) # if maze is finished, there will be no more neighbors if last: if hardPoints: # setting exit to be on the bottom right hand corner self.blocks[self.row - 1][self.col - 3].state = 'end' self.blocks[self.row - 1][self.col - 3].color = 'red' # set end block coordinates self.endBlockCoors.append(self.row - 1) self.endBlockCoors.append(self.col - 3) else: # If you want the exit to move around self.blocks[last.r][last.c].state = 'end' self.blocks[last.r][last.c].color = 'red' self.endBlockCoors.append(last.r) self.endBlockCoors.append(last.c) def setUpForSolving(self): # This function goes to the starting block and sets it up for someone to start finding a way to the end self.t.up() # To go back to starting block, move to starting coordinate and the away by how many blocks it started by x = self.startXCoor + (self.blockSize * self.startBlockCoors[0]) y = self.startYCoor - (self.blockSize * self.startBlockCoors[1]) # Adding visiting logic to starting block for later self.blocks[self.startBlockCoors[0]][ self.startBlockCoors[1]].visited += 2 # go to that location self.t.goto(x, y) self.t.fd(self.blockSize) self.t.down() # Gets current x and y coordinates of the block in the array self.currentArrayLocation.append(self.startBlockCoors[0]) self.currentArrayLocation.append(self.startBlockCoors[1]) # change turtle color self.t.color("yellow") self.t.shape("turtle") self.t.speed(0.5) # Turn animation back on self.t.screen.tracer(1) # This Function moves the turtle 1 block to the right if it is not a wall # Return True if it moved, False if it didn't def moveRight(self): nextBlock = self.blocks[self.currentArrayLocation[0] + 1][self.currentArrayLocation[1]] #currentArrayLocation[0] == x, + 1 for the block to the right of current block #self.currentArrayLocation[1] == y #Won't move past a wall if nextBlock.state == 'open' and self.currentlyDrawing is False: self.currentArrayLocation[0] += 1 # update current location nextBlock.visited += 1 # draw the block # (Make sure you are on the top left corner of the block you want to draw) self.drawBlock(nextBlock, True) def moveLeft(self): nextBlock = self.blocks[self.currentArrayLocation[0] - 1][self.currentArrayLocation[1]] if nextBlock.state == 'open' and self.currentlyDrawing is False: self.currentArrayLocation[0] -= 1 # update current location nextBlock.visited += 1 # Make sure you are on the top left corner of the block you want to draw) self.t.up() self.t.back(self.blockSize * 2) self.t.down() self.drawBlock(nextBlock, True) def moveDown(self): nextBlock = self.blocks[self.currentArrayLocation[0]][ self.currentArrayLocation[1] + 1] if nextBlock.state == 'open' and self.currentlyDrawing is False: self.currentArrayLocation[1] += 1 # update current location nextBlock.visited += 1 # Make sure you are on the top left corner of the block you want to draw) self.t.up() self.t.back(self.blockSize) self.t.right(90) self.t.fd(self.blockSize) self.t.left(90) self.t.down() self.drawBlock(nextBlock, True) def moveUp(self): nextBlock = self.blocks[self.currentArrayLocation[0]][ self.currentArrayLocation[1] - 1] if nextBlock.state == 'open' and self.currentlyDrawing is False: self.currentArrayLocation[1] -= 1 # update current location nextBlock.visited += 1 # Make sure you are on the top left corner of the block you want to draw) self.t.up() self.t.back(self.blockSize) self.t.left(90) self.t.fd(self.blockSize) self.t.right(90) self.t.down() self.drawBlock(nextBlock, True) def moveDirection(self, direction=""): if (direction == 'right'): self.moveRight() elif (direction == "left"): self.moveLeft() elif (direction == "up"): self.moveUp() elif (direction == "down"): self.moveDown() # returns the type of the block that is next to the current block, returns block instead if parameter is True def checkSpace(self, direction="", returnBlock=False): if (direction == "right"): if (returnBlock is False): return self.blocks[self.currentArrayLocation[0] + 1][ self.currentArrayLocation[ 1]].state # ["closed", 'start', 'end', 'wall','open']: else: return self.blocks[self.currentArrayLocation[0] + 1][self.currentArrayLocation[1]] elif (direction == "left"): if (returnBlock is False): return self.blocks[self.currentArrayLocation[0] - 1][self.currentArrayLocation[1]].state else: return self.blocks[self.currentArrayLocation[0] - 1][self.currentArrayLocation[1]] elif (direction == "up"): if (returnBlock is False): return self.blocks[self.currentArrayLocation[0]][ self.currentArrayLocation[1] - 1].state else: return self.blocks[self.currentArrayLocation[0]][ self.currentArrayLocation[1] - 1] elif (direction == "down"): if (returnBlock is False): return self.blocks[self.currentArrayLocation[0]][ self.currentArrayLocation[1] + 1].state else: return self.blocks[self.currentArrayLocation[0]][ self.currentArrayLocation[1] + 1] else: return "invalid" """
from turtle import Turtle, Screen rabbit = Turtle() rabbit.shape("turtle") rabbit.color(0.3, 0.4, 0.5) rabbit.penup() rabbit.back(500) for i in range(96): if i & 1: rabbit.pendown() else: rabbit.penup() rabbit.forward(10) rabbit.shape("classic") rabbit.stamp() rabbit.shape("turtle") rabbit.hideturtle() screen = Screen() screen.exitonclick()
print(f'{espacos} Ok {nome} vamos jogar um pouco! {espacos}') print(f'{espacos} Você vai movimentar a tartaruga! {espacos}') jogar = True ft = False de = False while jogar == True: frente_tras = input( 'Você quer ir com a tartaruga para frente ou para trás? (Responda "f" para frente e "t" para trás) ') if frente_tras == 'f' or frente_tras == 'frente': pf = int(input('Quantos px você quer que ela ande?: ')) tartaruga.forward(pf) else: pt = int(input('Quantos px você quer que ela ande?: ')) tartaruga.back(pt) direita_esquerda = input( 'Você quer ir com a tartaruga para direira ou para esquerda? (Responda "d" para direita e "e" para esuqerda)') if direita_esquerda in 'd' or 'direita': graus = int(input('Quantos graus você quer que ela vire?: ')) tartaruga.right(graus) p = int(input('Quantos px você quer que ela ande?: ')) tartaruga.forward(p) else: graus = int(input('Quantos graus você quer que ela vire?: ')) tartaruga.left(graus) p = int(input('Quantos px você quer que ela ande?: ')) tartaruga.forward(p)
from turtle import Turtle, Screen, mainloop, TurtleScreen michael = Turtle() screen = Screen() def print_something(): print("PRINT") def forward(): michael.forward(10) screen.onkeyrelease(forward, "w") screen.onkeyrelease(lambda: michael.back(10), "s") screen.onkeyrelease(lambda: michael.right(2), "d") screen.onkeyrelease(lambda: michael.left(2), "a") def clear(): michael.clear() michael.penup() # michael.goto(0, 0) # michael.setheading(0) michael.home() michael.pendown() screen.onkeyrelease(clear, "c")