简体   繁体   中英

How to make hexagon increase in size?

I want to know how to increase the size of the hexagon. So far I have the hexagons spaced out, but the hexagons need to increase in size. enter image description here Code:

import turtle
import math

def drawLine(num, length):
    if num % 2 ==0:
        for x in range (8):
            turtle.forward(15)
            turtle.right(45)
    else:
        for x in range (8):
            turtle.forward (15)
            turtle.right(-45)

def main():
    turtle.penup()
    turtle.goto(-300,0)
    turtle.speed(5)
    turtle.pensize(2)
    turtle.pendown()


    for x in range (40):
        drawLine(x,20+5*x)
        turtle.forward(20+5*x)
    turtle.hideturtle()

main()
input()

Your code is OK except for one very small detail. The function drawline(num, length) takes the parameter length and this parameter will be used in functions body, but doesn't. Putting length instead of 15 into turtle.forward( ) results in growing hexagons:

在此处输入图像描述

import turtle
import math

def drawLine(num, length):
    if num % 2 == 0:
        for x in range (8):
            turtle.forward(length)
            turtle.right(45)
    else:
        for x in range (8):
            turtle.forward (length)
            turtle.right(-45)

def main():
    turtle.penup()
    turtle.goto(-300,0)
    turtle.speed(5)
    turtle.pensize(2)
    turtle.pendown()


    for x in range (40):
        drawLine(x, 20+5*x)
        turtle.forward(20+5*x)
    turtle.hideturtle()

main()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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