Timber Game: Adding and Exploring Assets

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson
Download our mobile app to listen on the go
Get App

Questions and Answers

In the context of the Timber!!! game project setup, what is the primary purpose of creating separate 'graphics', 'sound', and 'fonts' folders within the project directory?

  • To reduce the overall size of the project folder.
  • To organize project assets for easier management and access. (correct)
  • To improve the game's loading speed by segmenting asset types.
  • To enable automatic asset optimization by the Visual Studio IDE.

When an image is drawn at the coordinates (960, 540) on a 1920x1080 screen, what part of the image is located at those coordinates, assuming the image's origin is at (0,0)?

  • The top-left corner of the image. (correct)
  • The part of the image that does not render, as the sprite is out of bounds.
  • The center of the image.
  • The bottom-right corner of the image.

When drawing a sprite on the screen, what role does the sprite's origin play?

  • It determines the point of rotation for the sprite.
  • It controls the layering order of sprites on the screen.
  • It specifies the color palette used for rendering the sprite.
  • It defines the point that will be located at the sprite's specified coordinates. (correct)

What is the purpose of the // symbols in C++ code?

<p>They denote single-line comments that are ignored by the compiler. (A)</p> Signup and view all the answers

What does the term 'syntax error' refer to in the context of C++ programming?

<p>A mistake in the code's structure or grammar that prevents the program from compiling. (B)</p> Signup and view all the answers

What is the purpose of the return 0; statement in the main() function of a C++ program?

<p>It indicates that the program executed successfully. (A)</p> Signup and view all the answers

What does the #include directive do in C++?

<p>It incorporates the contents of another file into the current source file. (C)</p> Signup and view all the answers

What does OOP stand for in the context of programming?

<p>Object-Oriented Programming (C)</p> Signup and view all the answers

What is the relationship between a class and an object in object-oriented programming?

<p>A class is a template for creating objects. (B)</p> Signup and view all the answers

In SFML, what is a Sprite?

<p>A 2D game object that can be drawn to the screen. (A)</p> Signup and view all the answers

What is a 'namespace' used for in C++?

<p>To organize code into logical groups and prevent naming conflicts. (C)</p> Signup and view all the answers

In SFML, what is the purpose of the VideoMode class?

<p>To define the resolution and color depth of the display window. (C)</p> Signup and view all the answers

What is the role of the RenderWindow class in SFML?

<p>To create and manage the application window for rendering graphics. (B)</p> Signup and view all the answers

What is a 'game loop', and what is its primary function?

<p>A continuous cycle that handles input, updates game state, and renders graphics. (C)</p> Signup and view all the answers

What are the fundamental steps involved in a basic game loop?

<p>Get input, update scene, draw scene, repeat. (A)</p> Signup and view all the answers

What is the purpose of the window.clear() function in SFML?

<p>It erases the previous frame from the window. (D)</p> Signup and view all the answers

What does 'double buffering' refer to in the context of graphics rendering?

<p>A technique using two frame buffers to prevent screen tearing. (B)</p> Signup and view all the answers

In SFML, what role does the Texture class play?

<p>It manages graphical images stored in memory on the GPU. (B)</p> Signup and view all the answers

What is the relationship between a Texture and a Sprite in SFML?

<p>A <code>Sprite</code> displays the image contained within a <code>Texture</code>. (C)</p> Signup and view all the answers

Why is it beneficial to disassociate the Texture from Sprite in game development?

<p>To optimize memory usage and decouple manipulation from the source image. (D)</p> Signup and view all the answers

What is the role of the modulus operator (%) when generating random numbers?

<p>It returns the remainder of a division, limiting the range of random numbers. (A)</p> Signup and view all the answers

What is the purpose of 'seeding' the random number generator?

<p>It initializes the random number generator with a starting value to produce different random sequences. (D)</p> Signup and view all the answers

What value is appropriate as a seed?

