• 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

[Lập Trình Game] Lesson 2: Gravity, moving surfaces and jumping

AnonyViet by AnonyViet
February 3, 2023
in Tips
0

Next with the series of game programming with godot today I will guide you to the next lesson that is Gravity, Moving Surface and Jumping.

Join the channel Telegram belong to AnonyViet 👉 Link 👈

Well before you do, if you want to reopen the sence you’re working on now, look under the FileSystem section and double click on the sence you saved before and yours is saved here. World.tscn

[Lập Trình Game]  Lesson 2: Gravity, moving surfaces and jumping 34

Moving Surface: The moving surface is the part that we let our character move on it can be said to be like the road surface for us humans to walk on.

To create a surface, you do as I do and this is just the basics of the basics, if you want to create a moving surface like other games, it’s called tilemap and tileset I will guide you in the following article:

Create Node StaticBody2D under the authority of World

[Lập Trình Game]  Lesson 2: Gravity, moving surface and jumping 35

[Lập Trình Game]  Lesson 2: Gravity, moving surface and jumping 36

LIVE StaticBody I will change my name back to floor (double click to change the name) and I changed the name too KinematicBody Fort Player Please.

And also in Node Player i will create 2 Nodes To be Sprite: the photo for floor and Collsion

Collsion It is very difficult to explain here, so I will briefly explain it for you to understand. VD:

When you create a sprite without collsion then it’s just an image, a painted image. If you create 1 sprite + collection then it means it’s like if you press a bag of balls against the wall with your hand, it will collapse, that’s when there is no collsion and when there is collsion it is like inside the ball bag there is a brick then when you press the ball bag there is a brick you cannot squeeze it because the brick it is hard. Generally speaking sprite is the skin still collsion is bone in that skin.

Or you can understand collection is the frame to prevent the inner part for the sprite to work

Here StaticBody2D used for physical movement if you want to build a trap, bridge, something stationary or moving as an obstacle, use StaticBody2D.

Then you do the same Lesson 1 with Node player like this:

[Lập Trình Game]  Lesson 2: Gravity, moving surfaces and jumping 37

I will not give specific instructions, but you have to remember each post to go up.

On the floor, click on the sprite to notice the right part below the CanvasItem, then click on it Visiable > select Modulate and change the color to any color.

[Lập Trình Game]  Lesson 2: Gravity, moving surface and jumping 38

Here I change to black

[Lập Trình Game]  Lesson 2: Gravity, moving surfaces and jumping 39

And as the photo above you can see that sprite changed to blackn but what collsion it’s on top again sprite then you change positions collsion go up sprite or click the eye on the right collsion to hide it and when hidden you don’t need to worry because it just hides and doesn’t do anything.

[Lập Trình Game]  Lesson 2: Gravity, moving surface and 40 . jump [Lập Trình Game]  Lesson 2: Gravity, moving surfaces and jumping 41

And you lock it up like Node Player:

[Lập Trình Game]  Lesson 2: Gravity, moving surfaces and jumping 42

Well at the end of the post I will leave the zoom buttons with the keyboard for those of you who use a laptop without a mouse or a damaged mouse scroll button like me.

And choose floor You guys notice for me the 2 parts I’ve circled in the picture

[Lập Trình Game]  Lesson 2: Gravity, moving surfaces and jumping 43

The side dark white fruit is that it displays the grid (squares like pixels).

[Lập Trình Game]  Lesson 2: Gravity, moving surfaces and jumping 44

This grid is a bit small here, so I’ll zoom in

To zoom in, press the 3 dots next to it:[Lập Trình Game]  Lesson 2: Gravity, moving surface and jumping 45

Choose the last one Configure Snaps. Change Grid Step Fort 32-32

[Lập Trình Game]  Lesson 2: Gravity, moving surfaces and jumping 46

And it looks like the picture:

[Lập Trình Game]  Lesson 2: Gravity, moving surfaces and jumping 47

You can move the floor but you can see that it is not in the correct box so Godot gave us one more function that is move in pixels.

On the one next to the one turn on pixels you click on floor and then turn it on:

