Snake Game Failure for Newbie

Godot Version

v4.3.stable.steam [77dcf97d8]

Question

I’ve disconnected and reconnected the “on_body_entered” signal for both the food and body as many times as I can stand and still can’t seem to get a score to display… Let alone add new segments to the body.

On the plus side? The snake head does move. It eats apples and the apple reappears somewhere else… But I can’t figure out why score and body don’t update as they should…

LEVEL:

extends Node2D

var food_scene = preload(“res://scenes/food.tscn”)

func _ready():
randomize()
spawn_food()

func spawn_food():
var food_instance = food_scene.instantiate()
add_child(food_instance)
var screen_size = get_viewport_rect().size
food_instance.position = Vector2(randi_range(0, screen_size.x), randi_range(0, screen_size.y))

if food_instance.has_signal("food_eaten"):
	food_instance.connect("food_eaten", Callable(self, "_on_food_eaten"))

func _on_food_eaten():
spawn_food()

PLAYER:
extends CharacterBody2D

@export var speed = 200
var score = 0
var body = preload(“res://scenes/body.tscn”)
var direction = Vector2.ZERO
var body_segments =
var segment_positions =

func _ready():
for segment in body_segments:
segment.connect(“player_collided_with_body”, Callable(self, “reset_game”))
print(“Snek gaem start”)
direction = Vector2.RIGHT
segment_positions.append(position)

func _process(delta):
if direction != Vector2.ZERO:
velocity = direction * speed
move_and_slide()
update_body_positions()

#screen wrap
var screen_size = get_viewport().get_visible_rect().size
position.x = fmod(position.x + screen_size.x, screen_size.x)
position.y = fmod(position.y + screen_size.y, screen_size.y)

#movement functions
func change_direction(new_direction):
if new_direction != -direction:
direction = new_direction

func _input(event):
if event is InputEventKey and event.pressed:
if event.is_action_pressed(“ui_up”):
change_direction(Vector2.UP)
if event.is_action_pressed(“ui_down”):
change_direction(Vector2.DOWN)
if event.is_action_pressed(“ui_right”):
change_direction(Vector2.RIGHT)
if event.is_action_pressed(“ui_left”):
change_direction(Vector2.LEFT)

func _on_food_eaten(food):
score += 1
add_body_segment()
print("SCORE: " + str(score))

func add_body_segment():
var new_segment = body.instantiate()
add_child(new_segment)
body_segments.append(new_segment)
if body_segments.size() == 1:
new_segment.position = position
else:
new_segment.position = body_segments[body_segments.size() - 2].position
segment_positions.append(new_segment.position)

func update_body_positions():
segment_positions.insert(0, position)
if segment_positions.size() > body_segments.size() + 1:
segment_positions.pop_back()

for i in range (body_segments.size()):
	body_segments[i].position = segment_positions[i + 1]

func reset_game():
for segment in body_segments:
segment.queue_free()
body_segments.clear()
segment_positions.clear()
score = 0
print("Game reset! SCORE: " + str(score))

FOOD:
extends Area2D

signal food_eaten

func _ready():
connect(“body_entered”, Callable(self, “_on_body_entered”))

func _on_body_entered(body):
if body is CharacterBody2D:
emit_signal(“food_eaten”)
queue_free()

BODY:
extends Area2D

signal player_collided_with_body

func _ready():
connect(“body_entered”, Callable(self, “_on_body_entered”))

func _on_body_entered(body):
if body is CharacterBody2D:
emit_signal(“player_collided_with_body”)
print(“Body collision!”)

Body collision is printing (if I instance a body part into the game), and the apples do disappear and respawn so I don’t think it’s a signal issue. I just cant add the segments. They don’t appear in the remote tab or anything. It’s driving me nuts.

I appreciate any help you can offer :slight_smile:

This function which is responsible for printing score and adding body segment is never called.

Here in your level scene you can add after spawn_food() something like Player._on_food_eaten().

Yeah, looks like you’re not connecting the _food_eaten function in the player’s script to the food item’s signal.

I think the easiest would be to call it in the food’s body_entered function though:

func _on_body_entered(body):
    if body is CharacterBody2D:
        emit_signal(“food_eaten”)
        body._food_eaten()
        queue_free()

Or you just connect the signal… :slight_smile:

The body segment is spawning now but the apples are not… I think it’s time to start again from zero. Or just take a break for a day or two. My brain has been melted. Thanks for your help!