官术网_书友最值得收藏!

Obstacles

Finally, the game can be made a bit more challenging by introducing an obstacle that the player must avoid. Touching the obstacle will end the game.

Create a new scene for the cactus and add the following nodes:

  • Area2D (named Cactus)
  • Sprite
  • CollisionShape2D

Drag the cactus texture from the FileSystem dock to the Texture property of the Sprite. Add a RectangleShape2D to the collision shape and size it so that it covers the image. Remember when you added if area.is_in_group("obstacles") to the player script? Add the Cactus body to the obstacles group using the Node tab (next to Inspector).

Now, add a Cactus instance to the Main scene and move it to a location in the upper half of the screen (away from where the player spawns). Play the game and see what happens when you run into the cactus.

You may have spotted a problem: coins can spawn behind the cactus, making them impossible to pick up. When the coin is placed, it needs to move if it detects that it's overlapping the obstacle. Connect the coin's area_entered() signal and add the following:

func _on_Coin_area_entered( area ):
if area.is_in_group("obstacles"):
position = Vector2(rand_range(0, screensize.x), rand_range(0, screensize.y))

If you've added the preceding Powerup object, you'll need to do the same for its area_entered signal.

主站蜘蛛池模板: 岳普湖县| 大渡口区| 巴彦淖尔市| 台中县| 乌兰察布市| 甘德县| 青州市| 四平市| 文水县| 综艺| 郓城县| 青冈县| 清水县| 温州市| 黎平县| 辽宁省| 新田县| 永宁县| 鄂尔多斯市| 阿坝县| 桓台县| 阿拉善盟| 仁化县| 深水埗区| 阿荣旗| 玉田县| 嘉善县| 义马市| 普安县| 乌鲁木齐市| 繁昌县| 蕉岭县| 宜都市| 平阴县| 阿鲁科尔沁旗| 六枝特区| 巍山| 兴文县| 揭东县| 上蔡县| 东海县|