Consulting, services, computer engineering. Implementation of technology solutions and support for businesses.

User Rating: 5 / 5

Star ActiveStar ActiveStar ActiveStar ActiveStar Active
 

Csharp testing on Windows 11 version 26058 with Dot net 8.0.100 and Godot engine 4.2.1 : Paddle game

 

Csharp testing on Windows 11 version 26058 with Dot net 8.0.100 and Godot engine 4 2 1 Paddle

Dot net 8 Csharp testing with Godot : Paddle game

 

 

 

Who am i / About the author...

ident021 180 facebook linkedin insider mcp learn metadatas sans cravate 128x128 autocolors 18052006 191123 mcse microsoft logomcsa microsoft logomcdba logo

since 2001, Insider since 2014.

 

Thanks to Microsoft EMEA GBS to let me participate in the year 2014 to the Insider project during my work. Then, This Windows test project, here in version 11, is based on the cycle of the insider Preview Builds, now in the Dev Channel: it is fun to test the new features. I can learn new features, validate them and share them with you :-)

 

This tutorial is made in a test environment, for pleasure and not to earn money. It is a non-profit project. It shows that the concept is possible, for a demontration, artistic, or educational purposes, school learning course. Don't test in a real environment in production.

 

 

Csharp testing on Windows 11 version 26058 with Dot net 8.0.100 and Godot engine 4.2.1 : paddle game : os testing in the last article

 

The Windows 11 Operating System version 26052 was tested in the last article. Check article version 26052 here . Now that operating system is ready, i can test an application.

 

 

Csharp testing on Windows 11 version 26058 with Dot net 8.0.100 and Godot engine 4.2.1 : paddle game :application testing

 

Now, OS Windows 11 version 26058 is installed and ready, so i can test the middleware apps, frameworks...

 

Recently, i saw a cool session by Michael Hawker at .NET Conf 2023 about creating a game with C# and a middleware called "Godot engine" (and thank you Michael for the inspiration for this paddle game).

 

Next, i read this cool article from Natalia Dalomba: "Using signal emitters to access C# scripts" with Unity game engine. I thought Unity was a good application to test because we can develop with C#. C# to create games? It could be so fun! Natalia write plenty of great articles and tutorials on Unity. Each day she writes a new article and i'm glad to read it, and think about the next article that will be greater!

 

But, meanwhile, i read this very interesting book from Mina Pêcheux : "L’ALMANACH – UNLOCK YOUR GODOT 4/C# POWER: GETTING STARTED" . So, what about testing Godot framework using C# ?  

 

 

Csharp testing on Windows 11 version 26058 with Dot net 8.0.100 and Godot engine 4.2.1: paddle game :last articles

 

Read this article to see how to install Godot engine

How to use signals with Godo Engine (Red bouncing ball)

 

 

 

 

Csharp testing on Windows 11 version 26058 with Dot net 8.0.100 and Godot engine 4.2.1: paddle game : setting up the template

 

 

 

I download the template here:

https://github.com/Mikeware/GoDotNet.BlankTemplate

 

I create the environment variable

 

Search for "environment"

Csharp testing on Windows 11 version 26058 with Dot net 8.0.100 and Godot engine 4 2 1 Paddle create environment variable

 

Select "Edit the system environment variables"

Click on the "Environment Variables..." button

Click on the "New..." button for either a User or System variable

Provide a variable name of GODOT4

Provide a variable value of the full path, executable name, and extension to Godot for your system, e.g.

C:\Users\loicb\Downloads\Godot_v4.2.1-stable_mono_win64\Godot_v4.2.1-stable_mono_win64\Godot_v4.2.1-stable_mono_win64.exe

(anciennement C:\Godot\Godot_v4.1.1-stable_mono_win64.exe ) 

Click the OK button for the "New User Variable" dialog

Click the OK button for the "Environment Variables" dialog

Restart VS Code if it's running.

 

Test this new variable :

Open Terminal Command prompt CMD

Type : 

C:\Users\loicb>set Godot4

And the result should be something like that : 

