How to make game snake running after answer question in matlab app designer

3 次查看(过去 30 天)
I make a snake game but i dont know how to make it running after answer question in matlab app designer
this is my half coding, when i run, it keep on asked first question after i got correct answer
while (1)
id = 0;
s_old = s;
%Update Head Position
if(app.dir==0)
s(1,1)=s_old(1,1)-1;
s(1,2)=s_old(1,2);
elseif(app.dir==1)
s(1,1)=s_old(1,1);
s(1,2)=s_old(1,2)+1;
elseif(app.dir==2)
s(1,1)=s_old(1,1)+1;
s(1,2)=s_old(1,2);
elseif(app.dir==3)
s(1,1)=s_old(1,1);
s(1,2)=s_old(1,2)-1;
end
%Update Body Position
for i=2:1:length(nonzeros(s(:,1)))
s(i,1) = s_old(i-1,1);
s(i,2) = s_old(i-1,2);
end
%Limit area
if(s(1,1)>dim||s(1,1)<0||s(1,2)>dim||s(1,2)<0)
msgbox(strcat('Out of the limits, game ended! Score: ',int2str(app.score)),'End');
break
end
%Touch body
if(isempty(find(prod(s(1,1:2)==s(2:end,1:2),2)))==0)
msgbox(strcat('Snake touch the body!, game ended! Score: ',int2str(app.score)),'End');
break
end
%Eating
if(s(1,[1 2])==f)
s(length(nonzeros(s(:,1)))+1,1) = s_old(length(nonzeros(s_old(:,1))),1);
s(length(nonzeros(s(:,2)))+1,2) = s_old(length(nonzeros(s_old(:,2))),2);
f(1) = round(rand*dim);
f(2) = round(rand*dim);
app.score = app.score+1;
v = v-0.02;
end
%Plotting
app.SCOREEditField.Value = int2str(app.score);
set(snake,'XData',nonzeros(s(:,1)),'YData',nonzeros(s(:,2)));
set(food,'XData',nonzeros(f(:,1)),'YData',nonzeros(f(:,2)));
pause(v)
%question popped up
if app.score==1
question1 = 'Would you like a dessert?';
title= 'Question 1';
answer1= questdlg(question1, title, 'Ice cream','Cake','Cake');
%answer1
if strcmp (answer1, 'Ice cream')
continue
elseif strcmp (answer1, 'Cake')
msgbox(strcat('Wrong answer, game ended! Score is ', int2str(app.score)),'End');
end
break
end
if app.score==2
question2 = 'What is 1+1?';
title= 'Question 2';
answer2= questdlg(question2, title, '1','2','2');
%answer1
if strcmp (answer2, '2')
continue
elseif strcmp (answer2, '1')
msgbox(strcat('Wrong answer, game ended! Score is ', int2str(app.score)),'End');
end
break
end
end
end
  1 个评论
Sandeep Mishra
Sandeep Mishra 2023-7-4
how to make it running after answer question in matlab app designer
@Wan Can you explain the above line
are you trying to start the game when the user presses any button?

请先登录,再进行评论。

回答(1 个)

Deep
Deep 2023-7-8
Your 'break' statements are outside the blocks for wrong answer. The loop would break on any answer, whether its correct or wrong.
To ensure the 'break' command is only executed when the answer is incorrect, you need to move the 'break' statements into the 'else if' blocks as follows:
if app.score==1
question1 = 'Would you like a dessert?';
title= 'Question 1';
answer1= questdlg(question1, title, 'Ice cream','Cake','Cake');
if strcmp (answer1, 'Ice cream')
% continue the game
elseif strcmp (answer1, 'Cake')
msgbox(strcat('Wrong answer, game ended! Score is ', int2str(app.score)),'End');
break; % end the game
end
end
if app.score==2
question2 = 'What is 1+1?';
title= 'Question 2';
answer2= questdlg(question2, title, '1','2','2');
if strcmp (answer2, '2')
% continue the game.
elseif strcmp (answer2, '1')
msgbox(strcat('Wrong answer, game ended! Score is ', int2str(app.score)),'End');
break; % end the game
end
end
Also, you can also use an 'else' statement instead of 'elseif' to mark any answer other than the correct answer as wrong. You won't need to define all the wrong answers, only define 1 correct answer and the rest can be wrong. :)

类别

Help CenterFile Exchange 中查找有关 Line Plots 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by