<p>The current time. (C)</p> Signup and view all the answers

What is the significance of 'frame rate' in game development, and how does it impact the player's experience?

<p>It is the number of times per second that the game loop gets executed, affecting the smoothness of animations and responsiveness of controls. (A)</p> Signup and view all the answers

What is the SFML Clock class used for?

<p>Measuring elapsed time and managing game timing. (C)</p> Signup and view all the answers

What does the term 'delta time' refer to in game development, and how is it typically used?

<p>The time elapsed since the last frame, used to ensure consistent game behavior regardless of frame rate. (D)</p> Signup and view all the answers

How does using delta time contribute to frame rate independence?

<p>By ensuring that game logic and physics calculations are based on real-time rather than frame intervals. (C)</p> Signup and view all the answers

What is C++ String concatenation, and how is it commonly achieved?

<p>Combining multiple strings into a single string, often done using the addition operator (+). (C)</p> Signup and view all the answers

What advantages does the sstream class provide in C++ when working with strings?

<p>It allows you to dynamically format strings by converting numbers and other data types to strings. (D)</p> Signup and view all the answers

Which of the following describes the process of implementing a Heads-Up Display (HUD)?

<p>Drawing information on screen, such as score, health, and time, to provide feedback to the player. (C)</p> Signup and view all the answers

If a Text sprite needs to be repositioned on the screen, what factor needs to be considered?

<p>It may be necessary to recalculate .setOrigin, and then setPosition again. (D)</p> Signup and view all the answers

With all of the Timber!!! assets loaded, which of the following is the proper draw order of the sprites?

<p>background, clouds, tree, bee, time-bar (A)</p> Signup and view all the answers

Flashcards

What is a Sprite?

A visual, 2D game object made from an image file.

What is a Sprite's origin?

The (0,0) coordinates of a sprite; its location anchor.

What does #include do?

Tells Visual Studio to add another file's content before compiling.

What is OOP?

Way of coding that structures code as reusable, maintainable classes.

Signup and view all the flashcards

What is a Class?

A template to make objects.

Signup and view all the flashcards

What is an Object?

A coded and functioning copy of a class.

Signup and view all the flashcards

What is a namespace?

Ensures class names don't conflict.

Signup and view all the flashcards

What does 'using namespace sf;' do?

Removes the 'sf::' prefix from SFML code.

Signup and view all the flashcards

What does VideoMode do?

Represents the resolution of the player's screen.

Signup and view all the flashcards

What does RenderWindow do?

Creates a window using SFML; is like the blank canvas

Signup and view all the flashcards

What is Style::Fullscreen?

Value defined in SFML, to set fullscreen mode.

Signup and view all the flashcards

What is the game loop?

Basic structure of a game; repeat until the game is closed.

Signup and view all the flashcards

What are the four phases of the game loop?

Get input, update, draw, and repeat

Signup and view all the flashcards

What is window.clear()?

Removes the previous frame of animation.

Signup and view all the flashcards

window.display()

Draws game objects to a hidden surface, ready to be displayed.

Signup and view all the flashcards

What is Tearing?

Graphical glitch, where the scene has all the sprites added to it

Signup and view all the flashcards

What is Texture Memory?

Graphics memory that is finite

Signup and view all the flashcards

What is casting?

The process of conversion from one type to another.

Signup and view all the flashcards

Why measure Time for the bee and clouds?

Method used to help implement time by measuring it

Signup and view all the flashcards

What does dt mean?

Delta Time, the time between 2 frames.

Signup and view all the flashcards

Variable Scope

Data inside if block is actually invisible outside of the if block.

Signup and view all the flashcards

What is the sstream class?

Useful for combining strings and other Variable sizes

Signup and view all the flashcards

Time Bar

Simple red rectangle on the screen that shrinks

Signup and view all the flashcards

timeBar

Object of the Rectangle shape type.

Signup and view all the flashcards

Study Notes

