制作了一个生命游戏的小游戏 上下左右移动红点 空格绘制生命 按键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 restart
n=50;
%n=input('the board size is:');
axis equal
axis(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(A2)=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'))
clf
close
end
function lifeGame(~,~)
chang=length(sum(A,2));
postion(postion>chang)=postion(postion>chang)-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脚手架写一个简单的页面?