Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
Introduction to coding - 4. Adding dirt

Introduction to coding - 4. Adding dirt

Adding dirt

1608515705328.webp


Arrays

When we have to create many variables of the same type, defining them individually may be inconvenient. In that case, it is better to create an array. An array is simply a group of variables which are individually accessed by their index within the array, rather than by their name.
Here, an array of 10 elements (variables) of type 'int' is defined:

C++:
int array[10];
We can access the elements of the array with their index (The first element has the index 0):
C++:
array[0] = 73;
array[4] = 32;

The loop for

Like the loop 'while', the loop 'for' is used to repeat a set of instructions. The loop 'for' is especially useful to iterate through arrays (Execute a set of instructions for each element within an array).

Inside its parentheses, the loop 'for' is divided into 3 sections: The counter, the condition and the action.

The counter is used to keep track of the number of times the loop has iterated, the condition is the condition that must be true for the loop to continue and the action is used to increment the counter. The action is executed after each iteration of the loop.

The following code assigns each element of the array with their index:
C++:
for(int c=0; c < 10; c++)
{
    array[c] = c;
}
The sections inside the parentheses:
  • Definition of the counter: int c=0;
  • Condition: c < 10;
  • Action: c++
Note that the following:
C++:
c++;
Simply increases the value of the variable 'c' by one.

The code inside the loop above has been executed 10 times and after its execution, array[0] equals 0, array[1] equals 1 and so on.

Adding the dirt tiles

To make the background of the game look less uniform, we will draw dirt tiles.

The code loading the dirt texture:
C++:
sf::Texture dirtTex;
    dirtTex.loadFromFile("resources/dirt.png");
We will display 10 tiles of dirt. Here, we define an array of 10 sprites and set their positions within the window:
C++:
sf::Sprite dirtTiles[10];

dirtTiles[0].setPosition(300, 60);
dirtTiles[1].setPosition(660, 300);
dirtTiles[2].setPosition(390, 480);
dirtTiles[3].setPosition(660, 60);
dirtTiles[4].setPosition(720, 420);
dirtTiles[5].setPosition(180, 300);
dirtTiles[6].setPosition(60, 180);
dirtTiles[7].setPosition(60, 420);
dirtTiles[8].setPosition(240, 420);
dirtTiles[9].setPosition(480, 120);
We assign the dirt texture to the sprites and scale by 5 each dirt tile:
C++:
for(int c = 0; c < 10; c++)
{
    dirtTiles[c].setTexture(dirtTex);
    dirtTiles[c].setScale(5, 5);
}
We draw each dirt tile:
C++:
for(int c = 0; c < 10; c++)
    win.draw(dirtTiles[c]);
Note that the order in which the sprites are drawn matters. We must draw the dirt tiles after the grass, otherwise, the grass will be drawn over them and will thus hide them.

The code of the game so far

[CODE lang="cpp" title="main.cpp"]#include <SFML/Graphics.hpp>

int main()
{
sf::RenderWindow win(sf::VideoMode(800, 600), "Introduction to coding", sf::Style::Close);

// Grass

sf::Texture grassTex;
grassTex.loadFromFile("resources/grass.png");
grassTex.setRepeated(true);

sf::Sprite grass;
grass.setTexture(grassTex);
grass.setScale(5, 5);
grass.setTextureRect(sf::IntRect(0,0,160,120));

// Dirt tiles

sf::Texture dirtTex;
dirtTex.loadFromFile("resources/dirt.png");

sf::Sprite dirtTiles[10];
dirtTiles[0].setPosition(300, 60);
dirtTiles[1].setPosition(660, 300);
dirtTiles[2].setPosition(390, 480);
dirtTiles[3].setPosition(660, 60);
dirtTiles[4].setPosition(720, 420);
dirtTiles[5].setPosition(180, 300);
dirtTiles[6].setPosition(60, 180);
dirtTiles[7].setPosition(60, 420);
dirtTiles[8].setPosition(240, 420);
dirtTiles[9].setPosition(480, 120);

for(int c = 0; c < 10; c++)
{
dirtTiles[c].setTexture(dirtTex);
dirtTiles[c].setScale(5, 5);
}

// Game loop

sf::Event event;

while(win.isOpen())
{
while(win.pollEvent(event))
{
if(event.type == sf::Event::Closed)
win.close();
}

win.clear();

win.draw(grass);

for(int c = 0; c < 10; c++)
win.draw(dirtTiles[c]);

win.display();
}

return 0;
}[/CODE]
Author
LTomy
Views
599
First release
Last update

Ratings

0.00 star(s) 0 ratings
Back
Top Bottom