• 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

Instructions on how to format text on the Windows 11 notepad
Tips

Instructions on how to format text on the Windows 11 notepad

August 16, 2025
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
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

Instructions on how to format text on the Windows 11 notepad

Instructions on how to format text on the Windows 11 notepad

August 16, 2025
Instructions for receiving 80GB of free data from VinaPhone from August 15

Instructions for receiving 80GB of free data from VinaPhone from August 15

August 15, 2025
Online driving exam preparation: Support theory and practice

Online driving exam preparation: Support theory and practice

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

How to add application to your favorite bar

August 14, 2025
Instructions on how to format text on the Windows 11 notepad

Instructions on how to format text on the Windows 11 notepad

August 16, 2025
Instructions for receiving 80GB of free data from VinaPhone from August 15

Instructions for receiving 80GB of free data from VinaPhone from August 15

August 15, 2025
Online driving exam preparation: Support theory and practice

Online driving exam preparation: Support theory and practice

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

Instructions on how to format text on the Windows 11 notepad

Instructions on how to format text on the Windows 11 notepad

August 16, 2025
Instructions for receiving 80GB of free data from VinaPhone from August 15

Instructions for receiving 80GB of free data from VinaPhone from August 15

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