If you can follow this part, it proves that you are a very hard worker :D. This is the last part in the series of basic game programming with godot already. About 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 an advanced one.
Join the channel Telegram of the AnonyViet 👉 Link 👈 |
In this part, I will guide you to collect coins, you can use it for many ways such as collecting, then the game ends, if you don’t collect enough in time, you will lose, etc.
Groups:
This is a very useful feature of Godot, it helps you to define a button through Group
Here Group is the group and you can give each node a different group and use it to identify the node.
You can define the button with the command is_in_groups()
or if you want to get the parent Node with the child node, use get_parent_in_group()
And to add node to the group you can do 2 ways.
first. Is added in the Node Panel next to Inspector
2. It is added by Script with the add_to_group() command and this add_to_group() part, you should leave it 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 it first runs the game it Will be added to the group immediately
Setting Item:
You can Download Coin Games assets to get resources
First we will create a new sense Area2D.
Here we will use area2d because it has a singal that recognizes 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 boides collided and collects the item.
Note: Here, our asset coin is SpriteSheet because there is only 1 row, so Vframe will = 0 Hframe, you can count it yourself. If it’s fuzzy, reimport it.
Then save it
Next we will create Animation for it with AnimationPlayer.Create Node and then create new Animation
Then add the track
Choose a Frame
Then you also add the key to the track as usual
Next we will create a script for the area2d.
Here we need to check if the body goes in is the player, it will disappear, otherwise if the monster goes in the coin it will disappear again, suffering :V and the player’s coin number will + 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 that are player with Groups. Then the player’s coin variable will + 1 and will self-destruct (queue_free()).
func _on_Coin_body_entered(body): if body.is_in_group("Player"): body.coin += 1 queue_free()
Then at the function _ready
Let’s run the animation.
Next we will create a coin variable in Player so that when the player comes in, the coin will +1
Then in Player’s func _ready() function, you add a command for it to Group
To be add_to_group("Player")
Then you set a mask/layer to make it sure :v
In Project Setting, I will add a new mask/layer, Coin
Then you set a mask/layer for it
Masks:
Layers:
Then in the Player’s Mask, please also tick the Coin box
So success r you can use it to do anything, it all depends on your imagination.
Next I will create a Label to display the number of Coins
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 songs here.