Create a new Scene with Area2D as root
Add Sprite2D and CollisionShape2D as children
Use the cactus.png as the Sprite2D texture
Set the CollisionShape2D to Rectangle
Add the Cactus to the obstacles group
Add an export variable for the cactus
Drag your cactus scene into the inspector
Create a function to spawn cacti
The obstacles introduce a new problem since they can spawn on the player or on the coins. We will create a helper function to spawn them in a safe area.
This function tries a few times to find a position at least 60–80 pixels away from the player and coins
Note that this function returns a valid position for the cactus to spawn at
Now update the spawn cactus function
To request a vailid spawn position from our helper
Add a line to clean up the cacti
In the game over function
What else could you add to the game to improve it?
Well done! You have completed the tutorial