Adding Assets to the Project

  • It's time to integrate them into the project after deciding on the assets.
  • It's assumed the assets from the book's download bundle will be used
  • Use same file name when replacing default sound or graphic file with your own.
  • The steps for adding assets include:
    • Go to the project folder: D : \VS Projects\Timber
    • Create three new folders: graphics, sound, and fonts
    • Copy the contents of Chapter 1/graphics into the graphics folder
    • Copy the contents of Chapter 1/sound into the sound folder
    • Download the Komika Poster font from http://www.1001freefonts.com/komika_poster.font
    • Extract the contents of the zipped download and add the KOMIKAP_.ttf file to the fonts folder

Exploring the Assets

  • Graphical assets constitute scene sections for the Timber!!! game.

Screen and Internal Coordinates

  • Images viewed on monitors consist of pixels, tiny light dots
  • Gaming monitors are often 1,920 pixels horizontally and 1,080 pixels vertically
  • Pixels are numbered from the top left of the screen
  • In a 1,920 x 1,080 example, pixels are numbered from 0 to 1,919 on the x axis and 0 to 1,079 on the y axis
  • Specific screen locations are identified using x and y coordinates when drawing characters, backgrounds, bullets, and text
  • A 1,920 x 1,080 screen's approximate center is at position 960, 540
  • Game objects have their own coordinate systems, beginning at 0,0 in the top left-hand corner
  • Location 0,0 of a character is drawn to position 960, 540 on the screen
  • Visual, 2D game objects like characters or zombies are called Sprites, typically made from an image file with an origin

