You are on page 1of 14

//// World /////

extends Node2D
var lost = false
var speed
var board_offset = Vector2(20,70)
var board_size = Vector2(400,400)
var board_end
var velocity
#var space = 40
onready var body_segment = preload("res://snake_body.tscn")
var inst
#var seg = 1
var score = 0
var init_pos = Vector2(40,170)
var direction = "right"
var pos_reg = {}
var c = [0]
var food_pos = Vector2()
var child_count_init
var board_pos = {}
var head_board_pos = Vector2()
var in_dir = ["",""]
var mdiff = 2 #diferencia necesaria para cambiar de dirección
var dir_change = false
var s = [25,50,75,100,125,150,175,200,225,250,275,300]
var d = [62,32,22,17,14,12,11,10,9,8,7,7]
var sd_ind = 8
onready var P1 = $"Power-Ups/Progress textures/PU1"
onready var P2 = $"Power-Ups/Progress textures/PU2"
onready var P3 = $"Power-Ups/Progress textures/PU3"
onready var P4 = $"Power-Ups/Progress textures/PU4"
var P1_value = 100
var P2_value = 100
var P3_value = 100
var P4_value = 100
var time = 30

# Called when the node enters the scene tree for the first time.
func _ready():
P1.set_value(P1_value)
speed = s[sd_ind]
print(speed)
randomize()
child_count_init = get_child_count()
board_end = board_offset + board_size

for i in range(1,11):
for j in range(1,11):
board_pos[Vector2(j,i)] = Vector2(20+board_offset.x+40*(j-
1),20+board_offset.y+40*(i-1))
#print(board_pos[Vector2(j,i)])
head_board_pos = Vector2(1,3)
init_pos = board_pos[head_board_pos]

food_repos(1,false)
$head.connect("eated",self,"_on_eated")
$head.connect("hit",self,"_on_hit")
#$head.call_deferred()
$score_label.text = str(score)

for i in range(child_count_init):
c.insert(i,0)
#print("c.insert: ",i)
for i in range(1):
body_adder()

$head.position = init_pos
$food.position = food_pos

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
#time += delta
#print(time)
#P1.set_value(25)
power_up_change(delta)
if Input.is_action_just_pressed("ui_accept"):
lost = false
if not lost:
tail_move()
input_move()
head_move(delta)
segment_move(delta)
head_orientation()
#print(in_dir)
#if $head.position.x-get_child(child_count_init).position.x != 0:
#print($head.position.x-get_child(child_count_init).position.x <= 26 and
$head.position.x-get_child(child_count_init).position.x >=24," ",$head.position.x-
get_child(child_count_init).position.x)

func _on_eated():
var pack
pack = seg_pos_detect()
food_repos(pack,true) #Relocate food
$food.position = food_pos
body_adder() # Adding a new segment for the body
score += 1
#if score == 10:
# set_process(false)
$score_label.text = str(score)
#print(score)
func tail_move():
if dir_change:
match direction:
"right":
for i in range(1,11):
if(abs(board_pos[Vector2(i,head_board_pos.y)].x-
$head.position.x) <= mdiff and abs($head.position.x-
board_pos[Vector2(head_board_pos.x,head_board_pos.y)].x) >= 30):
$head.position =
board_pos[Vector2(i,head_board_pos.y)]
head_board_pos.x = i
if in_dir[1] == "":
direction = in_dir[0]
dir_change = false
else:
direction = in_dir[0]
in_dir[0] = in_dir[1]
in_dir[1] = ""

"left":
for i in range(1,11):
if(abs(board_pos[Vector2(i,head_board_pos.y)].x-
$head.position.x) <= mdiff and abs($head.position.x-
board_pos[Vector2(head_board_pos.x,head_board_pos.y)].x) >= 30):
$head.position =
board_pos[Vector2(i,head_board_pos.y)]
head_board_pos.x = i
if in_dir[1] == "":
direction = in_dir[0]
dir_change = false
else:
direction = in_dir[0]
in_dir[0] = in_dir[1]
in_dir[1] = ""
"down":
for i in range(1,11):
if(abs(board_pos[Vector2(head_board_pos.x,i)].y-
$head.position.y) <= mdiff and abs($head.position.y-
board_pos[Vector2(head_board_pos.x,head_board_pos.y)].y) >= 30):
$head.position =
board_pos[Vector2(head_board_pos.x,i)]
head_board_pos.y = i
if in_dir[1] == "":
direction = in_dir[0]
dir_change = false
else:
direction = in_dir[0]
in_dir[0] = in_dir[1]
in_dir[1] = ""
"up":
for i in range(1,11):
if(abs(board_pos[Vector2(head_board_pos.x,i)].y-
$head.position.y) <= mdiff and abs($head.position.y-
board_pos[Vector2(head_board_pos.x,head_board_pos.y)].y) >= 30):
$head.position =
board_pos[Vector2(head_board_pos.x,i)]
head_board_pos.y = i
if in_dir[1] == "":
direction = in_dir[0]
dir_change = false
else:
direction = in_dir[0]
in_dir[0] = in_dir[1]
in_dir[1] = ""

