Lab 02 - Spaceship Game
Use the game loop pattern to build a spaceship game using the SFML library.
1. SFML Set up
Follow these instructions for getting the SFML framework set up:
2. Write the code
Your code should be architected as follows:
Spaceship
Your Spaceship
class will handle the game state and rendering for the spaceship.
In your constructor, you should load the ship's texture and create the sprite. If you use a texture, make sure that the image file is stored in the same directory as your source code files, and that the file has been added to your project.
Alternatively, you could draw the ship using primitive shapes.
You can find some royalty free spaceship sprites here.
Your spaceship should also contain one or more methods for moving it around.
Finally, your spaceship should contain a method for drawing, which should probably match one of these signatures:
// If you're passing the window around by reference:
void Spaceship::draw(sf::RenderWindow & window);
// If you're passing the window around by pointer:
void Spaceship::draw(sf::RenderWindow* window);
Game
Your Game
constructor should create an instance of the RenderWindow
and set up any other initial game state.
Your Game
class should have a run()
method that runs the game loop until the program ends. The game loop should do the following:
1. Keep running so long as the window is open.
You can determine if the window is open with by calling the isOpen()
function on the game window.
2. Poll the window events to see if it should exit
If the user clicks the close button on the window, we need to tell the window to close:
sf::Event event;
while (_gameWindow->pollEvent(event)) {
if (event.type == sf::Event::Closed)
_gameWindow->close();
}
3. Handle Input
You need some way to handle input. SFML provides easy ways to check for keyboard and mouse input. Ideally your input handling should occur in a separate function called by the game loop.
Note that only the Game
class should have any notion of the keyboard or mouse. Your Spaceship
(and any other classes you use) should only know that it's being moved a certain direction, not how that movement came about.
4. Update the World
Your game loop shold call the update()
function of every object in the world. (For more information, see the Update Pattern description).
5. Draw Everything
Your game loop should clear the screen, then tell everything to render to the window, then render the window contents using the window's display()
function.
Note that the proper way to set this up is to call each object's draw()
or display()
method, passing in the window as a parameter.
Grading
Out of 100 points:
Architecture (50 Points)
0 Points - Code doesn't compile
10 Points - Eventhing is in main.
25 Points - There's a spaceship class, but its movement isn't abstraced correctly.
30 Points - There's a game and spaceship class, but main is still doing too much.
50 Points - The main function just initializes the Game class and starts the game loop. The Game class has multiple functions for each part of the game loop, and the spaceship class is properly architected.
Features (50) Points:
0 Points - Code doesn't compile
10 Points - The spaceship appears
20 Points - The spaceship appears and moves
35 Points - The spaceship appears and moves, and rotates
50 Points - The spaceship appears, moves with inertia, and rotates.
Submission:
- Navigate to the folder containing your source code.
- Create a zip file of your entire project.
- Upload that zip file to mySVU.