Skip to content

Commit d08b9d6

Browse files
committed
feat: add snake game leader board
1 parent dc5d180 commit d08b9d6

File tree

4 files changed

+36
-8
lines changed

4 files changed

+36
-8
lines changed

addons/panku_console/modules/snake/env.gd

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,9 @@ var _module:PankuModule
33
const _HELP_execute = "Play Snake Game"
44
func play() -> void:
55
_module.add_snake_window()
6+
7+
func leader_board() -> String:
8+
var content = ""
9+
content += "== Learder Board ==\n"
10+
content += str(_module.leader_board_arr)
11+
return content

addons/panku_console/modules/snake/module.gd

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
class_name PankuModuleSnakeGame extends PankuModule
22

3+
var leader_board_arr = []
4+
5+
func init_module():
6+
leader_board_arr = load_module_data("leader_board", [])
7+
38
func add_snake_window():
49
var snake_ui := preload("res://addons/panku_console/modules/snake/snake.tscn").instantiate()
510
var window:PankuLynxWindow = core.windows_manager.create_window(snake_ui)
@@ -10,3 +15,23 @@ func add_snake_window():
1015
window.size = Vector2(
1116
snake_ui.snake_game.MAP_SIZE * snake_ui.CELL_SIZE, 0)
1217
window.size.y = window.size.x + 24
18+
19+
core.get_tree().root.get_viewport().gui_release_focus()
20+
21+
snake_ui.snake_game.game_over.connect(
22+
func():
23+
var record = {
24+
"timestamp": Time.get_datetime_string_from_system(),
25+
"score": snake_ui.snake_game.get_snake_length()
26+
}
27+
leader_board_arr.append(record)
28+
leader_board_arr.sort_custom(
29+
func(a, b):
30+
return a['score'] > b['score']
31+
)
32+
33+
if leader_board_arr.size() > 10:
34+
leader_board_arr.resize(10)
35+
36+
save_module_data("leader_board", leader_board_arr)
37+
)

addons/panku_console/modules/snake/snake.gd

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
signal game_over
2+
13
const MAP_SIZE = 24
24

35
const DIR_REV = {
@@ -7,12 +9,6 @@ const DIR_REV = {
79
Vector2.DOWN: Vector2.UP,
810
}
911

10-
enum MapMatrixCell {
11-
EMPTY,
12-
APPLE,
13-
SNAKE,
14-
}
15-
1612
var snake_dict:Dictionary
1713
var snake_arr:Array
1814
var apple:Vector2
@@ -60,9 +56,10 @@ func tick(input_dir:Vector2):
6056
# check if collide with self
6157
if snake_dict.has(new_head):
6258
# game over, restart
59+
game_over.emit()
6360
init()
6461
return
65-
62+
6663
# add new head
6764
snake_arr.push_front(new_head)
6865
snake_dict[new_head] = 0

addons/panku_console/modules/snake/snake_ui.gd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
extends Control
22

33
const CELL_SIZE = 12
4-
const COLOR_EMPTY = Color(0.0, 0.0, 0.0, 0.5)
4+
const COLOR_EMPTY = Color(0.0, 0.0, 0.0, 0.0)
55
const COLOR_APPLE = Color8(255, 112, 133)
66
const COLOR_SNAKE = preload("./snake_gradient.tres")
77

0 commit comments

Comments
 (0)