CSCI202 Practice Test 1

Structs

Write the declaration for a structure called SpriteStructthat will hold the following:

  • a Texture2D representing a texture to be drawn on the screen.
  • a Vector2 representing a position on the screen.
  • a Rectangle representing the source on the sprite sheet
  • a Color representing the color to be used when drawing the image
  • a SpriteEffect representing whether the sprite has been flipped horizontally, verticall, or not at all.

Declare a variable of type SpriteStruct called runner.

Spritesheets

The file Runner.png(in your Content folder) is a sprite sheet with 30 images, all on the same row. The sprite sheet is 1800 pixels wide by 74pixels tall, with each image 60 pixels wide.

Read the spritesheet into your program. Save it in the Texture2D field of the runner variable that you declared above.

Initially set the source rectangle to (0, 0, 60, 74).

Set the Color of the runner to White.

Set the SpriteEffectof the runner to None.

Position the runner on the left bottom part of the screen. Since each sprite is 74 pixels tall, position the sprite 74 pixels above the bottom of the screen. Make the sprite run by updating a frame (that is, moving the source rectangle one frame to the right) on every third tick of the clock. When you get to the 30th frame in the sprite sheet, go back to the first frame and start over. Make him move to the right one pixel on each tick of the clock. Make the runner run until he gets to the right edge of the screen and then have him turn around and run back to the left edge of the screen. Do this forever. Note that he moves position on every tick of the clock, but the source frame is updated on every third tick of the clock. Declare a speed variable that is an integer that keeps track of the runner's speed per tick (in pixels). Also declare a direction variable that is also an integer that keeps track of the runner's direction. When the direction is +1, the runner will move to the right (direction * speed is a positive number). And when the direction is -1, the runner will move to the left (direction * speed is a negative number). If you do this, reversing the direction just involves multiplying the direction times -1.

When drawing the runner, use version 6 of the Draw method.

Scaling

Create a floating point variable to set the scaling of the runner (this will be one of the arguments to the Draw method). Set its default value to 1. Allow the user to increase the scale by 2% by pressing on the "X" button on the gamepad. Allow the user to decrease the scale by 2% by pressing the "Y" button on the gamepad. Do not allow the scale to drop below .5. Note that this may cause you to have to change the method that you use to draw the runner. Note that when you increase the size of the runner, his feet may drop below the bottom of the screen. Don't worry about this (although it's easy to fix).

Gamepad input and procedures/methods

Use the gamepad to dothe following.Declare any needed variables. Write a method for each item below. Note that these are very short methods.

Write a void method called ReverseDirection. When the user presses the "B" button, immediately reverse the direction of the runner.

Write a void method called SpeedUp. When the user presses the left thumbstick up (the Y value will be positive), immediately increase the speed of the runner by one pixel per tick. Only do this once for each time the user presses the thumbstick in the up direction. That is, only make the change when the previous state of the left thumbstick'sY valuewas 0 and the current stateis positive.

Write a void method called SlowDown. When the user presses the left thumbstick down (the Y value will be negative), immediately decrease the speed of the runner by one pixel per tick. Only do this once for each time the user presses the thumbstick in the down direction. That is, only make the change when the previous state of the left thumbstick's up direction was 0 and the current state is negative. Do not let the value of the speed drop below 0.

Arrays

Write the declaration for an array of SpriteStructscalled fighter.

Do the following in the Initialize method:

Write the code to set aside the memory for the array to hold 5 elements.

Do the following in the LoadContent method:

Load theFighter picture into the texture for all 5 array elements.

Initialize the Position vector to draw the pictures across the top of the screen at 100-pixel intervals. That is, the first item's upper left corner's X coordinate will be 0, the next will be 100, then 200, then 300, and then 400.

For the Source rectangle, use null.

Set the Color for each to White.

Set the SpriteEffect to None.

You must use a loop; you cannot write 5 sets of assignment statements.

Do the following in the Draw method:

Draw the pictures from the array using the data you computed in the LoadContent method. You must use a loop; you cannot write 5 Draw statements.

1