最終調整

We have now completed all the functionality for our game. Below are some remaining steps to add a bit more "juice" to improve the game experience.

Feel free to expand the gameplay with your own ideas.

背景

預設的灰色背景並不是很吸睛,所以來改個顏色。其中一種改背景的方式是新增一個 ColorRect 節點。將 ColorRect 新增為 Main 底下的第一個節點,這樣 ColorRect 才會被繪製在其他節點的下方。ColorRect 只有一個屬性: Color (色彩)。選擇一個你喜歡的顏色,並選擇「畫面配置」->「全矩形」,這樣就會覆蓋整個畫面。

如果你有適合的圖片的話,也可以用圖片作為背景。改用 TextureRect 節點即可。

音效

聲音與音樂可以是一種最有效能增加遊戲體驗吸引力的方法。遊戲素材資料夾中有兩個聲音檔案:背景音樂用的「House In A Forest Loop.ogg」與玩家 Game Over 時用的「gameover.wav」。

Main 中新增兩個 AudioStreamPlayer 節點作為子節點。將其中一個命名為 Music ,另一個則為 DeathSound 。在各個節點上點擊 Stream (串流)屬性,選擇「載入」,並選擇對應的音訊檔。

要播放音樂,在 new_game() 函式中新增 $Music.play() ,並在 game_over() 函式中新增 $Music.stop()

最後,在 game_over() 函式中新增 $DeathSound.play()

鍵盤快捷鍵

由於這個遊戲是用鍵盤控制來玩的,如果能用鍵盤上的按鍵也能開始新遊戲的話會很方便。我們可以使用 Button 節點的「Shortcut」(快捷鍵)屬性來做到。

In a previous lesson, we created four input actions to move the character. We will create a similar input action to map to the start button.

Select "Project" -> "Project Settings" and then click on the "Input Map" tab. In the same way you created the movement input actions, create a new input action called start_game and add a key mapping for the Enter key.

In the HUD scene, select the StartButton and find its Shortcut property in the Inspector. Select "New Shortcut" and click on the "Shortcut" item. A second Shortcut property will appear. Select "New InputEventAction" and click the new "InputEventAction". Finally, in the Action property, type the name start_game.

../../_images/start_button_shortcut.png

Now when the start button appears, you can either click it or press Enter to start the game.

And with that, you completed your first 2D game in Godot.

../../_images/dodge_preview.gif

You got to make a player-controlled character, enemies that spawn randomly around the game board, count the score, implement a game over and replay, user interface, sounds, and more. Congratulations!

There's still much to learn, but you can take a moment to appreciate what you achieved.

And when you're ready, you can move on to Your first 3D game to learn to create a complete 3D game from scratch, in Godot.