[swift] Main

Viewer

  1. extends TileMap
  2.  
  3. var rng = RandomNumberGenerator.new()
  4. var tail = Array()
  5. var headGap = $"Snake Head".position
  6.  
  7. func onSnakeEat():
  8.         #Set x and y coords within map coords
  9.         var fruitX = 24 + rng.randi_range(0, 13) * 16
  10.         var fruitY = 24 + rng.randi_range(0, 13) * 16
  11.         var fruitVec = Vector2(fruitX, fruitY)
  12.         #Move fruit to coords
  13.         $Fruit.set_global_position(fruitVec)
  14.        
  15.         #Instance new tail
  16.         var tailScene = load("res://Snake Tail.tscn")
  17.         var tailClone = tailScene.instance()
  18.         add_child(tailClone)
  19.         #Add tail to array
  20.         tail.push_front(tailClone)
  21.        
  22.         #Move tail where head used to be
  23.         if tail.count > 0:
  24.                 print_debug("Tail added to array")
  25.                 #Move last tail to where head used to be
  26.                 tail.back().position = headGap
  27.                 #Add to front of list, remove from back
  28.                 tail.push_front(tail.back())
  29.                 tail.remove(tail.count - 1)
  30.  

Editor

You can edit this paste and save as new:


File Description
  • Main
  • Paste Code
  • 12 May-2021
  • 843 Bytes
You can Share it: