Custom-Drawn Sprites!
Goals:
- Design your own sprites
- Continue to develop proficiency working with animation queues
Coding Snippets:
- The
Spriteconstructor works similarly to theTurtleconstructor. - Pass a color to the Sprite constructor to change the background color. Choose color
transparentto hide the background rectangle completely. drawoncauses one sprite to draw on another. Set the drawon argument towindowto make a sprite stop drawing on another sprite and go back to drawing on the background instead.
Math and Computer Concepts:
- Create a blank sprite using the syntax
new Sprite, then enable editing using thedrawonfunction. For example, to create an ant which then moves about, use the code on the right. - Aside from telling a sprite to stop drawing on another sprite, the function call
drawon windowimplicitly calls thesyncfunction, which prevents the sprite that is being drawn on from moving until the sprite doing the drawing is finished.
- Omitting the call to
drawon windowcan really mess things up. In the ant example, if you leave out this line, you will get a "smeary ant." (Copy the code, and try it out for yourself!) drawonis a sprite-specific function. Use dot notation to have a sprite other than the default turtle do the drawing. For example,r.drawon scauses spriterto draw ons.
Activities:
Save each of the following in a folder called Sprites.
- SpiralSprite: create a Sprite that has a spiral drawn on it. Then, move this spiral around the screen and make it spin.
- SpinArt: Intentionally leave out
drawon windowto create a sprite that looks like it had paint dribbled on it when it was spinning. Experiment with different speeds for each sprite (potentially mixing it up using the random function) to get the desired amount of smearing. - Create a SpaceShip and sail it around the screen. Enhance your program by drawing a starry sky for the background.
-
Create a Walker, which will consist of two versions of the same sprite. Make them move in unison, but alternating which one is visible, to give the illusion of legs moving. To improve the animation, use
speed Infinityand have the spritespausehalf a second before taking another 'step'.