GODOT4=C:\Users\loicb\Downloads\Godot_v4.2.1-stable_mono_win64\Godot_v4.2.1-stable_mono_win64\Godot_v4.2.1-stable_mono_win64.exe

 

Csharp testing on Windows 11 version 26058 with Dot net 8.0.100 and Godot engine 4.2.1: paddle game : download the template

 

Download and install with a NuGet by typing in the Terminal Command prompt CMD:

 

C:\Users\loicb>dotnet new install Mikeware.GoDotNet.BlankTemplate

 

And the result should be something like that : 

The following template packages will be installed:

   Mikeware.GoDotNet.BlankTemplate

 

Success: Mikeware.GoDotNet.BlankTemplate::1.0.1 installed the following templates:

Template Name        Short Name  Language  Tags

-------------------  ----------  --------  ----------------

Godot C# Game Empty  godotnet    [C#]      Godot/Game/Empty

 

Csharp testing on Windows 11 version 26058 with Dot net 8.0.100 and Godot engine 4.2.1: paddle game : project creation

 

Now i create a new project with the template

 

C:\Users\loicb>dotnet new godotnet -n GodotPaddle

 

And the result should be something like that : 

The template "Godot C# Game Empty" was created successfully.

 

Now i launch the project by tipping :

 

C:\Users\loicb>code GodotPaddle

 

Csharp testing on Windows 11 version 26058 with Dot net 8.0.100 and Godot engine 4 2 1 Paddle visual studio code trust the author

 

I Trust the author by clicking « Yes, I Trust the authors »

The project is created with files, main scene with a C# file for this main scene, project.godot, sln solution file and project csproj, icon file, and so and so.

 

Csharp testing on Windows 11 version 26058 with Dot net 8.0.100 and Godot engine 4 2 1 Paddle visual studio code project files

 

The .vscode and .godot folders exist too.

In the.vscode/launch.json we can see the environment variable than i created early used.

 

Csharp testing on Windows 11 version 26058 with Dot net 8.0.100 and Godot engine 4 2 1 Paddle visual studio code launch json

 

Csharp testing on Windows 11 version 26058 with Dot net 8.0.100 and Godot engine 4.2.1: paddle game : launching godot engine UI

 

Now, i load the project in Godot :

I lauch godot UI, and click « Import « (Import project)

I type the path where is the project file is located : C:\Users\loicb\GodotPaddle\project.godot

Open & Edit

 

The UI launchs :

Csharp testing on Windows 11 version 26058 with Dot net 8.0.100 and Godot engine 4 2 1 Paddle godot engine ui

 

We have the game scene with filesystem, nodes…

Note that the Node2D scene comes with the C# scipt attached, thank you to the template

 

I can see there is already an object « icon » which inherits from  « Sprite2D » which inherits from Node2D which inherits from CanvasItem

Csharp testing on Windows 11 version 26058 with Dot net 8.0.100 and Godot engine 4 2 1 Paddle sprite2D

 

Csharp testing on Windows 11 version 26058 with Dot net 8.0.100 and Godot engine 4.2.1: paddle game : use external code editor

 

I will use the external editor Visual studio code, so i go to

 

godot editor/ editor settings/Dotnet/Editor

External script editor: Visual Studio Code.

 

Csharp testing on Windows 11 version 26058 with Dot net 8.0.100 and Godot engine 4 2 1 Paddle visual studio code external editor

 

Csharp testing on Windows 11 version 26058 with Dot net 8.0.100 and Godot engine 4.2.1: paddle game : script attached to the main node

 

Now i go back to Visual Studio Code and i open the main scene and this C# script attached :

 

using Godot;

 

namespace GodotPaddle.Game;

 

public partial class Main : Node2D

{

    // Called when the node enters the scene tree for the first time.

    public override void _Ready()

    {

        GD.Print("Game Loaded");

    }

 

    // Called every frame. 'delta' is the elapsed time since the previous frame.

    public override void _Process(double delta)

    {

    }

}

 

I create a debug point in the margin on the same line than « GD.Print("Game Loaded"); »

 

I press F5 to build & run and it reaches the break point.

 

