以前写的,主要是受到golly这个软件的启发(感觉是个很好玩的软件) 这个生命游戏可调节代数,存活条件,繁殖条件 like this 运行效果
代码:
function lifeinput(arg1)
% 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 restart
% size is standing of the size of the background
% live means the needs of lives around for living
% born means the needs of lives around for borning
% if you want the new lives to take place the elder input 1 else input 2
% try to type lifeinput('life'),lifeinput('brian'),lifeinput('firework')...
if nargin < 1||strcmp(arg1,'hot')||strcmp(arg1,'cold')
size=input('the board size is:');
live=input('living needs are:');
born=input('borning needs are:');
generation=input('generation is:');
background=[0 0 0];
if generation==1
coloris=[1 1 1];
end
if generation~=1
co=[0;sort((1./(1:(generation-1)))')];
coloris=[ones(generation,1),co,zeros(generation,1)];
if nargin < 1
else if strcmp(arg1,'hot')
coloris=[ones(generation,1),co,zeros(generation,1)];
else strcmp(arg1,'cold')
coloris=[zeros(generation,1),co,-sort(-co)];
end
end
end
arg1='start';
end
take=3;
if strcmp(arg1,'life')
size=50;live=[2 3];born=3;generation=1;coloris=[1 1 1];background=[0 0 0];take=1;
end
if strcmp(arg1,'star wars')
size=300;live=[3 4 5];born=2;generation=3;
co=[0;sort((1./(1:(generation-1)))')];
coloris=[ones(generation,1),co,zeros(generation,1)];background=[0 0 0];take=3;
end
if strcmp(arg1,'brian')
size=100;live=20;born=2;generation=2;
co=[0;sort((1./(1:(generation-1)))')];
coloris=[ones(generation,1),co,zeros(generation,1)];background=[0 0 0];take=3;
end
if strcmp(arg1,'signet')
size=100;live=[3 4 5];born=2;generation=4;
co=[0;sort((1./(1:(generation-1)))')];
coloris=[ones(generation,1),co,zeros(generation,1)];background=[0 0 0];take=2;
end
if strcmp(arg1,'fireworks')
size=300;live=2;born=[1 3];generation=21;
co=[0;sort((1./(1:(generation-1)))')];
coloris=[ones(generation,1),co,zeros(generation,1)];background=[0 0 0];take=3;
end
%..........................................................................
persistent save BT BI
%..........................................................................
axis equal
axis([-0.5,size+0.5,-0.5,size+0.5])
set(gca,'xtick',[],'ytick',[],'xcolor','w','ycolor','w')
set(gca,'color',background)
hold on
%A=randi(9,[size,size]);
%A(A2)=1;
A=zeros(size,size);
%A=[input sqare with only ones and zeros]
[a,b]=find(A(:,:)==1);
B=[b,length(sum(A,2))-a];
if ~isempty(B)
B=[B,ones(length(sum(B,2)),1)*coloris(1,:)];
else
B=[1 1 1 1 1];
B(1,:)=[];
end
postion=[floor(size/2),floor(size/2)];
control=1;
plotl=scatter(gca,B(:,1),B(:,2),1200/size,B(:,3:5),'s','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)
set(gcf,'tag','co','CloseRequestFcn',@clo);
function clo(~,~)
stop(game)
delete(findobj('tag','co'))
clf
close
end
function lifeGame(~,~)
B=[1 1 1 1 1];
B(1,:)=[];
chang=length(sum(A,2));
postion(postion>chang-1)=postion(postion>chang-1)-chang;
postion(postion
关注
打赏
最近更新
- 深拷贝和浅拷贝的区别(重点)
- 【Vue】走进Vue框架世界
- 【云服务器】项目部署—搭建网站—vue电商后台管理系统
- 【React介绍】 一文带你深入React
- 【React】React组件实例的三大属性之state,props,refs(你学废了吗)
- 【脚手架VueCLI】从零开始,创建一个VUE项目
- 【React】深入理解React组件生命周期----图文详解(含代码)
- 【React】DOM的Diffing算法是什么?以及DOM中key的作用----经典面试题
- 【React】1_使用React脚手架创建项目步骤--------详解(含项目结构说明)
- 【React】2_如何使用react脚手架写一个简单的页面?