2D Racing Game
This is a tutorial for a basic 2D racing game in Godot. This will follow on from previous learning we did in the 2D Platformer. We will be starting with a new project.
Please download the asset pack linked here (it is free so you don’t need to pay).
Making the Car
-
Make a CharacterBody2D, then give it 2 child nodes: Sprite2D and CollisionShape2D.
-
On sSprite2D, drag and drop spritesheet_vehicles.png into the empty texture in the inspector.
-
In the region drop down, enable the region, click on edit region, and then cover a single car using the red dots.
-
For the Collisionshape2D, select new CapsuleShape2D and cover the car.
-
Attach a script to CharacterBody2D and replace the code with this:
This will allow your car to turn, accelerate, and decelerate.
-
To test this, create a new scene and give it a Node2D, give it a child node Camera2D and your car scene as a child node. If you now play the track scene, you should be able to drive around.
Making the Track
-
Give track1 a TileMapLayer and name it Foreground. This is where we’ll make the track.
-
For the tileset, open the spritesheet_tiles.png. in the TileSet menu, set the texture region to 128 by 128 and in the inspector click on TileSet and change the tile size to 128 by 128.
-
Now you can draw your own track.
-
If you want to make a background, create a new TileMapLayer by duplicating the foreground, and draw using that as your base.
Making the Second Car
-
Duplicate the car scene and name it car2.
-
In Project > Project Settings > Input Map, we will create other keys for the second player to use so that both players can use one keyboard. In the “add new action” box, type in something like Left2 and then click add.
-
On the right, click the plus button and and then press a key to represent it. E.g. for left you could use a, right is d, backward is s, and forward is w. Repeat the steps for all 4 directions.
-
Now go into the car2 script and change the
"ui_left"
,"ui_right"
etc. to the inputs you made before. Now you can use the second car on the same keyboard!
Making the Menu
-
Make a new scene with a Control node. Add a Label child node. In the inspector will be a text box - write Racing Game or whatever you want to call it.
-
In the control section of the inspector there is a dropdown for theme overrides. This is where you can get creative and change the colors, the fonts, outlines etc. Save this scene as main.
-
Make a new scene and give it a Button node. Change the text the same way as the Label in the previous step. Type in Level 1 or whatever you want to call it. You can change the style a similar way to the text with theme overrides, but now there is an extra styles menu. You can customise the button to look however you like.
-
Attach a script to the button. In the node menu, connect the Pressed signal to the script and change the code to this:
-
Now in your menu script you can add in the button for track one. In the inspector near the top you should see a text box for Level. Right click on your track1.tscn and copy the path and paste it into that text box.
-
Now when you run the menu (main) and click the button, it should load the track and you can now play it. To add more levels, simply add another button and change the path in the Level text box.
Next Steps
- Add more levels and connect them to your menu
- Add items to pick up
- Make an end screen
- Make your menus more sophisticated - how could you add a settings menu?
- Make a character selection menu