• Home
  • News
  • Software
  • Knowledge
  • MMO
  • Tips
  • Security
  • Network
  • Office
AnonyViet - English Version
  • Home
  • News
  • Software
  • Knowledge
  • MMO
  • Tips
  • Security
  • Network
  • Office
No Result
View All Result
  • Home
  • News
  • Software
  • Knowledge
  • MMO
  • Tips
  • Security
  • Network
  • Office
No Result
View All Result
AnonyViet - English Version
No Result
View All Result

[Top Down Game với Godot] #1: Move

AnonyViet by AnonyViet
October 20, 2024
in Tips
0

Introduce

Hello everyone, welcome to the next series about game programming with Godot Engine. Today I will guide you to program Top Down game with Godot. In this article I will guide you to move characters for top down game.

Join the channel Telegram belong to AnonyViet 👉 Link 👈

With the Top Down game, we don't need gravity like the first series, we just need to move in 4 directions. Usually, when you search YouTube for articles about basic top down games, you will see that people often use the if command to check Input and Moving and doing it like that takes a lot of time so I'll do it in a quick way :3.

Note: This will be lesson 1 for both seires

Create Project

So as usual we will create a new project.

[Lập Trình Top Down Game]  #1: Move Top Down

Project Name: Project name

Project Path: Path

Renderer:

Choose OpenGL 3.0 if you want to make PC & Android games – Choose OpenGL 2.0 if you want to make web games.

After you have the path, click Create Folder and then click Create & Edit to start

Create Sence

Like the previous articles with Godot, we will create a new Sence that will contain characters and other things

[Lập Trình Top Down Game]  #1: Move Top Down

You can use Black Node or 2D Node. I will use 2D Node and name it World then press Ctrl + S to save it. I will save it in the folder containing Scene.

[Lập Trình Top Down Game]  #1: Move Top Down

Character Creation

Click on the + sign next to the Sence name to create a new sente

[Lập Trình Top Down Game]  #1: Move Top Down

Then click Other Node or the + Sign under the word Sence to add a Node. You guys find yourself Node KinematicBody2D. This is a “Kinesthetic Body” button that helps us move and perform more easily.

Then you add 2 Nodes for yourself: “Sprite” and “CollisionShape2D“.

Sprite: Is the image node for the parent node, in short, it is the node that displays images for the KinematicBody2D.

CollisionShape2D: is the node that represents collision data in 2D space, that is, the shapes used for collision. Anyone who has ever been in contact with it will understand.

Generally in Godot it is: KinematicBody2D is the soul – Sprite is the outer shape – CollisionShape2D is the skeleton

[Lập Trình Top Down Game]  #1: Move Top Down

I will change the name of KinematicBody2D to Player for easy identification.

For Sprite, I currently don't have an asset, so we'll temporarily use the icon.png available in godot's filesystem: v In the next post, I'll look for the asset.

Click on Sprite, then click on the Icon, then drag it over to the Sprite's Texture

[Lập Trình Top Down Game]  #1: Move Top Down

Next we will set CollisionShape2D for it. Click on the collision node and then choose a shape for it. Here, I'll choose a square shape so it's square with the Sprite.

[Lập Trình Top Down Game]  #1: Move Top Down[Lập Trình Top Down Game]  #1: Move Top Down

So we have created the basic character of the basic :').

After that, please save it

Next will be the Script part, click on the paper sign with the blue + sign to create the script. Remember to click on the Player button to create, not in Sprite or CollisionShape2D.

[Lập Trình Top Down Game]  #1: Move Top Down

Migration Code

We will use it GDScript by Godot. This is Godot's own language based on Python and Lua.

As mentioned at the beginning of the article, I will guide you through advanced moves: 3.

Create a Function to Take Input Data

We will create a new function called laydau_nhapvao(): that is, the function will take all input information from the outside. For example, if you press the K key, it will attack, which is taking information from an external input.

So I will create the func function laydau_nhapvao():

In it, I will create a variable called vector_dauvao assigned = Vector2.ZERO or Vector2() (the two = each other).

vector_dauvao is the variable used to determine Vector2(x,y), which is the beginning x,y. If you press left, it will move left (Vectoc(-1,0)), and if you press right, it will move right (Vector2(1,0)), up then (Vector2(0,-1) not is Vector2(0,1) as in mathematics, in the game vice versa).

func laydau_nhapvao():
var vector_dauvao = Vector2.ZERO
vector_dauvao.x = Input.get_action_strength("ui_right")- Input.get_action_strength("ui_left")
vector_dauvao.y = Input.get_action_strength("ui_down") - Input.get_action_strength("ui_up")

move_and_collide(vector_dauvao)

Here:

vector_dauvao.x = Input.get_action_strength(“ui_right”) – Input.get_action_strength(“ui_left”): means Input(input).get_action_strength(“ui_right”) (getting action intensity can be understood as taking input when pressing right) then subtract Input.get_action_strength(“ui_left”) => ui_right(Vector(1,0)) and ui_left(Vector(-1,0)) when pressing right but not pressing left, the pressing action Right then Vector2 = Vector2(1,0) and since left side = 0 then 1-0 will = 1 (then when we move to the right Vector(1,0) the x = 1 then we don't move to the left so it = 0 when we move to the left it = -1 so 1-0 will = 1 and = 1 then it moves to the right and to the left vice versa, vector_dauvao.y is similar)

Apply migration

After that, I will add a command to apply movement to the character: move_and_collide (move and collide is different from move_and-slide (move and slide), it also has the effect of pushing other objects :)) yes yes If it is move_and_slide, it can only move and slide but cannot be pushed otherwise. If you use Rigdbody2D, both slide and collide can push it: 3 pretty cool features of Godot)

