Quiz 7b#
Question 1#
The class code provided for this week’s mini-project supports an
ImageInfo
class to organize the data associated
with the image. Consider an ImageInfo
object of
the following form:
ImageInfo([45, 45], [90, 90], 35)
What is the radius of the shape associated with this
ImageInfo
object?
Enter answer here:\
35
Question 2#
Consider the provided ImageInfo
and
Sprite
class code. Assume we want ten asteroids
on the screen, each looking exactly alike and using the same image file.
How many ImageInfo
objects and how many
Sprite
objects should we create?
ten ImageInfo
objects, one
Sprite
object
one ImageInfo
object, ten
Sprite
objects
ten ImageInfo
objects, ten
Sprite
objects
one ImageInfo
object, one
Sprite
object \
one ImageInfo
object, ten
Sprite
objects \
Each sprite uses the same asteroid image. An image does not have any position and velocity but a sprite does.
Question 3#
The version of Rice Rocks that we will implement uses only a single
asteroid image and spawns multiple instances of the provided
Sprite
class using this image. In the original
Asteroids , a large asteroid split into two medium asteroids which
themselves split into two small asteroids.
If we only had one image and wanted to implement asteroids of varying sizes in our version, how should we do this?
Store the size in a global variable. Use this variable when drawing a
sprite.
Add a size attribute in the Sprite
class and a
size parameter to Sprite.__init__
. Use the size
attribute when drawing the sprite.
Store a list of sizes for each asteroid in a global variable. Use the
corresponding size when drawing a sprite.
Add a size attribute in the ImageInfo
class
and a size parameter to ImageInfo.__init__
. Use
this attribute when drawing the sprite. \
Add a size attribute in the Sprite
class and a
size parameter to Sprite.__init__
. Use the size
attribute when drawing the sprite. \
Question 4#
What is the supported range of sound volumes in
set_volume
? You can find out in the CodeSkulptor
documentation .
0 to 1
0 to 10
-1 to 1
1 to 100 \
0 to 1 \
Question 5#
Assume you have code that loads and plays a sound. Unfortunately, you don’t hear anything. Which of the following could be a reason?
The given URL exists, but is inaccessible due to network problems.
A file found with the given URL isn’t a sound file recognized by your
browser.
No file is found with the given URL.
You have set the volume level to 0.
Your browser is loading a big sound file. Wait longer. \
all of them
Question 6#
Which of the following are valid HTML representations of the color blue?
Refer to this page on HTML color values .
"Red"
"#0000FF"
"#00FF00"
"Blue"
"rgb(0, 0, 255)"
\
"#0000FF"
"Blue"
"rgb(0, 0, 255)"
\
According to CSS Legal Color Values rgb(0, 0, 255)
is also correct.
Question 7#
Imagine we are writing code for something like Rice Rocks , where things are moving in 2D toroidal space, i.e., the wrap around all four sides of the screen. How can we eliminate the duplicated code in the following function?
def move(position, vector):
"""Moves the position by the given vector in 2D toroidal space."""
position[0] = (position[0] + vector[0]) % SCREEN_SIZE[0]
position[1] = (position[1] + vector[1]) % SCREEN_SIZE[1]
NUM_DIMENSIONS = 2
def move(position, vector):
"""Moves the position by the given vector in 2D toroidal space."""
for d in range(NUM_DIMENSIONS):
position[d] = (position[d] + vector[d]) % SCREEN_SIZE[d]
\
def move_dimension(dimension, position, vector):
"""Moves the position component by the given vector component in 1D toroidal space."""
position[dimension] = (position[dimension] + vector[dimension]) % SCREEN_SIZE[dimension]
def move(position, vector):
"""Moves the position by the given vector in 2D toroidal space."""
move_dimension(0, position, vector)
move_dimension(1, position, vector)
\
def move(position, vector):
"""Moves the position by the given vector in 2D toroidal space."""
position = (position + vector) % SCREEN_SIZE
\
NUM_DIMENSIONS = 2
def move(position, vector):
for d in range(NUM_DIMENSIONS):
return position[d] = (position[d] + vector[d]) % SCREEN_SIZE[d]
\
def move(position, vector):
position = [(pos + vec) % size for pos in position for vec in vector for size in SCREEN_SIZE]
\
NUM_DIMENSIONS = 2
def move(position, vector):
"""Moves the position by the given vector in 2D toroidal space."""
for d in range(NUM_DIMENSIONS):
position[d] = (position[d] + vector[d]) % SCREEN_SIZE[d]
def move_dimension(dimension, position, vector):
"""Moves the position component by the given vector component in 1D toroidal space."""
position[dimension] = (position[dimension] + vector[dimension]) % SCREEN_SIZE[dimension]
def move(position, vector):
"""Moves the position by the given vector in 2D toroidal space."""
move_dimension(0, position, vector)
move_dimension(1, position, vector)
in the third and fifth example we cannot add the position and vector together like shown, because they are tuples
In the fourth example,
return
statement immediately exits the function
Question 8#
What is the primary reason for not duplicating code? It was the only reason mentioned in the Programming Tips #7 video.
It leads to faster code.
You only need to get the code correct once .
It takes less time to write the code. \
You only need to get the code correct once . \
Question 9#
What is Mike Massimino’s greatest accomplishment?
Fixing the Hubble Space Telescope in space
Being the first person to use Twitter in space
Receiving his PhD from MIT
Appearing on The Big Bang Theory
Appearing on An Introduction to Interactive Programming in Python \
He appears in the mini-project video
He was even a professor at Rice, how cool!