Building a Basic 2D Platformer

← Back to Home

Step 1: Setting Up the Project

First, open Godot and create a new project. Name it "2D Platformer" and choose a location on your computer where you'd like to save it. Make sure to set the renderer to **2D** mode for this project.

After creating the project, you will be brought to the main editor interface. Let's create a new scene for our platformer. Click on the + button in the Scene panel to create a new 2D scene.

Step 2: Creating the Player

We will start by creating the player character. The player will be a simple rectangle for now, and we will add physics to it later.

  1. In the Scene panel, right-click and select New Node.
  2. Choose a **KinematicBody2D** as the main node for the player. This node will allow us to add movement and collision detection.
  3. Next, add two child nodes: a **Sprite** and a **CollisionShape2D**.
  4. For the sprite, you can either use a custom image or just a simple rectangle. For now, we'll use a rectangle.

In the Inspector panel, choose the **RectangleShape2D** for the CollisionShape2D. Adjust the size to match the player's sprite.

Step 3: Adding Player Movement

Now we will add movement controls. To do this, we need to write some GDScript to handle the player's movement and jumping.

Create a new script for the **KinematicBody2D** by clicking the **Script** icon at the top of the editor. Name the script Player.gd and attach it to the player node.

Basic Movement Code:

extends KinematicBody2D

# Variables for movement
var velocity = Vector2()
const GRAVITY = 500
const JUMP_FORCE = -300
const SPEED = 200

# Called every frame
func _physics_process(delta):
    velocity.y += GRAVITY * delta

    if Input.is_action_pressed("ui_right"):
        velocity.x = SPEED
    elif Input.is_action_pressed("ui_left"):
        velocity.x = -SPEED
    else:
        velocity.x = 0

    if Input.is_action_just_pressed("ui_up") and is_on_floor():
        velocity.y = JUMP_FORCE

    velocity = move_and_slide(velocity, Vector2.UP)

**Explanation:**

Step 4: Creating Platforms

Let's now create some platforms for our player to jump on. We'll make a simple platform using a **StaticBody2D** and a **CollisionShape2D**.

  1. In the Scene panel, right-click and select New Node.
  2. Choose a **StaticBody2D** as the main node for the platform.
  3. Add a **Sprite** and **CollisionShape2D** as child nodes.
  4. In the Inspector panel, choose a **RectangleShape2D** for the CollisionShape2D and adjust its size to fit the platform's sprite.

Duplicate the platform as many times as you'd like and position them around the scene for your player to jump on.

Step 5: Adding Collectibles

Now, let's add some collectibles to the game. These will give the player a score when collected.

  1. Create a new scene and add a **Node2D** as the root node.
  2. Add a **Sprite** and a **CollisionShape2D** as child nodes.
  3. For the sprite, you can use a coin or any small image you prefer.
  4. Attach a script to the root node of the collectible scene to handle the player's interaction.

Here's a simple GDScript to detect when the player collects the item:

extends Node2D

# Called when the player collides with this collectible
func _on_Area2D_body_entered(body):
    if body.is_in_group("player"):
        queue_free()  # Remove the collectible from the scene

Remember to add the player to a group named "player" so the collectible can detect it properly.

Step 6: Testing the Game

Once you have the player, platforms, and collectibles set up, click the **Play** button to test your game. You should be able to move the player left and right, jump between platforms, and collect items.

Step 7: Adding a Score System

To display the player's score, we need to create a simple score system. Add a **Label** node to the main scene.

extends KinematicBody2D

var score = 0

func _on_Collectible_body_entered(body):
    if body.is_in_group("player"):
        score += 1
        get_node("ScoreLabel").text = "Score: " + str(score)
        queue_free()  # Remove the collectible from the scene

Step 8: Adding More Features

Now that you have a basic setup for your 2D platformer, here are some additional features you can implement:

Next Steps

After implementing these features, consider expanding your game further by adding:

Conclusion

Congratulations! You have built the foundation of a 2D platformer game in Godot. As you continue developing your game, keep exploring Godot's features and experimenting with different ideas. Don't forget to refer to the Godot documentation for more advanced techniques and tutorials.