By pressing continue, the line « Game loaded » appears in the console.

 

Now we create the game.

 

We need two paddles. 

 

 Csharp testing on Windows 11 version 26058 with Dot net 8.0.100 and Godot engine 4.2.1: paddle game : paddle creation

 

So we create a scene with the « + » button 

Csharp testing on Windows 11 version 26058 with Dot net 8.0.100 and Godot engine 4 2 1 Paddle new scene

 

 

We will create a physical object so we choose « other node »

 

 

Csharp testing on Windows 11 version 26058 with Dot net 8.0.100 and Godot engine 4 2 1 Paddle new node other node 

 

And in the pop-pup screen i select  « CharacerBody2D » (A 2D Physics body specialized for characters moved by script)

 

Csharp testing on Windows 11 version 26058 with Dot net 8.0.100 and Godot engine 4 2 1 Paddle CharacerBody2D

 

 The warning invite us to add a collision shape.

 

I click the button « center view » to center the view on the CharacterBody2D.

 

So i add a child node on the CharacterBody2D, Nodes2D/Polygon2D

 

With the « create points » icon (green plus) i create a basic polygon like that :

 

Csharp testing on Windows 11 version 26058 with Dot net 8.0.100 and Godot engine 4 2 1 Paddle basic paddle model

 

 Now in the inspector, in the polygon properties, data section, Packedvector2, and i can adjust the values :

 

Csharp testing on Windows 11 version 26058 with Dot net 8.0.100 and Godot engine 4 2 1 Paddle paddle model ok

 

It is the representation of the paddle. Now we need to create a physics body so that the physics engine can understand forces to apply to this representation.

 

I add a child node to the CharacterBody2D : Node2D/CollisionShape2D 

 

The info ask us to add a shape.

 

So, in the inspector, CollisionShape2D, shape, i clic « empty » and i select a new RectangleShape2D :

 

Csharp testing on Windows 11 version 26058 with Dot net 8.0.100 and Godot engine 4 2 1 Paddle game CollisionShape2D

 

I adjust the blue CollisionShape2D to the Polygon2D so that the physic body fill the representation of the paddle, like this :

 

Csharp testing on Windows 11 version 26058 with Dot net 8.0.100 and Godot engine 4 2 1 Paddle game CollisionShape2D adjusted 

 

I click « unsaved » and save scene as Paddle.tscn file. I’ve now a Paddle.tscn file in the file explorer.

 

I select the root node, CharacterBody2D and right clic and « attach script »

 

I change Gdscript to C# script , and hit create.

 

Csharp testing on Windows 11 version 26058 with Dot net 8.0.100 and Godot engine 4 2 1 Paddle game paddle csharp script

 

And Visual Studio Code opens and shows me this new script :

 

using Godot;

using System;

 

public partial class Paddle : CharacterBody2D

{

    public const float Speed = 300.0f;

    public const float JumpVelocity = -400.0f;

 

    // Get the gravity from the project settings to be synced with RigidBody nodes.

    public float gravity = ProjectSettings.GetSetting("physics/2d/default_gravity").AsSingle();

 

    public override void _PhysicsProcess(double delta)

    {

        Vector2 velocity = Velocity;

 

        // Add the gravity.

        if (!IsOnFloor())

            velocity.Y += gravity * (float)delta;

 

        // Handle Jump.

        if (Input.IsActionJustPressed("ui_accept") && IsOnFloor())

            velocity.Y = JumpVelocity;

 

        // Get the input direction and handle the movement/deceleration.

        // As good practice, you should replace UI actions with custom gameplay actions.

        Vector2 direction = Input.GetVector("ui_left", "ui_right", "ui_up", "ui_down");

        if (direction != Vector2.Zero)

        {

            velocity.X = direction.X * Speed;

        }

        else

        {

            velocity.X = Mathf.MoveToward(Velocity.X, 0, Speed);

        }

 

        Velocity = velocity;

        MoveAndSlide();

    }

}

 

I add

namespace Godot.Paddle.Game ;

 

Because we need a simple paddle moving up and down, not affected by gravity, we can comment this part of the code :

 

