python - 你如何让乌龟与正方形的某些区域重叠?
问题描述
所以我必须熟悉海龟才能与学校的一个项目。除了重叠正方形之外,我基本上得到了教授要求的所有东西。
但是他希望正方形像这样重叠
我根本无法复制这个。我想知道我是否需要将它们放入我的代码中以轻松解决它。
这是我的代码
import turtle #Imports the 'turtle module' which allows intricate shapes and pictures to be drawn
my_turtle_pos = (10 , 10)
def square(my_turtle,x,y,length) : #I set up a function that helps me determine the square
my_turtle.penup() #Picks 'up' the turtle pen
my_turtle.setposition(x-length/2,y-length/2) #Helps set positon
my_turtle.pendown() #Puts 'down' the turtle pen
my_turtle.color('black','red') #Allows black outline, with red filling
my_turtle.begin_fill() #Starts the filling of red and helps remember the starting point for a filled area
my_turtle.forward(length) #Moves the turtle by the specified amount 'length'
my_turtle.left(90) #Moves the turtle by given amount '90'
my_turtle.forward(length)
my_turtle.left(90)
my_turtle.forward(length)
my_turtle.left(90)
my_turtle.forward(length)
my_turtle.left(90)
my_turtle.end_fill() #Stops filling with red, which will close with the current color
def graphic_pattern(my_turtle,x,y,length,times): #recursive function
if times <= 0: #This just tells us how many 'times' it needs to repeat till given amount
return
newSize = length/2.2 #This will grab the new size
graphic_pattern(my_turtle,x-length/2,y-length/2,newSize,times-1) #Functions to help with writing 'smaller' squares
graphic_pattern(my_turtle,x-length/2,y+length/2,newSize,times-1)
graphic_pattern(my_turtle,x+length/2,y-length/2,newSize,times-1)
graphic_pattern(my_turtle,x+length/2,y+length/2,newSize,times-1)
square(my_turtle,x,y,length)
my_turtle = turtle.Turtle(shape="arrow") #You can use differen't shapes for the turtle, I chose arrow, though the turtle was cool :)
my_turtle.speed(100) #I am not sure how fast the turtle can go, I just chose 100 cause it went by quicker.
graphic_pattern(my_turtle,3,0,300,4) #Example pattern stated from homework assignment.
我认为这与笔首先绘制正方形的位置有关。感谢您的任何意见!
解决方案
听起来这是您graphic_pattern()
和square()
方法的简单排序。似乎您想先绘制右上角graphic_pattern()
,然后是中间的正方形,然后是其余的graphic_pattern()
调用:
import turtle #Imports the 'turtle module' which allows intricate shapes and pictures to be drawn
my_turtle_pos = (10 , 10)
def square(my_turtle,x,y,length) : #I set up a function that helps me determine the square
my_turtle.penup() #Picks 'up' the turtle pen
my_turtle.setposition(x-length/2,y-length/2) #Helps set positon
my_turtle.pendown() #Puts 'down' the turtle pen
my_turtle.color('black','red') #Allows black outline, with red filling
my_turtle.begin_fill() #Starts the filling of red and helps remember the starting point for a filled area
my_turtle.forward(length) #Moves the turtle by the specified amount 'length'
my_turtle.left(90) #Moves the turtle by given amount '90'
my_turtle.forward(length)
my_turtle.left(90)
my_turtle.forward(length)
my_turtle.left(90)
my_turtle.forward(length)
my_turtle.left(90)
my_turtle.end_fill() #Stops filling with red, which will close with the current color
def graphic_pattern(my_turtle,x,y,length,times): #recursive function
if times <= 0: #This just tells us how many 'times' it needs to repeat till given amount
return
newSize = length/2.2 #This will grab the new size
graphic_pattern(my_turtle,x+length/2,y+length/2,newSize,times-1)
square(my_turtle,x,y,length)
graphic_pattern(my_turtle,x-length/2,y-length/2,newSize,times-1)
graphic_pattern(my_turtle,x-length/2,y+length/2,newSize,times-1)
graphic_pattern(my_turtle,x+length/2,y-length/2,newSize,times-1)
my_turtle = turtle.Turtle(shape="arrow") #You can use differen't shapes for the turtle, I chose arrow, though the turtle was cool :)
my_turtle.speed(100) #I am not sure how fast the turtle can go, I just chose 100 cause it went by quicker.
graphic_pattern(my_turtle,3,0,300,4) #Example pattern stated from homework assignment.
推荐阅读
- python - 具有分水岭的图像分割:过度分割和合并连接区域
- python - reticulate 包 - 更改 python 版本
- java - 获取 bean 后对象为空
- javascript - 是否可以在关闭窗口时删除本地存储项?
- javascript - 使用 javascript/vue 的产品过滤器排序最高或最低
- php - spatie/laravel-translatable 5.6 无法翻译 slug 并抛出 404
- accelerated-mobile-page - 将用户同意传递给 amp 页面中的嵌套 iframe
- javascript - if else 语句 jquery(附加到不同的选项卡)
- java - 为什么我的程序无法检测到超过 90 的分贝?
- ubuntu - ubuntu 18.04 中的 Wacom ONE 数位板驱动程序