07.finishing-up.rst 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. ``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. To play the music, add ``$Music.play()`` in the ``new_game()``
  30. function and ``$Music.stop()`` in the ``game_over()`` function.
  31. Finally, add ``$DeathSound.play()`` in the ``game_over()`` function.
  32. .. tabs::
  33. .. code-tab:: gdscript GDScript
  34. func game_over():
  35. ...
  36. $Music.stop()
  37. $DeathSound.play()
  38. func new_game():
  39. ...
  40. $Music.play()
  41. .. code-tab:: csharp
  42. public void GameOver()
  43. {
  44. ...
  45. GetNode<AudioStreamPlayer>("Music").Stop();
  46. GetNode<AudioStreamPlayer>("DeathSound").Play();
  47. }
  48. public void NewGame()
  49. {
  50. ...
  51. GetNode<AudioStreamPlayer>("Music").Play();
  52. }
  53. Keyboard shortcut
  54. ~~~~~~~~~~~~~~~~~
  55. Since the game is played with keyboard controls, it would be convenient if we
  56. could also start the game by pressing a key on the keyboard. We can do this with
  57. the "Shortcut" property of the ``Button`` node.
  58. In a previous lesson, we created four input actions to move the character. We
  59. will create a similar input action to map to the start button.
  60. Select "Project" -> "Project Settings" and then click on the "Input Map"
  61. tab. In the same way you created the movement input actions, create a new
  62. input action called ``start_game`` and add a key mapping for the :kbd:`Enter`
  63. key.
  64. Now would be a good time to add controller support if you have one available.
  65. Attach or pair your controller and then under each input action that you wish
  66. to add controller support for, click on the "+" button and press the corresponding
  67. button, d-pad, or stick direction that you want to map to the respective input action.
  68. In the ``HUD`` scene, select the ``StartButton`` and find its **Shortcut**
  69. property in the Inspector. Create a new :ref:`Shortcut <class_Shortcut>` resource
  70. by clicking within the box, open the **Events** array and add a new array element
  71. to it by clicking on **Array[InputEvent] (size 0)**.
  72. .. image:: img/start_button_shortcut.webp
  73. Create a new :ref:`InputEventAction <class_InputEventAction>` and name it ``start_game``.
  74. .. image:: img/start_button_shortcut2.webp
  75. Now when the start button appears, you can either click it or press :kbd:`Enter`
  76. to start the game.
  77. And with that, you completed your first 2D game in Godot.
  78. .. image:: img/dodge_preview.gif
  79. You got to make a player-controlled character, enemies that spawn randomly
  80. around the game board, count the score, implement a game over and replay, user
  81. interface, sounds, and more. Congratulations!
  82. There's still much to learn, but you can take a moment to appreciate what you
  83. achieved.
  84. And when you're ready, you can move on to :ref:`doc_your_first_3d_game` to learn
  85. to create a complete 3D game from scratch, in Godot.