Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does Matlab realize gluttonous Snake Mini Game

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

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)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report