compsci_godot_albert/_subsections/sec03/lesson-15.org
2025-07-17 01:33:06 +03:00

2.3 KiB
Executable file

Section 03 - Lesson 15 | Scoring

Notes

add a Label node

  • add Label node to Root node of Game

    • child of CanvasItem and Control
    • NOT a Node2D
  • appears in upper left corner of the 2D screen

    /notes/compsci_godot_albert/media/commit/c59e28d3a8156577ead9c279e579c5a5753c19e2/_share/media/img/albert/section03/S03_L15_EX01.png

set label text

  • go Scene->Label node->Inspector->Label->text
  • type in whatever you want, e.g. '0000' for score

make the size bigger

  • Scene->Label node->Inspector->Control->Theme Override

    • set the font and font size

move the label

  • Scene->Label node->Inspector->Control->Layout->Transform
  • change the Position values

if using shadow and offsets

  • you set the colors in Theme Overrides
  • change shadow offset and outline size in

    • Inspector->Label->Theme Overrides->Constants

Displaying the score

create a var to hold the score

var _score: int

register when the paddle hits a gem

  • currently this is a signal on the paddle node
  • disconnect paddle from receiving the signal bc we only want game node to receive it
in the game scene
  • Scene->Paddle->Node->Signals
  • Area2D -> area_entered
  • set the register to the Game node

create a reference the Label node

  • purpose: modify what the Label displays from the script
using onready
  1. type it in
@onready var score_label: Label = $Label
  1. drag and drop

    • grab the node from the Scene panel
    • hold down CMD
    • drop it on the script

update the score and the label

  • update the _score variable in receiving method
  • set the text property in the label reference to the score

    func _on_paddle_area_entered(area: Area2D) -> void:
      _score +=1
        label.text = str(_score)

remove the gem from the scene if it hits the padde

  • the area2D node passed into the receiver method is a reference to the gem node
  • so you can use the queue_free method to remove it from the scene queue

    area.queue_free()