Error using pause Interrupt while evaluating figure KeyPressFcn ?
3 views (last 30 days)
Show older comments
I have downloaded and modified a snake game script from here. both in the original version and in my version you can only press direction keys exactly 30 times before the game runs into a bug and freezes or ends.
in my version, it seems for every key press there is an error after I push ctrl+c to terminate the program.
the error is: "Operation terminated by user during snark/keyPress (line 47)
Error using pause Interrupt while evaluating figure KeyPressFcn."
so what is wrong?
this is the part of the code that I think is relevant to the problem (pause in line 47 is the one below):
h_fig = figure;
set(h_fig,'menubar','none');
set(h_fig,'CurrentObject',imagesc(grid));
set(h_fig,'KeyPressFcn',@keyPress);
%Called for any key press.
function keyPress (~,evt)
youGottaMove(evt.Key);
% execute command only once
% then go straight
evt.Key = 'q';
while(~gameover)
pause(0.4);
youGottaMove(evt.Key);
end
end
the game shows a red box with an arrow. the user must either press right or left arrow keys to move the box (depending on the direction of the arrow right means move towards the right of the arrow and left to the left of the arrow)
this is the whole code :
%snark game;
function snark ()
%Size of the 'playing field'.
max_x = 4;
max_y = 4;
grid = zeros(max_x,max_y);
%Starting position.
x = 1;
y = 1;
grid(x,y) = 1;
length = 1; %length of snake
positionX = zeros(length); %array holds all the coordinates
positionY = zeros(length); % of the snake's body elements
positionX(1) = x;
positionY(1) = y;
%Position of the food.
x_f = x;
y_f = y;
getNewFood()
justGotFoodFlag = 0;
gameover = 0;
h_fig = figure;
set(h_fig,'menubar','none');
% ADJUST THE SIZE AND POSITION HERE IF DESIRED.
%set(h_fig,'position',[724 46 200 200]);
set(h_fig,'CurrentObject',imagesc(grid));
set(h_fig,'KeyPressFcn',@keyPress);
%Called for any key press.
function keyPress (~,evt)
youGottaMove(evt.Key);
% execute command only once
% then go straight
evt.Key = 'q';
while(~gameover)
pause(0.4);
youGottaMove(evt.Key);
end
end
%Called after a keypress or after timedelay
function youGottaMove(mov)
makeMovement(mov);
if ~gameover
%checkBody();
if ~gameover
justGotFoodFlag = 0;
grid(positionX(1),positionY(1)) = 0;
if (length~=1)
for i = 1:length-1
positionX(i) = positionX(i+1);
positionY(i) = positionY(i+1);
end
end
positionX(length) = x;
positionY(length) = y;
checkPosEqFood()
grid(x,y) = 1;
set(h_fig,'CurrentObject',imagesc(grid));
% update point
xlabel(['you have earned : ' num2str(points) ' points !' ]);
% draw snakes head
hold on ;
% set xl and yl to the right lower corner
xl = x+unit/2;
yl = y+unit/2;
% draw the arrow indocatin the orientation of head
switch(orient)
case 0
plot( [yl-1 , yl],[xl-1 , xl-unit/2] ,'g');
plot( [yl-1 , yl] , [xl , xl-unit/2] ,'g');
case 1
plot( [yl-1 , yl-unit/2], [xl-1 , xl] ,'g');
plot( [yl , yl-unit/2 ], [xl-1 , xl] ,'g');
case 2
plot ( [yl , yl-1], [xl-1 ,xl-unit/2 ] , 'g');
plot( [yl , yl-1], [xl , xl-unit/2] , 'g');
case 3
plot( [yl-1,yl-unit/2], [xl,xl-1] , 'g');
plot( [yl,yl-unit/2], [xl,xl-1] , 'g');
end
hold off;
end
end
end
% orientation of the head
orient = 0;
% unit of the square width
unit = 1;
%Moves the position of the snake in direction of 'mov' (key)
function makeMovement(mov)
switch(mov)
case 'q' % go straight
switch(orient)
case 0
y = y+1;
case 1
x = x+1;
case 2
y = y-1;
case 3
x = x-1;
end
case 'rightarrow'
switch(orient)
case 0
orient = 1;
x = x+1;
case 1
orient = 2;
y = y-1;
case 2
orient = 3;
x = x-1;
case 3
orient = 0;
y = y+1;
end
case 'leftarrow'
switch(orient)
case 0
orient = 3;
x =x-1;
case 1
orient = 0;
y = y+1;
case 2
orient = 1;
x = x+1;
case 3
orient = 2;
y = y-1;
end
end
% check for max and min
if (y > max_y)
y = 1;
end
if (y < 1)
y = max_y;
end
if (x > max_x)
x = 1;
end
if (x < 1)
x = max_x;
end
end
% points got (foods eaten)
points = 0;
%Check if you've reached the food
function checkPosEqFood()
if (x==x_f)&&(y==y_f)
%length = length +1 ;
% add point
points = points + 1;
% update point
xlabel(['you have earned : ' num2str(points) ' points !' ]);
positionX(length) = x;
positionY(length) = y;
getNewFood();
justGotFoodFlag = 1;
end
end
%Create new food element
function getNewFood()
flag = 1;
while (flag)
x_f = randi(max_x);
y_f = randi(max_y);
flag = 0;
for i = 1:length
if (x_f==positionX(i))&&(y_f==positionY(i))
flag=1;
end
end
end
grid(x_f,y_f) = 0.5;
end
end
0 Comments
Answers (1)
Jan
on 30 Jun 2017
Edited: Jan
on 30 Jun 2017
Nothing is wrong.
Operation terminated by user during snark/keyPress
is the expected message for a ctrl+c. It is likely that the keypress is caught in pause(0.4) and exaggerated that this is an "Error" message: it is wanted by the user obviously.
Please post the error message you get without pressing Ctrl-C. What does this mean in detail: "the game runs into a bug and freezes or ends"? A bug, and freeze or ends? It is unlikely that Matlab has 3 actions at the same time.
[EDITED]
The program design is flawed: Each keypress starts a new instance of the loop over the youGottaMove function. If you stop the game by closing the window, you get as many error messages about the "Invalid or deleted object" as you had key presses during the game. This is a bad idea.
Perhaps this causes the troubles on your computer: The RAM might be exhausted due to the recursive code. But this is a guess only, because I cannot reproduce this.
A better idea is using a timer for the loop and letting the keypress function set the UserData of the timer only, which contains the key code.
2 Comments
Jan
on 30 Jun 2017
Edited: Jan
on 30 Jun 2017
@Panonian: If I copy&paste the code to my computer, I can infact run it to debug it. And you can use the debugger also.
When I try to run the code in Matlab R2016b, I get this error at the first keypress already:
You cannot set the read-only property 'Key' of KeyData.
Error in snark/keyPress (line 44)
evt.Key = 'q';
Error while evaluating Figure KeyPressFcn
When I modify the code:
function keyPress (~,evt)
key = evt.Key;
youGottaMove(key);
key = 'q';
while(~gameover)
pause(0.4);
youGottaMove(key);
end
end
I do not have any problems: neither a crash, a freeze or an error.
See [EDITED] in my answer above for another problem.
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!