CS 70

Homework 3: Set Your Sprite in Motion

Until now, our sprites haven't moved, but our animation will be more interesting if the sprite can move around the screen. In this step, we'll add the pieces needed to get things moving!

Sprites will get a flag saying whether they should scroll (move one column to the right on every “redraw”). On every “redraw”, the Asciimation object will ask the Sprite whether it should be scrolling and, if so, tell the Sprite to move one column to the right.

If a sprite moves off the right edge of the screen, it should wrap back around to the other side of the screen. In particular, when its topLeftColumn reaches the right-most side of the movie screen, it should reposition itself just off the left side of the asciimation frame, so that in the next update, its rightmost column will appear on the left side of the movie frame.

Your Task

Add scrolling functionality to the Sprite and Asciimation classes:

  1. In the Sprite class, add

    • A shouldScroll_ private data member (boolean) that stores whether the sprite should be animated.
    • A setScrolling(bool flag) member function that sets the value of shouldScroll_.
    • A getScrolling() member function that returns the current value of shouldScroll_.
    • A moveRight(int maxWidth) member function that changes the Sprite's location one spot to the right and handles wrapping.
  2. In the Asciimation class, modify

    • The updateContents member function to check if the Sprite is scrolling and, if so, move it one step to the right before copying its character array.
  3. In your constructor, turn on scrolling for the sprite.

Helpful Hints

  • LHS Cow speaking

    Here are some tips for implementing sprite motion!

Wrapping Around the Screen

When working on the sprite wrapping, drawing a picture may help you sort out how to make that happen with maxWidth (which should be passed as the MOVIE_WIDTH value from the Asciimation class) and SPRITE_WIDTH.

The sprite should disappear completely off the right edge before reappearing on the left. When topLeftColumn_ equals maxWidth, the sprite is completely off-screen to the right. At this point, you should reset it to -SPRITE_WIDTH so it starts completely off-screen to the left.

Testing Motion

After implementing these changes,

  1. Compile and run your program.
  2. You should see the sprite scrolling smoothly from left to right.
  3. When it goes off the right edge, it should reappear on the left.

Smooth Animation

The play() function includes a small delay between frames to create smooth animation. If the sprite seems to be moving too fast or too slow, you can adjust the delay value in the play() function.

To Complete This Part of the Assignment

You'll know you're done with this part of the assignment when you've done all of the following:

(When logged in, completion status appears here.)