用matlab实现的那个找不同颜色的小游戏
游戏截图:
完整代码
function find_different_color hold on axis equal axis(0.5+[0,10,0,10]) set(gca,'xtick',[],'ytick',[],'xcolor','w','ycolor','w') set(gca,'color','w') others=ones(10,10);[m,n]=find(others==1);otherspostion=[m,n];f=@(x)exp(-x)/2; x=0.035;basiccolor=[0.3,0.3,0.3];changepostion=[3,5];theonecolor=[0.3,0.7,0.3]; changeplace=2;score=0;deadline=60;control=0;starts=0; plotsquare=scatter(gca,otherspostion(:,1),otherspostion(:,2),1080,basiccolor(:,1:3),'s','filled'); plotone=scatter(gca,changepostion(:,1),changepostion(:,2),1080,theonecolor(:,1:3),'s','filled'); set(gcf,'WindowButtonDownFcn',@begin); set(gcf,'tag','a','CloseRequestFcn',@clo); function clo(~,~) delete(findobj('tag','a')) control2=0; clf close end function drawsquare x=x+0.035;basiccolor=rand(1,3); changepostion=randi(10,[1,2]);changeplace=randi(3); if basiccolor(changeplace)>0.5 theonecolor=basiccolor; theonecolor(changeplace)=theonecolor(changeplace)-f(x); else theonecolor=basiccolor; theonecolor(changeplace)=theonecolor(changeplace)+f(x); end set(plotsquare,'XData',otherspostion(:,1),'YData',otherspostion(:,2),'CData',basiccolor(:,1:3)) set(plotone,'XData',changepostion(:,1),'YData',changepostion(:,2),'CData',theonecolor(:,1:3)) end function begin(~,~) xy=get(gca,'CurrentPoint');xp=xy(1,1);yp=xy(1,2);pos=[xp,yp];pos=round(pos); if pos==changepostion if control==0 drawsquare() score=score+1; end delete(textscore); textscore=text(-1,7,num2str(score),'FontSize',12); end end function time(~,~) while (deadline>0)&&control2==1 pause(0.75) deadline=deadline-1;delete(textdeadline) textdeadline=text(-1,9,num2str(deadline),'FontSize',15); end control=1; if control2==0 close end end text(-1.5,8,'score=','FontSize',13) textscore=text(-0.5,7,num2str(score),'FontSize',12); text(-2,10,'deadline','FontSize',15) textdeadline=text(-1,9,num2str(deadline),'FontSize',15); control2=1;time() end