C++ Code and Comments

  • The subsequent line observed is int main().
  • int represents a data type.
  • C++ supports multiple types of data; an int signifies a whole number or integer
  • main () is a section of code marked by opening ({) and closing ({) curly braces, referred to as a function
  • Every C++ program contains a main function where the running of the program begins.
  • The opening curly brace of the function main always begins execution.
  • The return keyword, alone or followed by a value, instructs the program to go back to the code that started the function and has importance.
  • The running of the program jumps back to the code that initiated the function
  • The operating system initiates the main function
  • Return 0 indicates that the main function exits and the program ends
  • Adding 0 after the return keyword also sends that value to the operating system
  • The code responsible for initiating a function is said to "call" the function, while the function "returns" the value

Opening A Window Using SFML

  • The code opens a 1,920-pixel by 1,080-pixel full-screen borderless SFML window
  • The #include directive directs Visual Studio to add the contents of another file before compiling.
  • Some code not written becomes part of the program
  • "Preprocessing" refers to adding code from other files
  • The file extension .hpp indicates being called a header file
  • The #include statement enables preprocessor inclusion of content in the folder named SFML

Understanding Objects

  • The code creates a vm object from the videoMode class setting up two internal values, 1920 and 1080
  • These values represent the resolution of the player's screen.
  • The code creates a window object, which is a new object named window provided from the RenderWindow
  • Furthermore, are setting up some values inside the window object.
  • A class is like an architectural blueprint, which allows creation of usable objects like houses

The Main Game Loop

  • A method to keep the program running until quitting is desired is needed, alongside marking different code sections.
  • Add the following highlighted code to the existing code and then going through it:
int main(){
    // Create a video mode object
    VideoMode vm(1920, 1080);
    
    // Create and open a window for the game
    RenderWindow window(vm,"Timber!!!", Style::Fullscreen);
    
    while(window.isOpen())
    {
    }
}

Loops

  • Everything between the opening ({) and closing (}) brackets of the while loop will continue to execute, over and over, potentially forever.
  • Once the window object is set to close, the execution of the code will break out of the while loop and move on to the next statement

Input, Update, Draw and Repeat

  • Although this first project uses the simplest version of a game loop, every game needs these phases in the code.
    • Get the player's input (if any).
    • Update the scene based on things such as artificial intelligence, physics, or the player's input.
    • Draw the current scene.
    • Repeat these steps at a fast-enough rate to create a smooth, animated game world.

Detecting a Key Press

  • The following code checks whether the Esc is being pressed:
if (Keyboard::isKeyPressed (Keyboard::Escape))
{
    window.close();
}

Preparing the Sprite

  • An object from the Sprite class requires an object from the Texture class to display itself as an image.
  • The following highlighted code is to be added:
int main(){
    // Create a video mode object
    VideoMode vm(1920, 1080);
    
    //Create and open a window for the game
    RenderWindow window(vm,"Timber!!!", Style::Fullscreen);
    
    // Create a texture to hold a graphic on the GPU
    Texture textureBackground;
    
    // Load a graphic into the texture
    textureBackground.loadFromFile("graphics/background.png");
    
    // Create a sprite
    Sprite spriteBackground;
    
    // Attach the texture to the sprite
    priteBackground.setTexture (textureBackground);
    
    // Set the spriteBackground to cover the screen
    spriteBackground.setPosition(0,0);
    
    while(window.isOpen())
    {
    }
}
  • In the highlighted code, the object called textureBackground from the SMFL Texture class is first created.
  • The graphics is loaded from the folder using the textureBackground into textureBackground

Key Notes

  • Textures load slow to the GPU.
  • Textures are fast access once they are on the GPU.
  • Texture is associated with Sprite object.
  • Update the scene with position and orientation of Sprite objects
  • Draw the Sprite object, which displays the Texture object in the Draw the scene section.

Game with Sprite

  • Finally, the new Sprite object (spriteBackground) should be seen after using the double buffering system with window object.

Adding More Sprites

  • You can add a tree, bee, and three clouds as sprites to the Timber!!! game.

Preparing, Drawing, and Moving Bee and Clouds

  • Bee are created in the same way we created a background and a tree.
  • For determining if the bee is active, there is a bool variable.
  • There are three bool variables for determining whether each cloud is active and three float variables to hold the speed for each cloud.
  • Apart the minor texture issue, the code we have just added is nothing new compared to the bee.

Random Numbers

  • Two functions, alongside syntax and steps, are needed to create a random number
  • Seeding the random number generator is required to make it not constantly return the same value
  • Seed the random number generator with the time
// Seed the random number generator with the time
srand((int)time(0));
  • The preceding code gets the time from the PC using the time function, that is, time(0)
  • The call to the time function is enclosed as the value to be sent to the srand fuction
  • With a conversion cast, the long code does the flowing conversion from one type to another:
    • Gets the time using time
    • Converts it into an int
    • Sends the value to srand, which seeds the random number generator

Timing

  • Consistently make game runs smoothly because C++ and SFML are exceptionally fast to draw sprites

Frame Rate Solution

  • Determine and use the frame rate from controlling games
 // How fast is each cloud?
float cloud1Speed
= 0;
float cloud2Speed = 0;

// Variables to control time itself
Clock clock;
while(window.isOpen())
{
  • Before we can move the bee and the clouds, we need to consider timing.

Time Control

  • clock type and name is delcared. with first class name and the object name the the clock type.
  • Now, in the update section of the game, add the following new value:
 time dt = clock.restart();
  • Then, add following to bee or cloud to give consistent time:
spriteBee.setPosition(spriteBee.getPosition().x - (beeSpeed dt.asSeconds())}, spriteBee.getPosition().y)

Pausing the Code

  • To pause the code:
// Start the game
if (Keyboard::isKeyPressed (Keyboard:: Return)){
    pause = false;
}

Time and the HUD

  • We can load, assign, and initialize SFML objects from String values.
  • Implemenation is to the heads up display (HUD)
  • sstream class provides useful implementation is combining Strings and other variable types
  • When a different font is chosen from the existing, modification will be needed for existing code.

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

Related Documents

More Like This

Use Quizgecko on...
Browser
Browser