func input_move():
velocity = Vector2()
if Input.is_action_just_pressed("ui_right") or Input.is_action_just_pressed("ui_d"):
if dir_change:
if in_dir[0] != "right" and in_dir[0] != "left":
in_dir[1] = "right"
else:
if direction != "right" and direction != "left":
in_dir[0] = "right"
dir_change = true
if Input.is_action_just_pressed("ui_left") or Input.is_action_just_pressed("ui_a"):
if dir_change:
if in_dir[0] != "right" and in_dir[0] != "left":
in_dir[1] = "left"
else:
if direction != "right" and direction != "left":
in_dir[0] = "left"
dir_change = true
if Input.is_action_just_pressed("ui_down") or Input.is_action_just_pressed("ui_s"):
if dir_change:
if in_dir[0] != "down" and in_dir[0] != "up":
in_dir[1] = "down"
else:
if direction != "down" and direction != "up":
in_dir[0] = "down"
dir_change = true
if Input.is_action_just_pressed("ui_up") or Input.is_action_just_pressed("ui_w"):
if dir_change:
if in_dir[0] != "down" and in_dir[0] != "up":
in_dir[1] = "up"
else:
if direction != "down" and direction != "up":
in_dir[0] = "up"
dir_change = true

if direction == "right":
velocity.x += 1
if direction == "left":
velocity.x -= 1
if direction == "down":
velocity.y += 1
if direction == "up":
velocity.y -= 1

if velocity.length() > 0:
velocity = velocity.normalized() * speed

func body_adder():
inst = body_segment.instance()
add_child(inst)
#print(child_count_init)
c.insert(get_child_count()-child_count_init+3,0)
#print("c.insert: ",get_child_count()-child_count_init+3)

func head_move(_delta):
$head.position += velocity * _delta
var k = 1
$head.position.x = clamp($head.position.x, board_offset.x+20*k, board_end.x-20*k)
$head.position.y = clamp($head.position.y, board_offset.y+20*k, board_end.y-20*k)

func segment_move(_delta):
if get_child_count() > child_count_init:
for i in range(child_count_init,get_child_count()):
record(i-1)
get_child(i).position = pos_reg[Vector2(i-1,0)]

func shift(var ind,var seg):


for i in range(0,ind-1):
pos_reg[Vector2(seg,i)] = pos_reg[Vector2(seg,i+1)]

func record(var ind):


var lim = d[sd_ind]
if c[ind] < lim:
pos_reg[Vector2(ind,c[ind])] = get_child(ind).position
c[ind] += 1
if c[ind] == lim:
c[ind] = lim
shift(c[ind]-1,ind)
pos_reg[Vector2(ind,c[ind]-2)] = get_child(ind).position

func food_repos(pack,start):
var ind
if start:
var ran_index = int(rand_range(0,pack[2]-1))
print("ran ind: ",ran_index)
ind = pack[1][pack[0][ran_index]]
#ind = pack[1][pack[0][pack[2]]]
else:
ind = Vector2(int(rand_range(1,10)),int(rand_range(1,10)))
food_pos = board_pos[ind]
#print(food_pos)

func head_orientation(): #Ajusta la orientación de la cabeza


if direction == "right" :
$head.rotation_degrees = 90
elif direction == "left" :
$head.rotation_degrees = -90
elif direction == "up" :
$head.rotation_degrees = 0
elif direction == "down" :
$head.rotation_degrees = 180

func _on_hit():
if not Input.is_action_pressed("ui_accept"):
lost = true

