You are on page 1of 4

6/10/22, 7:58 CSE 111 - 04 Prove



CSE 111 |
Programming with Functions
04 Prove Assignment: Writing Functions

Purpose
Prove that you can write functions with parameters and call those
functions
multiple times with arguments.

Problem Statement
Modern computers are capable of performing all sorts of
calculations to produce
numbers. However, they are also capable of
performing calculations to produce
art, illustrations, animations,
movies, and music.

Assignment
During the previous lesson's milestone, you wrote code to draw at
least the sky,
clouds, and ground of an outdoor scene. During this
lesson, you will write code
that draws the remaining objects in your
scene. Your program can draw any
outdoor scene that you like as long
as it meets these requirements:

1. The scene must be outdoor and include part of the


sky.
2. The sky must have clouds.
3. The scene must include repetitive objects, such as
blades of grass, trees,
leaves on a tree, birds, flowers,
insects, fish, pickets in a fence, dashed lines
on a road,
buildings, bales of hay, snowmen, snowflakes, or
icicles.

Your program must be divided into functions such as


draw_sky, draw_cloud,
draw_ground, draw_bird,
draw_flower, draw_insect,
draw_fish, or draw_snowman.
Each
repetitive object in your scene should be drawn by a function that
your
program calls repeatedly, once for each repeated object. For
example, your
program could include a function named
draw_leaf that your program
repeatedly calls to draw
each leaf on a tree at a different location.

As you write your program, write it so that it draws objects in


the order of
farthest away to nearest. For example, you program
should draw the sky first,
then clouds, then the ground, then trees,
then insects in the trees. Be creative.

Helpful Documentation
The prepare content for lesson 2 explains
how to call functions.

https://byui-cse.github.io/cse111-course/lesson04/prove.html 1/4
6/10/22, 7:58 CSE 111 - 04 Prove

The prepare content for the previous lesson explains


how to write
functions.

The prepare content for this lesson lesson explains the


properties of a good
function.

The prove milestone


of the previous lesson describes this assignment in
more detail
and contains additional helpful documentation.

The documentation for the Draw 2D library includes


example programs, a
function reference,
and a table of
supported colors.

The following videos walk through examples of using


functions to draw
with the Draw 2-D library.
Drawing with Functions, part 1 (14 minutes)
Drawing with Functions, part 2 (18 minutes)

Help from a Tutor


As a BYU-Idaho campus or online student you can get help from a
tutor to
complete your CSE 111 assignments. Each tutor is a current
BYU-Idaho student
employed by BYU-Idaho. Meeting with a tutor is
free. It will not cost you any
money to meet with a tutor. To get
help from a tutor, you simply make an
appointment and then meet with
the tutor. Campus students meet with tutors in
the tutoring center.
Online students meet with tutors in Zoom. To make an
appointment,
follow the instructions in the
course tutoring guide.

Testing Procedure
Verify that your program works correctly by following each step
in this testing
procedure:

1. Run your program and verify that it correctly opens a


window and draws
within that window a complete outdoor scene
that contains the sky, clouds,
the ground, and other
elements.

Exceeding the Requirements


If your program fulfills the requirements for this assignment as
described in the
previous prove milestone and the Assignment section
above, your program will
earn 93% of the possible points. In order
to earn the remaining 7% of points,
you will need to write your
drawing functions so that they correctly use
parameters.

Consider the following draw_pine_trees function that


draws three trees. It
correctly draws three trees. However, it has
only one parameter (canvas) but
https://byui-cse.github.io/cse111-course/lesson04/prove.html 2/4
6/10/22, 7:58 CSE 111 - 04 Prove

should have more. With


only one parameter, the draw_pine_trees function is not
easily reusable because it uses hard coded numbers for the
coordinates at lines
5, 6,
10, 11, 15, and 16. Because of the way that
the draw_pine_trees function is
written, it can draw
only three pine trees, and it will always draw them at the
same
locations. What if we decide to change the width and height of the
scene?
The draw_pine_trees function will draw pine
trees in the same locations as
before, but these may be the wrong
locations in a larger scene.

