# star and a rectangle (stripe) multiple times to create a copy of # the United States flag. # # As a further example of the way functions allow us to reuse code, # in this exercise we will import the flag_elements module into # this program and create a different flag. In the PDF document # accompanying this file you will find several flags which can be # constructed easily using the "star" and "stripe" functions already # defined. Choose one of these and try to draw it. # # First we import the two functions we need (make sure a copy of file # flag_elements.py is in the same folder as this one) from flag_elements import star, stripe # Import the turtle graphics functions from turtle import * # Set up the drawing environment setup(600, 400) ##### PUT YOUR CODE FOR DRAWING THE FLAG HERE bgcolor("red") penup() goto(0, 75) star(150, "yellow") # Exit gracefully hideturtle() done()
title("Seeing Stars") penup() # Draw a star for each of a large range of numbers for star_num in range(100): # Choose random coords (here being very careful to # ensure that stars don't go off the edges of the # screen, remembering that the current coordinate is the # star's top point, not its centre) x_pos = randint(-max_coord + (max_size // 2), max_coord - (max_size // 2)) y_pos = randint(-max_coord + max_size, max_coord) # Choose a random size size = randint(5, 20) # Choose a random, interestingly-named colour # (see http://www.tcl.tk/man/tcl8.4/TkCmd/colors.htm) colour = choice([ 'aquamarine', 'beige', 'bisque', 'blue', 'burlywood', 'coral', 'cornsilk', 'cyan', 'firebrick', 'gold', 'goldenrod', 'honeydew', 'ivory', 'khaki', 'lavender', 'lawn green', 'magenta', 'maroon', 'mint cream', 'misty rose', 'moccasin', 'olive drab', 'orchid', 'papaya whip', 'peach puff', 'peru', 'powder blue', 'salmon', 'tomato', 'turquoise', 'wheat', 'white smoke' ]) # Draw a star at the chosen position in the chosen size and colour goto(x_pos, y_pos) star(size, colour) # Exit gracefully hideturtle() done()
from turtle import * # Here we draw the Panamanian flag as an illustration. # # Comment: Since this is such a small example, we've hardwired all # the numeric constants below. For non-trivial programs, however, # such "magic numbers" (i.e., unexplained numeric values) are best # avoided. Named, fixed values should be defined instead. # Set up the drawing environment setup(600, 400) bgcolor("white") title("Panama") penup() # Draw the two rectangles goto(0, 200) stripe(300, 200, "red") goto(-300, 0) stripe(300, 200, "blue") # Draw the two stars goto(-150, 140) star(80, "blue") goto(150, -60) star(80, "red") # Exit gracefully hideturtle() done()
penup() ##### PUT YOUR CODE FOR DRAWING THE FLAG HERE ## Draw the bottom green strip goto(0, stripe_height) setheading(90) stripe_numbers = range(1) #Draw first green bottom stripe for stripe_no in stripe_numbers: stripe(flag_width, stripe_height, "dark green") forward(stripe_height) for stripe_no in stripe_numbers: stripe(flag_width, stripe_height, "yellow") forward(stripe_height) for stripe_no in stripe_numbers: stripe(flag_width, stripe_height, "red") forward(stripe_height) penup() goto(200, 400) pendown() star(star_height, star_colour) # Exit gracefully hideturtle() done()
# this program and create a different flag. In the PDF document # accompanying this file you will find several flags which can be # constructed easily using the "star" and "stripe" functions already # defined. Choose one of these and try to draw it. # # First we import the two functions we need (make sure a copy of file # flag_elements.py is in the same folder as this one) from flag_elements import star, stripe # Import the turtle graphics functions from turtle import * from flag_elements import * # Set up the drawing environment setup(600, 400) ##### PUT YOUR CODE FOR DRAWING THE FLAG HERE speed("fast") stripe(150, 100, "red") right(90) forward(75) right(90) forward(25) star(50, "yellow") # Exit gracefully hideturtle() done()
setheading(90) # point north stripe_numbers = range(7) # draw seven stripes for stripe_no in stripe_numbers: stripe(flag_width, stripe_height, "red") forward(stripe_height * 2) # go up to next red stripe's position # Draw the blue "union" in the top left # goto(0, flag_height) # top left-hand corner of the flag stripe(union_width, union_height, "blue") # Draw the stars (to save time, only 30 of them, in a 6 X 5 # matrix, as the US flag appeared in 1848) # goto(0 + x_offset, flag_height - y_offset) # near top left-hand corner of the flag row_numbers = range(5) # draw five rows of stars column_numbers = range(6) # draw six stars in each row for row_no in row_numbers: for column_no in column_numbers: star(star_size, "white") setheading(0) # point east forward(x_sep) goto(0 + x_offset, ycor()) # go back to left-hand edge setheading(270) # point south forward(y_sep) # move down to next row of stars # Exit gracefully hideturtle() done()