经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » MATLAB » 查看文章
教你用Matlab制作黄金矿工小游戏
来源:jb51  时间:2022/3/2 15:46:18  对本文有异议

效果

步骤

图片准备

本文所使用图片在这

背景构建

  1. function goldMiner
  2. Mainfig=figure('units','pixels','position',[50 100 750 500],...
  3. 'Numbertitle','off','menubar','none','resize','off',...
  4. 'name','goldMiner');
  5. axes('parent',Mainfig,'position',[0 0 1 1],...
  6. 'XLim', [0 750],...
  7. 'YLim', [0 500],...
  8. 'NextPlot','add',...
  9. 'layer','bottom',...
  10. 'Visible','on',...
  11. 'YDir','reverse',...
  12. 'XTick',[], ...
  13. 'YTick',[]);
  14.  
  15. bkgPic=imread('.\pic\bkg.png');
  16. image([0,750],[0,500],bkgPic)
  17.  
  18. [manPic,~,manAlp]=imread('.\pic\man.png');
  19. image([400-60,400+60],[49.5-45,49.5+45],manPic,'AlphaData',manAlp)
  20.  

绘制爪子

由于爪子要不断调整角度因此用surface格式绘制,我们需要将爪子图片矩阵范围调整至[0,1],并将透明处数值调成nan

  1. [clawPic,~,clawAlp]=imread('.\Pic\claw.png');
  2. clawPic=double(clawPic)./255;
  3. clawPicR=clawPic(:,:,1);
  4. clawPicG=clawPic(:,:,2);
  5. clawPicB=clawPic(:,:,3);
  6. clawPicR(clawAlp<1)=nan;
  7. clawPicG(clawAlp<1)=nan;
  8. clawPicB(clawAlp<1)=nan;
  9. clawPic(:,:,1)=clawPicR;
  10. clawPic(:,:,2)=clawPicG;
  11. clawPic(:,:,3)=clawPicB;
  12.  
  13. clawPos=[380,75];
  14. ropePos=[380,75];
  15.  
  16. [xgrid,ygrid]=meshgrid((1:size(clawAlp,2))./2,(1:size(clawAlp,1))./2);
  17. xgrid=xgrid-size(clawAlp,2)/4;
  18.  
  19. thetaList=linspace(-2*pi/5,2*pi/5,50);
  20. thetaIndex=1;
  21. theta=thetaList(thetaIndex);%当前爪子转动角度
  22. v=0;%爪子下移速度
  23. dir=1;%1或-1爪子转动方向
  24. grabbing=false;%是否正在抓取石块
  25.  
  26. cost=cos(theta);
  27. sint=sin(theta);
  28. rotateX=cost.*xgrid+sint.*ygrid;
  29. rotateY=cost.*ygrid-sint.*xgrid;
  30.  
  31. drawClawHdl=surface(rotateX+clawPos(1),rotateY+clawPos(2),...
  32. zeros(size(clawAlp)),clawPic,...
  33. 'EdgeColor','none');
  34. drawLineHdl=plot([clawPos(1),ropePos(1)],[clawPos(2),ropePos(2)],'k','LineWidth',2);

让爪子转起来

爪子旋转就是单纯的使用旋转矩阵:

  1. fps=20;
  2. game=timer('ExecutionMode', 'FixedRate', 'Period',1/fps, 'TimerFcn', @minergame);
  3. start(game)
  4.  
  5. function minergame(~,~)
  6. if ~grabbing
  7. switch 1
  8. case thetaIndex==1,dir=1;
  9. case thetaIndex==50,dir=-1;
  10. end
  11. thetaIndex=thetaIndex+dir;
  12. theta=thetaList(thetaIndex);
  13. cost=cos(theta);
  14. sint=sin(theta);
  15. rotateX=cost.*xgrid+sint.*ygrid;
  16. rotateY=cost.*ygrid-sint.*xgrid;
  17. else
  18. end
  19. end