func seg_pos_detect():
var mindex = []
var count = 0
var collind = []
var restind = []
#Convirtiendo indices de Vector2 a un indice de 1 a 100
for i in range(1,11):
for j in range(1,11):
mindex.insert(count,Vector2(j,i))
#print(mindex[count])
count += 1
#print("ind: ",Vector2(j,i)," count: ",count)
count = 0
#print("\n","Collind","\n")
#Buscando los indices de los cuadros ocupados por la serpiente
for i in range(child_count_init,get_child_count()):
for j in range(1,100):
if get_child(i).position.x > board_pos[mindex[j]].x-20 and
get_child(i).position.x < board_pos[mindex[j]].x+20 and get_child(i).position.y >
board_pos[mindex[j]].y-20 and get_child(i).position.y < board_pos[mindex[j]].y+20:
collind.insert(count,j)
#print(collind[count]," ",count)
#print(count,",",j)
count += 1
#print("\n")
var c_lim = count
count = 0
#Insertando los indices de los cuadros que no colisionan con la serpiente
var col_snake = false
for i in range(1,101):
for j in range(0,c_lim):
if i == collind[j]:
col_snake = true
if !col_snake:
restind.insert(count,i)
count += 1
col_snake = false

print(count)
#for i in range(count):
# pass#print(i," ",restind[i])
var pack = [restind,mindex,count]
return pack

