Next, with the game programming series with Godot today I will guide you the next article is gravity, moving surface and dancing.
Join the channel Telegram belong to Anonyviet 👉 Link 👈 |
Oh, before doing, if you want to reopen the Sentce, you are working on the filesystem section and double -click on the previous sense you saved before and yours saved here. World.TSCN
Move surface: The moving surface is the part that we let the character we move on it can say it is like the road surface for us to go.
To create the surface you do like you and this is just the basic of basic, if you want to create a moving surface like other games, people call it tilemap and tileset I will guide in the following article:
Create node Staticbody2d under the world
LIVE Staticbody I will change my name to Floor (double click on the name) and I also change the name Kinematicbody wall Player okay.
And like staying Node Player I will create 2 node To be Sprite: Pictures for Floor and Collset
Collset Here, the explanation is very difficult, so I explain briefly for those who understand it. Example:
When you create a sprite without Collset Then it is just an image, an image painted. If you create 1 Sprite + Collset Then it means that you use your hand to squeeze a bag of the ball into the wall, it will collapse. Collset And when you have collset, it is like inside the ball bag with a brick, when you press the ball, there is a brick you cannot squeeze it because the brick is hard. The general speaking is Sprite is a skin set still Collset is bone In that skin set.
Or can understand Collset is the frame To prevent the inner part for Sprite to operate
Here Staticbody2d Used for physical movement if you want to build a trap, a bridge, something standing still or moving as an obstacle, then use it Staticbody2d.
Then you do the same Lesson 1 With node player like this:
I will not instruct specifically, but you have to remember each post to go up.
In Floor, click Sprite to notice the right part below canvasitem, then click in Visiable> Select modulate And adjust the color into any color.
Here I change to black
And as the picture on the boards can see that Sprite has changed to threatn but that Collset It presses again Sprite Then you change the position Collset up Sprite or click The eye is on the right of Collsion To hide it and when hiding you do not need to worry because it is only hidden, not doing anything.
And you lock it again Node Player:
Well at the end of the post I will leave the zoom buttons with the keyboard for those who use a laptop without a mouse or the mouse button is damaged like me.
And choose Floor You pay attention to me 2 parts I have circulated in the photo
Side dark white fruit It displays Grid (square squares like Pixel Asia).
Here, this grid is a bit small so I will zoom
Let zoom up you press 3 dots next to:
Choose the last one Configure Snap. Change GIRD STEP wall 32-32
And as photos:
You can move the floor but you can see that it does not have the right umbrella so Godot has given us another function that is Move in Pixel.
In the side Turn on Pixel You press in the floor and then turn it on:
Our floor will then move in pixels
And you put it down to make the floor move
Under the photo you pay close attention, see a blue umbrella, the umbrella is our screen, remember to put Floor inside this umbrella.
Here I create 1 more Node Node Name is Wall And there contains Floor and Floor I change to 01
You put the position for Floor then click on the 01 Press the combination Ctrl + D It will double out and in the Floor 01 place you pull out is not understood, then look at the photo:
Double
Now pull out
It will produce 2 pieces
So 2 of you can do continuously to create a longer moving floor
But before creating the floor, you need to check whether the collset is equal or not to check the collability of 01 and 02
Like mys, it is not very tight but the player still moves deliciously. If your deviations, adjust it to be flat.
Now create many,
I have finished creating:
Running the program you will see this:
It looks quite ugly, right? But do not worry you just learn the basics to do it and this is just a guide, but the first lesson will guide you to create a player and a complete tilemap and even background, enemy too .
You can move it down and will see it will be blocked by Floor
And so you have created the moving surface, then the next is gravity.
Gravity:
When it comes to gravity, who doesn't know 😀 What makes us move easily.
To create gravity, you go to the Player script.
Declare variables:
Declare an additional gravity variable assigned it by 20
In Physics code for me as follows
Chuyendong It is what helps us move along the coordinate axis X, Y: Y is down, X It is right.
here chuyendong.y Meaning using a transformer to call the axis because we declare it chuyendong = Vector2()
mean chuyendong It is allowed to use the X axis, Y and here we call the Y axis to move down by chuyendong | chuyendong.y + = tocdo means chuyendong.y = chuyendong + inluc
And it means when chuyendong The y + axis with inluc is 20, it will move down with 20 times gravity.
And run the game:
We will see it falls down and the collision of floor it is an obstacle for our player not to let the player fall down.
If your movement is like me:
Then you will find it impossible to move up when pressing the arrow
And next is dance.
Dance
To code the dance part, you will code like me.
Delete if input up and if input down Go
And change
if input ui_left thành > elif input.is_action ui_left
And the code adds 1 part Else:
else:
Moting X axis is 0 || = 0, not += offline (I will write like this for you to understand and code it and also reform your code)
After the code you press F6 running the game, it will see it can move to the left and after stopping the movement will stand still.
Next to the jumping section.
Declare yourself a variable:
Nhaycao and assign it by -500
Why assign it by -500 Simply because when you create a gravity, our Player will fall down according to the axis, right? So here we assign -500 that it will move up in the opposite direction to the gravity when jumping to a certain height because inluc is always assigned by 20, so after dancing it will force your player down.
You declare a variable UP = Vector2(0, -1)
Variable Upload This is a variable to help you jump up 0, -1 || 0 is the X axis, -1 is the Y axis
Next code for yourself the image:
if is_on_floor
This means that when you are on the floor (Floor we have created) and only when it is on the floor it can do it.
if input ui_up
Then read the previous articles also know
chuyendong.y += nhaycao
To be: chuyendong Call the axis and assign it by variables nhaycao When you press the arrow key and when assigned it will jump on why? Then I explained in the declaration of variables
À in the part Move_and_slide () Add for me Upload
Note:
Some notes when code:
In the IF statement you can notice the character> | Fuzzy on the right of the command is the arrangement of the statements
Means the IF command that you go down to the code without> | Then it will not do this as this is not done
> | IF
> | Chuyendong = 0
Then it cannot be done and reported
If this is this:
> | IF
> | > | chuyendong = 2
Then it will work offline
How to move the sensing and zoom key for those who spoil the mouse roller or use a laptop:
- Ctrl + – (sign – that): zoom out
- Ctrl + + (sign + that): zoom in
Click between the surrounding or right mouse
Here you will see 3 white
- The first one (4 arrows): Move Sprite
- The second one: Turn the Sprite or press Ctrl to rotate
- The third: zooming in or zooming in the sprite
So the next post will be about Tilemap and Tileset then will then go to the character's motion.
Deathgm!