Adding dirt
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:
int array[10];
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:
for(int c=0; c < 10; c++)
{
array[c] = c;
}
- Definition of the counter: int c=0;
- Condition: c < 10;
- Action: c++
c++;
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:
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);
}
for(int c = 0; c < 10; c++)
win.draw(dirtTiles[c]);
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]