**This is an old revision of the document!**

Rub Past the Shrub - Infinite car game tutorial

This tutorial will help you get more familiar with GDevelop. Our primary focus in this tutorial will be to make an infinite car game without actually moving the background. We shall dodge the trees on the road and simultaneously, increase the score as we get past the trees.

Download GDevelop

If you do not have GDevelop yet, you can do so from GDevelop's official website. It is available for Windows, macOS as well as Linux.

After downloading GDevelop, you can proceed with the installation/extraction followed by the launching of GDevelop.

Create a new project

Click on CREATE A NEW OBJECT on the “START PAGE”. A dialog box appears.

Click on Empty game (you might have to scroll down to find this option) and choose the location, where you want to save your project.

Create a new scene

Scene is the area where we'll work for most of our time while creating the game. As soon as we click on Empty game, we see a panel on the left side of our screen. This is the Project Manager. Click on the Scenes option in the Project Manager.

Create a new scene using + button beside the “Click to add a new scene” option from the drop-down that appears. Our new scene has been created. Go to your new scene by clicking on “NewScene”.

You will need a few files, to create objects for this game. You can download them here. Download and extract the .zip file to the directory, that you have chosen as the location of our game.

Create a new object

Choose the + button besides “Click to add a new object” from the Objects panel on the right side.

Choose Sprite from the “Add a new object” pop-up. This will be followed by the appearance of a dialog box.

You can name the object as per your wish, but in this tutorial, we're going to name it Car. Let's add an animation to the object. Animations are containing images that are displayed on screen for Sprite objects.

To add an animation to the object, click on the + symbol beside “Click to add an animation”.

Add the image named 'car.png' as animation and click on the APPLY option.

Similarly, add a new Sprite object Highway which consists of the image, 'highway.png' as the animation.

Our next step will be to add a Sprite object Tree1 which would have the image 'tree.png' for the animation. This will be the object that we need to dodge.

Create a new Sprite object Tree2 which also has 'tree.png' for its animation. This will be the object we will mainly use to simulate a moving background.

Create a new Sprite object Explosion and select all the images from 'Explosion1.png' to 'Explosion8.png'.

Your scene by the end of this step should contain 5 objects.

Adding top-down movement to the car

Since the Car is to be controlled by the player, we add a behavior to it. First, we need to open the Object Editor.

To do this, you can either double-click on the Car object from the object side panel or click on the three-dot-menu besides the object and click on edit object.

Click on the BEHAVIORS tab at the top of the panel and click on the + button besides Click to add a behavior to the object.

Choose Top-down movement in the Add a new behavior to the object pop-up. This would open a wide variety of options to alter.

You can see the default values for the properties of the behavior. Set the deceleration to 400 and untick the boxes having the options Default controls and Rotate object and click APPLY. Rest of the default values are fine for our game.

Creating a new event

An event is a pair of conditions and actions. When a given set of conditions is true, the corresponding action or set of actions is performed.

While making this game, we will encounter a few events with no conditions and one or more actions assigned to them. In such a case, the action is performed on every frame created.

To add a new event, open the NEWSCENE (EVENTS) tab from the top

From the events editor toolbar, choose the option that says Add a new empty event.

This will create a new event, with an empty condition and an empty action.

Create instances of Car and Highway

To create an instance of the Car object, we can drag it from the Objects panel to the scene and give it the coordinates of our choice.

You need to click on the object Car in the Objects panel and drag it to the scene while holding the click. After getting it to the desired position, you can leave the click.

If you need to alter the coordinates of the object after getting it on the scene, you can do so by the click-and-drag method, but by dragging the object on the scene, rather than the one on the Objects panel.

Another method to move the object is by changing the X and Y-coordinates in the Object properties panel on the left side.

You can choose the values of your choice, but if you want results similar to that of the tutorial, you are expected to have the same values.

Similarly to create an instance of Highway, you can click-and-drag the object to the scene and alter its X and Y-coordinates.

At this point, if you wish, you can preview your project by clicking on Launch a preview of the scene icon above the scene.

You should see something similar to the image shown below.

We see a few problems:

  • The Car is not fully visible.
  • The part of the screen below the Highway is empty.
  • The Highway is too far away from the center of the screen.

Making the car visible

To make the car visible, we need to know the reason for it being hidden under the Highway. The answer is, Z Order.

Z Order of an object is the property, which decides whether the object should be displayed on the front or the back. An object with a certain Z Order assigned to it will always be shown in front of the objects with lower Z Order value and behind the objects with higher Z Order value.

In our case, we can assign a Z Order of 1 to the Highway and 2 to the Car.