Call the function to get data

Then we have to call it into the physical process function (physical_process(delta)) or process (_process(delta)) you can see the basics here: Godot GDScript

[Lập Trình Top Down Game]  #1: Move Top Down

Instance Node

Then go to the sense world and the player node instance to test

[Lập Trình Top Down Game]  #1: Move Top Down

Remember to drag the Player to the middle.

[Lập Trình Top Down Game]  #1: Move Top Down

Then you press F5 or F6 to run the game (F5 selects the scene to run, F6 runs the scene you are working on). When running the game, press the arrow button to move. But you notice that when moving straight horizontally and moving diagonally, the diagonal movement speed is faster and not as steady as the other two moves, so we have a command to normalize the speed as follows: .normalized().

So we'll type

 vector_dauvao = vector_dauvao.normalized()

[Lập Trình Top Down Game]  #1: Move Top Down

Speed ​​and Delta

I will use the export var variable to export to the Inspector panel (this is a quite OK feature of Godot).

For example:

[Lập Trình Top Down Game]  #1: Move Top Down

First we declare

export var speed = 100 and adjust it as much as you like

Then we will * it with the variable vector_dauvao and * with delta too

You can do this.

[Lập Trình Top Down Game]  #1: Move Top Down

But for me, I created a new variable containing the above 3 things to make it easier to understand.

[Lập Trình Top Down Game]  #1: Move Top Down

In laydau_nhapvao there is no delta, so I will create a delta right in func laydau_nhapvao():

Then in_physical_process(deta): I just need to call it in.

End

So we've finished lesson 1 in the game programming series with Godot, and see you again in the next lesson ;3

Previous Post

How to compress and decompress files on Android phones and iPhones

Next Post

Create a lightweight Windows 11 ISO with Tiny11Builder

AnonyViet

AnonyViet

Related Posts

How to view web access history in the anonymous mode (Incognito) of Chrome
Tips

How to view web access history in the anonymous mode (Incognito) of Chrome

August 22, 2025
How to graft the peach branch based on electric poles, family cycling, uncle
Tips

How to graft the peach branch based on electric poles, family cycling, uncle

August 21, 2025
Unlock checkpoint 72h often have photos already but hang nick
Tips

Unlock checkpoint 72h often have photos already but hang nick

August 19, 2025
GIF image creation tips with high quality snipping tool
Tips

GIF image creation tips with high quality snipping tool

August 19, 2025
How to unlock Facebook to download your latest photos
Tips

How to unlock Facebook to download your latest photos

August 18, 2025
Instructions on how to format text on the Windows 11 notepad
Tips

Instructions on how to format text on the Windows 11 notepad

August 16, 2025
Next Post
Create a lightweight Windows 11 ISO with Tiny11Builder

Create a lightweight Windows 11 ISO with Tiny11Builder

0 0 votes
Article Rating
Subscribe
Login
Notify of
guest

guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments

Recent News

Đá Gà Online Sonclub Đỉnh Cao Giải Trí 2025

August 22, 2025
How to view web access history in the anonymous mode (Incognito) of Chrome

How to view web access history in the anonymous mode (Incognito) of Chrome

August 22, 2025
How to automatically erase the web history after escaping to absolutely secure

How to automatically erase the web history after escaping to absolutely secure

August 22, 2025
Stainless steel flange price list at Asia Industry

Stainless steel flange price list at Asia Industry

August 21, 2025

Đá Gà Online Sonclub Đỉnh Cao Giải Trí 2025

August 22, 2025
How to view web access history in the anonymous mode (Incognito) of Chrome

How to view web access history in the anonymous mode (Incognito) of Chrome

August 22, 2025
How to automatically erase the web history after escaping to absolutely secure

How to automatically erase the web history after escaping to absolutely secure

August 22, 2025
AnonyViet - English Version

AnonyViet

AnonyViet is a website share knowledge that you have never learned in school!

We are ready to welcome your comments, as well as your articles sent to AnonyViet.

Follow Us

Contact:

Email: anonyviet.com[@]gmail.com

Main Website: https://anonyviet.com

Recent News

Đá Gà Online Sonclub Đỉnh Cao Giải Trí 2025

August 22, 2025
How to view web access history in the anonymous mode (Incognito) of Chrome

How to view web access history in the anonymous mode (Incognito) of Chrome

August 22, 2025
  • Home
  • Home 2
  • Home 3
  • Home 4
  • Home 5
  • Home 6
  • Next Dest Page
  • Sample Page

©2024 AnonyVietFor Knowledge kqxs hôm nay xem phim miễn phí mm88 8XBET mm88 trang chủ new88

No Result
View All Result
  • Home
  • News
  • Software
  • Knowledge
  • MMO
  • Tips
  • Security
  • Network
  • Office

©2024 AnonyVietFor Knowledge kqxs hôm nay xem phim miễn phí mm88 8XBET mm88 trang chủ new88

wpDiscuz
0
0
Would love your thoughts, please comment.x
()
x
| Reply