File tree Expand file tree Collapse file tree 4 files changed +36
-8
lines changed
addons/panku_console/modules/snake Expand file tree Collapse file tree 4 files changed +36
-8
lines changed Original file line number Diff line number Diff line change @@ -3,3 +3,9 @@ var _module:PankuModule
33const _HELP_execute = "Play Snake Game"
44func 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
Original file line number Diff line number Diff line change 11class_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+
38func 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+ )
Original file line number Diff line number Diff line change 1+ signal game_over
2+
13const MAP_SIZE = 24
24
35const 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-
1612var snake_dict :Dictionary
1713var snake_arr :Array
1814var 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
Original file line number Diff line number Diff line change 11extends Control
22
33const 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 )
55const COLOR_APPLE = Color8 (255 , 112 , 133 )
66const COLOR_SNAKE = preload ("./snake_gradient.tres" )
77
You can’t perform that action at this time.
0 commit comments