<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
function goldMiner Mainfig=figure('units','pixels','position',[50 100 750 500],... 'Numbertitle','off','menubar','none','resize','off',... 'name','goldMiner'); axes('parent',Mainfig,'position',[0 0 1 1],... 'XLim', [0 750],... 'YLim', [0 500],... 'NextPlot','add',... 'layer','bottom',... 'Visible','on',... 'YDir','reverse',... 'XTick',[], ... 'YTick',[]); bkgPic=imread('.picbkg.png'); image([0,750],[0,500],bkgPic) [manPic,~,manAlp]=imread('.picman.png'); image([400-60,400+60],[49.5-45,49.5+45],manPic,'AlphaData',manAlp)
由於爪子要不斷調整角度因此用surface格式繪製,我們需要將爪子圖片矩陣範圍調整至[0,1],並將透明處數值調成nan
[clawPic,~,clawAlp]=imread('.Picclaw.png'); clawPic=double(clawPic)./255; clawPicR=clawPic(:,:,1); clawPicG=clawPic(:,:,2); clawPicB=clawPic(:,:,3); clawPicR(clawAlp<1)=nan; clawPicG(clawAlp<1)=nan; clawPicB(clawAlp<1)=nan; clawPic(:,:,1)=clawPicR; clawPic(:,:,2)=clawPicG; clawPic(:,:,3)=clawPicB; clawPos=[380,75]; ropePos=[380,75]; [xgrid,ygrid]=meshgrid((1:size(clawAlp,2))./2,(1:size(clawAlp,1))./2); xgrid=xgrid-size(clawAlp,2)/4; thetaList=linspace(-2*pi/5,2*pi/5,50); thetaIndex=1; theta=thetaList(thetaIndex);%當前爪子轉動角度 v=0;%爪子下移速度 dir=1;%1或-1爪子轉動方向 grabbing=false;%是否正在抓取石塊 cost=cos(theta); sint=sin(theta); rotateX=cost.*xgrid+sint.*ygrid; rotateY=cost.*ygrid-sint.*xgrid; drawClawHdl=surface(rotateX+clawPos(1),rotateY+clawPos(2),... zeros(size(clawAlp)),clawPic,... 'EdgeColor','none'); drawLineHdl=plot([clawPos(1),ropePos(1)],[clawPos(2),ropePos(2)],'k','LineWidth',2);
爪子旋轉就是單純的使用旋轉矩陣:
fps=20; game=timer('ExecutionMode', 'FixedRate', 'Period',1/fps, 'TimerFcn', @minergame); start(game) function minergame(~,~) if ~grabbing switch 1 case thetaIndex==1,dir=1; case thetaIndex==50,dir=-1; end thetaIndex=thetaIndex+dir; theta=thetaList(thetaIndex); cost=cos(theta); sint=sin(theta); rotateX=cost.*xgrid+sint.*ygrid; rotateY=cost.*ygrid-sint.*xgrid; else end end
stoneName={'gold','gold','stone1','stone2','diamond'}; stonePic{length(stoneName)}=[]; stoneAlp{length(stoneName)}=[]; for i=1:length(stoneName) [C,~,Alp]=imread(['.pic',stoneName{i},'.png']); stonePic{i}=C; stoneAlp{i}=Alp; end stoneV=[-2,-3,-3,-3,-5];%拿起石頭後爪子移動速度 stonePrice=[800,500,200,100,1000]; stoneSize=[50,50;30,30;24,20;15,12;8,8]; stonePos=[200,300;400,350;500,200;50,240;50,300; 700,420;170,180]; stoneType=[1,2,3,4,5,1,2]; stoneTag=1:length(stoneType); stoneXrange=[stonePos(:,1)-stoneSize(stoneType',1),stonePos(:,1)+stoneSize(stoneType',1)]; stoneYrange=[stonePos(:,2)-stoneSize(stoneType',2),stonePos(:,2)+stoneSize(stoneType',2)]; for i=1:length(stoneTag) drawStone(stonePos(i,:),stoneType(i),stoneTag(i)) end function drawStone(pos,i,j) image([-stoneSize(i,1),stoneSize(i,1)]+pos(1),... [-stoneSize(i,2),stoneSize(i,2)]+pos(2),... stonePic{i},... 'AlphaData',stoneAlp{i},... 'UserData',j) end
set(gcf, 'KeyPressFcn', @key) function key(~,event) switch event.Key case 'downarrow' grabbing=true;v=4; end end function minergame(~,~) if ~grabbing %這裡是讓爪子轉動的一堆程式碼 %。。。。。。。。。。。。。 %。。。。。。。。。。。。。 else cost=cos(theta); sint=sin(theta); clawPos=clawPos+[sint,cost].*v; end set(drawClawHdl,'XData',rotateX+clawPos(1),'YData',rotateY+clawPos(2)); set(drawLineHdl,'XData',[clawPos(1),ropePos(1)],'YData',[clawPos(2),ropePos(2)]); end
function n=touchThing(clawPos) n=0; if clawPos(1)<20||clawPos(1)>730||clawPos(2)>480 n=-1; end flagX=clawPos(1)>=stoneXrange(:,1)&clawPos(1)<=stoneXrange(:,2); flagY=clawPos(2)>=stoneYrange(:,1)&clawPos(2)<=stoneYrange(:,2); flagXY=flagX&flagY; if any(flagXY) n=find(flagXY); end end
holdOnType=0; drawHoldOnHdl=image([0,1],[0,1],ones(1,1),'AlphaData',zeros(1,1)); text(10,40,'Money:','FontSize',20,'Color',[1 1 1],'FontName','Cambria','FontWeight','bold') money=0; moneyStrHdl=text(110,40,'$0','FontSize',20,'Color',[0.5137 0.7882 0.2157],'FontName','Cambria','FontWeight','bold'); function minergame(~,~) if ~grabbing switch 1 case thetaIndex==1,dir=1; case thetaIndex==50,dir=-1; end thetaIndex=thetaIndex+dir; theta=thetaList(thetaIndex); cost=cos(theta); sint=sin(theta); rotateX=cost.*xgrid+sint.*ygrid; rotateY=cost.*ygrid-sint.*xgrid; else cost=cos(theta); sint=sin(theta); clawPos=clawPos+[sint,cost].*v; n=touchThing(clawPos+5.*[sint,cost]); if n==-1 v=-abs(v); elseif n>0 delete(findobj('UserData',stoneTag(n))); v=stoneV(stoneType(n)); holdOnType=stoneType(n); stonePos(n,:)=[]; stoneType(n)=[]; stoneTag(n)=[]; stoneXrange(n,:)=[]; stoneYrange(n,:)=[]; set(drawHoldOnHdl,... 'XData',[-stoneSize(holdOnType,1),stoneSize(holdOnType,1)]+clawPos(1)+norm(stoneSize(holdOnType,:))*sint,... 'YData',[-stoneSize(holdOnType,2),stoneSize(holdOnType,2)]+clawPos(2)+norm(stoneSize(holdOnType,:))*cost,... 'CData',stonePic{holdOnType},'AlphaData',stoneAlp{holdOnType}); end if clawPos(2)<=ropePos(2) clawPos=ropePos; grabbing=false; if holdOnType>0 money=money+stonePrice(holdOnType); set(moneyStrHdl,'String',['$',num2str(money)]) end holdOnType=0; set(drawHoldOnHdl,'XData',[0,1],... 'YData',[0,1],... 'CData',ones(1,1),... 'AlphaData',zeros(1,1)); end if holdOnType~=0 set(drawHoldOnHdl,... 'XData',[-stoneSize(holdOnType,1),stoneSize(holdOnType,1)]+clawPos(1)+norm(stoneSize(holdOnType,:))*sint,... 'YData',[-stoneSize(holdOnType,2),stoneSize(holdOnType,2)]+clawPos(2)+norm(stoneSize(holdOnType,:))*cost); end end set(drawClawHdl,'XData',rotateX+clawPos(1),'YData',rotateY+clawPos(2)); set(drawLineHdl,'XData',[clawPos(1),ropePos(1)],'YData',[clawPos(2),ropePos(2)]); end
function goldMiner Mainfig=figure('units','pixels','position',[50 100 750 500],... 'Numbertitle','off','menubar','none','resize','off',... 'name','goldMiner'); axes('parent',Mainfig,'position',[0 0 1 1],... 'XLim', [0 750],... 'YLim', [0 500],... 'NextPlot','add',... 'layer','bottom',... 'Visible','on',... 'YDir','reverse',... 'XTick',[], ... 'YTick',[]); bkgPic=imread('.picbkg.png'); image([0,750],[0,500],bkgPic) [manPic,~,manAlp]=imread('.picman.png'); image([400-60,400+60],[49.5-45,49.5+45],manPic,'AlphaData',manAlp) [clawPic,~,clawAlp]=imread('.Picclaw.png'); clawPic=double(clawPic)./255; clawPicR=clawPic(:,:,1); clawPicG=clawPic(:,:,2); clawPicB=clawPic(:,:,3); clawPicR(clawAlp<1)=nan; clawPicG(clawAlp<1)=nan; clawPicB(clawAlp<1)=nan; clawPic(:,:,1)=clawPicR; clawPic(:,:,2)=clawPicG; clawPic(:,:,3)=clawPicB; clawPos=[380,75]; ropePos=[380,75]; [xgrid,ygrid]=meshgrid((1:size(clawAlp,2))./2,(1:size(clawAlp,1))./2); xgrid=xgrid-size(clawAlp,2)/4; thetaList=linspace(-2*pi/5,2*pi/5,50); thetaIndex=1; theta=thetaList(thetaIndex);v=0; dir=1;grabbing=false; cost=cos(theta); sint=sin(theta); rotateX=cost.*xgrid+sint.*ygrid; rotateY=cost.*ygrid-sint.*xgrid; drawClawHdl=surface(rotateX+clawPos(1),rotateY+clawPos(2),... zeros(size(clawAlp)),clawPic,... 'EdgeColor','none'); drawLineHdl=plot([clawPos(1),ropePos(1)],[clawPos(2),ropePos(2)],'k','LineWidth',2); %stone part====================================================== stoneName={'gold','gold','stone1','stone2','diamond'}; stonePic{length(stoneName)}=[]; stoneAlp{length(stoneName)}=[]; for i=1:length(stoneName) [C,~,Alp]=imread(['.pic',stoneName{i},'.png']); stonePic{i}=C; stoneAlp{i}=Alp; end stoneV=[-2,-3,-3,-3,-5]; stonePrice=[800,500,200,100,1000]; stoneSize=[50,50;30,30;24,20;15,12;8,8]; stonePos=[200,300;400,350;500,200;50,240;50,300; 700,420;170,180]; stoneType=[1,2,3,4,5,1,2]; stoneTag=1:length(stoneType); stoneXrange=[stonePos(:,1)-stoneSize(stoneType',1),stonePos(:,1)+stoneSize(stoneType',1)]; stoneYrange=[stonePos(:,2)-stoneSize(stoneType',2),stonePos(:,2)+stoneSize(stoneType',2)]; for i=1:length(stoneTag) drawStone(stonePos(i,:),stoneType(i),stoneTag(i)) end function drawStone(pos,i,j) image([-stoneSize(i,1),stoneSize(i,1)]+pos(1),... [-stoneSize(i,2),stoneSize(i,2)]+pos(2),... stonePic{i},... 'AlphaData',stoneAlp{i},... 'UserData',j) end holdOnType=0; drawHoldOnHdl=image([0,1],[0,1],ones(1,1),'AlphaData',zeros(1,1)); text(10,40,'Money:','FontSize',20,'Color',[1 1 1],'FontName','Cambria','FontWeight','bold') money=0; moneyStrHdl=text(110,40,'$0','FontSize',20,'Color',[0.5137 0.7882 0.2157],'FontName','Cambria','FontWeight','bold'); %========================================================================== set(gcf, 'KeyPressFcn', @key) fps=20; game=timer('ExecutionMode', 'FixedRate', 'Period',1/fps, 'TimerFcn', @minergame); start(game) function minergame(~,~) if ~grabbing switch 1 case thetaIndex==1,dir=1; case thetaIndex==50,dir=-1; end thetaIndex=thetaIndex+dir; theta=thetaList(thetaIndex); cost=cos(theta); sint=sin(theta); rotateX=cost.*xgrid+sint.*ygrid; rotateY=cost.*ygrid-sint.*xgrid; else cost=cos(theta); sint=sin(theta); clawPos=clawPos+[sint,cost].*v; n=touchThing(clawPos+5.*[sint,cost]); if n==-1 v=-abs(v); elseif n>0 delete(findobj('UserData',stoneTag(n))); v=stoneV(stoneType(n)); holdOnType=stoneType(n); stonePos(n,:)=[]; stoneType(n)=[]; stoneTag(n)=[]; stoneXrange(n,:)=[]; stoneYrange(n,:)=[]; set(drawHoldOnHdl,... 'XData',[-stoneSize(holdOnType,1),stoneSize(holdOnType,1)]+clawPos(1)+norm(stoneSize(holdOnType,:))*sint,... 'YData',[-stoneSize(holdOnType,2),stoneSize(holdOnType,2)]+clawPos(2)+norm(stoneSize(holdOnType,:))*cost,... 'CData',stonePic{holdOnType},'AlphaData',stoneAlp{holdOnType}); end if clawPos(2)<=ropePos(2) clawPos=ropePos; grabbing=false; if holdOnType>0 money=money+stonePrice(holdOnType); set(moneyStrHdl,'String',['$',num2str(money)]) end holdOnType=0; set(drawHoldOnHdl,'XData',[0,1],... 'YData',[0,1],... 'CData',ones(1,1),... 'AlphaData',zeros(1,1)); end if holdOnType~=0 set(drawHoldOnHdl,... 'XData',[-stoneSize(holdOnType,1),stoneSize(holdOnType,1)]+clawPos(1)+norm(stoneSize(holdOnType,:))*sint,... 'YData',[-stoneSize(holdOnType,2),stoneSize(holdOnType,2)]+clawPos(2)+norm(stoneSize(holdOnType,:))*cost); end end set(drawClawHdl,'XData',rotateX+clawPos(1),'YData',rotateY+clawPos(2)); set(drawLineHdl,'XData',[clawPos(1),ropePos(1)],'YData',[clawPos(2),ropePos(2)]); end function n=touchThing(clawPos) n=0; if clawPos(1)<20||clawPos(1)>730||clawPos(2)>480 n=-1; end flagX=clawPos(1)>=stoneXrange(:,1)&clawPos(1)<=stoneXrange(:,2); flagY=clawPos(2)>=stoneYrange(:,1)&clawPos(2)<=stoneYrange(:,2); flagXY=flagX&flagY; if any(flagXY) n=find(flagXY); end end function key(~,event) switch event.Key case 'downarrow' grabbing=true;v=4; end end end
以上就是教你用Matlab製作黃金礦工小遊戲的詳細內容,更多關於Matlab黃金礦工遊戲的資料請關注it145.com其它相關文章!
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45