使海龟对象彼此相邻而不是彼此重叠
Making turtle objects draw next to each other instead of on top of each other
我遇到了一个问题,我的乌龟对象并没有画一个车的长度,而是画在彼此的顶部。我已经尝试了几乎所有我能想到的循环变体,但就是无法弄清楚,尽管它可能是如此简单。除此之外,我一直无法弄清楚如何让用户输入的汽车名称出现在他们各自的汽车上方。
它应该是这样的:
https://i.stack.imgur.com/rubG3.png
import turtle
import random
turtle.setup(575,575)
pen = turtle.Turtle()
turtle.bgcolor("white")
pen.speed(0)
pen.pensize(2)
pen.hideturtle()
colorCar = []
carName = []
# ask user how many cars
carNum = int(turtle.numinput("cars", "Enter how many cars you have: ", 1, 1, 10))
# respective values (add more variables if needed for sizing)
widthPadding = turtle.window_width()/carNum
width= widthPadding * .8
padding = widthPadding * .1
length = width * 2
# controlls how long horizontally
carWidth = width * 0.5
wheelWidth = carWidth * .1
# controls how long vertically
carLength = width * 0.2
for row in range(carNum):
x = - turtle.window_width()/2 + carWidth
y = - turtle.window_height()/2 + padding + row * widthPadding
# ask user for color of car
for i in range(carNum):
color = turtle.textinput("car color", f"Enter Color of Car {i +1}").lower().strip()
colorCar.append(color)
# ask for car name
name = turtle.textinput("car name", f"Enter Name of Car {i +1}").lower().strip()
carName.append(name)
for row in range(carNum):
pen.up()
pen.setpos(x + (width - length)/2 + padding, y)
pen.down()
#draw cars
pen.color("black")
pen.pensize(4)
pen.fillcolor(color)
pen.begin_fill()
for i in range(4):
if i%2== 0:
pen.forward(carWidth)
else:
pen.forward(carLength)
pen.left(90)
pen.end_fill()
# write name above car
turtle.write(name)
turtle.done()
这段代码一团糟。您需要使用更好的变量名。目前尚不清楚各种“宽度”、“长度”和“填充”变量的作用。事实上,这是真正需要评论的地方,而不是像 # ask for car name
就在你明显要车名之前的评论。
下面我重新编写了您的代码以解决您提出的两个问题:汽车水平排列而不是彼此重叠;汽车名称出现在汽车上方:
from turtle import Screen, Turtle
screen = Screen()
screen.setup(575, 575)
# ask user how many cars
carNum = int(screen.numinput("cars", "Enter how many cars you have: ", 1, 1, 10))
# respective values (add more variables if needed for sizing)
widthPadding = screen.window_width() / carNum
width = widthPadding * 0.8
padding = widthPadding * 0.1
length = width * 2
# controls how long horizontally
bodyWidth = width / 2
# controls how long vertically
bodyHeight = width * 0.2
carColors = []
carNames = []
for i in range(1, carNum + 1):
# ask user for color of car
color = screen.textinput("car color", f"Enter Color of Car {i}").lower().strip()
carColors.append(color)
# ask for car name
name = screen.textinput("car name", f"Enter Name of Car {i}").lower().strip()
carNames.append(name)
# draw cars
turtle = Turtle()
turtle.hideturtle()
turtle.speed('fastest')
turtle.pensize(4)
for car in range(carNum):
x = bodyWidth - screen.window_width()/2 + car * widthPadding
y = padding - screen.window_height()/2
turtle.penup()
turtle.setpos(x + (width - length)/2 + padding, y)
turtle.pendown()
turtle.fillcolor(carColors[car])
turtle.begin_fill()
for side in range(4):
if side % 2 == 0:
turtle.forward(bodyWidth)
else:
turtle.forward(bodyHeight)
turtle.left(90)
turtle.end_fill()
# write name above car
turtle.penup()
turtle.sety(turtle.ycor() + 3*bodyHeight/2)
turtle.write(carNames[car])
screen.mainloop()
我遇到了一个问题,我的乌龟对象并没有画一个车的长度,而是画在彼此的顶部。我已经尝试了几乎所有我能想到的循环变体,但就是无法弄清楚,尽管它可能是如此简单。除此之外,我一直无法弄清楚如何让用户输入的汽车名称出现在他们各自的汽车上方。 它应该是这样的: https://i.stack.imgur.com/rubG3.png
import turtle
import random
turtle.setup(575,575)
pen = turtle.Turtle()
turtle.bgcolor("white")
pen.speed(0)
pen.pensize(2)
pen.hideturtle()
colorCar = []
carName = []
# ask user how many cars
carNum = int(turtle.numinput("cars", "Enter how many cars you have: ", 1, 1, 10))
# respective values (add more variables if needed for sizing)
widthPadding = turtle.window_width()/carNum
width= widthPadding * .8
padding = widthPadding * .1
length = width * 2
# controlls how long horizontally
carWidth = width * 0.5
wheelWidth = carWidth * .1
# controls how long vertically
carLength = width * 0.2
for row in range(carNum):
x = - turtle.window_width()/2 + carWidth
y = - turtle.window_height()/2 + padding + row * widthPadding
# ask user for color of car
for i in range(carNum):
color = turtle.textinput("car color", f"Enter Color of Car {i +1}").lower().strip()
colorCar.append(color)
# ask for car name
name = turtle.textinput("car name", f"Enter Name of Car {i +1}").lower().strip()
carName.append(name)
for row in range(carNum):
pen.up()
pen.setpos(x + (width - length)/2 + padding, y)
pen.down()
#draw cars
pen.color("black")
pen.pensize(4)
pen.fillcolor(color)
pen.begin_fill()
for i in range(4):
if i%2== 0:
pen.forward(carWidth)
else:
pen.forward(carLength)
pen.left(90)
pen.end_fill()
# write name above car
turtle.write(name)
turtle.done()
这段代码一团糟。您需要使用更好的变量名。目前尚不清楚各种“宽度”、“长度”和“填充”变量的作用。事实上,这是真正需要评论的地方,而不是像 # ask for car name
就在你明显要车名之前的评论。
下面我重新编写了您的代码以解决您提出的两个问题:汽车水平排列而不是彼此重叠;汽车名称出现在汽车上方:
from turtle import Screen, Turtle
screen = Screen()
screen.setup(575, 575)
# ask user how many cars
carNum = int(screen.numinput("cars", "Enter how many cars you have: ", 1, 1, 10))
# respective values (add more variables if needed for sizing)
widthPadding = screen.window_width() / carNum
width = widthPadding * 0.8
padding = widthPadding * 0.1
length = width * 2
# controls how long horizontally
bodyWidth = width / 2
# controls how long vertically
bodyHeight = width * 0.2
carColors = []
carNames = []
for i in range(1, carNum + 1):
# ask user for color of car
color = screen.textinput("car color", f"Enter Color of Car {i}").lower().strip()
carColors.append(color)
# ask for car name
name = screen.textinput("car name", f"Enter Name of Car {i}").lower().strip()
carNames.append(name)
# draw cars
turtle = Turtle()
turtle.hideturtle()
turtle.speed('fastest')
turtle.pensize(4)
for car in range(carNum):
x = bodyWidth - screen.window_width()/2 + car * widthPadding
y = padding - screen.window_height()/2
turtle.penup()
turtle.setpos(x + (width - length)/2 + padding, y)
turtle.pendown()
turtle.fillcolor(carColors[car])
turtle.begin_fill()
for side in range(4):
if side % 2 == 0:
turtle.forward(bodyWidth)
else:
turtle.forward(bodyHeight)
turtle.left(90)
turtle.end_fill()
# write name above car
turtle.penup()
turtle.sety(turtle.ycor() + 3*bodyHeight/2)
turtle.write(carNames[car])
screen.mainloop()