Heads up display

The final piece our game needs is a User Interface (UI) to display things like score, a "game over" message, and a restart button.

Create a new scene, and add a CanvasLayer node named HUD. "HUD" stands for "heads-up display", an informational display that appears as an overlay on top of the game view.

CanvasLayer solmu antaa meidän piirtää käyttöliittymäelementtimme muun pelin yläpuolella olevalle kerrokselle, niin että sen esittämä tieto ei jää pelielementtien, kuten pelaaja ja vihollinen, alle.

The HUD needs to display the following information:

  • Pistemäärä, joka muuttuu ScoreTimer solmun toimesta.

  • Viesti, kuten "Game Over" tai "Get Ready!"

  • "Start"-painike pelin aloittamiseksi.

Pohjasolmu käyttöliittymäelementeille on Control. Luodaksemme käyttöliittymämme käytämme kahdentyyppisiä Control solmuja: Label ja Button.

Luo seuraavat HUD solmun alle:

  • Label nimeltä ScoreLabel.

  • Label named Message.

  • Button nimeltä StartButton.

  • Timer nimeltä MessageTimer.

Click on the ScoreLabel and type a number into the Text field in the Inspector. The default font for Control nodes is small and doesn't scale well. There is a font file included in the game assets called "Xolonium-Regular.ttf". To use this font, do the following:

  1. Under Theme overrides > Fonts click on the empty box and select "New DynamicFont"

../../_images/custom_font1.png
  1. Click on the "DynamicFont" you added, and under Font > FontData, choose "Load" and select the "Xolonium-Regular.ttf" file.

../../_images/custom_font2.png

Set the "Size" property under Settings, 64 works well.

../../_images/custom_font3.png

Once you've done this on the ScoreLabel, you can click the down arrow next to the Font property and choose "Copy", then "Paste" it in the same place on the other two Control nodes.

Muista

Anchors and Margins: Control nodes have a position and size, but they also have anchors and margins. Anchors define the origin - the reference point for the edges of the node. Margins update automatically when you move or resize a control node. They represent the distance from the control node's edges to its anchor.

Arrange the nodes as shown below. Click the "Layout" button to set a Control node's layout:

../../_images/ui_anchor.png

Voit kiskoa solmuja asettaaksesi ne manuaalisesti, tai tarkempaa sijoittelua varten, voit käyttää seuraavia asetuksia:

ScoreLabel

  • Layout : "Top Wide"

  • Text : 0

  • Align : "Center"

Message

  • Layout : "HCenter Wide"

  • Text : Dodge the Creeps!

  • Align : "Center"

  • Autowrap : "On"

StartButton

  • Text : Start

  • Layout : "Center Bottom"

  • Margin :

    • Top: -200

    • Bottom: -100

On the MessageTimer, set the Wait Time to 2 and set the One Shot property to "On".

Lisää nyt tämä skripti HUD solmulle:

extends CanvasLayer

signal start_game

start_game signaali kertoo Main solmulle, että painiketta on painettu.

func show_message(text):
    $Message.text = text
    $Message.show()
    $MessageTimer.start()

This function is called when we want to display a message temporarily, such as "Get Ready".

func show_game_over():
    show_message("Game Over")
    # Wait until the MessageTimer has counted down.
    yield($MessageTimer, "timeout")

    $Message.text = "Dodge the\nCreeps!"
    $Message.show()
    # Make a one-shot timer and wait for it to finish.
    yield(get_tree().create_timer(1), "timeout")
    $StartButton.show()

Tätä funktiota kutsutaan kun pelaaja häviää. Se näyttää "Game Over" kahden sekunnin ajan, ja palaa sitten alkuruutuun ja näyttää "Start" painikkeen lyhyen tauon jälkeen.

Muista

Kun sinun täytyy pysäyttää koodi hetkeksi vaihtoehto ajastimelle on käyttää skenepuun create_timer() funktiota. Tämä voi olla todella hyödyllistä luomaan viiveitä kuten ylläolevassa koodissa, missä me haluamme odottaa hetken ennenkuin näytämme "Start" painikkeen.

func update_score(score):
    $ScoreLabel.text = str(score)

Tätä funktiota kutsutaan Main solmussa aina kun pistemäärä muuttuu.

Yhdistä MessageTimer solmun timeout() signaali ja StartButton solmun pressed() signaali, ja lisää seuraava koodi uusiin funktioihin:

func _on_StartButton_pressed():
    $StartButton.hide()
    emit_signal("start_game")

func _on_MessageTimer_timeout():
    $Message.hide()

HUD- ja Main-skenejen liittäminen

Now that we're done creating the HUD scene, go back to Main. Instance the HUD scene in Main like you did the Player scene. The scene tree should look like this, so make sure you didn't miss anything:

../../_images/completed_main_scene.png

Nyt meidän täytyy liittää HUD toiminnallisuus Main skriptiimme. Tämä edellyttää muutamia lisäyksiä Main skeneen:

In the Node tab, connect the HUD's start_game signal to the new_game() function of the Main node by typing "new_game" in the "Receiver Method" in the "Connect a Signal" window. Verify that the green connection icon now appears next to func new_game() in the script.

new_game() funktiossa, päivitä pistenäyttö ja näytä "Get Ready" viesti:

$HUD.update_score(score)
$HUD.show_message("Get Ready")

game_over() funktiossa meidän täytyy kutsua vastaavaa HUD funktiota:

$HUD.show_game_over()

Lopuksi, lisää tämä _on_ScoreTimer_timeout() funktioon pitääksesi näytön synkassa muuttuvan pistemäärän kanssa:

$HUD.update_score(score)

Nyt olet valmis pelaamaan! Napsauta "Pelaa" painiketta. Sinua pyydetään valitsemaan pääskene, joten valitse Main.tscn.

Vanhojen creepsien poisto

If you play until "Game Over" and then start a new game right away, the creeps from the previous game may still be on the screen. It would be better if they all disappeared at the start of a new game. We just need a way to tell all the mobs to remove themselves. We can do this with the "group" feature.

In the Mob scene, select the root node and click the "Node" tab next to the Inspector (the same place where you find the node's signals). Next to "Signals", click "Groups" and you can type a new group name and click "Add".

../../_images/group_tab.png

Now all mobs will be in the "mobs" group. We can then add the following line to the new_game() function in Main:

get_tree().call_group("mobs", "queue_free")

The call_group() function calls the named function on every node in a group - in this case we are telling every mob to delete itself.

The game's mostly done at this point. In the next and last part, we'll polish it a bit by adding a background, looping music, and some keyboard shortcuts.