• Home
  • Textbooks
  • You Can Do It!: A Beginners Introduction to Computer Programming
  • Functions as Objects and Simple Animation

You Can Do It!: A Beginners Introduction to Computer Programming

Francis Glassborow

Chapter 15

Functions as Objects and Simple Animation - all with Video Answers

Educators


Chapter Questions

Problem 1

Write a function (call it plot_every_thirdO) that plots a black point only if it did not plot a point the last two times it was called. Use that function to write a function that draws a dotted horizontal line. Test the result. You can assume that it is OK to always draw left to right.

Check back soon!
02:10

Problem 2

Write a function, using a Plot pol icy parameter, that draws a square given the bottom left corner and the length of the side. Use it to draw a variety of different squares, including the case where a single point moves round a square.

MS
Malcom Smith
Numerade Educator
00:48

Problem 3

Add a speed control to the sprite implementation. Note that there are two ways to do this, you could make the speed a property of a sprite by adding another data member that can be set by calling a member function, or you can do it the same way that we controlled the speed of a moving point (with a hidden variable and a set_speed $O$ function).

Jason Horton
Jason Horton
Numerade Educator

Problem 4

Write a function, save palette $\mathrm{O}$, that will extract the red/green/blue intensities of the whole palette (256 entries, 0 to 255 ) and write the answers to a file. The format of the file should be one line per triplet with space separating the three values.

Write a program to use the function and store the results in a file called bccsic.pcilette. Check that you can read the results in your editor.

Check back soon!

Problem 5

Write a function, restore_palette $\mathrm{O}$, that can read back a file of palette data and use it to reset the current playpen to that palette.

Check back soon!

Problem 6

Create some palettes for yourselt. For example create a palette that is entirely shades of red. Now try one where 0 is black, 255 is white, 1 to 85 are reds, 86 to 170 are greens and 171 to 254 are blues.

Instead of loading the background.png file to make sure that the colors match those originally used, you can load the butterfly.palette that I have also provided. You can only have a single palette active at a time, so trying to fly a butterfly across your favorite seascape will not work unless you do a large amount of extra work or choose which palette is loaded.

Check back soon!