Up to date

This page is up to date for Godot 4.2. If you still find outdated information, please open an issue.

Завершальна обробка

На разі ми завершили весь функціонал нашої гри. Нижче наведено декілька кроків, щоб додати трохи більше «соку» для покращення ігрового досвіду.

Не соромтеся розширювати геймплей власними ідеями.

Тло

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.

Звукові ефекти

Sound and music can be the single most effective way to add appeal to the game experience. In your game's art folder, you have two sound files: "House In a Forest Loop.ogg" for background music, and "gameover.wav" for when the player loses.

Додайте два вузли 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.

Щоб відтворити музику, додайте $Music.play() у функцію new_game() та $Music.stop() у функцію game_over().

Нарешті, додайте $DeathSound.play() у функцію game_over().

func game_over():
    ...
    $Music.stop()
    $DeathSound.play()

func new_game():
    ...
    $Music.play()

Гарячі клавіші

Оскільки гра проводиться за допомогою клавіатурних елементів управління, було б зручно, якби ми також могли розпочати гру, натиснувши клавішу на клавіатурі. Один із способів зробити це - використання властивості "Shortcut" вузла Button.

На попередньому уроці ми створили чотири вхідні дії для переміщення персонажа. Ми створимо аналогічну дію введення для зіставлення з кнопкою пуску.

Виберіть "Проект" -> "Параметри проекту", а потім натисніть на вкладку "Карта введення". Так само, як ви створили дії введення руху, створіть нову дію вводу під назвою 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, щоб розпочати гру.

І на цьому ви завершили свою першу 2D-гру в Godot.

../../_images/dodge_preview.gif

Ви мали зробити персонажа, керованого гравцем, ворогів, які виникають випадковим чином навколо ігрової дошки, рахунок, реалізувати завершення гру і її перезапуск, інтерфейс користувача, звуки тощо. Вітаємо!

Ще багато чому можна навчитися, але зараз ви можете скористатися моментом, щоб оцінити те, чого досягли.

І коли ви будете готові, то зможете перейти до:ref:doc_your_first_3d_game, щоб навчитися створювати повну 3D гру з нуля, в Godot.