07.finishing-up.rst 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. .. _doc_your_first_2d_game_finishing_up:
  2. Finishing up
  3. ============
  4. We have now completed all the functionality for our game. Below are some
  5. remaining steps to add a bit more "juice" to improve the game experience.
  6. Feel free to expand the gameplay with your own ideas.
  7. Background
  8. ~~~~~~~~~~
  9. The default gray background is not very appealing, so let's change its color.
  10. One way to do this is to use a :ref:`ColorRect <class_ColorRect>` node. Make it
  11. the first node under ``Main`` so that it will be drawn behind the other nodes.
  12. ``ColorRect`` only has one property: ``Color``. Choose a color you like and
  13. 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.
  14. You could also add a background image, if you have one, by using a
  15. :ref:`TextureRect <class_TextureRect>` node instead.
  16. Sound effects
  17. ~~~~~~~~~~~~~
  18. Sound and music can be the single most effective way to add appeal to the game
  19. experience. In your game's **art** folder, you have two sound files: "House In a
  20. Forest Loop.ogg" for background music, and "gameover.wav" for when the player
  21. loses.
  22. Add two :ref:`AudioStreamPlayer <class_AudioStreamPlayer>` nodes as children of
  23. ``Main``. Name one of them ``Music`` and the other ``DeathSound``. On each one,
  24. click on the ``Stream`` property, select "Load", and choose the corresponding
  25. audio file.
  26. All audio is automatically imported with the ``Loop`` setting disabled.
  27. If you want the music to loop seamlessly, click on the Stream file arrow,
  28. select ``Make Unique``, then click on the Stream file and check the ``Loop`` box.
  29. .. image:: img/unique_resource_music.webp
  30. To play the music, add ``$Music.play()`` in the ``new_game()``
  31. function and ``$Music.stop()`` in the ``game_over()`` function.
  32. Finally, add ``$DeathSound.play()`` in the ``game_over()`` function.
  33. .. tabs::
  34. .. code-tab:: gdscript GDScript
  35. func game_over():
  36. ...
  37. $Music.stop()
  38. $DeathSound.play()
  39. func new_game():
  40. ...
  41. $Music.play()
  42. .. code-tab:: csharp
  43. public void GameOver()
  44. {
  45. ...
  46. GetNode<AudioStreamPlayer>("Music").Stop();
  47. GetNode<AudioStreamPlayer>("DeathSound").Play();
  48. }
  49. public void NewGame()
  50. {
  51. ...
  52. GetNode<AudioStreamPlayer>("Music").Play();
  53. }
  54. Keyboard shortcut
  55. ~~~~~~~~~~~~~~~~~
  56. Since the game is played with keyboard controls, it would be convenient if we
  57. could also start the game by pressing a key on the keyboard. We can do this with
  58. the "Shortcut" property of the ``Button`` node.
  59. In a previous lesson, we created four input actions to move the character. We
  60. will create a similar input action to map to the start button.
  61. Select "Project" -> "Project Settings" and then click on the "Input Map"
  62. tab. In the same way you created the movement input actions, create a new
  63. input action called ``start_game`` and add a key mapping for the :kbd:`Enter`
  64. key.
  65. .. image:: img/input-mapping-start_game.webp
  66. Now would be a good time to add controller support if you have one available.
  67. Attach or pair your controller and then under each input action that you wish
  68. to add controller support for, click on the "+" button and press the corresponding
  69. button, d-pad, or stick direction that you want to map to the respective input action.
  70. In the ``HUD`` scene, select the ``StartButton`` and find its **Shortcut**
  71. property in the Inspector. Create a new :ref:`Shortcut <class_Shortcut>` resource
  72. by clicking within the box, open the **Events** array and add a new array element
  73. to it by clicking on **Array[InputEvent] (size 0)**.
  74. .. image:: img/start_button_shortcut.webp
  75. Create a new :ref:`InputEventAction <class_InputEventAction>` and name it ``start_game``.
  76. .. image:: img/start_button_shortcut2.webp
  77. Now when the start button appears, you can either click it or press :kbd:`Enter`
  78. to start the game.
  79. And with that, you completed your first 2D game in Godot.
  80. .. image:: img/dodge_preview.gif
  81. You got to make a player-controlled character, enemies that spawn randomly
  82. around the game board, count the score, implement a game over and replay, user
  83. interface, sounds, and more. Congratulations!
  84. There's still much to learn, but you can take a moment to appreciate what you
  85. achieved.
  86. And when you're ready, you can move on to :ref:`doc_your_first_3d_game` to learn
  87. to create a complete 3D game from scratch, in Godot.
  88. Sharing the finished game with others
  89. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  90. If you want people to try out your game without having to install Godot, you'll
  91. need to export the project for each operating system you want the game to be
  92. playable on. See :ref:`doc_exporting_projects` for instructions.
  93. After exporting the project, compress the exported executable and PCK file (not
  94. the raw project files) to a ZIP file, then upload this ZIP file to a file
  95. sharing website.