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

4.2 KiB
Executable file

Section 03 - Lesson 30 | Section Review

Notes

paddle scene

node composition

  • root: Area2D
  • Sprite2D
  • CollisionShape2D

script view: paddle.gd

  • extends Area2D
use @export to create speed variable
  • can be modified in Inspector
  • give default value
override _process function
  • check if Input.is_action_pressed

    • is 'left'
    • is 'right'
  • move paddle

    • get the Node2D.position property

      • multiply speed by delta and add or subtract it from position.x
      • delta is number of seconds that elapsed since the previous frame

gem scene

node composition

  • root: Area2D
  • Sprite2D
  • CollisionShape2D

script view: gem.gd

  • extends Area2D
set class name
  • class_name Gem
  • this allows us to instantiate Gem objects in scripts
set a signal property
  • signal on_gem_off_screen
  • allows us to attach listeners who will be notified when we emit a signal
set export property
  • @export var speed
  • lets us set the speed in the Inspector

modify _process

determine if the gem has gone out of bounds
  • this is going to be the edge of the viewport
  • check position property against the viewport rect property

    if position.y > get_viewport_rect().size.y
if gem went out of bounds
emit signal
on_gem_off_screen.emit()
stop the gem and remove it from the node tree
  • stop going through the process function
  • delete node at the end of the current frame
set_process(false)
queue_free()

Game scene

preload assets

  • using const to set builtin preload function

     const EXPLOSDE = preload("res://assets/explode.wav")

add a PackedScene variable

  • allows us to instantiate gem scenes
  • export allows us to drag and drop the scene we want into the inspector

    @export var gem_scene: PackedScene
    
     # when time comes to use it
     gem_scene.instantiate()
     add_child(gem_scene)
  • otherwise we would have to preload it

     gem_scene = preload("res://somescene.tscn").instantiate()
     add_child(gem_scene)

use onready to add nodes in tree scene

@onready var label: Label = $Label
@onready var timer: Timer = $Timer
@onready var audio_stream_player_2d: AudioStreamPlayer2D = $AudioStreamPlayer2D
  • these are all scenes in the Game tree
  • use onready to attach them to variables for use in game script
  • onready makes sure it's available so we dont end up with a null reference

spawn gem at _ready

  • in the ready function immediately call spawn_gem
  • spawn Gem instantiates a gem scene and type casts it to a Gem class
  • connect any signals
  • set it's position
  • add it as a child

    var new_gem: Gem = gem_scene.instantiate()
    new_gem.on_gem_off_screen.connect(game_over)
    new_gem.position = Vector2(xpos, ypos)
    add_child(new_gem)

stop all function

  • stop the timer
  • run through all children and set process to false

    timer.stop()
    for child in get_children():
     child.set_process(false)

turn off audio

  • get references to audio and call 'stop' function
  • you can also set another sound for ending and play that
 audio_stream_player_2d.stop()
 audio_stream_player_2d.stream = EXPLODE
 audio_stream_player_2d.play()

whenever timer times out spawn a gem

  • we attached the timer to a signal to call a function that calls spawn gem

when the paddle gets hit by a gem

  • increase score
  • set the position of where the paddle got hit to the audio and play a sound
  • free up the hit object (the gem)
func _on_padd_area_entered(area: Area2D) -> void:
	_score += 1
	label.text = "%05d" % _score
	audio_stream_player_2d.position = area.position
	audio_stream_player_2d.play()
	area.queue_free()