/* public const float JumpVelocity = -400.0f;

 

    // Get the gravity from the project settings to be synced with RigidBody nodes.

    public float gravity = ProjectSettings.GetSetting("physics/2d/default_gravity").AsSingle(); */

 

and same with that :

 

        /* // Add the gravity.

        if (!IsOnFloor())

            velocity.Y += gravity * (float)delta;

 

        // Handle Jump.

        if (Input.IsActionJustPressed("ui_accept") && IsOnFloor())

            velocity.Y = JumpVelocity; */

 

We replace the vector of the direction like this:

 

// velocity.X = direction.X * Speed;

velocity.Y = direction.Y * Speed;

 

and

 

// velocity.X = Mathf.MoveToward(Velocity.X, 0, Speed);

velocity.Y = Mathf.MoveToward(Velocity.Y, 0, Speed);

        

I save the script, and in the godot editor i save the Paddle scene.

 

Now i need to integrate the Paddle in the main scene.

 

I select main scene, and from the FileSystem section, i drag and drop the Paddle.tscn in the main scene :

 

 

Csharp testing on Windows 11 version 26058 with Dot net 8.0.100 and Godot engine 4 2 1 Paddle game integrate in the main scene

 

The new CharacterBody2D appears in the scene.

 

Csharp testing on Windows 11 version 26058 with Dot net 8.0.100 and Godot engine 4.2.1: paddle game : creating the second paddle

 

We now have one paddle. We need 2 paddle, so we right click on this appeared CharacterBody2D and select « duplicate » :

 

Csharp testing on Windows 11 version 26058 with Dot net 8.0.100 and Godot engine 4 2 1 Paddle game duplicate

 

 And now we have a second paddle with the same behavior than the first paddle.

 

I rename the paddles LeftPaddle - CharacterBody2D  and RightPaddle - CharacterBody2D

 

Csharp testing on Windows 11 version 26058 with Dot net 8.0.100 and Godot engine 4 2 1 Paddle game LeftPaddle CharacterBody2D and RightPaddle

 

 I save and run the project with F5 : i can move the two paddles up and down with up and down key

 

Csharp testing on Windows 11 version 26058 with Dot net 8.0.100 and Godot engine 4 2 1 Paddle game move the two paddles ok

 

Csharp testing on Windows 11 version 26058 with Dot net 8.0.100 and Godot engine 4.2.1: paddle game : creating the ball

 

How to create the ball with the icon?

 

In the root node, i add a child node, a rigidBody2D (controlled by the physics engine) and i rename it « Ball - RigidBody2D » 

 

I drag the icon into the « Ball - RigidBody2D » : 

 

Csharp testing on Windows 11 version 26058 with Dot net 8.0.100 and Godot engine 4 2 1 Paddle game Ball RigidBody2D

 

 I reset the icon position to the  ball position, so the icon and the ball are at the same position, by clicking the circle with an arrow on the left of position (to reset the position to the same position of the node parent).

 

Now we need to add the collision shape :

 

Right clic on the Ball - RigidBody2D node, and add child node CollisionShape2D. So that the ball has the physics boudaries that the physic engine knows.

 

Csharp testing on Windows 11 version 26058 with Dot net 8.0.100 and Godot engine 4 2 1 Paddle game Ball CollisionShape2D

 

 Same thing than last time, i add a shape New RectangleShape2D and fill it to the Ball - RigidBody2D shape.

 

Now we have a ball object with a visual icon and a collision.

 

I move the « Ball - RigidBody2D node » grouped with the child nodes, by selecting it and clicking the icon « move mode » and putting it between the two paddles.

 

Csharp testing on Windows 11 version 26058 with Dot net 8.0.100 and Godot engine 4 2 1 Paddle game move mode

 

I add a C# script to the ball by right clicking on the « Ball - RigidBody2D » node and « attach script » :

 

Csharp testing on Windows 11 version 26058 with Dot net 8.0.100 and Godot engine 4 2 1 Paddle game ball csharp script

 

This script is added automatically (it herits from RigidBody2D ):

 

