0% found this document useful (0 votes)
9 views6 pages

cs65 Lab3

Uploaded by

amato.salvatore
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views6 pages

cs65 Lab3

Uploaded by

amato.salvatore
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

CS65 Lab 3: Graphics library exploration Due: Saturday, Feb 19, 2022

Lab 3: Graphics Library Exploration


Course: CS65 - Introduction to Computer Science (Spring 2022)
Instructor: Md Alimoor Reza, Assistant Professor of Computer Science, Drake University
Due: Saturday February 19, 11:50 PM

You will be creating a python file for each task separately (with a *.py extension), where you will save
your python instructions. By default, Thonny opens an unnamed file in the a text editor (top text pane).
It is an excellent practice to write a formal header and suppress it as comments. A line of code with a
preceding # is considered as a comment in Python. You should type in the necessary parts as shown
below, e.g., your name, your contact email, description, etc.. Save the file using the format as follows first-
name lastname lab3 task*.py (all in lowercase letters). For example, I saved my file as md reza lab3 task3.py.

Driver and navigator: Now, you will be doing collaborative work for this lab. These exercises should
be completed with your assigned partner. Both of you will take turns playing the role of i) the driver and
ii) the navigator. The driver’s responsibility is to type into Thonny when completing each exercise. The
navigator’s responsibility is to support the driver by reading the exercises, looking up relevant readings, and
identifying errors.

Tasks and file submission: This lab consists of several small tasks. We expect that you submit separate
file for each task (a python file with a .py extension). In your python file, add comments to briefly explain
what you are doing. You should submit your individual copy by acknowledging your partner’s name at the
beginning of your python file (inside a comment).

Drawing circles, triangles, and other shapes


The package graphics needs to be installed in Thonny. You might have already installed it. In case you have
not, please follow the instructions in Lecture 5. You will implement a Python program that will draw basic
graphical objects such as circle, rectangle, triangle and line. It is recommended that you write a separate
function for each of the above-mentioned shapes.

Task 1: Drawing circles of different sizes, at different locations


Here is a function that draws a circle. Your goal is to write another function – possibly with a different name
– with the following criteria. Instead of hard-coding the values inside the function body, you should ask
the user to provide the values for you. Hint: you should use built-in function input(””) to prompt the user
to provide inputs. ‘Hard-coding’ refers to the fact that variables such as cir radius = 400 and cir center =
Point(100, 100) have been initialized with fixed and predetermined values. It is not a very flexible way of
writing your code. Your new function should have a different set of functions parameters. Determine how
many parameters do you need for your new function and finish making the necessary changes inside the
function. Once you define a function, you need to call it in order to see its execution.

Page 1 of 6
CS65 Lab 3: Graphics library exploration Due: Saturday, Feb 19, 2022

For example, the function above can be called with the following python line:
win default = create circle default coord()
Notice the function returns a variable window, which is defined inside it. When you call the function, you
need to save it in a separate variable. In the example above, it is saved in the variable win default. Can you
tell the difference between the two variables? More precisely – Is window a local-variable or global variable?
How about win default? Write the difference inside your python file as a comment (with a preceding #).
Define the function and call them as instructed. When you run your program, it should prompt you to do
the following:

Here is the output you should expect to see:

You should try run your program with different user inputs and see the differences.

Task 2: Drawing rectangles of different sizes, at different locations


Following the previous task, can you write another function which draws rectangles of different sizes. As
discussed in class, a rectangle (according to graphics library specification) is defined by the two corners:

Page 2 of 6
CS65 Lab 3: Graphics library exploration Due: Saturday, Feb 19, 2022

• left-most corner (2D point)

• right-most corner (2D point)

You can define a corner (2D point) the same you have defined it for the center of the circle. Here is a sample
function that draws a rectangle.

Instead of hard-coding the values inside the function body, you should ask the user to provide the values
for you. When you define your new function, it should have a different number of parameters. Determine
how many parameters do you need for your new function and finish making the necessary changes inside the
function. Once you define a function, you need to call it in order to see its execution. Define the function
and call them as instructed. When you run your program, it should prompt you to do the following:

Here is the output you should expect to see:

You should try run your program with different user inputs and see the differences.

Page 3 of 6
CS65 Lab 3: Graphics library exploration Due: Saturday, Feb 19, 2022

Task 3: Drawing triangle of different sizes, at different locations


Now can you write another function that draws triangles of different sizes? A triangle (according to
graphics library specification) is defined by the three corners. You can define a corner (2D point) the
same as you defined it for a rectangle. Read the specification and find out the appropriate graphical object
for this task: https://mcsp.wartburg.edu/zelle/python/graphics/graphics/graphref.html.
Instead of hard-coding the values inside the function body, you should ask the user to provide the values
for you. When you define your new function, it should have a different number of parameters. Determine
how many parameters do you need for your new function and finish making the necessary changes inside the
function. Once you define a function, you need to call it in order to see its execution. Define the function
and call them as instructed. When you run your program, it should prompt you to do the following:

Here is the output you should expect to see:

You should try run your program with different user inputs and see the differences.

Task 4: Drawing rectangle with mouse clicks


Write a new function for this task. Instead of receiving x- and y-coordinates from the user using input(),
you should click the mouse in two different locations in the graphics window. Those two locations should
serve as corners of the rectangle. Recall that two corners define a rectangle: i) left-most corner (2D point)
and ii) right-most corner (2D point). Additionally, the first corner should also serve as the center of a circle
of radius 100 pixels. Once you define a function, you need to call it in order to see its execution. Define
the function and call them as instructed. Here is the output you should expect to see:

Page 4 of 6
CS65 Lab 3: Graphics library exploration Due: Saturday, Feb 19, 2022

You should try run your program with different user inputs and see the differences.

Task 5: Building a staircase


Your task is to build a staircase. The building blocks of the staircase are rectangles with alternating colors
(as shown below). You have to create 4 rectangles. The length of each side of a rectangle should be 100
pixels. The coordinate system should be transformed so that the origin (0,0) is located at the bottom-left
corner. The size of the window remains 400 pixels by 400 pixels. There are various ways you can attempt
to solve this task. I recommend that you follow the basic structure of the code given below. In this code
structure, you should not be using any global variables. The function build staircase() should call another
function draw one rectangle(...) each time it needs to build a rectangle. In each function call, you have to
provide necessary arguments so that they match the parameters of function draw one rectangle(...).

Define the function and call them as instructed. Here is the output you should expect to see:

Page 5 of 6
CS65 Lab 3: Graphics library exploration Due: Saturday, Feb 19, 2022

What to turn in:


Please submit a zip file containing your source code (five python files with .py extension with proper naming
convention described above) for this lab. Please make sure that your code runs prior to submission. Log in
to Blackboard and find Lab 3 then submit your zip file.

Page 6 of 6

You might also like