Python Turtle模块 - 创建一个标志

时间:2017-10-15 02:53:28

标签: python-3.x turtle-graphics

我试图在海龟模块中创建一个标志,其边界由点(-200,-200),(0,-200),(0,0)和(-200,0)限定。 然后我想在(100,200)中随机设置宽度参数。但是,我坚持这一点并且不知道如何继续。

import turtle 
import random

tina = turtle.Turtle()

tina.color('blue')

tina.begin_fill()
tina.goto(-200,-200)
tina.goto(0, -200)
tina.goto(0,0)
tina.goto(-200,0)
tina.goto(200,-200)

tina.end_fill()
turtle.exitonclick()

2 个答案:

答案 0 :(得分:0)

你的方码很接近,你只是以错误的顺序对基数进行舍入,并且你需要额外的.goto()。我相信以下内容符合您的描述:

from turtle import Turtle, Screen
import random

screen = Screen()
tina = Turtle()  # starts at (0, 0)
tina.color('blue')

tina.begin_fill()
tina.goto(0, -200)
tina.goto(-200, -200)
tina.goto(-200, 0)
tina.goto(0, 0)
tina.end_fill()

tina.width(random.randint(100, 200))

# ... continue drawing

screen.exitonclick()

请注意,100 - 200是一个非常宽的笔,1 - 20是更典型的。

答案 1 :(得分:0)

是的,我的回答是错误的。这并不是我想做的事情。我必须设置边界,因此随机标志仅存在于第三象限内。以下是我修复它的方法: 来自随机进口* 来自龟进口*

def maritime():

  # allows you to color the square blue
  color('blue')
  shape('turtle')
  # declare a random integer within the boundary between -200 and 200
  x = randint(-200,0)
  y = randint(-200,0 )
  # declare a random integer with the boundary between 100 and 200
  width = randint(100, 200)



 # Lines 16 -27 allow you to create a filled blue square
 penup()
 setposition(x, y)
 pendown()
 begin_fill()
 left(90)
 forward(width)
 left(90)
 forward(width)
 left(90)
 forward(width)
 left(90)
 forward(width)
 penup()
 # makes the square proportional to the larger square
 goto(x - width/3.64, y + width/3.64)
 pendown()

 # Lines 35-42 allows you to create the smaller square
 left(90)
 forward(width/2)
 left(90)
 forward(width/2)
 left(90)
 forward(width/2)
 left(90)
 forward(width/2)
 end_fill()
 penup()
 setposition(x, y + width)
 pendown()

 # Lines 49-60 allows you to create a filled red vertical rectangle
 color("red")
 begin_fill()
 for count in range(2):
forward(50)
right(90)
forward(width)
right(90)
end_fill()
penup()
forward(100)
pendown()

# Lines 63-37 allow you to create a filled blue verticle rectangle
begin_fill()
color("blue")
for count in range(2):
   forward(50)
   right(90)
   forward(width)
   right(90)
end_fill()

back(100)
color("black")
for i in range(2):
   forward(150)
   right(90)
   forward(width)
   right(90)

 speed(20)
 hideturtle()
 exitonclick() # allows the user to exit turtle screen with a click
 print (maritime())