using Godot;

using System;

 

public partial class Ball : RigidBody2D

{

    // Called when the node enters the scene tree for the first time.

    public override void _Ready()

    {

    }

 

    // Called every frame. 'delta' is the elapsed time since the previous frame.

    public override void _Process(double delta)

    {

    }

}

 

When the ball appears on the screen, i want the physics engine to apply a force to send the ball to a direction. So i add the ApplyImpulse method with a vector in input. (Thanks to Michael Hawker for this nice algorithm)

 

 

So in the void _Ready() i add this line :

ApplyImpulse(Vector2.Up.Rotated(GD.Randf()*2*Mathf.Pi)*300f) ;

 

Because we don’t want gravity in this paddle game, we need to turn off the gravity. Go to Godot engine/Project/Project settings/ and in physics/2D, replace default gravity 980 to 0 :

 

 Csharp testing on Windows 11 version 26058 with Dot net 8.0.100 and Godot engine 4 2 1 Paddle game ball gravity to 0

 

Save

 

How to create the wall, the barriers to ovoid that the ball goes out of the screen ?

 

Csharp testing on Windows 11 version 26058 with Dot net 8.0.100 and Godot engine 4.2.1: paddle game : creating the walls

 

I create a new node, StaticBody2D node, with a child node CollisionShape2D and a shape rectangle. I adjust the shape rectangle to fill the top of the screen like this:

 

Csharp testing on Windows 11 version 26058 with Dot net 8.0.100 and Godot engine 4 2 1 Paddle game ball top wall

 

To add another wall in the bottom, i duplicate this last CollisionShape2D.

 

I rename the CollisionShape2D : Top-CollisionShape2D and Bottom-CollisionShape2D, like this : 

 

Csharp testing on Windows 11 version 26058 with Dot net 8.0.100 and Godot engine 4 2 1 Paddle game wall duplicate

 

 When i build and run with F5, the ball moves and stops in the wall.

 

I must add a physics material to the ball so that the ball « bounce » on the wall.

 

I select the Node ball, and in the Inspector, Ball, RigidBody2D : Physics material, New Physics Material.

And i clic « Edit » this new Physics Material and put these values :

Friction : 0

Bounce : 1

 

Csharp testing on Windows 11 version 26058 with Dot net 8.0.100 and Godot engine 4 2 1 Paddle game ball friction 0 bounce 1

 

Now the ball bounces on the wall, great.

 

BUT ! Each time the ball encouters a wall and bounces, it loose speed, because of the physics. I don’t want this, so i change my ball property again.

 

I select the Node ball, and in the Inspector, Ball properties, RigidBody2D , Linear, and change « Damp mode : combine » to « replace »

 

And for more fun and to add interest, i add acceleration each time the ball bounce on a wall or a paddle, put -0.5 in damp, and add angular speed 100 radians per seconds :

 

 

Csharp testing on Windows 11 version 26058 with Dot net 8.0.100 and Godot engine 4 2 1 Paddle game ball angular speed

 

When i build and run with F5, the game is playable. Awesome randoms and funs effects ! Because the ball and the collision shape are not a circle, the ball bounces with a square effects in random directions, and depending on which edge of the ball touchs the wall, it can go back in the other direction, very funnyyyyy effect! 

 

Csharp testing on Windows 11 version 26058 with Dot net 8.0.100 and Godot engine 4.2.1: paddle game : add a pause game and play resume game button

 

I need to add a play butto, so that i can put my fingers on up arrow and down arrow to control the paddles and be ready when the ball starts to move.

 

Industrialisation : if exists, import « ui_play_pause_control.tscn » from another scene already created.

 

 

In my Root scene i create a new child node : Control

 

Csharp testing on Windows 11 version 26058 with Dot net 8.0.100 and Godot engine 4 2 1 Paddle game control node

 

And create

 

 

I rename this node UI Play Pause Control

 

In thisnew control node just created, i create a new child node : Button.

I rename It Pause Button

In the inspector of this Pause Button, i add the text property: II Pause game