绘制石块

  1. stoneName={'gold','gold','stone1','stone2','diamond'};
  2. stonePic{length(stoneName)}=[];
  3. stoneAlp{length(stoneName)}=[];
  4. for i=1:length(stoneName)
  5. [C,~,Alp]=imread(['.\pic\',stoneName{i},'.png']);
  6. stonePic{i}=C;
  7. stoneAlp{i}=Alp;
  8. end
  9. stoneV=[-2,-3,-3,-3,-5];%拿起石头后爪子移动速度
  10. stonePrice=[800,500,200,100,1000];
  11. stoneSize=[50,50;30,30;24,20;15,12;8,8];
  12.  
  13.  
  14. stonePos=[200,300;400,350;500,200;50,240;50,300;
  15. 700,420;170,180];
  16. stoneType=[1,2,3,4,5,1,2];
  17. stoneTag=1:length(stoneType);
  18. stoneXrange=[stonePos(:,1)-stoneSize(stoneType',1),stonePos(:,1)+stoneSize(stoneType',1)];
  19. stoneYrange=[stonePos(:,2)-stoneSize(stoneType',2),stonePos(:,2)+stoneSize(stoneType',2)];
  20.  
  21. for i=1:length(stoneTag)
  22. drawStone(stonePos(i,:),stoneType(i),stoneTag(i))
  23. end
  24.  
  25. function drawStone(pos,i,j)
  26. image([-stoneSize(i,1),stoneSize(i,1)]+pos(1),...
  27. [-stoneSize(i,2),stoneSize(i,2)]+pos(2),...
  28. stonePic{i},...
  29. 'AlphaData',stoneAlp{i},...
  30. 'UserData',j)
  31. end

点击下箭头移动爪子

  1. set(gcf, 'KeyPressFcn', @key)
  2.  
  3. function key(~,event)
  4. switch event.Key
  5. case 'downarrow'
  6. grabbing=true;v=4;
  7. end
  8. end
  9.  
  10. function minergame(~,~)
  11. if ~grabbing
  12. %这里是让爪子转动的一堆代码
  13. %。。。。。。。。。。。。。
  14. %。。。。。。。。。。。。。
  15. else
  16. cost=cos(theta);
  17. sint=sin(theta);
  18. clawPos=clawPos+[sint,cost].*v;
  19. end
  20. set(drawClawHdl,'XData',rotateX+clawPos(1),'YData',rotateY+clawPos(2));
  21. set(drawLineHdl,'XData',[clawPos(1),ropePos(1)],'YData',[clawPos(2),ropePos(2)]);
  22. end

爪子与石头和边缘碰触判断

  1. function n=touchThing(clawPos)
  2. n=0;
  3. if clawPos(1)<20||clawPos(1)>730||clawPos(2)>480
  4. n=-1;
  5. end
  6. flagX=clawPos(1)>=stoneXrange(:,1)&clawPos(1)<=stoneXrange(:,2);
  7. flagY=clawPos(2)>=stoneYrange(:,1)&clawPos(2)<=stoneYrange(:,2);
  8. flagXY=flagX&flagY;
  9. if any(flagXY)
  10. n=find(flagXY);
  11. end
  12. end

抓取石块和显示金钱

  1. holdOnType=0;
  2. drawHoldOnHdl=image([0,1],[0,1],ones(1,1),'AlphaData',zeros(1,1));
  3.  
  4. text(10,40,'Money:','FontSize',20,'Color',[1 1 1],'FontName','Cambria','FontWeight','bold')
  5. money=0;
  6. moneyStrHdl=text(110,40,'$0','FontSize',20,'Color',[0.5137 0.7882 0.2157],'FontName','Cambria','FontWeight','bold');
  7.  
  8. function minergame(~,~)
  9. if ~grabbing
  10. switch 1
  11. case thetaIndex==1,dir=1;
  12. case thetaIndex==50,dir=-1;
  13. end
  14. thetaIndex=thetaIndex+dir;
  15. theta=thetaList(thetaIndex);
  16. cost=cos(theta);
  17. sint=sin(theta);
  18. rotateX=cost.*xgrid+sint.*ygrid;
  19. rotateY=cost.*ygrid-sint.*xgrid;
  20. else
  21. cost=cos(theta);
  22. sint=sin(theta);
  23. clawPos=clawPos+[sint,cost].*v;
  24. n=touchThing(clawPos+5.*[sint,cost]);
  25. if n==-1
  26. v=-abs(v);
  27. elseif n>0
  28. delete(findobj('UserData',stoneTag(n)));
  29. v=stoneV(stoneType(n));
  30. holdOnType=stoneType(n);
  31. stonePos(n,:)=[];
  32. stoneType(n)=[];
  33. stoneTag(n)=[];
  34. stoneXrange(n,:)=[];
  35. stoneYrange(n,:)=[];
  36. set(drawHoldOnHdl,...
  37. 'XData',[-stoneSize(holdOnType,1),stoneSize(holdOnType,1)]+clawPos(1)+norm(stoneSize(holdOnType,:))*sint,...
  38. 'YData',[-stoneSize(holdOnType,2),stoneSize(holdOnType,2)]+clawPos(2)+norm(stoneSize(holdOnType,:))*cost,...
  39. 'CData',stonePic{holdOnType},'AlphaData',stoneAlp{holdOnType});
  40. end
  41. if clawPos(2)<=ropePos(2)
  42. clawPos=ropePos;
  43. grabbing=false;
  44. if holdOnType>0
  45. money=money+stonePrice(holdOnType);
  46. set(moneyStrHdl,'String',['$',num2str(money)])
  47. end
  48. holdOnType=0;
  49. set(drawHoldOnHdl,'XData',[0,1],...
  50. 'YData',[0,1],...
  51. 'CData',ones(1,1),...
  52. 'AlphaData',zeros(1,1));
  53. end
  54. if holdOnType~=0
  55. set(drawHoldOnHdl,...
  56. 'XData',[-stoneSize(holdOnType,1),stoneSize(holdOnType,1)]+clawPos(1)+norm(stoneSize(holdOnType,:))*sint,...
  57. 'YData',[-stoneSize(holdOnType,2),stoneSize(holdOnType,2)]+clawPos(2)+norm(stoneSize(holdOnType,:))*cost);
  58. end
  59. end
  60. set(drawClawHdl,'XData',rotateX+clawPos(1),'YData',rotateY+clawPos(2));
  61. set(drawLineHdl,'XData',[clawPos(1),ropePos(1)],'YData',[clawPos(2),ropePos(2)]);
  62. end

完整代码

  1. function goldMiner
  2. Mainfig=figure('units','pixels','position',[50 100 750 500],...
  3. 'Numbertitle','off','menubar','none','resize','off',...
  4. 'name','goldMiner');
  5. axes('parent',Mainfig,'position',[0 0 1 1],...
  6. 'XLim', [0 750],...
  7. 'YLim', [0 500],...
  8. 'NextPlot','add',...
  9. 'layer','bottom',...
  10. 'Visible','on',...
  11. 'YDir','reverse',...
  12. 'XTick',[], ...
  13. 'YTick',[]);
  14.  
  15. bkgPic=imread('.\pic\bkg.png');
  16. image([0,750],[0,500],bkgPic)
  17.  
  18. [manPic,~,manAlp]=imread('.\pic\man.png');
  19. image([400-60,400+60],[49.5-45,49.5+45],manPic,'AlphaData',manAlp)
  20.  
  21. [clawPic,~,clawAlp]=imread('.\Pic\claw.png');
  22. clawPic=double(clawPic)./255;
  23. clawPicR=clawPic(:,:,1);
  24. clawPicG=clawPic(:,:,2);
  25. clawPicB=clawPic(:,:,3);
  26. clawPicR(clawAlp<1)=nan;
  27. clawPicG(clawAlp<1)=nan;
  28. clawPicB(clawAlp<1)=nan;
  29. clawPic(:,:,1)=clawPicR;
  30. clawPic(:,:,2)=clawPicG;
  31. clawPic(:,:,3)=clawPicB;
  32.  
  33. clawPos=[380,75];
  34. ropePos=[380,75];
  35.  
  36. [xgrid,ygrid]=meshgrid((1:size(clawAlp,2))./2,(1:size(clawAlp,1))./2);
  37. xgrid=xgrid-size(clawAlp,2)/4;
  38.  
  39. thetaList=linspace(-2*pi/5,2*pi/5,50);
  40. thetaIndex=1;
  41. theta=thetaList(thetaIndex);v=0;
  42. dir=1;grabbing=false;
  43.  
  44. cost=cos(theta);
  45. sint=sin(theta);
  46. rotateX=cost.*xgrid+sint.*ygrid;
  47. rotateY=cost.*ygrid-sint.*xgrid;
  48.  
  49. drawClawHdl=surface(rotateX+clawPos(1),rotateY+clawPos(2),...
  50. zeros(size(clawAlp)),clawPic,...
  51. 'EdgeColor','none');
  52. drawLineHdl=plot([clawPos(1),ropePos(1)],[clawPos(2),ropePos(2)],'k','LineWidth',2);
  53. %stone part======================================================
  54. stoneName={'gold','gold','stone1','stone2','diamond'};
  55. stonePic{length(stoneName)}=[];
  56. stoneAlp{length(stoneName)}=[];
  57. for i=1:length(stoneName)
  58. [C,~,Alp]=imread(['.\pic\',stoneName{i},'.png']);
  59. stonePic{i}=C;
  60. stoneAlp{i}=Alp;
  61. end
  62. stoneV=[-2,-3,-3,-3,-5];
  63. stonePrice=[800,500,200,100,1000];
  64. stoneSize=[50,50;30,30;24,20;15,12;8,8];
  65.  
  66.  
  67. stonePos=[200,300;400,350;500,200;50,240;50,300;
  68. 700,420;170,180];
  69. stoneType=[1,2,3,4,5,1,2];
  70. stoneTag=1:length(stoneType);
  71. stoneXrange=[stonePos(:,1)-stoneSize(stoneType',1),stonePos(:,1)+stoneSize(stoneType',1)];
  72. stoneYrange=[stonePos(:,2)-stoneSize(stoneType',2),stonePos(:,2)+stoneSize(stoneType',2)];
  73.  
  74. for i=1:length(stoneTag)
  75. drawStone(stonePos(i,:),stoneType(i),stoneTag(i))
  76. end
  77.  
  78. function drawStone(pos,i,j)
  79. image([-stoneSize(i,1),stoneSize(i,1)]+pos(1),...
  80. [-stoneSize(i,2),stoneSize(i,2)]+pos(2),...
  81. stonePic{i},...
  82. 'AlphaData',stoneAlp{i},...
  83. 'UserData',j)
  84. end
  85.  
  86. holdOnType=0;
  87. drawHoldOnHdl=image([0,1],[0,1],ones(1,1),'AlphaData',zeros(1,1));
  88.  
  89. text(10,40,'Money:','FontSize',20,'Color',[1 1 1],'FontName','Cambria','FontWeight','bold')
  90. money=0;
  91. moneyStrHdl=text(110,40,'$0','FontSize',20,'Color',[0.5137 0.7882 0.2157],'FontName','Cambria','FontWeight','bold');
  92.  
  93. %==========================================================================
  94. set(gcf, 'KeyPressFcn', @key)
  95. fps=20;
  96. game=timer('ExecutionMode', 'FixedRate', 'Period',1/fps, 'TimerFcn', @minergame);
  97. start(game)
  98.  
  99. function minergame(~,~)
  100. if ~grabbing
  101. switch 1
  102. case thetaIndex==1,dir=1;
  103. case thetaIndex==50,dir=-1;
  104. end
  105. thetaIndex=thetaIndex+dir;
  106. theta=thetaList(thetaIndex);
  107. cost=cos(theta);
  108. sint=sin(theta);
  109. rotateX=cost.*xgrid+sint.*ygrid;
  110. rotateY=cost.*ygrid-sint.*xgrid;
  111. else
  112. cost=cos(theta);
  113. sint=sin(theta);
  114. clawPos=clawPos+[sint,cost].*v;
  115. n=touchThing(clawPos+5.*[sint,cost]);
  116. if n==-1
  117. v=-abs(v);
  118. elseif n>0
  119. delete(findobj('UserData',stoneTag(n)));
  120. v=stoneV(stoneType(n));
  121. holdOnType=stoneType(n);
  122. stonePos(n,:)=[];
  123. stoneType(n)=[];
  124. stoneTag(n)=[];
  125. stoneXrange(n,:)=[];
  126. stoneYrange(n,:)=[];
  127. set(drawHoldOnHdl,...
  128. 'XData',[-stoneSize(holdOnType,1),stoneSize(holdOnType,1)]+clawPos(1)+norm(stoneSize(holdOnType,:))*sint,...
  129. 'YData',[-stoneSize(holdOnType,2),stoneSize(holdOnType,2)]+clawPos(2)+norm(stoneSize(holdOnType,:))*cost,...
  130. 'CData',stonePic{holdOnType},'AlphaData',stoneAlp{holdOnType});
  131. end
  132. if clawPos(2)<=ropePos(2)
  133. clawPos=ropePos;
  134. grabbing=false;
  135. if holdOnType>0
  136. money=money+stonePrice(holdOnType);
  137. set(moneyStrHdl,'String',['$',num2str(money)])
  138. end
  139. holdOnType=0;
  140. set(drawHoldOnHdl,'XData',[0,1],...
  141. 'YData',[0,1],...
  142. 'CData',ones(1,1),...
  143. 'AlphaData',zeros(1,1));
  144. end
  145. if holdOnType~=0
  146. set(drawHoldOnHdl,...
  147. 'XData',[-stoneSize(holdOnType,1),stoneSize(holdOnType,1)]+clawPos(1)+norm(stoneSize(holdOnType,:))*sint,...
  148. 'YData',[-stoneSize(holdOnType,2),stoneSize(holdOnType,2)]+clawPos(2)+norm(stoneSize(holdOnType,:))*cost);
  149. end
  150. end
  151. set(drawClawHdl,'XData',rotateX+clawPos(1),'YData',rotateY+clawPos(2));
  152. set(drawLineHdl,'XData',[clawPos(1),ropePos(1)],'YData',[clawPos(2),ropePos(2)]);
  153. end
  154.  
  155. function n=touchThing(clawPos)
  156. n=0;
  157. if clawPos(1)<20||clawPos(1)>730||clawPos(2)>480
  158. n=-1;
  159. end
  160. flagX=clawPos(1)>=stoneXrange(:,1)&clawPos(1)<=stoneXrange(:,2);
  161. flagY=clawPos(2)>=stoneYrange(:,1)&clawPos(2)<=stoneYrange(:,2);
  162. flagXY=flagX&flagY;
  163. if any(flagXY)
  164. n=find(flagXY);
  165. end
  166. end
  167.  
  168. function key(~,event)
  169. switch event.Key
  170. case 'downarrow'
  171. grabbing=true;v=4;
  172. end
  173. end
  174. end

以上就是教你用Matlab制作黄金矿工小游戏的详细内容,更多关于Matlab黄金矿工游戏的资料请关注w3xue其它相关文章!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号