simple_2d_game.rst 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. .. _doc_simple_2d_game:
  2. Simple 2D game
  3. ==============
  4. Pong
  5. ~~~~
  6. In this simple tutorial, a basic game of Pong will be created. There are
  7. plenty of more complex examples in the demos included with the engine,
  8. but this should get introduced to basic functionality for 2D Games.
  9. Assets
  10. ~~~~~~
  11. Some assets are included for this tutorial:
  12. :download:`pong_assets.zip </files/pong_assets.zip>`.
  13. Scene setup
  14. ~~~~~~~~~~~
  15. For the sake of the old times, the game will be in 640x400 pixels
  16. resolution. This can be configured in the Project Settings (see :ref:`doc_scenes_and_nodes-configuring_the_project`). The default background color should be set to black:
  17. .. image:: /img/clearcolor.png
  18. Create a :ref:`class_Node2D` node for the project root. Node2D is the base
  19. type for the 2D engine. After this, add some sprites :ref:`class_Sprite`
  20. node) and set each to the corresponding texture. The final scene layout
  21. should look similar to this (note: the ball is in the middle!):
  22. .. image:: /img/pong_layout.png
  23. The scene tree should, then, look similar to this:
  24. .. image:: /img/pong_nodes.png
  25. Save the scene as "pong.scn" and set it as the main scene in the project
  26. properties.
  27. Input actions setup
  28. ~~~~~~~~~~~~~~~~~~~
  29. There are so many input methods for video games... Keyboard, Joypad,
  30. Mouse, Touchscreen (Multitouch). Yet this is pong. The only input that
  31. matters is for the pads going up and down.
  32. Handling all possible input methods can be very frustrating and take a
  33. lot of code. The fact that most games allow controller customization
  34. makes this worse. For this, Godot created the "Input Actions". An action
  35. is defined, then input methods that trigger it are added.
  36. Open the project properties dialog again, but this time move to the
  37. "Input Map" tab.
  38. On it, add 4 actions:
  39. "left_move_up","left_move_down","right_move_up","right_move_down".
  40. Assign the keys that you desire. A/Z for left and Up/Down as keys
  41. should work in most cases.
  42. .. image:: /img/inputmap.png
  43. Script
  44. ~~~~~~
  45. Create a script for the root node of the scene and open it (as explained
  46. in :ref:`doc_scripting-adding_a_script`). The script will inherit Node2D:
  47. ::
  48. extends Node2D
  49. func _ready():
  50. pass
  51. In the constructor, two things will be done. The first is to enable
  52. processing, and the second to store some useful values. Such values are
  53. the dimensions of the screen and the pad:
  54. ::
  55. extends Node2D
  56. var screen_size
  57. var pad_size
  58. func _ready():
  59. screen_size = get_viewport_rect().size
  60. pad_size = get_node("left").get_texture().get_size()
  61. set_process(true)
  62. Then, some variables used for in-game will be added:
  63. ::
  64. #speed of the ball (in pixels/second0
  65. var ball_speed = 80
  66. #direction of the ball (normal vector)
  67. var direction = Vector2(-1,0)
  68. #constant for pad speed (also in pixels/second)
  69. const PAD_SPEED = 150
  70. Finally, the process function:
  71. ::
  72. func _process(delta):
  73. Get some useful values for computation. The first is the ball position
  74. (from the node), the second is the rectangles (Rect2) of the pads.
  75. Sprites by defaut center the textures, so a small adjustment of size/2
  76. must be added.
  77. ::
  78. var ball_pos = get_node("ball").get_pos()
  79. var left_rect = Rect2( get_node("left").get_pos() - pad_size/2, pad_size )
  80. var right_rect = Rect2( get_node("right").get_pos() - pad_size/2, pad_size )
  81. Since the ball pos was obtained, integrating it should be simple:
  82. ::
  83. ball_pos+=direction*ball_speed*delta
  84. Then, now that the ball has a new position, it should be tested against
  85. everything. First, the floor and the roof:
  86. ::
  87. if ( (ball_pos.y<0 and direction.y <0) or (ball_pos.y>screen_size.y and direction.y>0)):
  88. direction.y = -direction.y
  89. If one of the pads was touched, change direction and increase speed a
  90. little.
  91. ::
  92. if ( (left_rect.has_point(ball_pos) and direction.x < 0) or (right_rect.has_point(ball_pos) and direction.x > 0)):
  93. direction.x=-direction.x
  94. ball_speed*=1.1
  95. direction.y=randf()*2.0-1
  96. direction = direction.normalized()
  97. If the ball went out of the screen, it's game over. Game restarts:
  98. ::
  99. if (ball_pos.x<0 or ball_pos.x>screen_size.x):
  100. ball_pos=screen_size*0.5 #ball goes to screen center
  101. ball_speed=80
  102. direction=Vector2(-1,0)
  103. Once everything was done with the ball, the node is updated with the new
  104. position:
  105. ::
  106. get_node("ball").set_pos(ball_pos)
  107. Only updating the pads according to player input. the Input class is
  108. really useful here:
  109. ::
  110. #move left pad
  111. var left_pos = get_node("left").get_pos()
  112. if (left_pos.y > 0 and Input.is_action_pressed("left_move_up")):
  113. left_pos.y+=-PAD_SPEED*delta
  114. if (left_pos.y < screen_size.y and Input.is_action_pressed("left_move_down")):
  115. left_pos.y+=PAD_SPEED*delta
  116. get_node("left").set_pos(left_pos)
  117. #move right pad
  118. var right_pos = get_node("right").get_pos()
  119. if (right_pos.y > 0 and Input.is_action_pressed("right_move_up")):
  120. right_pos.y+=-PAD_SPEED*delta
  121. if (right_pos.y < screen_size.y and Input.is_action_pressed("right_move_down")):
  122. right_pos.y+=PAD_SPEED*delta
  123. get_node("right").set_pos(right_pos)
  124. And that's it! A simple Pong was written with a few lines of code.