简体   繁体   English

用 Turtle Python 创建砖金字塔

[英]Creating a Pyramid of Bricks with Turtle Python

Pyramid picture reference i'm very new to programming and in here i'm supposed to create a pyramid shape of bricks where the edge of the pyramid is filled with one color and the inside of it is filled with random colors, but i can't seem to figure it out how to move the bricks to create a new row, column and the random colors specified only to inside bricks of the pyramid. 金字塔图片参考我对编程非常陌生,在这里我应该创建一个金字塔形状的砖块,其中金字塔的边缘填充一种颜色,内部填充随机 colors,但我可以似乎没有弄清楚如何移动砖块以创建新的行、列和随机 colors 仅指定到金字塔内部的砖块。 Does anyone have a suggestion?有人有建议吗? Thanks in advance.提前致谢。 This is my code and it's still working for one brick only:这是我的代码,它仍然只适用于一块砖:

import turtle
import math
import random

bottom_brick = 10
top_brick = 1
brick_length = 35
brick_width = 25

from turtle import *
from turtle import Screen
screen = Screen()  
screen.bgcolor('white')

turtle.speed('fastest')

penup ()
goto(0, -100)
pendown()
fillcolor('#BC4A3C')
begin_fill()

#bottom_brick and top_brick are inputs from the user 
#to indicate how many bricks are used for the bottom layer and the top layer
# brick_length and brick_width are also input from user


for i in range (0, bottom_brick):
    turtle.setposition(0.5 * (i % 2), i)
   
    for j in range(i, bottom_brick):
        forward(brick_length)
        left(90)
        forward(brick_width)
        left(90)
        forward(brick_length)
        left(90)
        forward(brick_width)
        left(90)
        end_fill()
    done()

This will get you most of the way there with regards to making sure your bricks are laid out correctly:这将使您在确保正确布置砖块方面获得最大的成功:

import turtle

bottom_brick = 10
top_brick = 1
brick_length = 35
brick_width = 25


turtle.Screen().bgcolor('white')
turtle.speed('fastest')

turtle.penup()
turtle.goto(0, -100)
turtle.pendown()


#bottom_brick and top_brick are inputs from the user 
#to indicate how many bricks are used for the bottom layer and the top layer
# brick_length and brick_width are also input from user


for i in range(bottom_brick - top_brick + 2):
    for j in range(bottom_brick - i + 1):
        turtle.setposition((j + i / 2) * brick_length, i * brick_width)
        turtle.fillcolor('#BC4A3C')
        turtle.begin_fill()
        turtle.forward(brick_length)
        turtle.left(90)
        turtle.forward(brick_width)
        turtle.left(90)
        turtle.forward(brick_length)
        turtle.left(90)
        turtle.forward(brick_width)
        turtle.left(90)
        turtle.end_fill()
turtle.done()

Note that the fill color needs to be set inside the loop, and that's the spot where you'll want to randomize the color so that each brick is different.请注意,填充颜色需要在循环内设置,这就是您要随机化颜色的地方,以便每块砖都不同。 You'll also need to be mindful of when the pen is up and when it's down in order to avoid extra lines showing up.您还需要注意笔何时抬起和何时放下,以避免出现多余的线条。

This seems like an opportunity for better living through stamping instead of drawing :这似乎是一个通过冲压而不是绘画来改善生活的机会:

from turtle import Screen, Turtle
from random import random

top_brick = 1
bottom_brick = 10
brick_length = 35
brick_width = 25

CURSOR_SIZE = 20

screen = Screen()

turtle = Turtle()
turtle.hideturtle()
turtle.shape('square')
turtle.shapesize(brick_width / CURSOR_SIZE, brick_length / CURSOR_SIZE)
turtle.penup()
turtle.sety((bottom_brick - top_brick + 1) * brick_width / -2)

for row in range(bottom_brick, top_brick - 1, -1):
    turtle.goto(-brick_length * (row / 2), turtle.ycor() + brick_width)

    for column in range(row):
        if top_brick < row < bottom_brick and 0 < column < row - 1:
            turtle.fillcolor(random(), random(), random())
        else:
            turtle.fillcolor('#BC4A3C')

        turtle.stamp()
        turtle.forward(brick_length)

screen.exitonclick()

Plus an example of how you might handle the brick coloring issue.加上一个如何处理砖着色问题的例子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM