## Let’s Learn Godot 4 by Making a Procedurally Generated Maze Game — Part 2: Player Setup & Movement💣
Tags: godot, godotengine, gamedev, tutorial
Published: 2023-09-27 12:19:29


The first entity scene that we will create for our game is our Player. Entity scenes in our game refer to all the scenes that interact with the game and respond to player input or other entities — such as our bombs, enemies, players, and boosts. All of our entity scenes will be instanced by our Level scene, which will handle the spawning of our entities and our map generation. All of our entity scenes, and our Level scene, will be instanced by our Main scene, which is the main container for our game.

---

>  **What is a Scene and a Node?**
>  _A scene is a container that holds a collection of nodes arranged in a hierarchical structure — such as our Player or Enemy. A node is the basic unit within a scene that makes up the scene — such as a Sprite or Camera within our scene._

---

![Figure 5: Overview of a Node & a Scene.](https://cdn-images-1.medium.com/max/2000/1*rJ0z_2rY2UWlqz2plJVr5w.png)
> Overview of a Node & a Scene.

![Figure 6: Overview of our game’s scene hierarchy and logic.](https://cdn-images-1.medium.com/max/3548/1*i7rGjOav8easUt3VAxkomw.png)
> Overview of our game’s scene hierarchy and logic.

## PLAYER OVERVIEW

We will create the Main and Level scenes in the next few parts when we create our procedurally generated map, but for now, we will make our player who will move around the map and perform activities related to destroying tiles and enemies. Our player will also spawn with a random color on each load, so each time you start a new game, your player might be blue, grey, or orange.

We also will spawn our AI player later on with a random color — and this will help us establish some individuality between our players. Our player entity will have three lives and they will also be able to take damage, deal damage, and pick up boosts.

![Figure 7: Overview of our Player entity spawning logic.](https://cdn-images-1.medium.com/max/2242/1*B8PY6DoRU84iZNGz0DgHiw.png)
> Overview of our Player entity spawning logic.

## PLAYER ANIMATION SETUP

Open up your project and create a new 2D Scene.

![Learn Godot 4](https://cdn-images-1.medium.com/max/2000/1*g_-i7Pm5MAKxVLUjKYx-dw.png)

You will see that this creates a [Node2D](https://docs.godotengine.org/en/stable/classes/class_node2d.html#class-node2d-property-position) node as its root or parent. Let’s rename this node to be “Player”.

![Learn Godot 4](https://cdn-images-1.medium.com/max/2000/1*gzS22ddx_9qWQnD7FDrZBQ.png)

---

>  **What is a Node2D node?**
>  _A Node2D is the base 2D node in Godot that has functionalities for positioning, transformation, and hierarchy. We can use this node to contain or organize other nodes._

---

We don’t want our Player to have a Node2D node as its root because we need a node that can handle movement and physics, such as our [CharacterBody2D](https://docs.godotengine.org/en/stable/classes/class_characterbody2d.html) node. Right-click your root node, and change its type to CharacterBody2D.

![Learn Godot 4](https://cdn-images-1.medium.com/max/2298/1*g2-PEVkOawk0bAlOUPvXdw.png)

You will see it has a yellow warning icon next to it, and that is because it is missing a [CollisionShape2D ](https://docs.godotengine.org/en/stable/classes/class_collisionshape2d.html)node, which will allow our player to be blocked or sensed by other node’s collisions. Add a CollisionShape2D node to your root node, and assign it a new CircleShape2D.

![Learn Godot 4](https://cdn-images-1.medium.com/max/2380/1*bHlGno3vS-3kUPUL2dCHRw.png)

![Learn Godot 4](https://cdn-images-1.medium.com/max/3838/1*AEOzmKE7mBYyO2lJs59jLg.png)

To see our player and give it a movement animation, we need to assign it an [AnimatedSprite2D](https://docs.godotengine.org/en/stable/classes/class_animatedsprite2d.html) node. An AnimatedSprite2D node can contain multiple sprite frames and thus we can organize these frames to play an animation, whereas a [Sprite2D](https://docs.godotengine.org/en/stable/classes/class_sprite2d.html#class-sprite2d) node is a static object that contains one singular sprite frame.

![Learn Godot 4](https://cdn-images-1.medium.com/max/2368/1*DkAMLUfGPT29pNnT3ZNqVg.png)

This node will also give us a yellow warning, and that’s because we need to assign it with a [Sprite Frames](https://docs.godotengine.org/en/stable/classes/class_animatedsprite2d.html#class-animatedsprite2d-property-sprite-frames) resource which will allow us to load, create, edit, and delete animations. Select the AnimatedSprite2D node, and in the Inspector panel assign it with a new Sprite Frames resource.

![Learn Godot 4](https://cdn-images-1.medium.com/max/2000/1*OIAq8yV0t-vM3L2-8zmg7Q.png)

Now, if you look in your Assets directory underneath “players”, you will see that there are three “char_” folders with movement sprites (images) for three different colored characters. We will use these images to create an *idle, up, down, *and *side *animation for each color — so orange_idle, blue_idle, green_idle, etc.

![Learn Godot 4](https://cdn-images-1.medium.com/max/2000/1*exNAmLrta1vbGIclTbiZug.png)

Let’s start with blue. In your SpriteFrames panel below, add a new animation called “blue_idle”. It is important to prefix the animation state with the color value since we will use this value to play the animations based on the color generated when the player spawns.

Drag the “base.png” image underneath char_01 into your frames panel. We can leave the FPS to 5 and looping value to on.

![Learn Godot 4](https://cdn-images-1.medium.com/max/2388/1*gTmy2QhF3VVUGt7McklH_g.png)

Do the same for orange_idle and grey_idle.

![Learn Godot 4](https://cdn-images-1.medium.com/max/2000/1*WFoPGi19iDA5XhAfT60suA.png)

![Learn Godot 4](https://cdn-images-1.medium.com/max/2000/1*7Tr9-VA5zxdgNp2CjQMpIg.png)

Then, create a new animation called “blue_down”, and assign the “down_1” to “down_3” sprites underneath char_01 to your animation. Leave the FPS to 5 and looping value to on.

![Learn Godot 4](https://cdn-images-1.medium.com/max/2000/1*TSEquKiwoFWRUeujHILslQ.png)

Do the same for orange_down and grey_down.

![Learn Godot 4](https://cdn-images-1.medium.com/max/2000/1*9WLCY34lJ31uxXnarLS_bA.png)

![Learn Godot 4](https://cdn-images-1.medium.com/max/2000/1*qsJRvDPPrHToMR45m7VQEA.png)

Create a new animation called “blue_up”, and assign the “up_1” to “up_3” sprites underneath char_01 to your animation. Leave the FPS to 5 and looping value to on.

![Learn Godot 4](https://cdn-images-1.medium.com/max/2000/1*DG_XZyD4yDRLp_ic1s0wSw.png)

Do the same for orange_up and grey_up.

![Learn Godot 4](https://cdn-images-1.medium.com/max/2000/1*feUkv1BmijJV183Ori6o5A.png)

![Learn Godot 4](https://cdn-images-1.medium.com/max/2000/1*8utTOeOa20Ql-B_B2S5ykw.png)

Finally, create a new animation called “blue_side”, and assign the “side_1” to “side_3” sprites underneath char_01 to your animation. Leave the FPS to 5 and looping value to on.

![Learn Godot 4](https://cdn-images-1.medium.com/max/2000/1*DLpBxY2XwRQsIKm-BcaWYA.png)

Do the same for orange_side and grey_side.

![Learn Godot 4](https://cdn-images-1.medium.com/max/2000/1*hcf7Q8B5rWxPz-U1cgnjQA.png)

![Learn Godot 4](https://cdn-images-1.medium.com/max/2000/1*1mR83RQeLfK8pl8WVLL2Jg.png)

Your final animations list should look like this:

![Learn Godot 4](https://cdn-images-1.medium.com/max/2000/1*4ZRWkg7wF5Hh5fb5nhV4oA.png)

Save your scene as “Player” underneath your Scenes folder.

![Learn Godot 4](https://cdn-images-1.medium.com/max/2984/1*iCD6oSoRdk0QZCnSuyT_YQ.png)

## PLAYER COLOR GENERATION & MOVEMENT

Attach a new script to your Player scene’s root node and save it as “Player.gd” underneath your Scripts folder.

![Learn Godot 4](https://cdn-images-1.medium.com/max/2000/1*MmrGXmU3wCl41ekjZUMhiA.png)

We will also create a new Global [Autoload Singleton](https://docs.godotengine.org/en/stable/tutorials/scripting/singletons_autoload.html) script which will contain all the variables and methods that will be used throughout our game in multiple scenes. We will use this script throughout our game to store our level variables and signals.

---

>  **What is a Singleton Script?**
>  _A singleton script is a script that is automatically loaded when the game starts and remains accessible throughout the entire lifetime of the game. This makes it ideal for managing game-wide data, functions, and systems that need to be accessed from multiple scenes or scripts._

---


Underneath your Scripts folder, create a new script called “Global.gd”.

![Learn Godot 4](https://cdn-images-1.medium.com/max/2000/1*JEwqNEsG8_A_hkoVJkr_Dw.png)

Then, in your Project Settings > Autoload menu, assign the Global script as a new singleton. This will make the variables and functions stored in this script accessible in every scene.

![Learn Godot 4](https://cdn-images-1.medium.com/max/3840/1*bH-CvqtoKRsIU5NlLa_TSw.png)

Both our player and our AI use the same sprites, and they will both generate a random color on load, so it would be smart to save our Color array in our Global script. Our sprites have a blue, grey, and orange color, so let’s define an array in our Global script with these color values.
```python
    ### Global.gd
    extends Node
    # Color generation array for player and ai_player
    var color: Array = ["blue", "grey", "orange"]
```
Now in our Player script, we can use this Global variable to assign our player a color when they load into our Level or Main scene. We will do this in our built-in [ready()](https://docs.godotengine.org/en/stable/classes/class_node.html#class-node-method-ready) function by generating a random index within the range of valid indexes for the Global.color array. To generate a random index from 0 to 2 (which is our Global.color array size), we will use the [randi()](https://docs.godotengine.org/en/stable/classes/class_randomnumbergenerator.html#class-randomnumbergenerator-method-randi) method alongside the modulo operation (%) to ensure that the random integer stays within the array size, effectively randomizing the index.

---

>  **When to use _ready()?**
>  _We use the _ready() function whenever we need to set or initialize code that needs to run right after a node and its children are fully added to the scene. This function will only execute once before any _process() or _physics_process() functions._

---

```python
    ### Player.gd
    
    extends CharacterBody2D
    
    # Player states
    var color: String
    
    func _ready():
        # Randomly assign it a color on spawn
        color = Global.color[randi() % Global.color.size()]
```
Now, we can use this color variable to play the appropriate animation for our player. So if the color’s index is 0, it will play the animations for blue_ — and for 1 = grey_ and 2 = _orange. We will create a new function called movement_input() which will handle the player’s animations based on the color and current input of the player.

To make our code more organized, we will use the [@onready](https://docs.godotengine.org/en/stable/tutorials/scripting/gdscript/gdscript_basics.html) annotation to create an instance of a reference to our AnimatedSprite2D node. This way we can reuse the variable name instead of saying $AnimatedSprite2D.play() each time we want to change the animation. Take note that we also flip our sprite horizontally when playing the side_ animation. This is so that we can reuse the same animation for both left and right directions.
```python
    ### Player.gd
    
    extends CharacterBody2D
    
    # Node References
    @onready var animated_sprite = $AnimatedSprite2D
    
    # Player states
    var color: String
    
    func _ready():
        # Randomly assign it a color on spawn
        color = Global.color[randi() % Global.color.size()]
    
    # Player movement
    func movement_input():
        # -------- Animations by color ------------
        #left anim
        if Input.is_action_pressed("ui_left"):
            animated_sprite.play(color + "_side")
            animated_sprite.flip_h = false
        #right anim
        elif Input.is_action_pressed("ui_right"):
            animated_sprite.flip_h = true
            animated_sprite.play(color + "_side")
        #up anim
        elif Input.is_action_pressed("ui_up"):
            animated_sprite.play(color + "_up")
        #down anim
        elif Input.is_action_pressed("ui_down"):
            animated_sprite.play(color + "_down")
        #idle anim
        else:
            animated_sprite.play(color + "_idle")
            animated_sprite.flip_h = false
```
To play these animations, we will need to call our newly created movement_input() function in our [physics_process()](https://docs.godotengine.org/en/stable/tutorials/scripting/idle_and_physics_processing.html) function, which is responsible for our game’s physics engine. This function allows our player to use direction, speed, and velocity inputs to move around the map.

---

>  **When to use _processing() and _physics_processing()?**
>  _Use the _process() function for things that are graphical or need to respond quickly. Use the _physics_process() function for things that are physics-based or need to happen at a consistent, predictable rate._

---

```python
    ### Player.gd
    
    #older code
    
    # Movement Physics
    func _physics_process(delta):
        movement_input()
        
    # Player movement
    func movement_input():
        # -------- Animations by color ------------
        #left anim
        if Input.is_action_pressed("ui_left"):
            animated_sprite.play(color + "_side")
            animated_sprite.flip_h = false
        #right anim
        elif Input.is_action_pressed("ui_right"):
            animated_sprite.flip_h = true
            animated_sprite.play(color + "_side")
        #up anim
        elif Input.is_action_pressed("ui_up"):
            animated_sprite.play(color + "_up")
        #down anim
        elif Input.is_action_pressed("ui_down"):
            animated_sprite.play(color + "_down")
        #idle anim
        else:
            animated_sprite.play(color + "_idle")
            animated_sprite.flip_h = false
```
Now if you run your scene by pressing F5 (with your Player scene as your default scene), you will see that your player’s animations play when you press WASD, and the color should randomly generate when you run your scene again.

![Learn Godot 4](https://cdn-images-1.medium.com/max/2000/1*PFqJsx_wUBpaf5kHNxTihA.png)

![Learn Godot 4](https://cdn-images-1.medium.com/max/2000/1*xiU8f9sXdhRKae-85O_BWw.png)

To get our player to move in different directions, we need to define a new speed variable for our player. I want our player to be quite quick, so I will give it a high value such as 100.

```python
    ### Player.gd
    
    extends CharacterBody2D
    
    # Node References
    @onready var animated_sprite = $AnimatedSprite2D
    
    # Player states
    var color: String
    var speed = 100
    
    #older code
```

Then in our movement_input() function, we will define a new variable that will use Godot’s built-in [get_vector()](https://docs.godotengine.org/en/stable/classes/class_input.html#class-input-method-get-vector) method that returns a Vector2 based on the state of the specified input actions. The vector’s x-component will be -1, 0, or 1 depending on whether “ui_left” or “ui_right” is pressed. The vector’s y-component will be -1, 0, or 1 depending on whether “ui_up” or “ui_down” is pressed.

---

>  ***What is a Vector?***
>  _Vectors are objects that represent quantities like force, velocity, and position. In 2D games, we use Vectors to determine and calculate the position of entities on the X and Y axes._

---

![Figure 8: Overview of a Vector2 for position determination.](https://cdn-images-1.medium.com/max/2960/1*DFYRTksSZ5h_rZW66S9Wxw.png)
> Overview of a Vector2 for position determination.

```python
    ### Player.gd
    
    #older code
    
    # Player movement
    func movement_input():
        var input_direction = Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")
        
        # -------- Animations by color ------------
        #left anim
        if Input.is_action_pressed("ui_left"):
            animated_sprite.play(color + "_side")
            animated_sprite.flip_h = false
        #right anim
        elif Input.is_action_pressed("ui_right"):
            animated_sprite.flip_h = true
            animated_sprite.play(color + "_side")
        #up anim
        elif Input.is_action_pressed("ui_up"):
            animated_sprite.play(color + "_up")
        #down anim
        elif Input.is_action_pressed("ui_down"):
            animated_sprite.play(color + "_down")
        #idle anim
        else:
            animated_sprite.play(color + "_idle")
            animated_sprite.flip_h = false
```
We’ll then take this input_direction vector value and multiply it by our speed to get our player’s velocity. This will allow our player to move in a certain direction at our defined speed.
```python
    ### Player.gd
    
    #older code
    
    # Player movement
    func movement_input():
        var input_direction = Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")
        velocity = input_direction * speed
        
        # -------- Animations by color ------------
        #left anim
        if Input.is_action_pressed("ui_left"):
            animated_sprite.play(color + "_side")
            animated_sprite.flip_h = false
        #right anim
        elif Input.is_action_pressed("ui_right"):
            animated_sprite.flip_h = true
            animated_sprite.play(color + "_side")
        #up anim
        elif Input.is_action_pressed("ui_up"):
            animated_sprite.play(color + "_up")
        #down anim
        elif Input.is_action_pressed("ui_down"):
            animated_sprite.play(color + "_down")
        #idle anim
        else:
            animated_sprite.play(color + "_idle")
            animated_sprite.flip_h = false
```
Now to move our player, we will simply call the [move_and_slide()](https://docs.godotengine.org/en/stable/tutorials/physics/using_character_body_2d.html) method, which will smoothly move our player across the screen when an input is pressed.

---

>  **When to use move_and_slide and move_and_collide()?**
>  _Use move_and_slide() when you want the character to move in general directions, such as in platformer games. Use move_and_collide() when you want detailed information on your character’s collisions to perform custom actions, such as in puzzle games where characters need to move or dodge obstacles._

---

```python
    ### Player.gd
    
    #older code
    
    # Movement Physics
    func _physics_process(delta):
        movement_input()
        move_and_slide()
    
    # Player movement
    func movement_input():
        var input_direction = Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")
        velocity = input_direction * speed
        
        # -------- Animations by color ------------
        #left anim
        if Input.is_action_pressed("ui_left"):
            animated_sprite.play(color + "_side")
            animated_sprite.flip_h = false
        #right anim
        elif Input.is_action_pressed("ui_right"):
            animated_sprite.flip_h = true
            animated_sprite.play(color + "_side")
        #up anim
        elif Input.is_action_pressed("ui_up"):
            animated_sprite.play(color + "_up")
        #down anim
        elif Input.is_action_pressed("ui_down"):
            animated_sprite.play(color + "_down")
        #idle anim
        else:
            animated_sprite.play(color + "_idle")
            animated_sprite.flip_h = false
```
Before we wrap up this section and test our code, we need to add one more node to our Player scene tree: a Sprite2D node. This node should be above the player’s head to serve as their indicator. This will help us separate the player from enemies and AI Players.

Add a Sprite2D node to your scene and change its Texture to “Player_Indicator.png”, which can be found underneath Assets/players/char_indicators. We also want to change this node’s scale to 0.05, and its position on the y-axis to -15.

![Learn Godot 4](https://cdn-images-1.medium.com/max/3840/1*v2a6-2Wt9E8qJczPnNIwhQ.png)

Your final scene tree should look like this:

![Learn Godot 4](https://cdn-images-1.medium.com/max/2000/1*Q8XgMOm2nMiUMnZ6Jw7ZoQ.png)

Now if you run your scene, your player should play the appropriate animations and smoothly move across the screen!

![Learn Godot 4](https://cdn-images-1.medium.com/max/2000/1*Nz5o-0Zg7MDXvHeLG20lMA.png)

We will make amendments later on to our player, so we are not done yet, but it’s good that we have a Player character that can now spawn with a random color and play its respective animations in each input direction. Now would be a good time to save your project and make a small incremental backup of your code.

The final source code for this part can be found [here](https://github.com/christinec-dev/BomberClone_Parts/tree/main/Part_2).

---

## Unlock the Series!

If you like this series and would like to support me, you could [donate](https://ko-fi.com/christinedevs) any amount to my KoFi shop or you could [purchase the offline PDF](https://ko-fi.com/s/fa00d21069) that has the entire series in one on-the-go booklet!

This PDF gives you lifelong access to the full, offline version of the “Learn Godot 4 by Making a Procedurally Generated Maze Game” series. This is a 387-page document that contains all the tutorials of this series in a sequenced format, plus you get dedicated help from me if you ever get stuck or need advice. This means you don’t have to wait for me to release the next part of the tutorial series on Dev.to or Medium. You can just move on and continue the tutorial at your own pace — anytime and anywhere!

![Learn Godot 4](https://cdn-images-1.medium.com/max/3090/1*IJTAm4W2qak2cgM424ELrg.png)

*This book will be updated continuously to fix newly discovered bugs, or to fix compatibility issues with newer versions of Godot 4.*