Creating the bottom part of the Highway

To create the bottom part of the Highway, we can elongate the height of our object Highway.

To do this, you can either click and drag the white box at the corner of the Highway, or you can tick the box besides Custom size option in the Properties panel, and enter height and width of your choice.

Getting our game to the center of the window

To get our game to the center of the preview window, we will create our first event.

But before that, we need to determine the X Position and Y Position of the camera.

The coordinates of an object depict the location of the top-left corner of our object, rather than the center of the object.

For the X position, the camera has to be at the center of the window/highway. The width of the Highway is 440 pixels. So the center of the Highway will lie at 220 pixels.

Similarly, to set the Y position of the camera, we can keep it a little above the Car so that the player can get a better view of the trees approaching. In this tutorial, we will set it at 350 pixels. You can use the values of your choice.

First, create a new empty event using the explanation above.

We need to make an event such that the game lies in the center of the screen. Therefore, setting the X and Y-coordinates will be our action.

This action needs to be true for all frames, i.e. for all conditions. And so that the action is true for all conditions, we enter no condition.

To add an action, click on the “Add action” option.

To choose an action, you can either choose it from the various drop-down menus available or you can use the search bar at the top.

For example, to change the position of the camera, you can go to Layers and Cameras, followed by Camera center X position. The right part of the window will now display configurable parameters.

Because we want to set the X position equal - neither less nor more - to 220, choose the = (set to) option from the Modification's sign. In the Value field, you can enter the desired X coordinate of the camera.

Else, you can search for the action in the top Search bar.

Click OK.

You can now see the added action in the Events Editor.

To set the Y position of the camera center, click on Add action in the same window.

You can now follow a similar procedure for Y position as you did for X position but make sure that this time you choose the Camera center Y position option and enter the Y coordinate.

Your preview should now look something similar to this:

To make sure that all your progress has been saved, click on Save in the File menu, or press, Ctrl + S.

Moving the car

To move the Car, we need to create an event such that the car translates as well as rotates to some degree when left or right key is pressed.

As we can deduce from the above statement, the condition of the event is left key press and right key press.

Remember that each key press (left and right) should have different events because we need to have different motions for each of them.

We will start with the event that has the condition of left key press.

For the action, when the left key is pressed, the Car should have a movement on the left side. This can be easily done using an action Simulate left key press that would simulate the left side movement for the object selected (Car).

But, make sure to use the action with the same behavior as your object (top-down movement). So, we need to choose it from the Top-down movement drop-down.

Now, we need to give our Car the desired rotation for a better visual experience. If we just give rotation to the object, it will rotate up to 180 degrees.

To avoid such a result, we need to set a final angle too. We will assume it to be 30 degrees on both sides, i.e. -30 degrees for the left key press and 30 degrees for the right key press.

But, before we move ahead, we also need to assign the Angular speed for the rotation. We are using a value of 60 degrees/second for the same.

Remember that the angular speed will be the same for both the key presses. It cannot be negative, because, it is the “Angular speed” and speed cannot be negative.

We can repeat a similar event for right key press.

If you preview your project, you should be able to control your Car using the left and right arrow keys.

But we still encounter one problem, the car does not return to its original state when we leave the keys. Instead, it stays oriented at an angle. Let's solve this problem now.

Returning the car to its original orientation

We need to return the car to an angle of 0 degrees when none of the arrow keys are pressed. In other words, we need to return the car to an angle of 0 degrees when the conditions, right key press and left key press are inverted.

To achieve this, we add a new event and try to add the above 2 conditions, i.e. right key press inverted and left key press inverted. We add a condition Left key is pressed, and turn on the switch below, that says Invert conditions.

In the same event, we add a similar inverted condition for right key press.

For the action, we will make an action for rotating it towards 0 degrees with an angular velocity of choice, similar to what we did for the non-inverted conditions of key press.

Make sure to include both the conditions in the SAME EVENT, because we want the vehicle to return to 0 degrees only when neither left nor right arrow key is pressed.

Now, the car moves and rotates to an extent when an arrow key is pressed and rotates back to the original angle when the keys are released.

Setting the background color

If you wish, you can change the background color of the scene.

To do so, you can right-click anywhere on the scene and select Scene properties.

Click on the box below Scene background color that says Click to choose.

You can choose the color of your choice and click OK in the Scene properties.

Next Step: Create trees and simulate a moving background

Although we have given motion to the Car, it still doesn't look like moving in any other direction other than the two sides. To simulate a moving background, we will make use of the objects Tree1 and Tree2.

To read the next part of the tutorial, click here.