INTRODUCTION TO COMPUTATIONAL MEDIA

116 views 10:14 am 0 Comments March 12, 2023

SPRING 2023 CSci 203
INTRODUCTION TO COMPUTATIONAL MEDIA
Module 4 Assignment – Loops
Due Date: February 26th @ 11:59 P.M.
In this assignment, you will demonstrate the use of loops to
repeat code without writing duplicate code (the same code
repeated over and over). You will also use concepts from
Modules 1-4. Although your alien ship doesn’t have to be exact,
you should draw an alien ship which closely resembles the ship
on the next page. You do need to use the shapes and functions
listed below and your objects should do the actions defined.
Directions:
0. Save your work in the format, YourInitials_AlienShips.
YourInitials should be
your initials, for me it would be
TH_AlienShips. Be sure to save your work often and make
comments above the different sections of code.
1. At the top of your code, include the programming
standards (your name, date, etc.) found in Blackboard in
the “Course Information” section. (
1 point)
2.
Scene Details. The scene should be 800 by 800 pixels in
size.
3. Create a spaceship in the top left corner of the scene
window. You may want to turn stroke off for everything
except lines. The description of the objects in the scene
is below:
a. Spaceship window – 5 ellipses. (
2 points)
b. Spaceship and Beam Origin – 4 ellipses and any other
shapes you need to get the black bottom of the ship,
I used many rectangles. (
2 points)
c. Beam and Beam light – 3 ellipses; 1 triangle; 2
lines. The lines may not be needed depends on how
you use the triangle. (
2 points)
d. Planet and stars – 7 ellipses (
1 point)
Background and Spaceship Window Color:
4. Create a global Boolean variable to know when to change
the color (you can name it what you want) and assign it a
value of False.
1 point)
5.

Below and outside of the draw function (not in), make a
keyPressed function
point)
(see Chapter 2 and 3 slides). (1

a. In the keyPressed function, inform the computer that
the value of the global Boolean variable you made in
step 4 will change in the function by using the
keyword “global”. (
1 point)
b. Using the not operator, flip the value of the global
Boolean variable. (
1 point)
c. In the draw function create a conditional structure
(if – else) to test
Spaceship window
beam
beam light
beam origin
Spaceship
star
planet
star

i. If the variable equals False – change the
background color
ii. else – change the background color back to the
original color (2 points)

The beam should be some consistent color but for one
bonus point
, you should color the beam the same color as
one of the background colors when that color isn’t the
background. When the background is that color, the beam
should change to white (this is where you can use the
color of the triangle).
Planet and Beam Origin Color:
6. Create a global Boolean variable and assign it the value
True. (
1 point)
7. Make a mousePressed function below and outside of the
draw function and keyPressed functions (see Chapter 2 and
3 slides). Use a global statement with the variable to
inform the computer that the value will change. Next in
an assignment statement flip the variables value using
the keyword not. (
1 point).
8. In the draw() function before the planet and beam origin
code, use a conditional statement (if – else) to test the
Boolean variable. (
2 points)
a. In the if condition – the fill should be the
original color.
b. In the else – the color should be different.
You can use whatever two colors you would like.
The beam light and stars:
9. The beam light and stars should just use any random
colors. Both lines in a particular star should be the
same color but each star can be different as well as the
beam light. It should kind of flicker when the scene is
playing (refer to the video). (
1 point)
Using loops to make multiple ships:
10. Following examples from Chapter 4 slides, you will create
a nested loop (an outer “while loop” and an inner “for
loop”) that will repeat the code you used to make the
alien ship to fill the screen. This will require local
variables (known as loop control variables) in the draw
function, one for the “while loop” and another for the
“for loop”. (*hint: the loop control variables will be
used as the x and y coordinates of the shapes).
a. In the draw() function: before the code for the
alien ship, create a local variable (it will be used
as the loop control variable for the “while loop”)
for the x OR y coordinates of the shapes. You should
assign this variable the value of your starting
position of your alien ship’s x OR y coordinate of
the shape you used as your starting/center shape
(probably the ellipse of your ship but you should
decide). (
1 point)
b. After the local variable (but before the code for
the ship), create a “while loop” that tests the
value of the variable you made above (the loop
control variable) is less than the height or width
of your scene window (depending on the coordinate
you picked). (
1 point)
i. Highlight all the code for the alien ship,
stars beam and any other object you have and
tab it all over once, so the code is part of
(inside) the “while loop”.
ii. Before you start changing numbers, you may want
to copy your shapes code, comment out the
original and change the copy so you keep all
the original values.
iii. At the end of the code (at the bottom) for your
alien ship and objects code (tabbed over the
same as the other code), use a combination
assignment operator or math operation to
increase the value of the “while loops” loop
control variable. Be sure the value is large
enough so that the alien ships and other
objects don’t overlap. (
1 point)
iv. Replace all the x OR y coordinates (whichever
one you picked for the while loop) of the alien
ships and other objects with the “while loops”

loop control variable and adjust the offsets.
You may want to adjust one shape at a time and
run your code after each change to ensure its
working correctly. You should have a line of
alien ships and other objects either across the
window or down the window depending on whether
you replaced the x OR y coordinates. I replaced
the y coordinates, so mine go down. You’ve now
used a loop to repeat code without needing to
write extra code. (
2 points)
c. Beneath the “while loop” but before the alien ship
and other objects code, create a “for loop” with a
loop control variable (you can name it whatever you
want) for the other coordinate (x OR y that you
didn’t chose before). The range function of the “for
loop” should have 3 values: a start value (should be
the Alien ships x OR y coordinate of the center
shape whichever one you didn’t use in the while
loop), an end value (the width or height of your
window), and a step value (how much space you want
between the different objects so that they don’t
overlap) (
2 points)
i. Highlight all the alien ships and other objects
code and tab it all over again once so that the
code is now part of (inside) the “for loop”.
ii. Replace each shape’s value coordinates
(whichever one you didn’t use for the “while
loop”) with the “for loop” loop control
variable and adjust the offsets. Be sure to run

your code as you make changes. Your window
should be full of alien ships and other
objects. (
2 points)
11. Use of good programming structure, comments, etc. (
2
points
)
Submission:
12. Save your finished work (make sure the naming format
matches:
YourInitials_AlienShips and remember the
Yourintials part are your actual initials).
13. Zip the folder containing your finished work and upload
the zipped file to Blackboard under Assignment 4.
How to zip a file:
Windows:
Right click on the folder/file you want to zip.

In the drop-down menu, select Send to, and in that
drop-down menu, select Compressed (zipped) folder.
Mac:
Right click on the folder/file you want to zip.
In the drop-down menu, select Compress
“theNameOfTheFolder”