Images!
Goals:
- Extend your knowledge of sprites to include working with images
Coding Snippets:
- Use
wear
to select an image for a sprite. The optional second argument controls its height. - Use
slide
to move a sprite to the right or left without turning. Add a second argument afterslide
to simultaneously move forward or backwards. mirror
flips the image across the sprite's main axis.
Math and Computer Concepts:
- Pencil Code searches for images from a public database. Preface the search term with
t-
to have it return an image with a transparent background. The example on the right shows the results from'fish'
and't-fish'
, both drawn on a blue background. - Specify an image of your choice by referencing a string showing its web address, or URL (Uniform Resource Locator) as the argument to the
Sprite
constructor orwear
function. The URL must reference ajpg
,png
, orsvg
file. - The variables
on
andoff
are aliases for the Boolean valuestrue
andfalse
. An alias is simply a second name for something. In this case,on
andoff
are useful because they are easier to read.
Activities:
Save each of the following in a folder called Images.
- Code a DogAndCat program which loads images for
't-dog'
and't-cat'
, making the cat half as tall as the dog. Then replace these images with images from the web that you like better. - Find images of hair without a face (such as from pngall.com) and add it to a sprite. Size and position the hair on top of your CrazyFace sprite, or an image of a real person, saving the result as Hairdos.
- Create a SharkTank containing an image of a shark. Use
slide
andmirror
to make the shark swim back and forth realistically. Also, usegrow
to make the shark appear to move closer and further away. - Using images, either create a Seascape with a variety of sprites for sea life in front of an image of water and sand; or generate a HauntedHouse by superimposing images of ghosts or other spooky characters in front of a suitable background image; or come up with an analagous concept of your own design. Whatever your choice, be sure to move sprites around realistically using
slide
andmirror
.