-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainGame.gd
153 lines (120 loc) · 4.74 KB
/
MainGame.gd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
extends Node2D
const SNAKE = 0;
const APPLE = 1;
var apple_pos;
var snake_body=[Vector2(5,10),Vector2(4,10),Vector2(3,10)]
var snake_direction = Vector2(1,0)
var add_apple = false
func _ready():
apple_pos = place_apple();
draw_apple()
draw_snake()
func place_apple():
randomize()
var x = randi() % 20
var y = randi() % 20
return Vector2(x,y)
func draw_apple():
$SnakeApple.set_cell(apple_pos.x,apple_pos.y,APPLE)
func draw_snake():
# for block in snake_body:
# $SnakeApple.set_cell(block.x,block.y,SNAKE,false,false,false,Vector2(8,0))
for block_index in snake_body.size():
var block = snake_body[block_index]
if block_index == 0:
var head_direction = relation2(snake_body[0],snake_body[1])
if head_direction == 'right':
$SnakeApple.set_cell(block.x,block.y,SNAKE,true,false,false,Vector2(2,0))
if head_direction == 'left':
$SnakeApple.set_cell(block.x,block.y,SNAKE,false,false,false,Vector2(2,0))
if head_direction == 'up':
$SnakeApple.set_cell(block.x,block.y,SNAKE,false,false,false,Vector2(3,0))
if head_direction == 'down':
$SnakeApple.set_cell(block.x,block.y,SNAKE,false,true,false,Vector2(3,0))
elif block_index == snake_body.size()-1:
var tail_direction = relation2(snake_body[-1],snake_body[-2])
if tail_direction == 'right':
$SnakeApple.set_cell(block.x,block.y,SNAKE,true,false,false,Vector2(1,0))
if tail_direction == 'left':
$SnakeApple.set_cell(block.x,block.y,SNAKE,false,false,false,Vector2(1,0))
if tail_direction == 'up':
$SnakeApple.set_cell(block.x,block.y,SNAKE,false,false,false,Vector2(1,1))
if tail_direction == 'down':
$SnakeApple.set_cell(block.x,block.y,SNAKE,false,true,false,Vector2(1,1))
else:
var previous_block = snake_body[block_index + 1] - block
var next_block = snake_body[block_index - 1] - block
if previous_block.x == next_block.x:
$SnakeApple.set_cell(block.x,block.y,SNAKE,false,false,false,Vector2(4,1))
elif previous_block.y == next_block.y:
$SnakeApple.set_cell(block.x,block.y,SNAKE,false,false,false,Vector2(4,0))
else:
if previous_block.x == -1 and next_block.y == -1 or previous_block.y == -1 and next_block.x == -1:
$SnakeApple.set_cell(block.x,block.y,SNAKE,true,true,false,Vector2(5,0))
if previous_block.x == -1 and next_block.y == 1 or previous_block.y == 1 and next_block.x == -1:
$SnakeApple.set_cell(block.x,block.y,SNAKE,true,false,false,Vector2(5,0))
if previous_block.x == 1 and next_block.y == -1 or previous_block.y == -1 and next_block.x == 1:
$SnakeApple.set_cell(block.x,block.y,SNAKE,false,true,false,Vector2(5,0))
if previous_block.x == 1 and next_block.y == 1 or previous_block.y == 1 and next_block.x == 1:
$SnakeApple.set_cell(block.x,block.y,SNAKE,false,false,false,Vector2(5,0))
func relation2(first_block:Vector2,second_block:Vector2):
var block_relation = second_block - first_block
if block_relation == Vector2(-1,0): return 'left'
if block_relation == Vector2(1,0): return 'right'
if block_relation == Vector2(0,1): return 'down'
if block_relation == Vector2(0,-1): return 'up'
func move_snake():
if add_apple:
delete_tiles(SNAKE)
var body_copy = snake_body.slice(0, snake_body.size() - 1)
var new_head = body_copy[0] + snake_direction
body_copy.insert(0,new_head)
snake_body = body_copy
add_apple = false
else:
delete_tiles(SNAKE)
var body_copy = snake_body.slice(0, snake_body.size() - 2)
var new_head = body_copy[0] + snake_direction
body_copy.insert(0,new_head)
snake_body = body_copy
func delete_tiles(id:int):
var cells = $SnakeApple.get_used_cells_by_id(id)
for cell in cells:
$SnakeApple.set_cell(cell.x,cell.y,-1)
func _input(event):
if Input.is_action_just_pressed("ui_up"):
if not snake_direction == Vector2(0,1):
snake_direction = Vector2(0,-1)
if Input.is_action_just_pressed("ui_right"):
if not snake_direction == Vector2(-1,0):
snake_direction = Vector2(1,0)
if Input.is_action_just_pressed("ui_left"):
if not snake_direction == Vector2(1,0):
snake_direction = Vector2(-1,0)
if Input.is_action_just_pressed("ui_down"):
if not snake_direction == Vector2(0,-1):
snake_direction = Vector2(0,1)
func check_game_over():
var head = snake_body[0]
#snake leaves the screen
if head.x > 20 or head.x<0 or head.y > 20 or head.y <0:
reset()
#snake bites its own tail
for block in snake_body.slice(1,snake_body.size() - 1):
if block == head:
reset()
func reset():
snake_body = [Vector2(5,10),Vector2(4,10),Vector2(3,10)]
snake_direction = Vector2(1,0)
func check_apple_eaten():
if apple_pos == snake_body[0]:
apple_pos = place_apple()
add_apple = true
func _on_SnakeTick_timeout():
move_snake()
draw_apple()
draw_snake()
check_apple_eaten()
check_game_over()
func _process(delta):
check_game_over()