4.6.2. Draw a Star

4.6.2.1. Simple program

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
'''
Draw a star
'''

from turtleplotbot import TurtlePlotBot
bot=TurtlePlotBot()

def star(bot, points, length):
    '''
    Draw a 'n' pointed star with 'length' sides

    Args:
        sides: number of points
        length: length of each side
    '''
    angle = 180.0 - 180.0 / points
    bot.pendown()

    for _ in range(points):
        bot.forward(length)
        bot.left(angle)
        bot.forward(length)

    bot.penup()

star(bot, 5, 30)

__import__("menu")      # return to turtleplotbot menu

star.py


4.6.2.2. Using the tftui module

Advanced version using the tftui module to set the number of points and the length of the sides of a star to be drawn.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
'''
Draw a star from user provided values
'''
#pylint: disable-msg=import-error
from turtleplotbot import TurtlePlotBot
import vga2_bold_16x16 as font
import button
import tftui

def star(bot, points, length):
    '''
    Draw a 'n' pointed star with 'length' sides

    Args:
        sides: number of points
        length: length of each side
    '''
    angle = 180.0 - 180.0 / points
    bot.pendown()

    for _ in range(points):
        bot.forward(length)
        bot.left(angle)
        bot.forward(length)

    bot.penup()

def main(ui):
    """
    Main routine
    """
    points = 5
    length = 20
    ok = 0

    form = [
        [ui.HEAD, 0, "Draw A Star"],
        [ui.INT, 0, 2, "Points:", 8, 2, 2, points],
        [ui.INT, 0, 4, "Length:", 8, 4, 2, length],
        [ui.OK, 0, 7, ("Next", "Cancel"), ok],
    ]

    btn, ok = ui.form(form)
    if btn == button.CENTER and ok == 0:
        points = form[1][ui.VAL]
        length = form[2][ui.VAL]

        bot = TurtlePlotBot()
        star(bot, points, length)

main(tftui.UI(font))

__import__("menu")      # return to turtleplotbot menu

stars.py