[Lập Trình Game]  Lesson 2: Gravity, moving surfaces and jumping 48

Then our floor will move in pixels

And you put it down to make the floor move

Below the picture, if you pay close attention, you will see a blue umbrella, that umbrella is our screen, remember to put the floor inside this box.

Here I create more 1 the Node name is wall and there contains floor and floor I changed to 01

[Lập Trình Game]  Lesson 2: Gravity, moving surfaces and jumping 49

You set the location for floor then click on the 01 press combination Ctrl + EASY it will be duplicated and in the place of floor 01, you can pull it out if you don’t understand, look at the picture:

Doubled

[Lập Trình Game]  Lesson 2: Gravity, moving surface and 50 . jump

Now pull out

[Lập Trình Game]  Lesson 2: Gravity, moving surfaces and jumping 51

It will come out 2

[Lập Trình Game]  Lesson 2: Gravity, moving surfaces and jumping 52

So with 2 you can do it continuously to create a longer moving floor

But before creating the floor, you need to check if the collsion is equal or not to check if you turn on the collsion of 01 and 02.

[Lập Trình Game]  Lesson 2: Gravity, moving surfaces and jumping 53

Like mine, it’s not very tight but the player still moves well. If yours is skewed, adjust it to be flat.

Now create multiple ones,

I have created it:

[Lập Trình Game]  Lesson 2: Gravity, moving surfaces and jumping 54Run the program and you will see something like this:

[Lập Trình Game]  Lesson 2: Gravity, moving surface and jumping 55

Does it look pretty bad? But don’t worry you only learn the basics to do it and this is just a tutorial, but the first lesson will guide you to create 1 player and a complete tilemap and background, enemy too. .

You can move it down and it will be blocked by the floor

And that’s it, you have created the Moving Surface, followed by Gravity.

Gravitation:

When it comes to gravity, who doesn’t know 😀 the thing that helps us move easily.

To create gravity, you go to the player’s script.

Declare variable:

Declare 1 more gravity variable set it equal to 20

[Lập Trình Game]  Lesson 2: Gravity, moving surfaces and jumping 56In physics code for me as follows

[Lập Trình Game]  Lesson 2: Gravity, moving surfaces and jumping 57

Chuyendong is what helps us move along the x, y coordinate axes: Y is down-up, X is right-left.

here chuyendong.y that is, use the variable to call the y-axis because when we declare chuyendong = Vector2() mean change allowed to use x, y axes and here we call y-axis moving down by transition| chuyendong.y += tocdo means chuyendong.y = chuyendong + inluc

and it means when change y + axis with inluc is 20 then it will move down with 20 times gravity.

And run the game:

[Lập Trình Game]  Lesson 2: Gravity, moving surfaces and jumping 58

We will see it drop and its Floor Collsion is an obstacle for our player to prevent the player from falling.

If your migration is like mine:

[Lập Trình Game]  Lesson 2: Gravity, moving surfaces and jumping 59

Then you won’t be able to move up when you press the up arrow

And next is dancing.

Dance

To Code the dance part, you will code like me.

Delete section if input up and if input down Go

[Lập Trình Game]  Lesson 2: Gravity, moving surface and 60 . jump

And change

if input ui_left thành > elif input.is_action ui_left

And the code adds an else part:

else:

The x-axis motion is 0|| = 0 rather than += (I will write it like this for you to understand and code it and also improve your coding ability)

After the code you press F6 to run the game will see it can move right to left and after stopping moving will stand still.

Next comes the jumping part.

Declare yourself a variable as:

Nhaycao and assign it with -500

Why assign it with -500 simply because when you create the gravity variable, our player will fall along the y-axis right? So here we assign -500 so that it will move upwards against gravity when jumping to a certain height because inluc is always set to 20, so after jumping it will force your player down.

You declare yourself 1 more variable UP = Vector2(0, -1)

[Lập Trình Game]  Lesson 2: Gravity, moving surfaces and jumping 61

Variable UP This is a variable that allows you to jump to 0, -1 || 0 is the x-axis, -1 is the y-axis