1
def draw_pine_trees(canvas):

2
"""Draw three pine trees, each at a fixed location."""

4
# Draw a tree.

5
draw_rectangle(canvas, 300, 70, 320, 120, fill="brown")

6
draw_polygon(canvas, 240, 110, 310, 320, 380, 110,

7
fill="forestGreen")

9
# Draw another tree.

10
draw_rectangle(canvas, 200, 60, 220, 110, fill="brown")

11
draw_polygon(canvas, 140, 100, 210, 310, 280, 100,

12
fill="forestGreen")

13

14
# Draw a third tree.

15
draw_rectangle(canvas, 100, 50, 120, 100, fill="brown")

16
draw_polygon(canvas, 40, 90, 110, 300, 180, 90,

17 fill="forestGreen")

Now consider the draw_pine_tree function in the next


example. Notice that the
draw_pine_tree function has
more parameters than the draw_pine_trees function
in
the previous example. The two extra parameters (peak_x
and peak_y)
deteremine where the
draw_pine_tree function will draw one tree. These two
parameters (peak_x and peak_y) make the
draw_pine_tree function more
reusable than the
draw_pine_trees function in the previous example.
Notice at
lines 30–34,
that the main function reuses (calls) the
draw_pine_tree function
five times.

1
def draw_pine_tree(canvas, peak_x, peak_y):

2
"""Draw one pine tree at location (peak_x, peak_y)"""

4
# Compute the coordinates of the skirt and trunk.

5
skirt_left = peak_x - 70

6
skirt_right = peak_x + 70

7
skirt_bottom = peak_y - 210

8
trunk_left = peak_x - 10

9
trunk_right = peak_x + 10

10
trunk_bottom = peak_y - 260

11

12
# Draw the tree trunk.

13
draw_rectangle(canvas, trunk_left, trunk_bottom,

14
trunk_right, skirt_bottom, fill="brown")

15

16
# Draw the tree skirt.

17
draw_polygon(canvas, skirt_left, skirt_bottom, peak_x, peak_y,

18
skirt_right, skirt_bottom, fill="forestGreen")

19

20

21
def main():

22
# Width and height of the scene in pixels

23
scene_width = 800

24
scene_height = 500

25

26
# Call the start_drawing function in the draw2d.py

27
# library which will open a window and create a canvas.

https://byui-cse.github.io/cse111-course/lesson04/prove.html 3/4
6/10/22, 7:58 CSE 111 - 04 Prove

28
canvas = start_drawing("Scene", scene_width, scene_height)

29

30
draw_pine_tree(canvas, 240, 320)

31
draw_pine_tree(canvas, 160, 280)

32
draw_pine_tree(canvas, 100, 250)

33
draw_pine_tree(canvas, 500, 350)

34
draw_pine_tree(canvas, 600, 280)

35

36
# Call the finish_drawing function

37
# in the draw2d.py library.

38 finish_drawing(canvas)

In summary, if you wish to earn the last 7% of points for this


assignment, you
must write your drawing functions to have and use
parameters like the
draw_pine_tree function in the
second example above.

Ponder
After you finish this assignment, congratulate yourself because
you wrote a
Python program with many user-defined functions. As you
wrote your program,
what did you learn about organizing a program
into functions? Did you learn
that you can work more efficiently by
writing a function once and calling it many
times with different
arguments?

Submission
To submit your program, return to I-Learn and do these two
things:

1. Upload your program (the .py file) for feedback.


2. Add a submission comment that specifies the grading
category that best
describes your program along with a one or
two sentence justification for
your choice. The grading criteria
are:
a. Some attempt made
b. Developing but significantly deficient
c. Slightly deficient
d. Meets requirements
e. Exceeds requirements

Copyright © 2020, Brigham Young University - Idaho. All


rights reserved.

https://byui-cse.github.io/cse111-course/lesson04/prove.html 4/4

You might also like