MATLAB Answers

Can't Stop program

23 views (last 30 days)
ziv koren
ziv koren on 7 Jan 2021
Answered: Geoff Hayes on 20 Jan 2021
Hello,
In the code attached to the question I am trying to draw multiple rectangles and erasing them randomly.
My problem occurs when I want the game to end, it seems that the loop keeps on ruuning making a new matrix (even though I didn't order it to do so).
Can you please help me find a way to stop the program from running after this line in the code :
cla; text(Limit/2,Limit/2,'Game Over','HorizontalAlignment','center','fontsize',50); pause(2);
And if you can explain to me why the programm does not stop running it will be even better.
Thanks in advance !!

Answers (1)

Geoff Hayes
Geoff Hayes on 20 Jan 2021
ziv - I think the problem is that you are getting stuck in a loop between two functions: DrawBoard and GamePlay. DrawBoard calls GamePlay and then GamePlay will call DrawBoard, and this pattern will continue. You may want to order your code so that it something more like
  • create game board
  • while loop
  • play game
  • update game board
The condition on the while loop will tell you when you are finished. It may be necessary to keep the update game board call in the GamePlay. I've attached something that might work (it also relies on returning the updated SurfaceMatrix with each call to the functions).

Categories

Community Treasure Hunt

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

Start Hunting!