matlab畫一個女孩,matlab 生命游戲 可自己輸入圖像

 2023-10-22 阅读 29 评论 0

摘要:制作了一個生命游戲的小游戲 上下左右移動紅點 空格繪制生命 按鍵B開始運動 按鍵C暫停 按鍵A清空 按鍵backspace刪除生命 游戲截圖 動圖 代碼部分 function lifecontrol % life game which can input point as you will. % move the red cross with the key % 'uparrow

制作了一個生命游戲的小游戲
上下左右移動紅點
空格繪制生命
按鍵B開始運動
按鍵C暫停
按鍵A清空
按鍵backspace刪除生命

游戲截圖
在這里插入圖片描述
在這里插入圖片描述
動圖
在這里插入圖片描述
代碼部分

function lifecontrol
% life game which can input point as you will.
% move the red cross with the key
% 'uparrow','downarrow','leftarrow','rightarrow'
% use the key 'space'to input
% 'bacspace'to delete
% 'b' to begin 'c'to stop and 'a' to restartn=50;%n=input('the board size is:');axis equalaxis(0.5+[0,n,0,n])set(gca,'xtick',[],'ytick',[],'xcolor','w','ycolor','w')set(gca,'color','w')hold on%axis set.........................................................................%A=randi(9,[n,n]);%A(A<2.1)=0;%A(A>2)=1;A=zeros(n,n);%A=[input sqare with only ones and zeros][a,b]=find(A(:,:)==1);B=[b,length(sum(A,2))-a];postion=[floor(n/2),floor(n/2)];control=1;plotl=scatter(gca,B(:,1),B(:,2),1200/n,'ks','filled');plotpostion=scatter(gca,postion(1,1),postion(1,2),150,'rx');set(gcf, 'KeyPressFcn', @key)                                                     fps = 20;                                    game = timer('ExecutionMode', 'FixedRate', 'Period',1/fps, 'TimerFcn', @lifeGame);start(game) 
%beginning set.............................................................................
set(gcf,'tag','co','CloseRequestFcn',@clo);function clo(~,~)stop(game)delete(findobj('tag','co'))clfcloseend
    function lifeGame(~,~)chang=length(sum(A,2));postion(postion>chang)=postion(postion>chang)-chang;postion(postion<1)=postion(postion<1)+chang;if control==2B1=[A;zeros(1,chang)];B1(1,:)=[];B2=[zeros(1,chang);A];B2(end,:)=[];B3=[zeros(chang,1),A];B3(:,end)=[];B4=[A,zeros(chang,1)];B4(:,1)=[];C1=[zeros(chang,1),B1];C1(:,end)=[];C2=[B1,zeros(chang,1)];C2(:,1)=[];C3=[zeros(chang,1),B2];C3(:,end)=[];C4=[B2,zeros(chang,1)];C4(:,1)=[];NewA=B1+B2+B3+B4+C1+C2+C3+C4;progressA=zeros(chang,chang);progressA(NewA==3)=1;t3=find(NewA==2);t2=find(A==1);[lalala,~,~]=intersect(t3,t2);progressA(lalala)=1;A=progressA;[a,b]=find(A(:,:)==1);B=[b,length(sum(A,2))-a];endif control==1[a,b]=find(A(:,:)==1);B=[b,length(sum(A,2))-a]; endset(plotl,'XData',B(:,1),'YData',B(:,2))set(plotpostion,'XData',postion(1,1),'YData',postion(1,2))endfunction key(~,event)switch event.Keycase 'uparrow'postion=postion+[0,1];case 'downarrow'postion=postion+[0,-1];case 'leftarrow'postion=postion+[-1,0];case 'rightarrow'postion=postion+[1,0];case 'space'A(length(sum(A,2))-postion(1,2),postion(1,1))=1;case 'backspace'A(length(sum(A,2))-postion(1,2),postion(1,1))=0;case 'b'control=2;case 'c'control=1;case 'a'A=zeros(100,100);control=1;endend
end

將兩段代碼復制粘貼至同一個m文件即可

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://808629.com/164665.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 86后生记录生活 Inc. 保留所有权利。

底部版权信息