-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnake_update.m
More file actions
45 lines (36 loc) · 1.33 KB
/
Copy pathsnake_update.m
File metadata and controls
45 lines (36 loc) · 1.33 KB
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
function [new_snake,x1,y1] = snake_update(snake,d,x,y)
%snake_update updates position of snake
%depending on the direction, changes elements of the snake array so they
%move to the spot in front of them
if d == 28 %if the snake is moving left
x = x-1; %updates head x coordinate
for n = size(snake,1):-1:2 %makes each element in the snake array follow the one before it
snake(n,2) = snake(n-1,2);
snake(n,1) = snake(n-1,1);
end
end
if d == 29 %if the snake is moving right
x = x+1; %updates head x coordinate
for n = size(snake,1):-1:2 %makes each element in the snake array follow the one before it
snake(n,2) = snake(n-1,2);
snake(n,1) = snake(n-1,1);
end
end
if d == 30; %if the snake is moving up
y = y-1; %updates head y coordinate
for n = size(snake,1):-1:2 %makes each element in the snake array follow the one before it
snake(n,2) = snake(n-1,2);
snake(n,1) = snake(n-1,1);
end
end
if d == 31; %if the snake is moving down
y = y+1; %updates head y coordinate
for n = size(snake,1):-1:2 %makes each element in the snake array follow the one before it
snake(n,2) = snake(n-1,2);
snake(n,1) = snake(n-1,1);
end
end
new_snake = snake;
x1 = x;
y1 = y;
end