In the signals node, i connect the button_down() signal with the Receiver Method to the Pause Button: _on_button_pause_button_down

 

Csharp testing on Windows 11 version 26058 with Dot net 8.0.100 and Godot engine 4 2 1 Paddle game signal pause button down

 

 On the button node, i attach the C# script PauseButton.cs

 

Csharp testing on Windows 11 version 26058 with Dot net 8.0.100 and Godot engine 4 2 1 Paddle game pause button csharp script

 

In this C# script PauseButton.cs i add the method _on_pause_button_button_down() 

 

    void _on_pause_button_button_down()

    {

        GD.Print("Pause Button pressed");

        // Unpause the game

        var tree = GetTree();

        tree.Paused = true;

        //ou plus simple: GetTree().Paused = true;

    }

 

In this Control node, i create another child node : Button.

I rename It Play Button

In the inspector, i add the text property: Play/Resume game

 

In the inspector, Control, Layout, i add the text property: Transform/Position X=200, and the Node process mode to « When Pause » (Because when the Pause Button is pressed, all nodes are freezed);

In the signals node, i attach the button_down() signal with the Receiver Method : _on_play_button_button_down()

 

Csharp testing on Windows 11 version 26058 with Dot net 8.0.100 and Godot engine 4 2 1 Paddle game play button signal

 

On the Play Button node, i attach the script PlayButton.cs

 

Csharp testing on Windows 11 version 26058 with Dot net 8.0.100 and Godot engine 4 2 1 Paddle game play button csharp

 

 I add a new method _on_play_button_button_down with 

 

    void _on_play_button_button_down()

    {

        GD.Print("Button Play pressed");

        // Unpause the game

        var tree = GetTree();

        tree.Paused = false;

        //ou plus simple: GetTree().Paused = true;

    }

 

 

Now i want the game to be paused as soon as it starts.

 

In the PlayButton.cs i add a « tree.Paused=true ; » when the game starts, like this :

 

    // Called when the node enters the scene tree for the first time.

    public override void _Ready()

    {

        // pause the game

        GD.Print("Game paused");

        var tree = GetTree();

        tree.Paused = true;

    }

 

 

Done

 

I export this scene, so that, in a future demo, if i want a Pause/Play Resume mode, i just need to import this scene.

Click left button on the « UI Play Pause Control » node, open menu, and save branch as scene

 

ui_play_pause_control.tscn

 

When i build and run with F5, the game is playable, have fun !

 

I add a new method _on_play_button_button_down with 

 

    void _on_play_button_button_down()

    {

        GD.Print("Button Play pressed");

        // Unpause the game

        var tree = GetTree();

        tree.Paused = false;

        //ou plus simple: GetTree().Paused = true;

    }

 

 

Now i want the game to be paused as soon as it starts.

 

In the PlayButton.cs i add a « tree.Paused=true ; » when the game starts, like this :

 

    // Called when the node enters the scene tree for the first time.

    public override void _Ready()

    {

        // pause the game

        GD.Print("Game paused");

        var tree = GetTree();

        tree.Paused = true;

    }

 

 

Done

 

I export this scene, so that, in a future demo, if i want a Pause/Play Resume mode, i just need to import this scene.

Click left button on the « UI Play Pause Control » node, open menu, and save branch as scene

 

ui_play_pause_control.tscn

 

When i build and run with F5, the game is playable, have fun !

Csharp testing on Windows 11 version 26058 with Dot net 8.0.100 and Godot engine 4 2 1 Paddle

 

Csharp testing on Windows 11 version 26058 with Dot net 8.0.100 and Godot engine 4.2.1: paddle game : done

 

Done! We can create a paddle game with C# in Godot 4.2.1 on windows 11 version 26058.

Csharp testing on Windows 11 version 26058 with Dot net 8.0.100 and Godot engine 4.2.1: need help ?

 

Need help to write C# code or use godot engine on Windows 11 ?

Fill this form or send an email on loic @consultingit.fr or This email address is being protected from spambots. You need JavaScript enabled to view it. and i will be happy to help you :)

 

 

Thank you to Michael Hawker for the inspiration from