Homework 3: Define the Sprite
Class
Sprites are normally colorful little creatures, but for us they'll just be a simple ASCII image.
In this step, we will define a new Sprite
class. A sprite is a collection of characters which will be displayed in our asciimation. Together, this collection of characters forms an image that we will eventually move around the screen as it is displayed.
Your Task
The Sprite
class stores two things:
- A fixed-size image, where the width denotes the number of columns and the height denotes the number of rows.
- A position (row and column) which is the row and column where the top-left corner of the
Sprite
's image should be drawn.
The Sprite
class should have the following private data members:
- An
int
calledtopLeftColumn_
that stores the column where the top-left corner of the image should be drawn on the screen. - An
int
calledtopLeftRow_
that stores the row where the top-left corner of the image should be drawn on the screen. - An array of characters of length
SPRITE_WIDTH * SPRITE_HEIGHT
calledcontents_
that holds the contents of the image.
To build our asciimations, non-Sprite
objects will need to be able to read the location and contents of our sprites. Those data members are private, which is good—it means that other objects can't change them unless we let them! We will provide access to these values through the following public member functions to the Sprite class:
- A
getTopLeftColumn()
function that returns the sprite's current x-location. - A
getTopLeftRow()
function that returns the sprite's current y-location. - A
getCharAt(int row, int col)
function that returns thecol
th character in therow
th row of the sprite's character array. - A
setLocation(int x, int y)
function that moves the sprite to the location (x,y).
We've given you the start of a parameterized constructor for a Sprite
. It takes three parameters:
- A reference to a string that represents the filename containing the characters we should read into the
Sprite
's contents and - Two
int
s giving the initial location of theSprite
.
There's still one (commented) gap in the constructor, and we're not currently doing anything with two of the parameters. It'll be up to you to finish the function so that it initializes a valid Sprite
object.
Helpful Hints
These hints will help you write your
Sprite
class!
Static Constants
The Sprite
class has two public static constants:
static constexpr int SPRITE_WIDTH = 40;
static constexpr int SPRITE_HEIGHT = 10;
These define the fixed size for all sprites in our program.
Reading the Sprite File
The sprite files have a specific format:
- Line 1: The width (should be 40).
- Line 2: The height (should be 10).
- Line 3: All 400 characters of the sprite in a single line.
The starter code includes error checking for the dimensions. You'll need to read the characters into your contents_
array.
Array Arithmetic
For the getCharAt
function, a little array arithmetic should get you the right offset into the one-dimensional character array. Consider drawing a picture of your sprite as a 2-D grid and numbering each item with its index in the array. What formula will turn the row and column into the index?
Invalid Inputs
Think about what you want your getCharAt
function to do if it's given an invalid row or column. From the standpoint of the assignment specification, that behavior is undefined. One option is to just assume that you'll always get good inputs—hey, it could happen! More complicated choices, however, can make debugging easier. For example, you might choose a deterministic character that you'll always return if you get bad inputs to the function. Or you might throw an error (take a look at the starter code for asciimation.cpp
, which generates an error if it can't read from the sprite file). Your choice here won't affect your grade, but might make your coding process smoother.
Disabling the Default Constructor
It won't make any sense to have default Sprite
s, so disable the default constructor.
(When logged in, completion status appears here.)