If you can follow this part, it proves that you are a very hard-working person :D. This is the last part in the basic game programming series with godot Already. Regarding the Menu – Continue and Install, I will write it in the Tutorials article because it is outside the Series because this Series only guides you to program a basic game, not advanced.
Join the channel Telegram belong to AnonyViet 👉 Link 👈 |
In this part, I will guide you to collect coins. You can use it in many ways such as after collecting it, the game ends, if you don't collect enough within the time, you lose, etc.
Groups:
This is a very useful feature of Godot that helps you identify a button through a Group
Here Group is the group and you can put each button into a different group and use it to identify the button.
You can identify the node using the command is_in_groups()
or if you want to get the parent node equal to the child node, use it get_parent_in_group()
And to add nodes to the group you can do 2 ways.
1. It is added in the Node Panel next to the Inspector
2. It is added by Script with the command add_to_group() and this add_to_group() part you should leave in the _ready function because the ready function is the function that will run the code when it first enters the game so we will put it there so that when we first run the game it will run. will be added to the group immediately
Setting Item:
You can download assets Coin Games to get resources
First we will create a new sense Area2D.
Here we will use area2d because it has a singal that identifies bodies or areas that collide with it, so we will use it so that when the player touches it, it will send a signal that the boides have collided and collect the item.
Note: Here, our asset coin is SpriteSheet because there is only 1 row, so Vframe will = 0 Hframe, then you can count it yourself. If it is blurry, reimport it again.
Then save it
Next we will create an Animation for it with AnimationPlayer. Create a Node and then create a new Aniamtion
Then add the track
Choose Frame
Then you also add a key to the track as usual
Next we will create a script for the area2d.
Here we need to check that if the body goes into the player, it will disappear, otherwise if the monster goes into the coin it will disappear, then woe :V and the player's coin count will be + 1.
So we will connect the signal body_entered
And after the connection is complete, we will create an if statement to identify the bodies as players with Groups. Then the player's coin variable will + 1 and then self-destruct (queue_free()).
func _on_Coin_body_entered(body): if body.is_in_group("Player"): body.coin += 1 queue_free()
Then in the jaw _ready
Let's run the animation.
Next we will create a coin variable in Player so that when the player enters, the coin will + 1
Then in the Player's func _ready() function, you add a command for it to go to Group
To be add_to_group("Player")
Then you set the mask/layer to make sure it works :v
In Project Settings, I will add a new mask/layer called Coin
Then you set a mask/layer for it
Mask:
Layer:
Then in the Player's Mask, you also tick the Coin box
So if it's successful, you can use it to do anything, it all depends on your imagination.
Next, I will create a Label to display the Coin number
So that's the end of the basic game programming series with godot and later I will write 2D Tutorials and Visual Scripting with Godot.
You can Download Full Project Code Game Godot of 14 lessons here.