• 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

[Tạo 2D Platformer Game với Godot] Part 26: Add scores at the end of the game

AnonyViet by AnonyViet
January 26, 2023
in Tips
0

In this section, I will show you how to add scores to the LevelMap so that it can update the number of stars after the game is over.

Join the channel Telegram of the AnonyViet 👉 Link 👈

Scores

Solution

About how to handle the problem of this part, I will explain as follows:

+ First I will need a script and that script can be accessed anywhere, this script will be responsible for storing specific data here is the score of each map.

+ I will calculate the score achieved by 1 map in the script of EndLevelMenu, this score if you want to calculate it will need: (number of coins collected / total number of coins in the map) * 100.

+ To get the total number of coins, I will add the coin scene to a group called dongxu and I have a function get_tree().get_nodes_in_group(“dongxu) which will allow me to get all the coins available on the scene and This function will return array.

+ Then in LevelMap some children Level1,2,.. I will assign the variable SoDiem did in the previous part equal to the score in the script containing the data.

Autoload scripts

This is a feature of Godot that allows you to turn a script into an autoload script, meaning it always runs no matter what scene you are in and can be accessed anywhere.

[Tạo 2D Platformer Game với Godot]  Part 25: Add scores at the end of the game

My friend made me a new script called PlayerData in Scenes

[Tạo 2D Platformer Game với Godot]  Part 26: Adding scores at the end of the game 19

Then go to Project -> project settings -> Autoload

[Tạo 2D Platformer Game với Godot]  Part 26: Adding scores at the end of the game 20

Click on the folder icon.

[Tạo 2D Platformer Game với Godot]  Part 26: Adding scores at the end of the game 21

Find the script just created[Tạo 2D Platformer Game với Godot]  Part 26: Adding scores at the end of the game 22 [Tạo 2D Platformer Game với Godot]  Part 26: Adding scores at the end of the game 23Click on add and you will see it displayed in the box below.

Add group scene coin

[Tạo 2D Platformer Game với Godot]  Part 26: Adding scores at the end of the game 24

Through the coin scene

[Tạo 2D Platformer Game với Godot]  Part 26: Adding scores at the end of the game 25

[Tạo 2D Platformer Game với Godot]  Part 26: Adding scores at the end of the game 26 [Tạo 2D Platformer Game với Godot]  Part 26: Adding scores at the end of the game 27On the right side next to signals, click on the group and type the group name -> click add to add the node to the group

Map

[Tạo 2D Platformer Game với Godot]  Part 26: Adding scores at the end of the game 28 [Tạo 2D Platformer Game với Godot]  Part 26: Adding scores at the end of the game 29

In the Map1 scene, I will add 1 Node2D to contain DongXu nodes to keep it neat.

Then I edited it so it’s easy to collect

How much you adjust the coin is up to you, not necessarily like me.

Player

In the Player’s script, you just need to add yourself a variable that is:

onready var max_dongxu = get_tree().get_nodes_in_group("dongxu")

This variable I use to determine the total number of coins in the map.

PlayerData

In the Playerdata autoload script that I just created above, I added a Dictonary variable named score and it is responsible for carrying the score of each map.

extends Node


var diemso = {
    "Map1": 0,
    "Map2":0,
    "Map3:": 0,
}



extends CanvasLayer



onready var level = get_parent().get_node("KhuVucDiChuyen")
onready var map = get_parent().name
onready var player = get_parent().get_node("Player")
var diemso 
func _on_NutLevelKeTiep_pressed():
    get_tree().paused = false
    get_tree().change_scene(level.LevelKeTiep)
    diemso = int(( float(player.dongxu) /player.max_dongxu) * 100)
    if diemso > PlayerData.diemso[map]: 
        PlayerData.diemso[map] = diemso
    pass # Replace with function body.

func _on_NutChoiLai_pressed():
    get_tree().paused = false
    get_tree().reload_current_scene()
    print("choi lai")
    pass # Replace with function body.


func _on_NutVeMenu_pressed():
    get_tree().paused = false
    get_tree().change_scene("res://Scences/Map/LevelMap.tscn")
    diemso = int(( float(player.dongxu) /player.max_dongxu.size()) * 100)
    if diemso > PlayerData.diemso[map]: 
        PlayerData.diemso[map] = diemso
    pass # Replace with function body.

Here, I declare 3 more variables:

+ map: for me to identify the name of the map I am playing.

+ player: so that I can get the number of coins collected and the total number of coins in the map.

+ diemso: calculate the score you achieved.

func _on_NutLevelKeTiep_pressed():
    get_tree().paused = false
    get_tree().change_scene(level.LevelKeTiep)
    diemso = int(( float(player.dongxu) /player.max_dongxu) * 100)
    if diemso > PlayerData.diemso[map]: 
        PlayerData.diemso[map] = diemso

