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.
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.
In the Inspector panel, choose the **RectangleShape2D** for the CollisionShape2D. Adjust the size to match the player's sprite.
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.
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:**
GRAVITY
to apply downward force to the player, making it fall naturally.move_and_slide()
function moves the player and handles collision detection.Let's now create some platforms for our player to jump on. We'll make a simple platform using a **StaticBody2D** and a **CollisionShape2D**.
Duplicate the platform as many times as you'd like and position them around the scene for your player to jump on.
Now, let's add some collectibles to the game. These will give the player a score when collected.
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.
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.
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
Now that you have a basic setup for your 2D platformer, here are some additional features you can implement:
RigidBody2D
node. Implement basic AI for enemies that move back and forth or follow the player.AudioStreamPlayer
node to play sounds when actions occur.AnimationPlayer
node for actions like running, jumping, and collecting items.After implementing these features, consider expanding your game further by adding:
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.