Adding dirt

Location
  1. Tutorials

    /

  2. Introduction to coding

    /

  3. Adding dirt

Window with a background of grass and 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];

We can access the elements of the array with their index (The first element has the index 0):

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; }

The sections inside the parentheses:

Note that the following:

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:

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:

1
2
3
4
5
6
7
8
9
10
11
12
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:

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

We draw each dirt tile:

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

main.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#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; }