How does Matlab realize gluttonous Snake Mini Game
Editor to share with you how to achieve gluttonous Snake Mini Game, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to understand it!
Make up the picture:
Yeah, it's kind of like a snake in the battle of the Snake.
Then improvements include:
Do not report an error setting when closing the window, because the function timer is used, it will report an error when closing the image, so we can add a callback function:
Set (gcf,'tag','co','CloseRequestFcn',@clo); function clo (~, ~) stop (game) delete (findobj ('tag','co')); clf close end
It means that the function clo is called every time there is a closed request.
Then to prevent him from prompting you to call close recursively
Plus delete (findobj ('tag','co')); delete the previous settings.
Complete code
Function snake%axis set...axis equallen=40 Axis (0.5 +, 'ytick', [],' xcolor','w','ycolor','w') set (gca,'color','k') hold on%snake and food begining set...snaketop= (gca,'xtick', [], 'ytick', [],' Len) Len]; body= [5 mint Teng 4.5 Len 5]; 3.5 beacon 5; food= [10]; direction= [1]; long=6;plotsnake=scatter (gca,body (:, 1), body (:, 2), 120); plotfood=scatter (gca,food (1), food (2), 120) % timer set...set (gcf,'WindowButtonMotionFcn',@snakefcn) fps = 8 snakegame game = timer ('ExecutionMode',' FixedRate', 'Period',1/fps,' TimerFcn', @ snakegame) Start (game)%.... set (gcf,'tag','co','CloseRequestFcn',@clo) Function clo (~, ~) stop (game) delete (findobj ('tag','co')); clf close end function snakegame (~, ~) snaketop= [body (1) + direction;body (1) + 1/2*direction]; snaketop (snaketop > len) = snaketop (snaketop > len)-len; snaketop (snaketop)