Question: what does this part of the code mean? code below figure( 'KeyPressFcn' , @my_callback) function my_ callback(so,event) The whole code is here function game() close

what does this part of the code mean? code below

figure( 'KeyPressFcn' , @my_callback)

function my_ callback(so,event)

The whole code is here

function game()

close all

difficulty = input('difficulty from 1-10: ');

bounds=input('Bounds? 1 or 0: ');

edge= 30;

global d ex;

x =round(edge/2); %starting point

y =round(edge/2); %starting point

d =randi([1,4]);% generates random direction to start in for snake

a =randi([1 edge-1],1);%generates random x coordinate for food

b =randi([1 edge-1],1);%generates random y coordinate for food

snake(1,1:2)=[x y];%defines the snake for x and y coordinates

size_snake=1;

ate=1; %snake ate food

ex=0; % used to exit game

food=[a b];%defines food

draw(snake,food,size_snake,edge)

figure('KeyPressFcn',@my_callback);

function my_callback(so,event)%callback function for movement

switch event.Character

case 'q'

ex=1;

case 'w' % arrow direction

if (d~=2) %It couldn't turn back

d = 1; %up d=1

end

case 's'

if (d~=1)

d = 2; %down d=2

end

case 'd'

if (d~=4)

d = 3; %right d=3

end

case 'a'

if (d~=3)

d = 4; %left d=4

end

end

end

while (ex~=1)%runs the snake as long as q is not pressed

size_snake=size(snake);

size_snake=size_snake(1);

for l=size_snake+ate:-1:2

snake(l,:)=snake(l-1,:);

end

switch d

case 1

snake(1,2)=snake(1,2)+1;%add value of 1 to y position

case 2

snake(1,2)=snake(1,2)-1;%subtract value of 1 to y position

case 3

snake(1,1)=snake(1,1)+1;%add value of 1 to x position

case 4

snake(1,1)=snake(1,1)-1;%subtracts value of 1 to x position

end

draw(snake,food,size_snake,edge)%draws the snake

pause((105-difficulty*10)/(10*edge)) %diffculty makes game faster;

if snake(1,1)==food(1) && snake(1,2)==food(2)%if the snake and food are in the same position

ate=1;

food(1) = randi([1 edge-1]);

food(2) = randi([1 edge-1]);

else

ate=0;

end

if bounds==1

snake(1,:)

if snake(1,1)==0

msgbox('YOU LOST! HAHA')

ex=1;

elseif snake(1,2)==0

msgbox('YOU LOST! HAHA')

ex=1;

elseif snake(1,1)==edge

msgbox('YOU LOST! HAHA')

ex=1;

elseif snake(1,2)==edge

msgbox('YOU LOST! HAHA')

ex=1;

end

else

snake=snake-((snake>edge).*(edge+1));

snake=snake+((snake<0).*(edge+1));

end

if (sum(snake(:, 1) ==snake(1, 1)& snake(:, 2) == snake(1, 2) )>1) %if snake hits itself

msgbox('YOU LOST!')

break

end

end

close all

end

function draw(snake,food,size_snake,axis_limit)

for p = 1:size_snake

plot(snake(p,1),snake(p,2), 'wo')

hold on

end

plot(food(1,1),food(1,2), 'rs')

whitebg([0 0 0])%creates black background

axis([0, axis_limit, 0, axis_limit])

hold off

end

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!