diemso = int(( float(player.dongxu) /player.max_dongxu) * 100): is the formula I calculate the score.

if diemso > PlayerData.diemso[map]: I check if the diemso of this level is greater than the existing score, it will add this score and vice versa.

I will do the same with the NutVeMenu function because when you show the EndLevelMenu, you have already completed it, so exiting or continuing to play will count and if you press play again, it will not.

Level Script

[Tạo 2D Platformer Game với Godot]  Part 26: Add scores at the end of the game 30

You passed yourself the Level scene in the LevelMap.

And in the _process function, I add 1 line to update the score.

SoDiem = PlayerData.diemso[MapName]

I will have the full code as below:

extends Control

export (String) var MapName
export (String,FILE) var Scene
export (String) var Level
onready var ngoisaovang = preload("res://Assets/LevelMap/3 UI/Star1.png")
var SoDiem = 0

func _ready():
    $SoLevel.text = Level
func _process(delta):
    SoDiem = PlayerData.diemso[MapName]
    if SoDiem >= 1 and SoDiem <= 30:
        $NgoiSao1.texture = ngoisaovang
    elif SoDiem >= 30 and SoDiem <= 95:
        $NgoiSao1.texture = ngoisaovang
        $NgoiSao2.texture = ngoisaovang
    elif SoDiem >= 95:
        $NgoiSao1.texture = ngoisaovang
        $NgoiSao2.texture = ngoisaovang
        $NgoiSao3.texture = ngoisaovang


func _on_HinhTron_pressed():
    get_tree().change_scene(Scene)
    pass # Replace with function body.

Result

[Tạo 2D Platformer Game với Godot]  Part 26: Adding scores at the end of the game 31

So after collecting all the coins I added and returning to the Level Menu, I will get 3 stars.

Note

In that Menu, you edit the code where the Start button is, the change_scene is the scene Level Map.

[Tạo 2D Platformer Game với Godot]  Part 26: Adding scores at the end of the game 32 [Tạo 2D Platformer Game với Godot]  Part 26: Adding scores at the end of the game 33 [Tạo 2D Platformer Game với Godot]  Part 26: Adding scores at the end of the game 34

Summary

So I have finished guiding you how to add the number of stars achieved, in the next part I will guide you how to lock the level again and unlock the next level.

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

Tags: AddGameGodotPartPlatformerscorestạovới
Previous Post

Lesson 5: Hide Rows or Columns – Basic Excel

Next Post

Làm thế nào để trở thành Hacker – Lộ trình cho người mới bắt đầu

AnonyViet

AnonyViet

Related Posts

Top 5 game programming languages ​​to learn now
Tips

Top 5 game programming languages ​​to learn now

June 8, 2025
[Godot Shooter] #2: Creating characters & shooting bullets
Tips

[Godot Shooter] #2: Creating characters & shooting bullets

June 7, 2025
What do you need to learn game programming? Is it difficult? How long does it take?
Tips

What do you need to learn game programming? Is it difficult? How long does it take?

June 6, 2025
Instructions for registering chatgpt team at $ 1
Tips

Instructions for registering chatgpt team at $ 1

June 5, 2025
How to engrave the right mouse menu error on Windows
Tips

How to engrave the right mouse menu error on Windows

June 5, 2025
How to create online meme photos is very easy with a few steps
Tips

How to create online meme photos is very easy with a few steps

June 5, 2025
Next Post
Làm thế nào để trở thành Hacker – Lộ trình cho người mới bắt đầu

Làm thế nào để trở thành Hacker - Lộ trình cho người mới bắt đầu

0 0 votes
Article Rating
Subscribe
Login
Notify of
guest

guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments

Recent News

Top 5 game programming languages ​​to learn now

Top 5 game programming languages ​​to learn now

June 8, 2025
The iPhone list is updated with iOS 26

The iPhone list is updated with iOS 26

June 8, 2025
Discover the glowing effect next to the iPhone ios 18 screen

Discover the glowing effect next to the iPhone ios 18 screen

June 8, 2025
[Godot Shooter] #2: Creating characters & shooting bullets

[Godot Shooter] #2: Creating characters & shooting bullets

June 7, 2025
Top 5 game programming languages ​​to learn now

Top 5 game programming languages ​​to learn now

June 8, 2025
The iPhone list is updated with iOS 26

The iPhone list is updated with iOS 26

June 8, 2025
Discover the glowing effect next to the iPhone ios 18 screen

Discover the glowing effect next to the iPhone ios 18 screen

June 8, 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

Top 5 game programming languages ​​to learn now

Top 5 game programming languages ​​to learn now

June 8, 2025
The iPhone list is updated with iOS 26

The iPhone list is updated with iOS 26

June 8, 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í SHBET https://kubet88.yoga/ bj88

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í SHBET https://kubet88.yoga/ bj88

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