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

BIN
assets/bg.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 374 B

34
assets/bg.png.import Normal file
View file

@ -0,0 +1,34 @@
[remap]
importer="texture"
type="CompressedTexture2D"
uid="uid://byibhn8fi6ve0"
path="res://.godot/imported/bg.png-23a59c2e9cba2223a50fa3fe41b70b25.ctex"
metadata={
"vram_texture": false
}
[deps]
source_file="res://assets/bg.png"
dest_files=["res://.godot/imported/bg.png-23a59c2e9cba2223a50fa3fe41b70b25.ctex"]
[params]
compress/mode=0
compress/high_quality=false
compress/lossy_quality=0.7
compress/hdr_compression=1
compress/normal_map=0
compress/channel_pack=0
mipmaps/generate=false
mipmaps/limit=-1
roughness/mode=0
roughness/src_normal=""
process/fix_alpha_border=true
process/premult_alpha=false
process/normal_map_invert_y=false
process/hdr_as_srgb=false
process/hdr_clamp_exposure=false
process/size_limit=0
detect_3d/compress_to=1

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

View file

@ -1,6 +1,7 @@
extends Node2D
@export var segements : Array[snake_segment]
var segements : Array[snake_segment]
@export var SNAKE_SPEED : float = 0.2
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
@ -11,6 +12,8 @@ func _ready() -> void:
segements[i].is_head = true
else:
segements[i].next_segment = segements[i-1]
segements[i].snake_speed = SNAKE_SPEED
segements[i].z_index = 100-i
# Called every frame. 'delta' is the elapsed time since the previous frame.

View file

@ -1,13 +1,20 @@
[gd_scene load_steps=4 format=3 uid="uid://daqqurdrnk61h"]
[gd_scene load_steps=5 format=3 uid="uid://daqqurdrnk61h"]
[ext_resource type="Script" uid="uid://bue4xxejdsmqh" path="res://snakeManager.gd" id="1_6uqi0"]
[ext_resource type="PackedScene" uid="uid://bwoeu7ask0fck" path="res://prefabs/snake_segment.tscn" id="1_8uh7m"]
[ext_resource type="Texture2D" uid="uid://byibhn8fi6ve0" path="res://assets/bg.png" id="1_ppyta"]
[ext_resource type="PackedScene" uid="uid://cusdydgg4b1bo" path="res://prefabs/braincell.tscn" id="2_6uqi0"]
[node name="Node2D" type="Node2D"]
[node name="Sprite2D" type="Sprite2D" parent="."]
position = Vector2(-160, -160)
scale = Vector2(320, 320)
texture = ExtResource("1_ppyta")
[node name="Snake" type="Node2D" parent="."]
script = ExtResource("1_6uqi0")
SNAKE_SPEED = 1.0
[node name="SnakeSegment" parent="Snake" instance=ExtResource("1_8uh7m")]
is_head = true
@ -15,19 +22,12 @@ is_head = true
[node name="Braincell" parent="Snake/SnakeSegment" instance=ExtResource("2_6uqi0")]
[node name="SnakeSegment2" parent="Snake" node_paths=PackedStringArray("next_segment") instance=ExtResource("1_8uh7m")]
position = Vector2(-320, 0)
next_segment = NodePath("../SnakeSegment")
[node name="SnakeSegment3" parent="Snake" node_paths=PackedStringArray("next_segment") instance=ExtResource("1_8uh7m")]
next_segment = NodePath("../SnakeSegment2")
[node name="SnakeSegment4" parent="Snake" node_paths=PackedStringArray("next_segment") instance=ExtResource("1_8uh7m")]
next_segment = NodePath("../SnakeSegment2")
[node name="SnakeSegment5" parent="Snake" node_paths=PackedStringArray("next_segment") instance=ExtResource("1_8uh7m")]
next_segment = NodePath("../SnakeSegment2")
[node name="SnakeSegment6" parent="Snake" node_paths=PackedStringArray("next_segment") instance=ExtResource("1_8uh7m")]
position = Vector2(-640, 0)
next_segment = NodePath("../SnakeSegment2")
[node name="Camera2D" type="Camera2D" parent="."]
zoom = Vector2(0.1, 0.1)
zoom = Vector2(0.05, 0.05)