based snake gaming

This commit is contained in:
Tabby 2025-08-07 22:58:16 +10:00
parent 50e2dad3e7
commit 737128614a
5 changed files with 55 additions and 14 deletions

View file

@ -9,18 +9,21 @@ var want_direction : Vector2 = Vector2.RIGHT
var start_pos : Vector2
var target_pos : Vector2
var move_progress : float
var snake_speed : float = 1
# if snake is the head, it chooses where it goes
# if snake is not the head, it goes where the one infront went
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
target_pos = position
start_pos = position
get_new_target()
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
move_progress += delta
move_progress += delta * snake_speed
if Input.get_vector("left","right","up","down").length() > 0:
want_direction = Input.get_vector("left","right","up","down")
position = lerp(start_pos,target_pos,move_progress)
@ -30,7 +33,8 @@ func _process(delta: float) -> void:
func get_new_target():
start_pos=target_pos
if is_head:
direction = want_direction
if want_direction != direction*-1:
direction = want_direction
target_pos = start_pos + (direction * 320)
else:
target_pos = next_segment.position