-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
snake // add score, add food spawning, add menu buttons, add game over
- Loading branch information
1 parent
6fd91ef
commit 879d9d1
Showing
8 changed files
with
175 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
[gd_scene load_steps=3 format=3 uid="uid://c2qpcf4un5wgv"] | ||
|
||
[ext_resource type="Script" path="res://scripts/game_control.gd" id="1_0xttm"] | ||
[ext_resource type="Theme" uid="uid://dlygxgt8hfakm" path="res://pixel_theme.tres" id="1_23hjq"] | ||
|
||
[node name="Control" type="Control"] | ||
layout_mode = 3 | ||
anchor_right = 0.057 | ||
anchor_bottom = 0.088 | ||
offset_top = 1.0 | ||
offset_right = 0.335999 | ||
offset_bottom = -0.0240059 | ||
grow_horizontal = 2 | ||
grow_vertical = 2 | ||
script = ExtResource("1_0xttm") | ||
|
||
[node name="MenuButton" type="Button" parent="."] | ||
layout_mode = 0 | ||
offset_left = 10.0 | ||
offset_top = 9.0 | ||
offset_right = 102.0 | ||
offset_bottom = 43.0 | ||
scale = Vector2(0.5, 0.5) | ||
focus_mode = 0 | ||
theme = ExtResource("1_23hjq") | ||
theme_override_font_sizes/font_size = 26 | ||
action_mode = 0 | ||
text = "Menu" | ||
|
||
[node name="RestartButton" type="Button" parent="."] | ||
layout_mode = 0 | ||
offset_left = 10.0 | ||
offset_top = 29.0 | ||
offset_right = 102.0 | ||
offset_bottom = 63.0 | ||
scale = Vector2(0.5, 0.5) | ||
focus_mode = 0 | ||
theme = ExtResource("1_23hjq") | ||
theme_override_font_sizes/font_size = 26 | ||
action_mode = 0 | ||
text = "Restart" | ||
|
||
[connection signal="pressed" from="MenuButton" to="." method="_on_menu_button_pressed"] | ||
[connection signal="pressed" from="RestartButton" to="." method="_on_restart_button_pressed"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
extends Control | ||
|
||
|
||
func _on_menu_button_pressed(): | ||
get_tree().change_scene_to_file("scenes/game_selection.tscn") | ||
|
||
|
||
func _on_restart_button_pressed(): | ||
get_tree().reload_current_scene() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
extends Node | ||
|
||
@export var snake_food_scene: PackedScene | ||
|
||
var current_score = 0 | ||
var score_increment = 100 | ||
var width = 64 | ||
var height = 32 | ||
var cell_size = 4 | ||
|
||
func to_world(x, y) -> Vector2: | ||
return Vector2(x * cell_size - width/2.0 * cell_size, y * cell_size - height/2.0 * cell_size) | ||
|
||
func _ready(): | ||
$GameOverLabel.visible = false | ||
$Bounds.size = Vector2(width * cell_size, height * cell_size) | ||
$Bounds.position = to_world(0, 0) | ||
|
||
|
||
func _on_snake_head_self_collided(): | ||
$GameOverLabel.visible = true | ||
$SnakeHead.set_process(false) | ||
|
||
|
||
func _on_snake_head_food_collected(): | ||
current_score += score_increment | ||
$ScoreLabelValue.text = str(current_score) | ||
|
||
var food = snake_food_scene.instantiate() | ||
food.position = to_world(randi_range(1, width - 2), randi_range(1, height - 2)) | ||
call_deferred("add_child", food) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters