Attention: Here be dragons

This is the latest (unstable) version of this documentation, which may document features not available in or compatible with released stable versions of Godot.

完成了

现在,我们已经完成了游戏的所有功能。以下是一些剩余的步骤,为游戏加点“料”,改善游戏体验。

随意用你自己的想法扩展游戏玩法。

背景

The default gray background is not very appealing, so let's change its color. One way to do this is to use a ColorRect node. Make it the first node under Main so that it will be drawn behind the other nodes. ColorRect only has one property: Color. Choose a color you like and select "Layout" -> "Anchors Preset" -> "Full Rect" either in the toolbar at the top of the viewport or in the inspector so that it covers the screen.

如果您有背景图片, 您也可以通过使用 TextureRect 节点来添加背景图片.

音效

声音和音乐可能是增强游戏沉浸的最有效方法。在游戏资产包中,你有两个声音文件:“House in a Forest Loop.ogg”用于背景音乐,而“gameover.wav”用于当玩家失败时。

添加两个 AudioStreamPlayer 节点作为 Main 的子节点。将其中一个命名为 Music,将另一个命名为 DeathSound。 在每个节点选项上,点击 Stream 属性,选择 加载,然后选择相应的音频文件。

All audio is automatically imported with the Loop setting disabled. If you want the music to loop seamlessly, click on the Stream file arrow, select Make Unique, then click on the Stream file and check the Loop box.

要播放音乐, 在 new_game() 函数中添加 $Music.play(), 在 game_over() 函数中添加 $Music.stop() .

最后, 在 game_over() 函数中添加 $DeathSound.play() .

键盘快捷键

当游戏使用键盘控制,可以方便地按键盘上的键来启动游戏。一种方法是使用 Button 节点的 “Shortcut”(快捷键)属性。

在上一课中,我们创建了四个输入动作来移动角色。我们将创建一个类似的输入动作来映射到开始按钮。

选择“项目 -> 项目设置”,然后单击“输入映射”选项卡。与创建移动输入动作的方式相同,创建一个名为 start_game 的新输入操作,并为 Enter 添加按键映射。

Now would be a good time to add controller support if you have one available. Attach or pair your controller and then under each input action that you wish to add controller support for, click on the "+" button and press the corresponding button, d-pad, or stick direction that you want to map to the respective input action.

In the HUD scene, select the StartButton and find its Shortcut property in the Inspector. Create a new Shortcut resource by clicking within the box, open the Events array and add a new array element to it by clicking on Array[InputEvent] (size 0).

../../_images/start_button_shortcut.webp

Create a new InputEventAction and name it start_game.

../../_images/start_button_shortcut2.webp

这样,开始按钮出现时,您就可以点击它或按 Enter 来启动游戏。

就这样,你在 Godot 中完成了你的第一个 2D 游戏。

../../_images/dodge_preview.gif

你已经能够制作由玩家控制的角色、在游戏区域内随机产生的敌人、计算分数、实现游戏结束和重玩、用户界面、声音,以及更多内容。祝贺!

还有很多东西需要学习,但你可以花点时间来欣赏你所取得的成就。

当你准备好了,你可以继续学习 您的第一个 3D 游戏,学习在 Godot 中从头开始创建一个完整的 3D 游戏。