Next, give me the code for the part like the picture:

[Lập Trình Game]  Lesson 2: Gravity, moving surfaces and jumping 62

if is_on_floor which means when you are on the floor (the floor we created) and only when on the floor it can do it.

if input ui_up Well, if you read the previous posts, you already know

 chuyendong.y += nhaycao To be: change call y-axis and assign it with variable haycao when you press the up arrow key And When assigning it jumps why? Then I explained in the declaration of the variable nhaycao

Ah in the move_and_slide() more for me UP

[Lập Trình Game]  Lesson 2: Gravity, moving surfaces and jumping 63

Note:

Some notes when coding:

In the if statement you can notice the character >| The blur to the right of the command is what sorts the commands

That is, the if statement that you enter the line to code without >| then it will not execute eg this is not done

>| if

>|Chuyendong = 0

Then it failed to execute and reported an error

And if like this:

>| if

>| >| chuyendong = 2

Then it will work

How to move the sence and zoom keys for anyone who has a broken mouse wheel or uses a laptop:

  • Ctrl + – (the – sign): zoom out
  • Ctrl + + (the + sign there): zoom in

Middle click to move around or right click

Here you will see 3 white ones

[Lập Trình Game]  Lesson 2: Gravity, moving surfaces and jumping 64

  • 1st one (4 those arrows): move Sprite
  • 2nd one: Rotate the Sprite or press Ctrl to rotate
  • 3rd one: Zoom in or out of the Sprite

That’s it, the next lesson will be About Tilemaps and Tilesets, and then the character’s movement will be.

DeathGM!

The article achieved: 5/5 – (100 votes)

Tags: GameGravityjumpinglậpLessonmovingsurfacestrình
Previous Post

How to activate the Google Assistant Vietnamese virtual assistant 100% success

Next Post

How to Enable Microsoft Edge’s New CrapWare Blocker

AnonyViet

AnonyViet

Related Posts

4 ways to fix bluetooth connectivity on Windows 11
Tips

4 ways to fix bluetooth connectivity on Windows 11

August 8, 2025
How to know the computer is tracked and processed by Keylogger
Tips

How to know the computer is tracked and processed by Keylogger

August 7, 2025
Opal: Create applications who do not need to write code
Tips

Opal: Create applications who do not need to write code

August 3, 2025
How to activate a new Start menu on Windows 11
Tips

How to activate a new Start menu on Windows 11

July 29, 2025
Intellgpt: AI tool for osint and data science
Tips

Intellgpt: AI tool for osint and data science

July 28, 2025
How to create Google Ai Pro 12 months free with Indian student account
Tips

How to create Google Ai Pro 12 months free with Indian student account

July 27, 2025
Next Post
How to Enable Microsoft Edge’s New CrapWare Blocker

How to Enable Microsoft Edge's New CrapWare Blocker

0 0 votes
Article Rating
Subscribe
Login
Notify of
guest

guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments

Recent News

How to add application to your favorite bar

How to add application to your favorite bar

August 14, 2025
Wowhay.com – The door opens the world of modern knowledge and network culture

Wowhay.com – The door opens the world of modern knowledge and network culture

August 13, 2025
Instructions on how to fix Screen Time Limited Reached on RoBlox

Instructions on how to fix Screen Time Limited Reached on RoBlox

August 13, 2025
How to install GPT-suns on who do not need the Internet

How to install GPT-suns on who do not need the Internet

August 12, 2025
How to add application to your favorite bar

How to add application to your favorite bar

August 14, 2025
Wowhay.com – The door opens the world of modern knowledge and network culture

Wowhay.com – The door opens the world of modern knowledge and network culture

August 13, 2025
Instructions on how to fix Screen Time Limited Reached on RoBlox

Instructions on how to fix Screen Time Limited Reached on RoBlox

August 13, 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

How to add application to your favorite bar

How to add application to your favorite bar

August 14, 2025
Wowhay.com – The door opens the world of modern knowledge and network culture

Wowhay.com – The door opens the world of modern knowledge and network culture

August 13, 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