func power_up_change(delta):
time -= delta
P1_value = (10*(time))/3
P1_value = clamp(P1_value,0,100)
P1.set_value(P1_value

///// head //////


extends Area2D
signal hit
signal eated

# Called when the node enters the scene tree for the first time.
func _ready():
pass # Replace with function body.

# Called every frame. 'delta' is the elapsed time since the previous frame.
#func _process(delta):
# pass

func _on_head_area_entered(area):
if area.get_name().substr(0,6) == "@body@" and area.get_name().substr(0,7) !=
"@body@2":# or area.get_name().substr(0,6) == "body":
call_deferred("emit_signal","hit")
elif area.get_name().substr(0,6) == "food":
#print(area.get_name().substr(0,6))
call_deferred("emit_signal","eated")
///////// 3/17/2020/////////////////////

extends Node2D

var board_offset = Vector2(20,70)


var board_size = Vector2(400,400)
var board_end = board_offset + board_size
var board_pos = {}

var lost = false


var speed
#var board_offset = Vector2(20,70)
#var board_size = Vector2(400,400)
#var board_end
var velocity = Vector2()
onready var body_segment = preload("res://snake_body.tscn")
var inst
var init_pos = Vector2(40,170)
var direction = "right"
var pos_reg = {}
var c = [0]
var child_count_init

var head_board_pos = Vector2(1,3)


var in_dir = ["",""]
var mdiff = 2 #diferencia necesaria para cambiar de dirección
var dir_change = false
var s = [25,50,75,100,125,150,175,200,225,250,275,300]
var d = [62,32,22,17,14,12,11,10,9,8,7,7]
var sd_ind = 8

# Called when the node enters the scene tree for the first time.
func _ready():
speed = s[sd_ind]
randomize()
child_count_init = get_child_count()
for i in range(1,11):
for j in range(1,11):
board_pos[Vector2(j,i)] = Vector2(20+board_offset.x+40*(j-
1),20+board_offset.y+40*(i-1))

init_pos = board_pos[head_board_pos]
for i in range(1):
body_adder()

$head.position = init_pos

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
#speed = s[sd_ind]
#print("Speed: ",speed,", Space: ",d[sd_ind],", Index: ",sd_ind)

if Input.is_action_just_pressed("ui_accept"):
lost = false
if not lost:
tail_move()
input_move()
head_move(delta)
segment_move(delta)
head_orientation()

func tail_move():
if dir_change:
match direction:
"right":
for i in range(1,11):
if(abs(board_pos[Vector2(i,head_board_pos.y)].x-
$head.position.x) <= mdiff and abs($head.position.x-
board_pos[Vector2(head_board_pos.x,head_board_pos.y)].x) >= 30):
$head.position =
board_pos[Vector2(i,head_board_pos.y)]
head_board_pos.x = i
if in_dir[1] == "":
direction = in_dir[0]
dir_change = false
else:
direction = in_dir[0]
in_dir[0] = in_dir[1]
in_dir[1] = ""

"left":
for i in range(1,11):
if(abs(board_pos[Vector2(i,head_board_pos.y)].x-
$head.position.x) <= mdiff and abs($head.position.x-
board_pos[Vector2(head_board_pos.x,head_board_pos.y)].x) >= 30):
$head.position =
board_pos[Vector2(i,head_board_pos.y)]
head_board_pos.x = i
if in_dir[1] == "":
direction = in_dir[0]
dir_change = false
else:
direction = in_dir[0]
in_dir[0] = in_dir[1]
in_dir[1] = ""
"down":
for i in range(1,11):
if(abs(board_pos[Vector2(head_board_pos.x,i)].y-
$head.position.y) <= mdiff and abs($head.position.y-
board_pos[Vector2(head_board_pos.x,head_board_pos.y)].y) >= 30):
$head.position =
board_pos[Vector2(head_board_pos.x,i)]
head_board_pos.y = i
if in_dir[1] == "":
direction = in_dir[0]
dir_change = false
else:
direction = in_dir[0]
in_dir[0] = in_dir[1]
in_dir[1] = ""
"up":
for i in range(1,11):
if(abs(board_pos[Vector2(head_board_pos.x,i)].y-
$head.position.y) <= mdiff and abs($head.position.y-
board_pos[Vector2(head_board_pos.x,head_board_pos.y)].y) >= 30):
$head.position =
board_pos[Vector2(head_board_pos.x,i)]
head_board_pos.y = i
if in_dir[1] == "":
direction = in_dir[0]
dir_change = false
else:
direction = in_dir[0]
in_dir[0] = in_dir[1]
in_dir[1] = ""

func input_move():
velocity = Vector2()
if Input.is_action_just_pressed("ui_right") or Input.is_action_just_pressed("ui_d"):
if dir_change:
if in_dir[0] != "right" and in_dir[0] != "left":
in_dir[1] = "right"
else:
if direction != "right" and direction != "left":
in_dir[0] = "right"
dir_change = true
if Input.is_action_just_pressed("ui_left") or Input.is_action_just_pressed("ui_a"):
if dir_change:
if in_dir[0] != "right" and in_dir[0] != "left":
in_dir[1] = "left"
else:
if direction != "right" and direction != "left":
in_dir[0] = "left"
dir_change = true
if Input.is_action_just_pressed("ui_down") or Input.is_action_just_pressed("ui_s"):
if dir_change:
if in_dir[0] != "down" and in_dir[0] != "up":
in_dir[1] = "down"
else:
if direction != "down" and direction != "up":
in_dir[0] = "down"
dir_change = true
if Input.is_action_just_pressed("ui_up") or Input.is_action_just_pressed("ui_w"):
if dir_change:
if in_dir[0] != "down" and in_dir[0] != "up":
in_dir[1] = "up"
else:
if direction != "down" and direction != "up":
in_dir[0] = "up"
dir_change = true

if direction == "right":
velocity.x += 1
if direction == "left":
velocity.x -= 1
if direction == "down":
velocity.y += 1
if direction == "up":
velocity.y -= 1

if velocity.length() > 0:
velocity = velocity.normalized() * speed
func body_adder(): #Agrega un segmento nuevo a la serpiente
inst = body_segment.instance()
add_child(inst)
c.insert(get_child_count()-2,0)

func head_move(_delta): #Mueve la cabeza a voluntad del jugador


$head.position += velocity * _delta #La posición del objeto $head siguiento la fórmula para
el movimiento rectilíneo uniforme x(t) = vi*t
var k = 1
$head.position.x = clamp($head.position.x, board_offset.x+20*k, board_end.x-20*k)
$head.position.y = clamp($head.position.y, board_offset.y+20*k, board_end.y-20*k)

func segment_move(_delta): #Mueve cada uno de los segmentos del cuerpo de la serpiente, cada
uno siguiendo al que está al frente
if get_child_count() > child_count_init: #Esta condición se asegura de mover solo los
segmentos y no la cabeza
for i in range(child_count_init,get_child_count()):
record(i-1)
get_child(i).position = pos_reg[Vector2(i-1,0)]

func shift(var ind,var seg):


for i in range(0,ind-1):
pos_reg[Vector2(seg,i)] = pos_reg[Vector2(seg,i+1)]

func record(var ind): #El segmento número 'ind' guarda los movimientos del anterior, con un
retraso 'lim'
var lim = d[sd_ind] #Cantidad de espacio en retraso
if c[ind] < lim: #
pos_reg[Vector2(ind,c[ind])] = get_child(ind).position
c[ind] += 1
if c[ind] == lim:
c[ind] = lim
shift(c[ind]-1,ind)
pos_reg[Vector2(ind,c[ind]-2)] = get_child(ind).position

func head_orientation(): #Ajusta la orientación de la cabeza


if direction == "right" :
$head.rotation_degrees = 90
elif direction == "left" :
$head.rotation_degrees = -90
elif direction == "up" :
$head.rotation_degrees = 0
elif direction == "down" :
$head.rotation_degrees = 180

You might also like